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

  	reason += validateFirstname(contactForm.firstname);
	reason += validateLastname(contactForm.lastname);
	reason += validateEmail(contactForm.email);
	reason += validateCity(contactForm.city);
	reason += validatePhone(contactForm.phone);

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

function validateFirstname(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#eefb9a'; 
        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 = '#eefb9a'; 
		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 = '#eefb9a';
        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 validateCity(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#eefb9a'; 
        error = "Please enter your city.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

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



function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter your phone number.\n";
        fld.style.background = '#eefb9a';
    } else if (isNaN(parseInt(stripped))) {
        error = "Please enter a valid phone number.\n";
        fld.style.background = '#eefb9a';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you include your area code.\n";
        fld.style.background = '#eefb9a';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

