function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateFirstNameEmpty(theForm.FirstName);
  reason += validateLastNameEmpty(theForm.LastName);
  reason += validateCompanyNameEmpty(theForm.CompanyName);
  reason += validateBillingAddressEmpty(theForm.BillingAddress);
  reason += validateBillingCityEmpty(theForm.BillingCity);
  reason += validateBillingZipEmpty(theForm.BillingZip);
  reason += validateBillingPhoneEmpty(theForm.BillingPhone1,theForm.BillingPhone2,theForm.BillingPhone3);
  reason += validateEmail(theForm.BillingEMail);
  reason += validateBillingPackageEmpty(theForm.BillingPackage);
  reason += validateUsername(theForm.Username);
  reason += validatePassword(theForm.Password);
  reason += validatecardnumberEmpty(theForm.cardnumber);
  reason += validatecvmvalueEmpty(theForm.cvmvalue);
  reason += validateCCHolderNameEmpty(theForm.CCHolderName);
  reason += validateCCBillingAddress1Empty(theForm.CCBillingAddress1);
  reason += validateCCBillingCityEmpty(theForm.CCBillingCity);
  reason += validateCCBillingZipEmpty(theForm.CCBillingZip);
  reason += validateCCBillingPhoneEmpty(theForm.CCBillingPhone1,theForm.CCBillingPhone2,theForm.CCBillingPhone3);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason + "\n\nPlease check your submitted data for errors");
    return false;
  }

}

function validateFirstNameEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "First Name cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateLastNameEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Last Name cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCompanyNameEmpty(fld) {
    var error = "";
	var illegalChars = /[\[\]\&\#\@\!\%\&\*\(\)]/;
 
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Company Name cannot be blank.\n"
	} else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Pink'; 
        error = "Entered company name contains illegal characters. Please use letters and numbers only\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateBillingAddressEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Billing Address cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateBillingCityEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "City cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateBillingZipEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Zip Code cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateBillingPhoneEmpty(fld1,fld2,fld3) {
    var error = "";
 	if (fld1.value.length == 0 && fld2.value.length == 0 && fld3.value.length == 0) {
        fld1.style.background = 'Pink'; 
		fld2.style.background = 'Pink';
		fld3.style.background = 'Pink';
        error = "Phone number cannot be blank.\n"
    } else {
		var error = "";
		if ((((fld1.value.length > 0)&&(fld1.value.length <3)) || ((fld2.value.length > 0)&&(fld2.value.length <3)) || ((fld3.value.length > 0)&&(fld3.value.length <4)))) {
        fld1.style.background = 'Pink'; 
		fld2.style.background = 'Pink';
		fld3.style.background = 'Pink';
        error = "Invalid phone number.\n"
    } else {
        fld1.style.background = 'White';
		fld2.style.background = 'White';
		fld3.style.background = 'White';
    }
	}
    return error;  
}

function validateBillingPackageEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 4) {
        fld.style.background = 'Pink'; 
        error = "Please select a billing package.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Pink'; 
        error = "Please enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Pink'; 
        error = "Entered username is the wrong length (6 to 14 characters).\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Pink'; 
        error = "Entered username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Pink';
        error = "Please enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length (8 to 14 characters). \n";
        fld.style.background = 'Pink';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Pink';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Pink';
    } else {
        fld.style.background = 'White';
    }
   return error;
}  
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Pink';
        error = "E-Mail address cannot be blank.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Pink';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Pink';
        error = "Entered email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatecardnumberEmpty(fld) {
    var error = "";
 
    if (fld.value.length < 3) {
        fld.style.background = 'Pink'; 
        error = "Credit card number cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatecvmvalueEmpty(fld) {
    var error = "";
 
    if (fld.value.length < 3) {
        fld.style.background = 'Pink'; 
        error = "Credit card security code cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCCHolderNameEmpty(fld) {
    var error = "";
 
    if (document.subscription_form.CCAddress.checked && fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Credit card holder name cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCCBillingAddress1Empty(fld) {
    var error = "";
 
    if (document.subscription_form.CCAddress.checked && fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Credit card billing address cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCCBillingCityEmpty(fld) {
    var error = "";
 
    if (document.subscription_form.CCAddress.checked && fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Credit card billing city cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCCBillingZipEmpty(fld) {
    var error = "";
 
    if (document.subscription_form.CCAddress.checked && fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Credit card billing zip code cannot be blank.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCCBillingPhoneEmpty(fld1,fld2,fld3) {
    var error = "";
 	if ((document.subscription_form.CCAddress.checked) && (fld1.value.length == 0 && fld2.value.length == 0 && fld3.value.length == 0)) {
        fld1.style.background = 'Pink'; 
		fld2.style.background = 'Pink';
		fld3.style.background = 'Pink';
        error = "Credit card billing phone cannot be blank.\n"
    } else {
		var error = "";
		if ((document.subscription_form.CCAddress.checked) && (((fld1.value.length > 0)&&(fld1.value.length <3)) || ((fld2.value.length > 0)&&(fld2.value.length <3)) || ((fld3.value.length > 0)&&(fld3.value.length <4)))) {
        fld1.style.background = 'Pink'; 
		fld2.style.background = 'Pink';
		fld3.style.background = 'Pink';
        error = "Invalid credit card billing phone.\n"
    } else {
        fld1.style.background = 'White';
		fld2.style.background = 'White';
		fld3.style.background = 'White';
    }
	}
    return error;  
}

/*function validateReferralTypeEmpty(fld) {
    var error = "";
 
    if (fld.value == "bob") {
        fld.style.background = 'Pink'; 
        error = "Please select a referral type.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}*/
