var reqString = "* Required";

$(document).ready(function(){
	$("table.form .required").change(function(){
		if($(this).val() != ""){
			$("#"+$(this).attr("id")+"Error").hide();
			$("#"+$(this).attr("id")+"ValidError").hide();
		}
	});
});

function validateContactForm(){		
	
	var fname = document.getElementById("fname");
	var lname = document.getElementById("lname");
	var tel = document.getElementById("tel");
	var addy = document.getElementById("address");
	var zip = document.getElementById("zip");
	var email = document.getElementById("email");
	var captcha = document.getElementById("captchaTxt");
	
	var valid = true;
	
	$(".errorDiv").hide();
	
	if(fname.value == "" || fname.value == reqString){
		$("#fnameError").show();
		fname.focus();
		valid = false;
	}
	if(lname.value == "" || lname.value == reqString){
		$("#lnameError").show();
		if(valid) lname.focus();
		valid = false;
	}
	if(tel.value == "" || tel.value == reqString){
		$("#telError").show();
		if(valid) tel.focus();
		valid = false;
	}
	if(addy.value == "" || addy.value == reqString){
		$("#addressError").show();
		if(valid) addy.focus();
		valid = false;
	}
	if(zip.value == "" || zip.value == reqString){
		$("#zipError").show();
		if(valid) zip.focus();
		valid = false;
	}
	if(email.value == "")
	{
		$("#emailError").show();
		if(valid) email.focus();
		valid = false;
	}
	else if(!validateEmail(email.value)){
		$("#emailValidError").show();
		if(valid) email.focus();
		valid = false;
	}	
	
	$("#captchaError").hide();
	if(captcha.value == "" || captcha.value.toLowerCase() != captchaList[captchaNum]){
		$("#captchaError").show();
		if(valid) captcha.focus();
		valid = false;
	}	
	
	return valid;		
}
function validateDealerForm(){		
	
	var fname = document.getElementById("fname");
	var lname = document.getElementById("lname");
	var tel = document.getElementById("tel");
	var territory = document.getElementById("territory");

	var email = document.getElementById("email");

	var valid = true;
	
	if(fname.value == "" || fname.value == reqString){
		fname.value = reqString;
		fname.focus();
		valid = false;
	}
	if(lname.value == "" || lname.value == reqString){
		lname.value = reqString;
		if(valid) lname.focus();
		valid = false;
	}
	if(tel.value == "" || tel.value == reqString){
		tel.value = reqString;
		if(valid) tel.focus();
		valid = false;
	}
	if(territory.value == "" || territory.value == reqString){
		territory.value = reqString;
		if(valid) territory.focus();
		valid = false;
	}
	
	if(!validateEmail(email.value)){
		email.value = reqString;
		if(valid) email.focus();
		valid = false;
	}	
	
	return valid;		
}
function validateEmail(email) {
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(email);
}
function clearField(input){
	var val = input.value;
	if(val == reqString){
		input.value = "";
	}
}
