
var userRegEx = /^[a-zA-Z0-9]+$/;
var phoneRegEx = /^\d[\d|-]+\d$/;
var numRegex = /^\d+$/;

//password ascii range = (32,126)

function showMore(url) {
	//popWindow(url, name, width, height)
	//Calling syntax: <a href="javascript:showMore('url')">Link</a>
	var url = 'projects/'+ url;
	popWindow(url, '', 550, 500);
}

//------------------------------------------------

function submitCForm(form) {
	if (form.formAction.value == "Cancel") {
		return true;
	}
	else {
		return validateForm(form);
	}
}

//------------------------------------------------

function validate_password(text) {

	//var passwd = new String(text);
	//var len = text.length;
	//var passwordChars = text.toCharArray();

	error ="";

	for(i=0; i<text.length; i++ ) {
		intValue =  text.charCodeAt(i);
		if(intValue < 32 || intValue > 126 ) {
			error = "Password contains special character, which is not allowed! Please, check the password. Allowd Ascii values are from 32 to 126. ";
			break;
		}
	}
    return error;
}




//------------------------------
// validate Customer Admin Form
//------------------------------
function validateCustomerAdminForm(form) {

	var error = "";

	error += validateOtherFields(form);
	error += validateCreditTerms(form);

	if(error == null || error == "" ) {
		return true;
	}
	else {
		error = "Error: There is slight problem. \n\n" + error;
		alert(error);
		return false;
	}


}



//------------------------------

function validateForm(form) {

    //alert("Valdiating form");
 	var error = "";


var _action = form.formAction.value;
//alert("  form.formAction.value = " +  form.formAction.value);

if( _action != "Update" && _action != "update" ) {

	//validate login, password and password help
	error += validateLoginAndPasswordHelp(form);

}  //end of insert validations


error += validateOtherFields(form);


if(error == null || error == "" ) {
	return true;
}
else {
	error = "Error: There is slight problem. \n\n" + error;
	alert(error);
	return false;
}


}



//------------------------------------------



function validateOtherFields(form) {

var error = "";


//validate name
if ( form.name.value == null || form.name.value == ""  ) {
			error += " Name  can not be blank!\n";
		 }
if (form.name.value != null && form.name.value.length >  100  ) {
					error += " Name  can not exceed 100 characters !\n";
				}

//validate contactName
if ( form.contactName.value == null || form.contactName.value == ""  ) {
		error += " Contact Name  can not be blank!\n";
}
if (form.contactName.value != null && form.contactName.value.length >  100  ) {
		error += " Contact Name  can not exceed 100 characters !\n";
}

//validate email
if ( form.email.value == null || form.email.value == ""  ) {
			error += " Email  can not be blank!\n";
		 }
if (form.email.value != null && form.email.value.length >  150  ) {
			error += " Email  can not exceed 150 characters !\n";
}

regex = /^\s*[a-z][a-z\-\_0-9\.]+@[a-z-_0-9\.]+\.[a-z]{2,4}\s*$/;
if(form.email.value != null && form.email.value != "" && !regex.test(form.email.value)) {
		error += " Incorrect format for Email. Please, correct it.  Example - xyz@domain.com !\n";
}


//validate email1
if (form.email1.value != null && form.email1.value.length >  150  ) {
					error += " Email1  can not exceed 150 characters !\n";
				}
if(form.email1.value != null && form.email1.value != "" && !regex.test(form.email1.value)) {
		error += " Incorrect format for Alternate Email. Please, correct it.  Example - xyz@domain.com !\n";
}


//validate phone
if ( form.phone.value == null || form.phone.value == ""  ) {
			error += " Phone  can not be blank!\n";
		 }
if (form.phone.value != null && form.phone.value.length >  20  ) {
					error += " Phone  can not exceed 20 characters !\n";
				}

//regex = /\d{10}/;

if(form.phone.value != null && form.phone.value != "" && !phoneRegEx.test(form.phone.value)) {
		error += " Incorrect format for Phone. Please, correct it. Hyphens are allowed.  Example - 6504085402 !\n";}


//validate mobile
if (form.mobile.value != null && form.mobile.value.length >  20  ) {
					error += " Mobile  can not exceed 20 characters !\n";
				}
if(form.mobile.value != null && form.mobile.value != "" && !phoneRegEx.test(form.mobile.value)) {
		error += " Incorrect format for Mobile Phone. Please, correct it. Hyphens are allowed.  Example - 6504085402 !\n";
}

//validate fax
if (form.fax.value != null && form.fax.value.length >  20  ) {
					error += " Fax  can not exceed 20 characters !\n";
				}
if(form.fax.value != null && form.fax.value != "" && !phoneRegEx.test(form.fax.value)) {
		error += " Incorrect format for Fax. Please, correct it.  Hyphens are allowed.  Example - 6504085402 !\n";
}


//validate pager
if (form.pager.value != null && form.pager.value.length >  20  ) {
					error += " Pager  can not exceed 20 characters !\n";
				}
if(form.pager.value != null && form.pager.value != "" && !phoneRegEx.test(form.pager.value)) {
		error += " Incorrect format for Pager. Please, correct it.  Hyphens are allowed.  Example - 6504085402 !\n";
}


//validate shipping address
error += validateShippingAddress(form);


//validate billing address
error += validateBillingAddress(form);



//validate shippingAcctNo
if (form.shippingAcctNo.value != null && form.shippingAcctNo.value.length >  30  ) {
					error += " Shipping Account No  can not exceed 30 characters !\n";
				}


//validate notes
if (form.notes.value != null && form.notes.length >  1000  ) {
					error += " Notes  can not exceed 1000 characters !\n";
				}


return error;


}


//--------------------------------------------------

function validateShippingAddress(form) {


var error = "";


//validate shipToAddress1
if ( form.shipToAddress1.value == null || form.shipToAddress1.value == ""  ) {
			error += " Ship To Address1  can not be blank!\n";
		 }
if (form.shipToAddress1.value != null && form.shipToAddress1.value.length >  100  ) {
					error += " Ship To Address1  can not exceed 100 characters !\n";
				}

//validate shipToAddress2
if (form.shipToAddress2.value != null && form.shipToAddress2.value.length >  100  ) {
					error += " Ship To Address2  can not exceed 100 characters !\n";
				}

//validate shipToAddress3
if (form.shipToAddress3.value != null && form.shipToAddress3.value.length >  100  ) {
					error += " Ship To Address3  can not exceed 100 characters !\n";
				}

//validate shipToCity
if ( form.shipToCity.value == null || form.shipToCity.value == ""  ) {
			error += " Ship To City  can not be blank!\n";
		 }
if (form.shipToCity.value != null && form.shipToCity.value.length >  50  ) {
					error += " Ship To City  can not exceed 50 characters !\n";
				}

//validate shipToCountry
if ( form.shipToCountry.value == null || form.shipToCountry.value == ""  ) {
			error += " Ship To Country  can not be blank!\n";
		 }


//validate shipToState
if ( form.shipToCountry.value != null && form.shipToCountry.value == "US"  ) {
	if ( form.shipToState.value == null || form.shipToState.value == ""  ) {
			error += " Ship To State  can not be blank!\n";
	}
}
else {
	if ( form.shipToOtherState.value == null || form.shipToOtherState.value == ""  ) {
			error += " Ship To State  can not be blank!\n";
	}
}



if ( form.shipToCountry.value != null && form.shipToCountry.value == "US"  ) {

	//validate shipToZip
	if ( form.shipToZip.value == null || form.shipToZip.value == ""  ) {
		error += " Ship To Zipcode  can not be blank for US Address!\n";
	 }
	 else {
		if(!numRegex.test( form.shipToZip.value)) {
			error += " Ship To Zipcode should be number only!\n";
		}
	 }
	if (form.shipToZip.value != null && form.shipToZip.value.length >  9  ) {
		error += " Ship To Zipcode  can not exceed 9 characters !\n";
	}

}


return error;

}


//--------------------------------------------------


function processIsBilltoShiptoSameField(form, radio) {

	//alert("Value1 = " + radio );

	var isBilltoShiptoSameValue = getRadioValue(form.isBilltoShiptoSame);
	//alert("isBilltoShiptoSameValue = " + isBilltoShiptoSameValue);


	if (isBilltoShiptoSameValue != null && isBilltoShiptoSameValue == "Y" ) {

		form.billToAddress1.value = "";
		form.billToAddress2.value = "";
		form.billToAddress3.value = "";
		form.billToCity.value = "";
		form.billToState.value = "";
		form.billToOtherState.value = "";
		form.billToZip.value = "";
		form.billToCountry.value = "";

		form.billToAddress1.disabled = true;
		form.billToAddress2.disabled = true;
		form.billToAddress3.disabled = true;
		form.billToCity.disabled = true;
		form.billToState.disabled = true;
		form.billToOtherState.disabled = true;
		form.billToZip.disabled = true;
		form.billToCountry.disabled = true;

	}
	else {

		form.billToAddress1.disabled = false;
		form.billToAddress2.disabled = false;
		form.billToAddress3.disabled = false;
		form.billToCity.disabled = false;
		form.billToState.disabled = false;
		form.billToOtherState.disabled = false;
		form.billToZip.disabled = false;
		form.billToCountry.disabled = false;

		//if(form.billToCountry.value == null || form.billToCountry.value == "") {
			//form.billToCountry.value == "US";  //default
		//}
		valueIndex = setSelectValue(form, "billToCountry", form.billToCountry.options, "US");
		processBillToStateDisplay(valueIndex);

		form.billToAddress1.focus();
	}

}


//--------------------------------------------------

function setSelectValue(form, fieldName, selectOptions, fieldValue) {

	var valueIndex=0;
	//alert("selectOptions Length = " + selectOptions.length);
	for(var i=0; i< selectOptions.length; i++ ) {
		//form.shippingService.options[i] = new Option(UPS_options[i]);
		if(fieldValue != null &&  fieldValue == selectOptions[i].value ) {
			var evalStmt =  'form.' + fieldName + '.selectedIndex' + ' = ' + i;
			//alert("evalStmt = " + evalStmt);
			eval(evalStmt);
			valueIndex = i;
			break;
		}
	}

	return valueIndex;
}


//--------------------------------------------------

function validateBillingAddress(form) {

var error = "";

//alert("Value1 = " + radio );

var isBilltoShiptoSameValue = getRadioValue(form.isBilltoShiptoSame);

if(isBilltoShiptoSameValue != null && isBilltoShiptoSameValue != "Y" ) {


	//validate billToAddress1
	if ( form.billToAddress1.value == null || form.billToAddress1.value == ""  ) {
				error += " Bill To Address1  can not be blank, when billing address is different from shipping address!\n";
			 }
	if (form.billToAddress1.value != null && form.billToAddress1.value.length >  100  ) {
						error += " Bill To Address1  can not exceed 100 characters !\n";
					}

	//validate billToAddress2
	if (form.billToAddress2.value != null && form.billToAddress2.value.length >  100  ) {
						error += " Bill To Address2  can not exceed 100 characters !\n";
					}

	//validate billToAddress3
	if (form.billToAddress3.value != null && form.billToAddress3.value.length >  100  ) {
						error += " Bill To Address3  can not exceed 100 characters !\n";
					}

	//validate billToCity
	if ( form.billToCity.value == null || form.billToCity.value == ""  ) {
				error += " Bill To City  can not be blank, when billing address is different from shipping address!\n";
			 }
	if (form.billToCity.value != null && form.billToCity.value.length >  50  ) {
						error += " Bill To City  can not exceed 50 characters !\n";
					}

	//validate billToCountry
	if ( form.billToCountry.value == null || form.billToCountry.value == ""  ) {
				error += " Bill To Country  can not be blank, when billing address is different from shipping address!\n";
			 }


	//validate billToState
	if ( form.billToCountry.value != null && form.billToCountry.value == "US"  ) {
		if ( form.billToState.value == null || form.billToState.value == ""  ) {
				error += " Bill To State  can not be blank, when billing address is different from shipping address!\n";
		}
	}
	else {
		if ( form.billToOtherState.value == null || form.billToOtherState.value == ""  ) {
				error += " Bill To State  can not be blank, when billing address is different from shipping address!\n";
		}
	}



	if ( form.billToCountry.value != null && form.billToCountry.value == "US"  ) {

		//validate billToZip
		if ( form.billToZip.value == null || form.billToZip.value == ""  ) {
			error += " Bill To Zipcode  can not be blank for US Address, when billing address is different from shipping address!\n";
		 }
		 else {
			if(!numRegex.test( form.billToZip.value)) {
				error += " Bill To Zipcode should be number only!\n";
			}
		 }
		if (form.billToZip.value != null && form.billToZip.value.length >  9  ) {
			error += " Bill To Zipcode  can not exceed 9 characters !\n";
		}

	}


}

return error;


}


//--------------------------------------------------


function validateCreditTerms(form) {

var error = "";

//validate creditTerm
if (form.creditTerm.value != null && form.creditTerm.value.length >  30  ) {
					error += " Credit Term  can not exceed 30 characters !\n";
				}

//validate taxCode
if (form.taxCode.value != null && form.taxCode.value.length >  4  ) {
					error += " Tax Code  can not exceed 4 characters !\n";
				}

//validate sellerPermitNumber
if (form.sellerPermitNumber.value != null && form.sellerPermitNumber.value.length >  50  ) {
					error += " Seller Permit Number  can not exceed 50 characters !\n";
				}

//validate creditLimit
if (form.creditLimit.value != null && isNaN(form.creditLimit.value)  ) {
					error += " Credit Limit  should be number only!\n";
				}

return error;

}


//---------------------------------------------------


function validateLoginAndPasswordHelp(form) {

var error = "";


//validate login
if ( form.login.value == null || form.login.value == ""  ) {
			error += " Login  can not be blank!\n";
}
else {
	if (form.login.value != null && form.login.value.length < 6  ) {
						error += " Login  can not be less then 6 characters !\n";
	}
	if (form.login.value != null && form.login.value.length >  15  ) {
						error += " Login  can not exceed 15 characters !\n";
	}
}


if (form.login.value != null &&
	form.login.value != "" &&
	form.login.value.length <=  15  &&
	form.login.value.length >=  6 ) {
		//alert("form.login.value = " + form.login.value);
		if(!userRegEx.test(form.login.value) ) {
			error += " Login name can not have special character. \n Only alpha numeric values are allowed in Login name, which are 0 to 9 or a to z or A to Z. !\n";
		}

	}


//validate password
if ( form.password.value == null || form.password.value == ""  ) {
			error += " Password  can not be blank!\n";
		 }
else {
	if (form.password.value != null && form.password.value.length <  6  ) {
						error += " Password  can not be less then 6 characters !\n";
					}
	if (form.password.value != null && form.password.value.length >  15  ) {
						error += " Password  can not exceed 15 characters !\n";
					}
}

//validate password1
if ( form.password1.value == null || form.password1.value == ""  ) {
			error += " Re-entered Password  can not be blank!\n";
		 }
else {
	if (form.password1.value != null && form.password1.value.length <  6  ) {
					error += " Re-entered Password  can not be less then 6 characters !\n";
			}

	if (form.password1.value != null && form.password1.value.length >  15  ) {
						error += " Re-entered Password  can not exceed 15 characters !\n";
					}
}


//validate whether new passwords match
	if (form.password.value != null &&
		form.password.value.length <=  15  &&
	    form.password.value.length >= 6  &&
		form.password1.value != null &&
		form.password1.value.length <=  15  &&
	    form.password1.value.length >= 6   ) {

			if(form.password.value != form.password1.value) {
				error += "New password does not match. Please, make sure that both of new passwords match.!\n";
			}
			else {
				//only password value needs to be checked as both should be same at this point.
				error += validate_password(form.password.value);
			}

		}



//validate secretQuestion
if ( form.secretQuestion.value == null || form.secretQuestion.value == ""  ) {
			error += " Secret Question   can not be blank!\n";
		 }
if (form.secretQuestion.value != null && form.secretQuestion.length >  100  ) {
					error += " Secret Question  can not exceed 100 characters !\n";
				}

//validate secretAnswer
if ( form.secretAnswer.value == null || form.secretAnswer.value == ""  ) {
			error += " Secret Answer   can not be blank!\n";
		 }
if (form.secretAnswer.value != null && form.secretAnswer.length >  30  ) {
					error += " Secret Answer  can not exceed 30 characters !\n";
				}


return error;

} //end of function




