// The functions in this file are used to validate the data in the contact_us.html files for all the main 
// sites. There is a separate function for each email destination and any required field should be validated.

var GM = "\" field is required.\nPlease fill in all required fields.";
var OLM = "\" field is required to have letters.\nPlease fill in all required fields.";
var TLM = "\" field is required to have 2 letters.\nPlease fill in all required fields.";
var NM = "\" field is required to have numbers.\nPlease fill in all required fields.";
var PM = "\" field is required. \nPlease include the area code.";
		
function validEmail(email) {
	returnValue = true;
	if (window.RegExp) {
		var emailFilter1 = new RegExp("^[A-Z0-9][A-Z0-9._-]*[A-Z0-9]@[A-Z0-9][A-Z0-9._-]*[A-Z0-9]\.[A-Z0-9]{2,4}$","i");
		var emailFilter2 = new RegExp("[.]{2,}");
		var emailFilter3 = new RegExp("--");
		var emailFilter4 = new RegExp("__");
		returnValue = (emailFilter1.test(email) && !emailFilter2.test(email) && !emailFilter3.test(email) && !emailFilter4.test(email));
	} else {
		returnValue = !(email.length==0 || email.indexOf("@")==-1 || email.indexOf(".")==-1 || email.indexOf(".")==0 || email.indexOf("_")==0 || email.indexOf("-")==0 || email.indexOf(".")==email.length || email.indexOf("_")==email.length || email.indexOf("-")==email.length || email.indexOf("..")!=-1 || email.indexOf("--")!=-1 || email.indexOf("__")!=-1 || email.indexOf("@.")!=-1 || email.indexOf(".@")!=-1 || email.indexOf("@")!=email.lastIndexOf("@") || email.substr(email.indexOf("@"),email.length-1).indexOf(".")==-1);
		for (i=0;i<email.length;i++) {
			if ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-@.".indexOf(email.toUpperCase().substring(i,i+1))==-1) {
				returnValue = false;
				break;
			}
		}
	}
	return returnValue;
}

function ValidateCS() {  
	with (document.csform){
		var newModel = Model.replace(/\./,'');
			Model.value = newModel;
			
		if (First.value.length==0 || !First.value.match(/^[a-zA-Z].*/)) {
			alert("The \"First Name"+OLM);
			First.focus();
			return false;
		}
		if (Last.value.length==0 || !Last.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"Last Name"+TLM);
			Last.focus();
			return false;
		}
		if (Address.value.length==0) {
			alert("The \"Address"+GM);
			Address.focus();
			return false;
		}
		if (City.value.length==0 || !City.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"City"+TLM);
			City.focus();
			return false;
		}
		if (State.value.length==0 || !State.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"State"+TLM);
			State.focus();
			return false;
		}
		if (Zip.value.length==0 || !Zip.value.match(/^[\w]{3,}.*/)) {
			alert("The \"Zip Code"+GM);
			Zip.focus();
			return false;
		}
		if (Country.value.length==0 || !Country.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"Country"+TLM);
			Country.focus();
			return false;
		}
		if (Phone.value.length==0 || !Phone.value.match(/[0-9]{3}.*[0-9]{3}.*[0-9]{4}.*/)) {
			alert("The \"Home Phone"+PM);
			Phone.focus();
			return false;
		}
		if (Email.value.length==0) {
			alert("The \"E-mail address"+GM);
			Email.focus();
			return false;
		}
		if (!validEmail(Email.value)) {
			alert("The \"E-mail Address you have entered is invalid.\nPlease enter a valid email address.");
			Email.focus();
			return false;
		}
		if (DateMonth.selectedIndex==0) {
			alert("The \"Date of Purchase"+GM);
			DateMonth.focus();
			return false;
		}
		if (DateYear.selectedIndex==0) {
			alert("The \"Date of Purchase"+GM);
			DateYear.focus();
			return false;
		}
		if (where.selectedIndex==0) {
			alert("The \"Location of Purchase"+GM);
			where.focus();
			return false;
		}
		if (Model.value.length==0 || !Model.value.match(/^[\w]{2,}.*/)) {
			alert("The \"Model"+GM);
			Model.focus();
			return false;
		}
		if (Serial.value.length==0 || !Serial.value.match(/^[\w]{2,}.*/)) {
			alert("The \"Serial Number"+GM);
			Serial.focus();
			return false;
		}
		if (Comments.value.length==0) {
			alert("The \"Comments"+GM);
			Comments.focus();
			return false;
		}
	}
	return true;
} 

function ValidateSI() {  
	with (document.siform){
		if (First.value.length==0 || !First.value.match(/^[a-zA-Z].*/)) {
			alert("The \"First Name"+OLM);
			First.focus();
			return false;
		}
		if (Last.value.length==0 || !Last.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"Last Name"+TLM);
			Last.focus();
			return false;
		}
		if (Country.value.length==0 || !Country.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"Country"+TLM);
			Country.focus();
			return false;
		}
		if (Phone.value.length==0 || !Phone.value.match(/[0-9]{3}.*[0-9]{3}.*[0-9]{4}.*/)) {
			alert("The \"Home Phone"+PM);
			Phone.focus();
			return false;
		}
		if (Email.value.length==0) {
			alert("The \"E-mail address"+GM);
			Email.focus();
			return false;
		}
		if (!validEmail(Email.value)) {
			alert("The \"E-mail Address you have entered is invalid.\nPlease enter a valid email address.");
			Email.focus();
			return false;
		}
		if (Comments.value.length==0) {
			alert("The \"Comments"+GM);
			Comments.focus();
			return false;
		}
		if (Order.value.length>=3 && (Order.value.substring(0,3)=='WCS' || Order.value.substring(0,3)=='ICS')) {
			emailto.value='service';
		}
	}
	return true;
} 

function ValidateBI() {  
	with (document.biform){
		if (First.value.length==0 || !First.value.match(/^[a-zA-Z].*/)) {
			alert("The \"First Name"+OLM);
			First.focus();
			return false;
		}
		if (Last.value.length==0 || !Last.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"Last Name"+TLM);
			Last.focus();
			return false;
		}
		if (Country.value.length==0 || !Country.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"Country"+TLM);
			Country.focus();
			return false;
		}
		if (Phone.value.length==0 || !Phone.value.match(/[0-9]{3}.*[0-9]{3}.*[0-9]{4}.*/)) {
			alert("The \"Home Phone"+PM);
			Phone.focus();
			return false;
		}
		if (Email.value.length==0) {
			alert("The \"E-mail address"+GM);
			Email.focus();
			return false;
		}
		if (!validEmail(Email.value)) {
			alert("The \"E-mail Address you have entered is invalid.\nPlease enter a valid email address.");
			Email.focus();
			return false;
		}
		if (Order.value.length==0 || !Order.value.match(/^[\w]{2,}.*/)) {
			alert("The \"Order Number"+GM);
			Order.focus();
			return false;
		}
		if (Comments.value.length==0) {
			alert("The \"Comments"+GM);
			Comments.focus();
			return false;
		}
	}
	return true;
} 

function ValidateWM() {  
	with (document.wmform){
		if (First.value.length==0 || !First.value.match(/^[a-zA-Z].*/)) {
			alert("The \"First Name"+OLM);
			First.focus();
			return false;
		}
		if (Last.value.length==0 || !Last.value.match(/^[a-zA-Z]{2,}.*/)) {
			alert("The \"Last Name"+TLM);
			Last.focus();
			return false;
		}
		if (Email.value.length==0) {
			alert("The \"E-mail address"+GM);
			Email.focus();
			return false;
		}
		if (!validEmail(Email.value)) {
			alert("The \"E-mail Address you have entered is invalid.\nPlease enter a valid email address.");
			Email.focus();
			return false;
		}
		if (Comments.value.length==0) {
			alert("The \"Comments"+GM);
			Comments.focus();
			return false;
		}
	}
	return true;
} 
