/* SVN FILE: $Id$ */
/**
 * Contact form validation
 *
 * Copyright 2008, Freeman Fox Ltd
 *
 * Licensed under The Creative Commons License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright       Copyright 2007, FreemanFox Ltd
 * @version         $Revision$
 * @modifiedby      $LastChangedBy$
 * @lastmodified    $Date$
 * @license			http://creativecommons.org/licenses/by/3.0/ The Creative Commons License
 */

var WOCForm = 
{
	_formLength: null,
	validator: null,
	validation: 
	{
		rules: 
		{
			full_name: 'required',
			phone: 'required',
			email: 
			{
				required: true,
				email: true
			},
			post_code: 'required'
		},
		messages: 
		{
			full_name: 'Please enter your full name',
			phone: 'Please enter your phone number',
			email: 
			{
				required: 'Please enter your email address',
				email: 'That email address does not appear to be valid'
			},
			post_code: 'Please enter your post code'
		}
	},

	
	init: function(validation)
	{
		// Check to ensure woc-form exists
		if($('#woc-form').length === 0)
		{
			alert('The contact form must have an id of #woc-form, aborting validator setup');
			return false;
		}
		
		// Use validation argument if it was supplied
		if(validation != undefined) { this.validation = validation; }
		
		// Setup Validation plugin and check to make sure validator plugin exists
		try
		{
			$.validator.setDefaults(
			{
				submitHandler: function() { WOCForm.handleSubmit(); }
			});
		}
		catch(e)
		{
			alert('Validator jQuery plugin does not seem to exist, aborting validator Setup');
			return false;
		}

		/**
		 * For top page error reporting, make cursor into pointer on hover,
		 * or older browsers that don't support psueudo classes on elements apart from links
		 */
		$('#error-sucess-notice .error label').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); 
		});
		
		this.validator = $('#woc-form').validate(this.validation);
	},
		

	handleSubmit: function()
	{
		if(this.validator.form() === true)
		{
			$('#woc-form')[0].submit();
		}
		else
		{
			this.validator.focusInvalid();
		}
	},
	
	
	requiredStar: function()
	{
		return '<em><img src="http://www.freemanfoxmedia.com/img/required-star.gif" alt="required" class="required"></em>';
	}	
};
	
$(document).ready(function() 
{ 
	WOCForm.init(); 
});