//Form validation functions
//Stephen Rojas 27 Sept. 2000

//*************************************
//User Profile Function
//*************************************

function ValidateUserProfile(form){
//variable declarations
 	var strValidChars;
	var strCheck;
	var strFailMessage;
	var strMinMessage;

	var strAll = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,'.~!@#$%^&*()-_+={}[]:;<>?0123456789";
	var strName = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '.-~";
	var strAddress = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456798.#-'*:(),"
	var strNumbers = "0123456798 -()";
	var strZip = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456798abcdefghijklmnopqrstuvwxyz- ";

//checking the validity of the common elements name, last name, title field etc.
	strValidChars = strName;
	strCheck = form.firstname;
	strFailMessage = "Please enter only valid characters in the 'First Name' field.";
	strMinMessage = "'First Name' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) { return (false);}

	strCheck = form.lastname;
	strFailMessage = "Please enter only valid characters in the 'Last Name' field.";
	strMinMessage = "'Last Name' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

	strCheck = form.title;
	strFailMessage = "Please enter only valid characters in the 'Title' field.";
	strMinMessage = "'Title' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

	if (!(CheckPickListValue(form.role,"Role"))){return (false);}
	
	strValidChars = strAll;
	strCheck = form.company;
	strFailMessage = "Please enter only valid characters in the 'Company Name' field.";
	strMinMessage = "'Company Name' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

	
//address data minus state	
	strValidChars = strAddress;
	strCheck = form.userAddress1;
	strFailMessage = "Please enter only valid characters in the 'Address 1' field.";
	strMinMessage = "'Company Address' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

	strValidChars = strAddress;
	strCheck = form.userAddress2;
	strFailMessage = "Please enter only valid characters in the 'Address 2' field.";
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}

	strValidChars = strName;
	strCheck = form.userCity;
	strFailMessage = "Please enter only valid characters in the 'City' field.";
	strMinMessage = "'City' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

//checking zip code validity
	strValidChars = strZip;
	strCheck = form.userZip;
	strFailMessage = "Please enter only valid characters in the 'Zip' field.";
	strMinMessage = "'Zip' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}


//Country verifications
//checking for at least one country 
var isUSCanada; 
if ( form.userSelectCountry.options[form.userSelectCountry.selectedIndex].value.length < 1 ){alert('Please select an option from the country drop down menu'); form.userSelectCountry.focus(); return (false);}
if ( (form.userOtherCountry.value.length<1) && (form.userSelectCountry.options[form.userSelectCountry.selectedIndex].value.length<1) ){alert('Country is a required field'); alert(form.userSelectCountry.value); form.userSelectCountry.focus(); return (false);}
if (  (( (form.userSelectCountry.selectedIndex) == 1) || ( (form.userSelectCountry.selectedIndex) == 2))) 
{isUSCanada = true;}
else 
{isUSCanada = false;}

//********USA/Canada Validation************
if (isUSCanada){
	if (form.userState.options[form.userState.selectedIndex].value.length<1){alert("Please select a State from the dropdown menu"); form.userState.focus(); return (false);}

	if ((form.userMainarea.value.length<1) && (form.userMainphone2.value.length<1)){alert("'Company Phone' is a required field"); form.userMainarea.focus(); return (false);}
	else
	{
		strCheck = form.userMainarea;
		strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
		strMinMessage = "'Company Phone' requires a 3 digit area code."
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
		
	    strCheck = form.userMainphone2;
		strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
		strMinMessage = "'Company Phone' requires a 3 digit exchange.";	
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
		
	    strCheck = form.userMainphone3;
		strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
		strMinMessage = "'Company Phone' requires a 4 digit number.";	
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 4, strMinMessage ) ) {return (false);}
		
	    strCheck = form.userExtension;
		strFailMessage = "Please enter only valid characters in the 'Extension' field.";
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}
	}   //end of validation on domestic company phone

	if ((form.directarea.value.length<1) && (form.directphone2.value.length<1)){alert("'Direct Phone' is a required field"); form.directarea.focus(); return (false);} 
	else
	{
		strCheck = form.directarea;
		strFailMessage = "Please enter only valid characters in the 'Direct Phone' field.";
		strMinMessage = "'Direct Phone' requires a 3 digit area code."
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
		
	    strCheck = form.directphone2;
		strFailMessage = "Please enter only valid characters in the 'Direct Phone' field.";
		strMinMessage = "'Direct Phone' requires a 3 digit exchange.";	
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
		
	    strCheck = form.directphone3;
		strFailMessage = "Please enter only valid characters in the 'Direct Phone' field.";
		strMinMessage = "'Direct Phone' requires a 4 digit number.";	
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 4, strMinMessage ) ) {return (false);}
	
		strCheck = form.directExtension;
		strFailMessage = "Please enter only valid characters in the 'Direct Extension' field.";
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}
	}   //end of validation on domestic direct phone

}         //******End of USA/Canada Validation********


else{     //*******Beginning of International Validation**********
if (form.userOtherCountry.value.length<1){alert("Please enter a country in the 'Other' country field"); form.userOtherCountry.focus(); return (false);}
if (form.userOtherState.value.length<1){alert("Please enter an international state or province"); form.userOtherState.focus(); return (false);}

strCheck = form.userOtherMainPhone;
strFailMessage = "Please enter only valid characters in the 'International Company Phone' field.";
strMinMessage = "'International Company Phone' is a required field";
if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

strCheck = form.otherDirectPhone;
strFailMessage = "Please enter only valid characters in the 'International Direct Phone' field.";
strMinMessage = "'International Direct Phone' is a required field";
if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}	
}         //*******End of International Validation

//Email validation
form.email.focus();
if (!CheckEmail(form.email.value)) {return (false);} 

return(true);	
}

//*************************************
//End of User Profile Function
//*************************************



//******************************************
//Shipping Address Validation Function
//******************************************
function ValidateShipping(form){
//variable declarations
 	var strValidChars;
	var strCheck;
	var strFailMessage;
	var strMinMessage;
	
	var strAll = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,'.~!@#$%^&*()-_+={}[]:;<>?0123456789";
	var strName = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '.-~";
	var strAddress = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456798.#-'*:(),"
	var strNumbers = "0123456798 -()";
	var strZip = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456798- abcdefghijklmnopqrstuvwxyz";

//checking the validity of the address fields
	strValidChars = strAddress;
	strCheck = form.address1;
	strFailMessage = "Please enter only valid characters in the 'Address Line 1' field.";
	strMinMessage = "The 'Address Line 1' field is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

	strValidChars = strAddress;
	strCheck = form.address2;
	strFailMessage = "Please enter only valid characters in the 'Address Line 2' field.";
	strMinMessage = "The 'Address Line 2' field is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}

	strValidChars = strAddress;
	strCheck = form.address3;
	strFailMessage = "Please enter only valid characters in the 'Address Line 3' field.";
	strMinMessage = "The 'Address Line 3' field is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}

	strValidChars = strAddress;
	strCheck = form.address4;
	strFailMessage = "Please enter only valid characters in the 'Address Line 4' field.";
	strMinMessage = "The 'Address Line 4' field is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}

	strValidChars = strAddress;
	strCheck = form.address5;
	strFailMessage = "Please enter only valid characters in the 'Address 5' field.";
	strMinMessage = "The 'Address Line 5' field is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}

//Validation of City

	strValidChars = strName;
	strCheck = form.city;
	strFailMessage = "Please enter only valid characters in the 'City' field.";
	strMinMessage = "'City' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}

//checking zip code validity
	strValidChars = strZip;
	strCheck = form.zip;
	strFailMessage = "Please enter only valid characters in the 'Zip' field.";
	strMinMessage = "'Zip' is a required field.";	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}


//Country verifications
//checking for at least one country 
	var isUSCanada; 
	if ( form.selectCountry.options[form.selectCountry.selectedIndex].value.length < 1 ){alert('Please select an option from the country drop down menu'); form.selectCountry.focus(); return (false);}
	if ( (form.otherCountry.value.length<1) && (form.selectCountry.options[form.selectCountry.selectedIndex].value.length<1) ){alert('Country is a required field'); form.selectCountry.focus(); return (false);}
	if (  (( (form.selectCountry.selectedIndex) == 1) || ( (form.selectCountry.selectedIndex) == 2))) 
		{isUSCanada = true;}
	else 
		{isUSCanada = false;}


	//********USA/Canada Validation************
	if (isUSCanada){
		if (form.State.options[form.State.selectedIndex].value.length<1){alert("Please select a State from the dropdown menu"); form.State.focus(); return (false);}
	
	/*	if ((form.mainarea.value.length<1) && (form.mainphone2.value.length<1)){alert("'Company Phone' is a required field"); form.mainarea.focus(); return (false);}
		else
		{
			strCheck = form.mainarea;
			strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
			strMinMessage = "'Company Phone' requires a 3 digit area code."
			if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
			
			strCheck = form.mainphone2;
			strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
			strMinMessage = "'Company Phone' requires a 3 digit exchange.";	
			if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
			
			strCheck = form.mainphone3;
			strFailMessage = "Please enter only valid characters in the 'Phone' field.";
			strMinMessage = "'Company Phone' requires a 4 digit number.";	
			if (!CheckField ( strValidChars, strCheck, strFailMessage, 4, strMinMessage ) ) {return (false);}
			
			strCheck = form.extension;
			strFailMessage = "Please enter only valid characters in the 'Extension' field.";
			if (!CheckField ( strValidChars, strCheck, strFailMessage, 0, strMinMessage ) ) {return (false);}
		}   //end of validation on domestic company phone

		if ((form.faxarea.value.length<1) && (form.fax2.value.length<1)){alert("'Fax Number' is a required field"); form.faxarea.focus(); return (false);}
		else
		{
			strCheck = form.faxarea;
			strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
			strMinMessage = "'Company Phone' requires a 3 digit area code."
			if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
			
			strCheck = form.fax2;
			strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
			strMinMessage = "'Company Phone' requires a 3 digit exchange.";	
			if (!CheckField ( strValidChars, strCheck, strFailMessage, 3, strMinMessage ) ) {return (false);}
		
		    strCheck = form.fax3;
			strFailMessage = "Please enter only valid characters in the 'Company Phone' field.";
			strMinMessage = "'Company Phone' requires a 4 digit number.";	
			if (!CheckField ( strValidChars, strCheck, strFailMessage, 4, strMinMessage ) ) {return (false);}		
		}   //end of validation on domestic company phone
	*/
	}         //******End of USA/Canada Validation********


	else{     //*******Beginning of International Validation**********
		if (form.otherCountry.value.length<1){alert("Please enter a country in the 'Other' country field"); return (false);}
		if (form.otherState.value.length<1){alert("Please enter an international state or province"); return (false);}
		
	/*	strCheck = form.otherMainPhone;
		strFailMessage = "Please enter only valid characters in the 'International Company Phone' field.";
		strMinMessage = "'International Company Phone' is a required field";
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}
		
		strCheck = form.otherFax;
		strFailMessage = "Please enter only valid characters in the 'International Direct Phone' field.";
		strMinMessage = "'International Fax' is a required field";
		if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return (false);}
	*/	
	}         //*******End of International Validation

return (true);
}
//******************************************
//End of Address Validation Function
//******************************************

//******************************************
//Cross Check 4 Variables Function
//******************************************
function CrossCheckX4(a,b,c,d){
if ( (a.value.length >= 1) && ( (b.value.length < 1) ||
	 (c.value.length < 1) || (d.value.length < 1) ) )
	 {return (false);}

if ( (b.value.length >= 1) && ( (a.value.length < 1) ||
	 (c.value.length < 1)|| (d.value.length < 1) ) )
	 {return (false);}

if ( (d.value.length >= 1) && ( (a.value.length < 1) ||
	 (b.value.length < 1)|| (c.value.length < 1) ) )
	 {return (false);}

if ( (c.value.length >= 1) && ( (a.value.length < 1) ||
	 (b.value.length < 1)|| (d.value.length < 1) ) )
	 {return (false);}
	
return true;
}
//******************************************
//End of Cross Check 4 Variables Function
//******************************************

//******************************************
//Cross Check 3 Variables Function
//******************************************
function CrossCheckX3(a,b,c){
if ( (a.value.length >= 1) && ( (b.value.length < 1) ||
	 (c.value.length < 1) ) )
	 {return (false);}

if ( (b.value.length >= 1) && ( (a.value.length < 1) ||
	 (c.value.length < 1) ) )
	 {return (false);}

if ( (c.value.length >= 1) && ( (a.value.length < 1) ||
	 (b.value.length < 1) ) )
	 {return (false);}
	
return true;
}
//******************************************
//End of Cross Check 3 Variables Function
//******************************************

//******************************************
//Form Date Validation Function
//******************************************
function dateValidation(testMonth, testDay, testYear){
var monthNumber
switch(testMonth){
	case "Jan":monthNumber="01";
		break;
	case "Feb":monthNumber="02";
		break;
	case "Mar":monthNumber="03";
		break;
	case "Apr":monthNumber="04";
		break;
	case "May":monthNumber="05";
		break;
	case "Jun":monthNumber="06";
		break;
	case "Jul":monthNumber="07";
		break;
	case "Aug":monthNumber="08";
		break;
	case "Sep":monthNumber="09";
		break;
	case "Oct":monthNumber="10";
		break;
	case "Nov":monthNumber="11";
		break;
	case "Dec":monthNumber="12";
		break;
	default:
}
var today=new Date();
var month=today.getMonth();
month=month+1;
var day=today.getDate();
var year=today.getYear();
var description = "delivery dates"

var Nav, IE
if (parseInt(navigator.appVersion) >= 4){
	if (navigator.appName == "Netscape"){
		Nav = true
	}
	else{
		IE = true
	}
}
if (Nav){
	year = year + 1900;
}

//alert("testmonth:" + testMonth + "| monthNumber: " + monthNumber + " | month:" + parseInt(month)); 

if ( (testDay == "") || (testMonth == "") || (testYear == "")){
	alert("Delivery date is a required field"); return(false);}
if (parseInt(testYear) < parseInt(year)){alert("The date entered is in the past"); return (false);}
if ((parseInt(testYear) == parseInt(year)) && (parseInt(monthNumber.replace(/^[0]+/g,"")) < parseInt(month))){alert("The date entered is in the past"); return (false);}
if ((parseInt(testYear) == parseInt(year)) && (parseInt(monthNumber.replace(/^[0]+/g,"")) == parseInt(month)) && (parseInt(testDay) < parseInt(day))){alert("The date entered is in the past"); return (false);}
if (!checkDate(monthNumber, testDay, testYear, description)){return false;}



return true;
}

//******************************************
//End Form Date Validation Function
//******************************************

//************************************************
//Validate Password and Confirmation Function
//************************************************

function ValidatePasswordAndConfirmation(form){
//Password variables
	var strCheck = form.password;
	var minLength = 6;
	var maxLength = 12;
	var minNumbers = 2;
	var minLetters = 3;
	var string1 = form.password;
	var string2 = form.passwordconfirm;
	
	//we don't use the function of ValidatePassword anymore since the requirement of password changed     
	//if (!ValidatePassword(strCheck, minLength, maxLength, minNumbers, minLetters)){return false; form.password.focus()}
	if (strCheck.value.length<6){alert("Password requires at least 6 characters.");return false; form.password.focus()}
	if (!CompareStrings(string1, string2)){alert("Password and confirmation do not match"); return false; form.password.focus()}

    return true;
}
//************************************************
//End Validate Password and Confirmation Function
//************************************************

//************************************************
//Username Validation Function
//************************************************
function ValidateUsername(form){
//Username Variables
	var strCheck = form.username;
	var strAll = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ,.~!@#$%^&*()-_+={}[]:;<>?0123456789";
	var strValidChars = strAll;
	var strFailMessage = "Please enter only valid characters in the 'User Name' field.";
	var strMinMessage = "'User Name' is a required field.";
	
	if (!CheckField ( strValidChars, strCheck, strFailMessage, 1, strMinMessage ) ) {return false; form.username.focus()} 
	return true;
}
//************************************************
//End Username Validation Function
//************************************************

//************************************************
//ZMA - Change Password Validation Function
//************************************************
function validateChangePassword(form, strOldPassword){
	var formValid = true;
	//Password variables
	var strOldPassword = strOldPassword;
	var strCheck = form.oldPassword;
	var minLength = 6;
	var maxLength = 12;
	var minNumbers = 2;
	var minLetters = 3;
	//alert(strOldPassword); alert(strCheck.value); 	    
	if (!ValidatePassword(strCheck, minLength, maxLength, minNumbers, minLetters)){formValid = false; form.oldPassword.focus(); return false;}
	if (strCheck.value.indexOf(strOldPassword)!=0){alert("You didn't enter a correct old password! Please check again."); formValid = false; form.oldPassword.focus(); return false;}
		
	strCheck = form.newPassword;
	
	var string1 = form.newPassword;
	var string2 = form.confirmPassword;
	//we don't use the function of ValidatePassword anymore since the requirment of password changed     
	//if (!ValidatePassword(strCheck, minLength, maxLength, minNumbers, minLetters)){return false; form.password.focus()}
	if (strCheck.value.length<6){alert("Password requires at least 6 characters.");}
	if (!CompareStrings(string1, string2)){alert("Password and confirmation do not match"); formValid = false; return false;}

	if (formValid){form.submit();}
}
//************************************************
//End Change Password Validation Function
//************************************************
