
$(function() {
	$('.error').hide();
	$('input.required').focus(function(){
		$(this).css({backgroundColor:"#fdf1d6"});
	});
	$('input.required').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});
	$('textarea.required').focus(function(){
		$(this).css({backgroundColor:"#fdf1d6"});
	});
	$('textarea.required').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});
  
	$("#submit").click(function() {
							  
		$('.error').hide();
		
		var name = $("input#name").val();
		if (name == "") {
			$("label#name_error").show();
			$("input#name").focus();
			return false;
		}
		
		var phone = $("input#phone").val();
		if (phone == "") {
			$("label#phone_error").show();
			$("input#phone").focus();
			return false;
		}
		
		var email = $("input#email").val();
		if (email == "") {
			$("label#email_error").show();
			$("input#email").css({"background-image": "none"});
			$("input#email").focus();
			return false;
		} else {
			if(email != 0){
				if(isValidEmailAddress(email)){
					$("input#email").css({"background": "#FFF url('img/validYes.png') no-repeat right center"});
				} else {
					$("input#email").css({"background": "#FFF url('img/validNo.png') no-repeat right center"});
					return false;
				}
			} else {
				$("input#email").css({"background-image": "none"});		
			}
		}
		
		var message = $("textarea#message").val();
		if (message == "") {
			$("label#message_error").show();
			$("textarea#message").focus();
			return false;
		}
																
		var dataString = 'name='+ name + '&phone=' + phone + '&email=' + email + '&message=' + message;
		//alert (dataString);return false;
		
				$.ajax({
					type: "POST",
					url: "contact.php",
					data: dataString,
					success: function() {
					$('#contact_form').html("<div id='alert'></div>");
					$('#alert').html("<h1>Contact Form Submitted!</h1>")
					.append("<p>Thank you for your enquiry, you will be contacted shortly.</p>")
					.hide()
					.fadeIn(1000, function() {
						$('#alert');
					});
				}
		});
	return false;
	});
  
});

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}