// validations

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.username);
  reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert("Following fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmpty(theForm.BillFirstName);
  reason += validateEmpty(theForm.BillLastName);
  reason += validateEmpty(theForm.BillAddress);
  reason += validateEmpty(theForm.BillCity);
  reason += validateEmpty(theForm.BillState);
  reason += validateEmpty(theForm.BillZip);
  reason += validateEmpty(theForm.BillCountry);
  reason += validatePhone(theForm.BillPhone);
  reason += validateEmail(theForm.BillEMail);
  
  reason += validateEmpty(theForm.ShipFirstName);
  reason += validateEmpty(theForm.ShipLastName);
  reason += validateEmpty(theForm.ShipAddress);
  reason += validateEmpty(theForm.ShipCity);
  reason += validateEmpty(theForm.ShipState);
  reason += validateEmpty(theForm.ShipZip);
  reason += validateEmpty(theForm.ShipCountry);
  reason += validatePhone(theForm.ShipPhone);
  reason += validateEmail(theForm.ShipEMail);
      
  if (reason != "") {
    alert("Following fields need correction:\n" + reason);
    return false;
  }

  return true;
}

/* validate for required field - not empty */
function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

/* validate correct phone format */
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "Please enter your phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "You have entered invalid phone number.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Do not forget to enter your area code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}


/* validate correct email format */
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 = 'Yellow';
        error = "Please enter an email.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "You have entered invalid email.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}