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

  	reason += validateFirstname(contactForm.firstname);
	reason += validateLastname(contactForm.lastname);
	reason += validateEmail(contactForm.email);
	reason += validateMessage(contactForm.message);

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

function validateFirstname(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fdfcde'; 
        error = "Please enter your first name.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}


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

function validateMessage(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fdfcde'; 
        error = "Please enter a message.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
