function validateLFormOnSubmit(theForm) {
var reason = "";

  reason += validateLEmail(theForm.lemail);
  reason += validateLPassword(theForm.lpwd);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateRFormOnSubmit(theForm) {
var reason = "";

  reason += validateFUsername(theForm.fname);
  reason += validateLUsername(theForm.lname);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateSubDomain(theForm.subdomain);
  reason += validateCategory(theForm.category);
  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateRegFormOnSubmit(theForm) {

var reason = "";

  reason += validateFUsername(theForm.fname);
  reason += validateLUsername(theForm.lname);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateSubDomain(theForm.subdomain);
  reason += validateCategory(theForm.category);
  
  reason += validateCompanyName(theForm.company);
  reason += validateAddress1(theForm.address1);
  reason += validateCity(theForm.city);
  reason += validateZipCode(theForm.zip);
  reason += validateState(theForm.state);
  reason += validateCountry(theForm.country);
  reason += validateLPassword(theForm.pwd);

  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#CCFFFF'; 
        error = "- The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateFUsername(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, underscores, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF'; 
        error = "- You didn't enter a First Name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        fld.style.background = '#CCFFFF'; 
        error = "- The First Name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFFFF'; 
        error = "- The First Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateLUsername(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, underscores, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF'; 
        error = "- You didn't enter a Last Name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        fld.style.background = '#CCFFFF'; 
        error = "- The Last Name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFFFF'; 
        error = "- The Last Name 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 = '#CCFFFF';
        error = "- You didn't enter a password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 15)) {
        error = "- The password is the wrong length. Your password must be between 6 and 15 characters long. \n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The password contains illegal characters.\n";
        fld.style.background = '#CCFFFF';
    } 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 = '#CCFFFF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function validateLPassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "" || fld.value == "Password") {
        fld.style.background = '#CCFFFF';
        error = "- You didn't enter a password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 15)) {
        error = "- The password is the wrong length. Your password must be between 6 and 15 characters long.\n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The password contains illegal characters.\n";
        fld.style.background = '#CCFFFF';
    } 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 = '#CCFFFF';
    } 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 = '#CCFFFF';
        error = "- You didn't enter an Email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#CCFFFF';
        error = "- Please enter a valid Email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#CCFFFF';
        error = "- The Email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCategory(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, underscores, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF'; 
        error = "- You didn't enter a Category.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 40)) {
        fld.style.background = '#CCFFFF'; 
        error = "- The Category is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFFFF'; 
        error = "- You must select a Category.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateSubDomain(fld) {
    var error = "";
    var illegalChars = /\W/g; // allow letters, numbers
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF'; 
        error = "- You didn't enter a SubDomain Name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        fld.style.background = '#CCFFFF'; 
        error = "- The SubDomain Name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCFFFF'; 
        error = "- The SubDomain Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateLEmail(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 = '#CCFFFF';
        error = "- You didn't enter an Email address.\n";
    } else if (fld.value == "Admin@OurCompany.com") {              //test email for illegal characters
        fld.style.background = '#CCFFFF';
        error = "- Please enter a valid Email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#CCFFFF';
        error = "- Please enter a valid Email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#CCFFFF';
        error = "- The Email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "- You didn't enter a Phone Number.\n";
        fld.style.background = '#CCFFFF';
    } else if (isNaN(parseInt(stripped))) {
        error = "- The Phone Number contains illegal characters.\n";
        fld.style.background = '#CCFFFF';
    } else if (!(stripped.length == 10)) {
        error = "- The Phone Number is the wrong length.\n";
        fld.style.background = '#CCFFFF';
    } 
    return error;
}

function validateCompanyName(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, underscores, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF';
        error = "- You didn't enter a Company Name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 20)) {
        error = "- The Company Name is the wrong length. Your Company Name must be between 2 and 20 characters long. \n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The Company Name contains illegal characters. Only letters, numbers, and space are allowed.\n";
        fld.style.background = '#CCFFFF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateAddress1(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF';
        error = "- You didn't enter an Address.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 50)) {
        error = "- The Address is the wrong length. Your Address must be between 2 and 50 characters long. \n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The Address contains illegal characters. Only letters, numbers, and space are allowed.\n";
        fld.style.background = '#CCFFFF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateCity(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, underscores, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF';
        error = "- You didn't enter a City.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 50)) {
        error = "- The City is the wrong length. Your City must be between 2 and 50 characters long. \n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The City contains illegal characters. Only letters, numbers, and space are allowed.\n";
        fld.style.background = '#CCFFFF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateZipCode(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF';
        error = "- You didn't enter a Zip/Postal Code.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 7)) {
        error = "- The Zip/Postal Code is the wrong format. \n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The Zip/Postal Code contains illegal characters. Only letters, numbers, and space are allowed.\n";
        fld.style.background = '#CCFFFF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateState(fld) {
    var error = "";
    var illegalChars = /\W /g; // allow letters, numbers, and spaces
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF';
        error = "- You didn't enter a State/Province. \n";
    } else if ((fld.value.length < 2) || (fld.value.length > 50)) {
        error = "- The State/Province is the wrong length. At least 2 characters long. \n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The State/Province contains illegal characters. Only letters, numbers, and space are allowed.\n";
        fld.style.background = '#CCFFFF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}
function validateCountry(fld) {
    var error = "";
    var illegalChars = /\W/g; // allow letters only.
 
    if (fld.value == "") {
        fld.style.background = '#CCFFFF';
        error = "- You didn't enter a Country. \n";
    } else if ((fld.value.length < 3) || (fld.value.length > 20)) {
        error = "- The Country is the wrong length. Country must be between 3-20 characters. \n";
        fld.style.background = '#CCFFFF';
    } else if (illegalChars.test(fld.value)) {
        error = "- The Country contains illegal characters. Only letters, numbers, and space are allowed.\n";
        fld.style.background = '#CCFFFF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}






