function validateForm(theForm) {

  var reason = "";

  reason += validateFirstname(theForm.firstname);
  reason += validateLastname(theForm.lastname);
  reason += validateState(theForm.state);
  reason += validatePhone(theForm.phone);
  reason += validateEmail(theForm.email);

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

  return true;
}


function validateState(fld) {
    var error = "";
  
    if (fld.selectedIndex == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't select a state.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;   
}


function validateFirstname(fld) {
    
    var error="";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You didn't enter your first name.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateLastname(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter your last name.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;   
}


function validatePhone(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a phone number.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;   
}		
	

function validateEmail(fld) {
    var error = "";
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
    
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter an email address.\n";	
    } else  if (!filter.test(fld.value)) { 
    	error = "Please enter a valid email address.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}
