$(document).ready(function () {

	$('.div_fckeditor').show();

  // Set default Delete Question
  var fmv_del_question = 'Are you sure you wish to delete this record?';

  /**
   * If form has class form_validate add hidden field form_validate with value of 2
   * and add a div to contain the error messages
   */
  $("form.form_validate").prepend('<input type="hidden" name="form_validate" value="2" /><div class="form_error"></div>');

  /**
   * Run this code if the form has been submitted
   */
  $("form.form_validate").submit(function () {
    my_form = $(this);

    // Set the value of form_validate to 1 for the purposes of the processing php script
    my_form.children("input[name='form_validate']").attr('value','1');

    // Reset form_error div to be empty
    my_form.children(".form_error").html('');

	if (window.preSubmit)
	{
		preSubmit();
	}

    // Serialise form and set variables del_question, forward_url and action_url to default values
    var data = my_form.formSerialize();
    var del_question = fmv_del_question;
    var forward_url = document.referrer;
    var action_url = my_form.attr('action');

    // optional override of del_question variable set in form
    if (my_form.children("input[name='form_del_question']").getValue()!="")
      del_question = my_form.children("input[name='form_del_question']").getValue();

    // optional override of forward_url variable set in form
    if (my_form.children("input[name='form_forward_url']").getValue()!="")
      forward_url = my_form.children("input[name='form_forward_url']").getValue();

    // run form_validate_run function with collected data from submitted form
    form_validate_run(my_form,action_url,data,del_question,forward_url);

    // block the form from being sent conventionally
    return false;
  });

  $("form.form_validate_del").submit(function () {

  	my_form = $(this);

	if (window.preSubmit)
	{
		preSubmit();
	}

  	var del_question = fmv_del_question;

  	if (my_form.children("input[name='form_del_question']").getValue()!="")
      del_question = my_form.children("input[name='form_del_question']").getValue();

    if (my_form.find("input[name='del']").attr('checked') == true) {
      if (confirm(del_question)) {
        return true;
      } else {
        my_form.find("input[name='del']").attr('checked',false);
        if (window.postDelCancel)
		{
			postDelCancel();
		}
        return false;
      }

    }

  });


  /**
   * Run this code if the a keyup event has been detected within the form boundarys
   */
  $("form.form_validate").change(function(){
    my_form = $(this);

    // Check that the form_error div already displays an error
    if (my_form.children(".form_error").html()!='' && my_form.children("input[name='form_updating']").getValue()=="1") {
      // set form_validate value to 2 for the purposes of the processing php script
      my_form.children("input[name='form_validate']").attr('value','2');

      // Serialise form and set variable action_url to default value
      var data = my_form.formSerialize();
      var del_question = fmv_del_question;
	  var action_url = my_form.attr('action');

	  // run form_validate_run function with collected data from submitted form
      form_validate_run(my_form,action_url,data,del_question);
    }
  });

  /**
   * form_validate_run
   *
   * @param object which_form
   * @param string action_url
   * @param serialised_data data
   * @param string del_question
   * @param string forward_url
   */
  function form_validate_run(which_form,action_url,data,del_question,forward_url) {
  	if (which_form.find("input[name='del']").attr('checked') == true && which_form.children("input[name='form_validate']").attr('value') == '1') {
      if (confirm(del_question)) {
        $.post(action_url,data,function () {if (forward_url!=null) {window.location = forward_url;}});
      } else {
        which_form.children("input[name='form_validate']").attr('value','2');
        which_form.find("input[name='del']").attr('checked',true);
        if (window.postDelCancel)
		{
			postDelCancel();
		}
      }
    } else {
    	$.post(action_url,data,function (out) {
			which_form.children(".form_error").html(out+' ');
			mark_errors();
			if (out=='' && forward_url!=null)
			{
				window.location = forward_url;
			}
			else
			{
				if ($.scrollTo)
					$.scrollTo('.error_list', 'slow');
			}
		});
    }
  }

	/**
	 * add the mark class to the labels for fields for which errors have been found
	 * also focus on the first field for which an error has been found
	 */
	function mark_errors()
	{
		$("form label").removeClass('mark');
		var first_label = "";
		$('ul.error_list label').each(function () {
			var label_for = $(this).attr('for');
			if (first_label == "")
			{
				first_label = label_for;
			}
			$("form label").each(function () {
				if ($(this).attr('for')==label_for)
				{
					$(this).addClass('mark');
				}
			});
			$("form ul.error_list label").removeClass('mark');
		});
		if(first_label != "")
			$("#"+first_label).focus();
	}
});

