/** jQuery form validation for all forms on Clive Ralph's website  **/

	// <![CDATA[
var emailRegExp = /(^[-!#$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+\/=?^_`{}|~0-9A-Z]+)*|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*")@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i;

	$(document).ready(function(){
		$('#form').submit(function(){ // If the submit button is clikced on a form with a form id=form, do the following:
			var formIsValid = true;
			$('#'+$(this).attr('id')+' .required').each(function() { // Find any input field with 'class="required"'
				if ($.trim($(this).val()) == "") { // If the form field is blank/empty, then create an error.
					//$(this).parent().addClass('error');
					$(this).parent().children('.errormessage').html('This field is required');
					formIsValid = false;
				} else {
					//$(this).parent().removeClass('error');
					$(this).parent().children('.errormessage').html('');
				}
			});
			
			$('#'+$(this).attr('id')+' .email').each(function() { // Find any input field with 'class="required"'
				if (!emailRegExp.test($(this).val())) { // if the email in the email field does not pass the regex test defined at top of this script, then create an error.
					//$(this).parent().addClass('error'); 
					$(this).parent().children('.errormessage').html('Please enter a valid email address');
					formIsValid = false;
				} else { 
					//$(this).parent().removeClass('error');
					$(this).parent().children('.errormessage').html('');
				}
			});
						
			return formIsValid;
		});
	});
	
	// ]]>
	//</script>
