// JavaScript Document
function validateEmptyName(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#CCCCCC'; 
        error = "Please enter your name.\n"
    } 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 = '#CCCCCC';
        error = "Please enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#CCCCCC';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#CCCCCC';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateChoice(fld) {
    var error = "";
  
    if (fld.value == "Please Choose...") {
        fld.style.background = '#CCCCCC'; 
        error = "Please choose if you would like to be included on our seasonal special mailing list.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

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

  reason += validateEmptyName(theForm.MailingListName);
  reason += validateEmail(theForm.MailingListEmail);
  reason += validateChoice(theForm.Choice);

  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}