
function validateFormOnSubmit(form) {
var reason = "";

  reason += validateName(form.name);
  reason += validateCity(form.city);
  reason += validateEmail(form.email);
  reason += validateState(form.state);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  //Uncomment the following for debugging purposes.

  //alert("All fields are filled correctly");
  //return false;
}
function validateName(fld) {
    var error = "";
    var illegalChars = /[^a-z A-Z-]/; // allow letters and spaces.
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Please enter your name.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 25)) {
        fld.style.background = 'Yellow'; 
        error = "The name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Your name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateCity(fld) {
    var error = "";
    var illegalChars = /[^a-z A-Z]/; // allow letters and spaces. 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter your city.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 50)) {
        error = "The city name is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The city name contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } 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 = 'Yellow';
        error = "Please enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateState(fld) {
    var error = "";
    var illegalChars = /[^a-z A-Z]/; // allow letters and spaces.
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Please select your state.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 25)) {
        fld.style.background = 'Yellow'; 
        error = "The state name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


