jQuery(document).ready(function($) {
	
	$('input#contact_btn').click(function() {
	
		var name = $("input#name_txt").val();
		var email = $("input#email_txt").val();
		var message = $("textarea#message_txt").val();
		
		if (name == "") {
			alert('You will need to provide a name to contact us.');
			return false;
		}
		
		if (email == "") {
			alert('We will need your email address so that we can respond to you.');
			return false;
		}

		if (message == "") {
			alert('Please enter a message so that we know how we can help you.');
			return false;
		}

		if(isValidEmailAddress(email))
		{
			var postParam = "<contacts><contact><name>" + name + "</name><email>" + email + "</email><message>" + message + "</message></contact></contacts>"

			postContact(postParam)

			clearText();
		}
		else
		{
			alert('We need a valid email address so that we can respond to you.');
			return false;
		}

		return;
	});

	function postContact(params)
	{
		var xmlhttp;
		if (window.XMLHttpRequest)
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else if (window.ActiveXObject)
		  {
		  // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }

		xmlhttp.open("POST","http://www.redwindsoftware.com/Inquisitor/postContact.ashx",true);
		xmlhttp.send(params);

	}

	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 clearText() {
 	$("input#name_txt").val("");
	$("input#email_txt").val("");
	$("textarea#message_txt").val("");
	return true;
}
	
