Please see script below. Why are variables defined as functions?
Code:
<!--
var hightLightColor = "yellow";
var normalBackgroundColor; // To be set in validateForm
function highLight(theField)
{
theField.style.backgroundColor = hightLightColor;
//theField.style.fontFamily = "Times New Roman,Georgia,Times";
}
function unHighLight(theField)
{
theField.style.backgroundColor = normalBackgroundColor;
//theField.style.fontFamily = "Times New Roman,Georgia,Times";
}
function validateForm(theForm)
{
//Set normal background color
normalBackgroundColor = theForm.Cust_Address2.style.backgroundColor;
var errorMsg = checkBillingAddressRequired(theForm);
if(errorMsg.length > 0)
{
alert(errorMsg);
return false;
}
errorMsg = checkShippAddressRequired(theForm);
if(errorMsg.length > 0)
{
alert(errorMsg);
return false;
}
//Check all errors other than required errors
errorMsg = checkPhone(theForm.Cust_HomePhone,"Home Phone");
var anotherMsg = checkPhone(theForm.Cust_WorkPhone,"Work Phone");
if (errorMsg.length > 0 && anotherMsg.length > 0)
errorMsg = errorMsg + "\n\r";
errorMsg = errorMsg + anotherMsg;
anotherMsg = checkZip(theForm.Cust_Zip);
if (errorMsg.length > 0 && anotherMsg.length > 0)
errorMsg = errorMsg + "\n\r";
errorMsg = errorMsg + anotherMsg;
anotherMsg = validEmail(theForm.Cust_Email,"E-mail");
if (errorMsg.length > 0 && anotherMsg.length > 0)
errorMsg = errorMsg + "\n\r";
errorMsg = errorMsg + anotherMsg;
anotherMsg = checkAddress(theForm);
if (errorMsg.length > 0 && anotherMsg.length > 0)
errorMsg = errorMsg + "\n\r";
errorMsg = errorMsg + anotherMsg;
if (errorMsg.length >0)
{
alert(errorMsg);
return false;
}
return true;
// <--------- End
}
function checkBillingAddressRequired(theForm)
{
// Check Required fields
var requiredList = new Array(new Array("Cust_First","First Name"),
new Array("Cust_Last","Last Name"),
new Array("Cust_Address1","Street Address"),
new Array("Cust_City","City"),
new Array("Cust_State","State"),
new Array("Cust_Zip","Zip Code"),
new Array("Cust_HomePhone","Home Phone"),
new Array("Cust_Email","E-mail")
);
var errorMsg = "";
errorMsg = validateRequiredFields(theForm,requiredList);
if (errorMsg.length > 0)
{
errorMsg = "To proceed, please enter the following required field(s) in billing address."
+"\n\r\n\r"
+ errorMsg;
}
return errorMsg;
}
function checkShippAddressRequired(theForm)
{
//Validate required fields for shipping address
var errorMsg = "";
if(!theForm.ShipToBilling.checked)
{
var requiredList = new Array(new Array("Ship_First","First Name in Shipping"),
new Array("Ship_Last","Last Name in Shipping"),
new Array("Ship_Address1","Street Address in Shipping"),
new Array("Ship_City","City in Shipping"),
new Array("Ship_State","State in Shipping"),
new Array("Ship_Zip","Zip Code in Shipping"),
new Array("Cust_HomePhone","Home Phone")
);
errorMsg = validateRequiredFields(theForm,requiredList);
if (errorMsg.length > 0)
{
errorMsg = "If you are shipping to an address different from billing address,\n\r"
+ "please enter the following required field(s).\n\r\n\r"
+ errorMsg;
}
}
return errorMsg;
}
function validateRequiredFields(theForm, requiredList)
{
var errorMsg = "";
var field;
var fieldDesc;
for(var i=0;i<requiredList.length;i++)
{
field = theForm.elements[requiredList[i][0]];
fieldDesc = requiredList[i][1];
if (!validRequired(field,fieldDesc))
{
errorMsg = errorMsg + fieldDesc + " is required.\n\r";
//field.style.backgroundColor = hightLightColor;
highLight(field);
}
else
//field.style.backgroundColor = normalBackgroundColor;
unHighLight(field);
}
return errorMsg;
}
function validRequired(formField,fieldLabel)
{
var result = true;
if (formField.value.length == 0)
result = false;
return result;
}
function checkAddress(theForm)
{
var errorMsg = "";
var address1 = theForm.Ship_Address1;
if(theForm.ShipToBilling.checked)
address1 = theForm.Cust_Address1;
var stripped = address1.value.replace(/\./g,""); //remove .
stripped = stripped.replace(/ /g,""); //remove space
stripped = stripped.toLowerCase();
if(stripped.indexOf("pobox") >=0 && theForm.shipMethod.value.indexOf('UPS') >= 0)
{
errorMsg ="You have to select Registered mail for shipping to a P.O. Box."
+" \n\rOr you have to modify your address to be a non P.O.Box address.";
}
if (errorMsg.length > 0)
{
highLight(theForm.shipMethod);
highLight(address1);
}
else
{
unHighLight(theForm.shipMethod);
unHighLight(address1);
}
return errorMsg;
}
function checkPhone(formField,fieldLabel) {
var errorMsg = "";
var stripped = formField.value.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
if ((formField.value != "") && isNaN(stripped)) {
errorMsg = errorMsg + "The \"" + fieldLabel + "\" number contains illegal characters.";
}
if ((formField.value != "") &&!(stripped.length == 10)) {
if (errorMsg.length >0)
errorMsg = errorMsg + "\n\r";
errorMsg = errorMsg + "The \"" + fieldLabel + "\" number is in the wrong length. Make sure you included an area code.";
}
if (errorMsg.length > 0)
highLight(formField);
else
unHighLight(formField);
return errorMsg;
}
function isDigit(formField,fieldLabel) {
var result = true;
var stripped = formField.value.replace(/[\.\ ]/g, ''); //strip out acceptable non-numeric characters
if (isNaN(stripped)) {
alert('The "' + fieldLabel +'" number contains illegal characters.');
formField.focus();
result = false;
}
return result;
}
function checkZip(formField) {
var errorMsg = "";
if ((formField.value != "")&&!(formField.value.length == 5)) {
errorMsg = "Please enter a 5-digit zip code.";
}
if(errorMsg.length > 0)
highLight(formField);
else
unHighLight(formField);
return errorMsg;
}
function validEmail(formField,fieldLabel)
{
var errorMsg = "";
if ((formField.value != "") && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
{
errorMsg = "Please enter a valid email address. eg: johnsmith@domainname.com";
}
if(errorMsg.length > 0)
highLight(formField);
else
unHighLight(formField);
return errorMsg;
}
function isEmailAddr(email)
{
var result = false;
var theStr = new String(email);
var index = theStr.indexOf("@");
if (index > 0)
{
var pindex = theStr.indexOf(".",index);
if ((pindex > index+1) && (theStr.length > pindex+1))
result = true;
}
return result;
}
//-->