/*
| -------------------------------------------------------------------------
| FORM JS
| -------------------------------------------------------------------------
| These are the javascript function for use on pages with forms.
| The functions will validate a form using the jQuery form validation
| plugin and controls helper statements.
|
| The function must be run once for each form in the following way:
| form_controller ($("#my_form"))
|
| Dependent on the jQuery core and jQuery validate libraries.
|
| ALso required jquery-ui and jquery-ui-timepicker for date and time picker
| fields.
|
| Copyright Okio Ltd.
| June 2009
|
*/
function form_controller (form) {
	//Initiate the form validation, including deactivation of the submit button on validation
	$(document).ready(function(){
    	$(form).validate({
			errorElement: "span",
			errorClass: "error",
			submitHandler: function(form) {
				$('button[type=submit]', form).attr('disabled', 'disabled')
				$('button[type=submit]', form).addClass("inactive")
				form.submit()
		   	}
		})
  	})

	//Show and hide helper functions
	function hide_form_helpers() {
		$("span.helper")
			.hide()
	}
	function show_form_helper(helper) {
		$(helper).show()
	}

	//Hide all helpers on load
	hide_form_helpers()

	//Add events to each div area so that the helper element shows on mouseover
	$("form div:not(:has(span.error))").mouseover(function() {
		hide_form_helpers()
		$("span.helper", this)
			.show()
	})
	$("form div").mouseout(function() {
		hide_form_helpers()
	})

	// Add events to each input element so that the helper element shows on focus
	$("form input, form textarea, form select").focus(function() {
		hide_form_helpers()
		show_form_helper($(this).parent().find("span.helper"))
	})
	$("form input, form textarea, form select").blur(function() {
		hide_form_helpers()
	})
}

//Prevent lost form data
var catcher = function() {
  var changed = false
  $('form').each(function() {
    if ($(this).data('initialForm') != $(this).serialize()) {
      changed = true
      $(this).addClass('changed')
    } else {
      $(this).removeClass('changed')
    }
  })
  if (changed) {
    return 'One or more forms have changed but not been saved, and your changes will be lost if you continue.'
  }
}
$(function() {
  $('form').each(function() {
    $(this).data('initialForm', $(this).serialize())
  }).submit(function(e) {
    var formEl = this
    var changed = false
    $('form').each(function() {
      if (this != formEl && $(this).data('initialForm') != $(this).serialize()) {
        changed = true
        $(this).addClass('changed')
      } else {
        $(this).removeClass('changed')
      }
    })
    if (changed && !confirm('Another form has been changed. Continue with submission?')) {
      e.preventDefault()
    } else {
      $(window).unbind('beforeunload', catcher)
    }
  })
  $(window).bind('beforeunload', catcher)
})
