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);
}

$(document).ready(function(){
	$(".error").hide();
	
	$("form#contact_form").submit(function(){
		var str = $('#'+this.id).serialize();
	    
	    

		$("label#name_error").hide();
		$("label#email_error").hide();
	
		var name = $("input#name").val();
		if (name == "") {
			$("label#name_error").show();
			$("input#name").focus();
		}
		var email = $("input#email").val();
		if (!isValidEmailAddress(email)) {
			$("label#email_error").show();
			$("input#email").focus();
		}
		
		if ((name == "") || (!isValidEmailAddress(email))) {
			return false;		
		}

		$("#form_left", this).html('<div id="loading"> </div>');
		$.ajax({
			type: "POST",
			url: "contact.php",
			data: str,
			success: function(msg){
			$("#notification").ajaxComplete(function(event, request, settings){
			// Message Sent? Show the 'Thank You' message and hide the form
				if(msg == 'OK')
				{
					result = '<div id="sent_ok"></div><div class="notification_ok">Your message has been sent. Thank you!</div>';
					$("#form_left").hide();
					$("#notification").show();
					var blank = "";
					$("input#name").val(blank);
					$("input#email").val(blank);
					$("input#phone").val(blank);
					$("textarea#message").val(blank);
				}
				else
				{
					result = '<div id="sent_fail"></div><div class="notification_ok">Your message has been sent. Thank you!</div>';
					
					$("#form_left").hide();
					$("#notification").show();
					var blank = "";
					$("input#name").val(blank);
					$("input#email").val(blank);
					$("input#phone").val(blank);
					$("textarea#message").val(blank);
				}
				$(this).html(result);
			});
		}
	});
		return false;
});

});
