$(document).ready(function(){
	$("#submit").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var emailVal = $("#email").val();
		if(emailVal == '') {
			$("#email").after('<span class="error"><br />Enter a valid email address</span>');
			hasError = true;
			scrollTo(0,0);
		} else if(!emailReg.test(emailVal)) {	
			$("#email").after('<span class="error"><br />Enter a valid email address</span>');
			hasError = true;
			scrollTo(0,0);
		}
		
		var captchaVal = $("#captcha").val();
		if(captchaVal == '') {
			$("#captcha").after('<span class="error"><br />Enter the code you see in the security image</span>');
			hasError = true;
		}
		
		var firstnameVal = $("#first_name").val();
		if(firstnameVal == '') {
			$("#first_name").after('<span class="error"><br />Enter your first name</span>');
			hasError = true;
			scrollTo(0,0);
		}
		
		var surnameVal = $("#surname").val();
		if(surnameVal == '') {
			$("#surname").after('<span class="error"><br />Enter your surname</span>');
			hasError = true;
			scrollTo(0,0);
		}
		
		if(hasError == false) {
			//$(this).hide();
			$("#sendEmail li.buttons").append('<img src="images-2/flag-uk.jpg" alt="Loading" id="loading" />');

			$.post("form-handling/contact-form-process.php", $("#sendEmail").serialize(),
   					function(data){
						if(data.indexOf("OK") != -1) 
						{
						$("#sendEmail").slideUp("normal", function() 
						{				   
						$("#sendEmail").before('<h2>Thank You!</h2><p>Your message has been received by our system and is being processed.</p><p>Your unique reference is: <strong>' + $("#message_ref").val() + '</strong> please make a note of this and use it in any correspondence.</p><p>We aim to get our response to you within 1 hour <strong>during normal office hours</strong>.  If you have not heard from us within this time please call on 0800 44 88 001.</p><p>We have also sent an email to: <strong>' + $("#email").val() + '</strong> with this information.');											
						} );
						} else {
						alert(data); 
   						}}
				 );
		}
		
		return false;
	});						   
});