// JavaScript validation, client side validation
$(document).ready(function(){
	$("#name").click(function(){
	  $(".nameError").animate({ opacity: "hide" }, "slow");
	});
	$("#company").click(function(){
	  $(".companyError").animate({ opacity: "hide" }, "slow");
	});
	$("#email").click(function(){
	  $(".emailError").animate({ opacity: "hide" }, "slow");
	});
	$("#address").click(function(){
	  $(".addressError").animate({ opacity: "hide" }, "slow");
	});
	$("#message").click(function(){
	  $(".messageError").animate({ opacity: "hide" }, "slow");
	});
	$("#city").click(function(){
	  $(".cityError").animate({ opacity: "hide" }, "slow");
	});
	$("#zipcode").click(function(){
	  $(".zipcodeError").animate({ opacity: "hide" }, "slow");
	});

});

function validate(fld) {
	//function validates form onsubmit
	var isError = new Boolean(false);				//note: the reason there are no function calls is because onsubmit() returns any 
	var message = fld.message.value;
	message = message.replace(/^\s+|\s+$/g, '');
	str=fld.email.value;						//returns to any functions calls therefore making validation useless. 
	var at="@";
	var dot=".";		
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);			
	// Declaring required variables
	var i;
    var theString = "";
	if((fld.name.value == "") || (fld.name.value == null))				//null string validation on name
	{
		$(".nameError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
   if((fld.company.value == "") || (fld.company.value == null))				//null string validation on name
	{
		$(".companyError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
	/*if((fld.address.value == null) || (fld.address.value == ""))				//null string validation on name
	{
		$(".addressError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
	if((fld.city.value == null) || (fld.city.value == ""))				//null string validation on name
	{
		$(".cityError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
	if((fld.zipcode.value == null) || (fld.zipcode.value == ""))				//null string validation on name
	{
		$(".zipcodeError").animate({ opacity: "show" }, "slow");
		isError = true;
	}*/
	if((message == " ") || (message == null) || (message == ""))				//null string validation on name
	{
		$(".messageError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
	if((fld.email.value == "") || (fld.email.value == null) )		//validation for email 
	{
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
	else if (str.indexOf(at)==-1){
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}

	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}

	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}

	else if (str.indexOf(at,(lat+1))!=-1){
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}

	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}

	else if (str.indexOf(dot,(lat+2))==-1){
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
		
	else if (str.indexOf(" ")!=-1){
		$(".emailError").animate({ opacity: "show" }, "slow");
		isError = true;
	}
	
	if(isError == true)												//if error exist return false to onsubmit()
	{
		return false;
	}	
	else															//else start the progress bar and submit
	{
		return true;
	}
}

