function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
		return (false);
		}	
	} 
	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}


function ifNum(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	 return true;
}

function checkVar(theForm,theVar){
	var theFormA = document.forms[theForm];
	if (theFormA.elements[theVar].value.replace(/\s+/i,"")==""){
         theFormA.elements[theVar].className = 'errorSelect';
        return false;
	}else{
        theFormA.elements[theVar].className = '';
        return true;
    }
}

function checkNum(theForm,theVar,theSize){
	var theFormN = document.forms[theForm];
	if (theFormN.elements[theVar].value.length < theSize){
         theFormN.elements[theVar].className = 'errorSelect';
        return false;
	}else{
        theFormN.elements[theVar].className = '';
        return true;
    }
}

function checkVarEmail(theForm,theVar){
	var theFormE = document.forms[theForm];
	if(!check_email(theFormE.elements[theVar].value)){
         theFormE.elements[theVar].className = 'errorSelect';
		 return false;
    }else{
        theFormE.elements[theVar].className = '';
        return true;
    }
}

function clearPhones(theFormName){
	document.forms[theFormName].elements['HomePhone'].className = '';
	document.forms[theFormName].elements['WorkPhone'].className = '';
	document.forms[theFormName].elements['MobilePhone'].className = '';
}

function checkFIN(){//contact enquiry form
	var theFormName = 'financeForm';
	var firstName = checkVar(theFormName,'FirstName');
	var postCode = checkNum(theFormName,'PostCode',4);
	var email = checkVarEmail(theFormName,'EmailAddress');
	var phone = false;
	var homePhone = checkNum(theFormName,'HomePhone',8);
	var workPhone = checkNum(theFormName,'WorkPhone',8);
	var mobilePhone = checkNum(theFormName,'MobilePhone',8);
	var financeAmount = checkNum(theFormName,'FinanceAmount',1);
	var comments = checkVar(theFormName,'Comments');

	if(homePhone || workPhone || mobilePhone){
		phone = true;
		clearPhones(theFormName);
	}
	if(firstName && postCode && email && phone && financeAmount && comments){
        document.forms[theFormName].submit();
    }
}

function checkFL(){//fleet enquiry form
	var theFormName = 'fleetForm';
	var firstName = checkVar(theFormName,'firstName');
	var suburb = checkVar(theFormName,'suburb');
	var postCode = checkNum(theFormName,'postCode',4);
	var email = checkVarEmail(theFormName,'email');
	var phone = checkNum(theFormName,'phone',8);
	var howHear = checkVar(theFormName,'howHear');

	if(firstName && suburb && postCode && email && phone && howHear){
        document.forms[theFormName].submit();
    }
}

function checkSER(){//service enquiry form
	var theFormName = 'serviceForm';
	var firstName = checkVar(theFormName,'FirstName');
	var lastName = checkVar(theFormName,'SurName');
	var suburb = checkVar(theFormName,'Suburb');
	var postCode = checkNum(theFormName,'PostCode',4);
	var email = checkVarEmail(theFormName,'EmailAddress');
	var phone = false;
	var homePhone = checkNum(theFormName,'HomePhone',8);
	var workPhone = checkNum(theFormName,'WorkPhone',8);
	var mobilePhone = checkNum(theFormName,'MobilePhone',8);
	var make = checkVar(theFormName,'Make');
	var model = checkVar(theFormName,'Model');
	var odometer = checkNum(theFormName,'VehOdometer',2);
	var year = checkNum(theFormName,'VehYear',2);
	var preferredDate = checkVar(theFormName,'Pref_Date');
	var preferredTime = checkVar(theFormName,'Pref_Time');

	if(homePhone || workPhone || mobilePhone){
		phone = true;
		clearPhones(theFormName);
	}

	if(firstName && lastName && suburb && postCode && email && phone && make && model && year && odometer && preferredDate && preferredTime){
        document.forms[theFormName].submit();
    }
}

function checkPAR(){//parts enquiry form
	var theFormName = 'partsForm';
	var firstName = checkVar(theFormName,'FirstName');
	var lastName = checkVar(theFormName,'SurName');
	var suburb = checkVar(theFormName,'Suburb');
	var postCode = checkNum(theFormName,'PostCode',4);
	var email = checkVarEmail(theFormName,'EmailAddress');
	var phone = false;
	var homePhone = checkNum(theFormName,'HomePhone',8);
	var workPhone = checkNum(theFormName,'WorkPhone',8);
	var mobilePhone = checkNum(theFormName,'MobilePhone',8);
	var make = checkVar(theFormName,'Make');
	var model = checkVar(theFormName,'Model');
	var year = checkNum(theFormName,'VehYear',2);
	var reg = checkVar(theFormName,'RegNum');
	var partDescription = checkVar(theFormName,'PartDesc1');
	var partQty = checkVar(theFormName,'PartQty1');

	if(homePhone || workPhone || mobilePhone){
		phone = true;
		clearPhones(theFormName);
	}
	if(firstName && lastName && suburb && postCode && email && phone && make && model && year && reg && partDescription && partQty){
        document.forms[theFormName].submit();
    }
}

function checkCON(){//contact enquiry form
	var theFormName = 'contactForm';
	var firstName = checkVar(theFormName,'FirstName');
	var postCode = checkNum(theFormName,'PostCode',4);
	var email = checkVarEmail(theFormName,'EmailAddress');
	var phone = false;
	var homePhone = checkNum(theFormName,'HomePhone',8);
	var workPhone = checkNum(theFormName,'WorkPhone',8);
	var mobilePhone = checkNum(theFormName,'MobilePhone',8);
	var comments = checkVar(theFormName,'Comments');

	if(homePhone || workPhone || mobilePhone){
		phone = true;
		clearPhones(theFormName);
	}
	if(firstName && postCode && email && phone && comments){
        document.forms[theFormName].submit();
    }
}