// http://www.reynoldsftw.com/2009/03/live-email-validation-with-jquery/
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);
}

$(function() {
	$('.error').hide();
	$('input.enquiry_textfield').css({backgroundColor:"#FFFFFF"});
	$('input.enquiry_textfield').focus(function(){
		$(this).css({backgroundColor:"#FFDDAA"});
	});
	$('input.enquiry_textfield').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});

	$(".btnSubmit").click(function() {
		// validate and process form
		// first hide any error messages
		$('.error').hide();
	
		var name = $("input#nameTB").val();
		if (name == "") {
			$("label#nameTB_error").show();
			$("input#nameTB").focus();
			return false;
		}

		var email = $("input#emailTB").val();
		if ((email == "") || (!isValidEmailAddress(email))) {
			$("label#emailTB_error").show();
			$("input#emailTB").focus();
			return false;
		}

		var str = $("#contact_form").serialize();
		//alert(str);
		var path = 'inc/process-contact.php';

		//show in progress screen
		$('#form_container').html("<div id='form_message'></div>");
		$('#form_message').html("<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr><td><h1><img src='content/images/wait30.gif'> Submitting Enquiry</h1></td></tr></table>");
		$.post(
			path, 
			str,
			function(data){
				// alert("Data Loaded: " + data); 
				$('#form_container').html("<div id='form_message'></div>");
				$('#form_message').html("<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr><td><h1>Enquiry Submitted!</h1></td></tr></table>")
					.append(data)
					.hide()
					.fadeIn(1500, function() {
					});
			},
			"html"
		);
		
		return false;
	});
});

runOnLoad(function(){
  $("input#nameTB").select().focus();
});

