function validateForm() {

	if ($("input[@name='firstName']").val() == "") {
		alert('Please enter a first name.');
	} else if ($("input[@name='lastName']").val() == "") {
		alert('Please enter a last name.');
	} else if ($("input[@name='add1']").val() == "") {
		alert('Please fill out the Address 1 field.');
	} else if ($("input[@name='city']").val() == "") {
		alert('Please fill out the City field.');
	} else if ($("input[@name='state']").val() == "") {
		alert('Please fill out the State field.');
	} else if ($("input[@name='zipcode']").val() == "") {
		alert('Please fill out the Zipcode field.');
	} else if ($("input[@name='email']").val() == "") {
		alert('Please enter a vaild e-mail address.');
	} else {
		createFormObject();
	}
}

function createFormObject(){

	var formObject = {};

	formObject.firstName = $("input[@name='firstName']").val();	
	formObject.lastName = $("input[@name='lastName']").val();
	formObject.add1 = $("input[@name='add1']").val();	
	formObject.add2 = $("input[@name='add2']").val();
	formObject.city = $("input[@name='city']").val();
	formObject.state = $("input[@name='state']").val();
	formObject.zipcode = $("input[@name='zipcode']").val();
	formObject.country = $("input[@name='country']:checked").val();
	formObject.email = $("input[@name='email']").val();
	formObject.phone = $("input[@name='phone']").val();
	formObject.dealership = $("input[@name='dealership']:checked").val();
	formObject.businessName = $("input[@name='businessName']").val();	
	formObject.comments = $("textarea[@name='comments']").val();	
	
	sendForm(formObject);

}


var formVisible = "no";

function showHideForm() {

	if(formVisible=="no"){
		formVisible="yes";
		$('#contactFormFailed').css("display","none");
		$('#contactFormThankyou').css("display","none");
		$('#contactFormWrapper').css("display","block");
		$('#contactFormInstructions').css("display","block");
		$('#contactForm').css("display","block");
		$("input[@name='firstName']").val("");
		$("input[@name='lastName']").val("");
		$("input[@name='phoneName']").val("");
	} else {
		formVisible="no";
		$('#contactFormWrapper').css("display","none");
	}
}


function sendForm(formObject) {

	$('#contactForm').css("display","none");
	$('#contactFormSending').css("display","block");

	var theData = "beginning=none";
	
	theData += "&firstName="+escape(formObject.firstName);
	theData += "&lastName="+escape(formObject.lastName);
	theData += "&add1="+escape(formObject.add1);
	theData += "&add2="+escape(formObject.add2);
	theData += "&city="+escape(formObject.city);
	theData += "&state="+escape(formObject.state);
	theData += "&zipcode="+escape(formObject.zipcode);
	theData += "&country="+escape(formObject.country);
	theData += "&email="+escape(formObject.email);
	theData += "&phone="+escape(formObject.phone);
	theData += "&dealership="+escape(formObject.dealership);
	theData += "&businessName="+escape(formObject.businessName);
	theData += "&comments="+escape(formObject.comments);

	$.ajax({
	   type: "post",
	   url: "process_form.asp",
	   data: theData,
	   processData: false,
	   success: handleResponse
	 });

}

function handleResponse(theData) {

	var theArray = [];
    $("theResponse", theData).each(function(i) 
    {
		var theResponse = {};
        theResponse.name = $('theStatus', this).text();
        theArray.push(theResponse);
    });

	if(theArray[0].name=="success"){
		$('#contactFormSending').css("display","none");
		$('#contactFormThankyou').css("display","block");
	} else {
		$('#contactFormSending').css("display","none");
		$('#contactForm').css("display","block");
		$('#contactFormFailed').css("display","block");
	}
	
}
