$(function() {
	$('.error').hide();
	$('input.text-input').css({backgroundColor:"#FFFFFF"});
	$('input.text-input').focus(function(){
		$(this).css({backgroundColor:"#FFDDAA"});
	});
	$('input.text-input').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});

	$(".submit").click(function() {
		// validate and process form
		// first hide any error messages
		$('.errorBox').text("");
		$('.errorBox').hide();
		$('.messageBox').hide();
		$('.positiveBox').hide();
		
		var name = $("input#name").val();
		if (name == "") {
			$('.errorBox').text("You must provide a name!");
			$('.errorBox').show();
			$("input#name").focus();
			return false;
		}
		var email = $("input#email").val();
		if (email == "") {
			$('.errorBox').text("You must provide an e-mail!");
			$('.errorBox').show();
			$("input#email").focus();
			return false;
		}
		
		$('.messageBox').text("Please wait while we process your request...");
		$('.messageBox').show();
		
		setTimeout(function(){ self.SubmitForm() }, 750);
		
    return false;
	
	});
	
});

function SubmitForm() {

	var dataString = 'name='+ $("input#name").val() + '&address=' + $("input#address1").val() + '&towncity=' + $("input#town-city").val() + '&county=' + $("input#county").val() + '&email=' + $("input#email").val() + '&comments=' + $("textarea#comments").val();
	//alert (dataString);return false;
	
	$.ajax({
		type: "POST",
		url: "ContactSend.cfm",
		data: dataString,
		success: function() {
			$('.messageBox').hide();
			$('.positiveBox').text("Contact form submitted! We will be in touch soon!");
			$('.positiveBox').show();
			$(':input').val("");
			$('input.submit').val("Submit");
		}
	});
	
}

function IsEmailValid(sEmail)
{
	// Use Regular Expressions for Email Verification
	// Credit Reg-Ex Check: Gavin Sharp (http://www.glensharp.com/gavin/)
	REEmail = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/
	
	thisField = sEmail.name;
	
	//Check if blank, if so, ignore for now
	if (sEmail.value != '') {
		if (!sEmail.value.match(REEmail)) {
			alert("Please enter a valid email address.");	
			$("input#email").val("");
			$("input#email").focus();
		}
	}
}

$(document).ready(function(){
	$("input#name").select().focus();
});
