$(document).ready(function() {
	//Bind Click event to the short lead forms submit button once
	$('#short-lead-form input[name|="submit"]').one('click', shortLeadFormSubmit);
	});

var shortLeadFormSubmit = function () {
	var formData = [];
	formData[0] = document.forms.short_lead_form.name.value;
	formData[1] = document.forms.short_lead_form.email.value;
	formData[2] = document.forms.short_lead_form.phone.value;
	var isError = shortLeadFormValidate(formData);
	if(!isError){
		$("#ajax-message").load("js/ajax/ajax_short_lead_form.php",
								{name: formData[0], email: formData[1], phone: formData[2]},
								function () {
									//Clear form after data is sent to server to prevent duplicate submissions
									shortLeadFormClear();
									});
		} else {
		$("#ajax-message").html('<p class="error">Please correct the fields marked in red.</p>');
		}
	$('#short-lead-form input[name|="submit"]').one('click', shortLeadFormSubmit);
	};

var shortLeadFormClear = function () {
	document.forms.short_lead_form.reset();
	};
	
var shortLeadFormValidate = function (formArray) {
	var isError = false;
	//Check Name Field
	if(formArray[0] == ''){
		$('#short-lead-form input[name|="name"]')
			.stop()
			.animate({ 'background-color': '#ffffff' }, 1, function() {
				$('#short-lead-form input[name|="name"]')
				.addClass('error')
				.effect("highlight", {color: '#D44040'}, 1500);
				});
		isError = true;
		} else {
		$('#short-lead-form input[name|="name"]')
			.stop()
			.animate({ 'background-color': '#ffffff' }, 1)
			.removeClass('error');
		}
	//Check Email Field
	if(formArray[1].match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/) == null){
		$('#short-lead-form input[name|="email"]')
			.stop()
			.animate({ 'background-color': '#ffffff' }, 1, function() {
				$('#short-lead-form input[name|="email"]')
				.addClass('error')
				.effect("highlight", {color: '#D44040'}, 1500);
				});
		isError = true;
		} else {
		$('#short-lead-form input[name|="email"]')
			.stop()
			.animate({ 'background-color': '#ffffff' }, 1)
			.removeClass('error');		
		}
	//Check Phone Field
	if(formArray[2].match(/((\(\d{3}\)?)|(\d{3}))([\s-./]?)(\d{3})([\s-./]?)(\d{4})/) ==  null){
		$('#short-lead-form input[name|="phone"]')
			.stop()
			.animate({ 'background-color': '#ffffff' }, 1, function() {
				$('#short-lead-form input[name|="phone"]')
				.addClass('error')
				.effect("highlight", {color: '#D44040'}, 1500);
				});
			
		isError = true;
		} else {
		$('#short-lead-form input[name|="phone"]')
			.stop()
			.animate({ 'background-color': '#ffffff' }, 1)
			.removeClass('error');		
		}
	return isError;
	};
