var fmv_del_question = 'Are you sure you wish to delete this record?';

function form_validate_func(my_form)
  {
	    // Set the value of form_validate to 1 for the purposes of the processing php script
	    $('input[name="form_validate"]', my_form).val('1');

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

		// disable the submit button to prevent multiple submits.
		$('input[name="submit_all"]', my_form).attr("disabled", true);

		
	    // 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');
	    
	    // Reset form_error div to be empty
	    $(".form_error", my_form).html('');

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

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

	    // 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;
  }

function form_validate_run(which_form,action_url,data,del_question,forward_url) {
	   
		if ($('input[name="del"]', which_form).attr('checked') == true && $('input[name="form_validate"]', which_form).val() == '1') {
	      if (confirm(del_question)) {
	        $.post(action_url,data,function () {if (forward_url!=null) {window.location = forward_url;}});
	      } else {
	        ('input[name="form_validate"]', which_form).val('2');
	        ('input[name="del"]', which_form).attr('checked',true);
	        if (window.postDelCancel)
			{
				postDelCancel();
			}
	      }
	    } else {
	    	$.post(action_url,data,function (out) {
				$(".form_error", which_form).html(out+' ');			
				if (out=='' && forward_url!=null)
				{
					window.location = forward_url;
				}
				else
				{
					mark_errors(which_form);
					if ($.scrollTo)
						$.scrollTo('.error_list', 'slow');
					
					// enable the submit button again.
					$('input[name="submit_all"]', which_form).attr("disabled", false);
				}
			});
	    }
	  }

		/**
		 * 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(which_form)
		{
			$('label', which_form).removeClass('mark');
			var first_label = "";
			$('ul.error_list label', which_form).each(function () {
				var label_for = $(this).attr('for');
				if (first_label == "")
				{
					first_label = label_for;
				}
				$('label', which_form).each(function () {
					if ($(this).attr('for')==label_for)
					{
						$(this).addClass('mark');
					}
				});
				$('ul.error_list label', which_form).removeClass('mark');
			});

			if (first_label != '')
				$("#"+first_label).focus();
		}


$(document).ready(function () {

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

  // Set default Delete Question

  /**
   * 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);
    
    //alert(my_form.attr('id'));
    
    return form_validate_func(my_form, fmv_del_question);

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

	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');

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

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

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

    // 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 ($('input[name="form_del_question"]', my_form).val()!="")
      del_question = $('input[name="form_del_question"]', my_form).val();

    if ($('input[name="del"]', my_form).attr('checked') == true) {
      if (confirm(del_question)) {
        return true;
      } else {
        $('input[name="del"]', my_form).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 ($(".form_error", my_form).html()!='' && $('input[name="form_updating"]', my_form).val()=="1") {
      // set form_validate value to 2 for the purposes of the processing php script
      my_form.children('input[name="form_validate"]').val('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
   */
  
});
