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

	  reason += validateFirstName(theForm.FirstName);
	  reason += validateLastName(theForm.LastName);	
	  reason += validatePhoneType(theForm.PhoneType);	
	  reason += validatePhone(theForm.Telephone);	
	  reason += validateEmail(theForm.Email);	
	  reason += validateZip(theForm.PostalZipCode);
	  reason += validateLocation(theForm.CampusID);
		  
	  if (reason != "") {
		alert("PLEASE FIX THE FOLLOWING ERRORS:\n" + reason);
		return false;
	  }
	return true;
}


function validateFirstName(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        error = "The required field has not been filled in.\n"
    } else if (fld.value == "First Name") {
        error = "You must enter your first name.\n";
		fld.style.color = "red";
    } else {
    }
    return error;   
}

function validateLastName(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        error = "The required field has not been filled in.\n"
    } else if (fld.value == "Last Name") {
        error = "You must enter your last name.\n";
		fld.style.color = "red";
    } else {
    }
    return error;   
}


function validatePhoneType(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        error = "Please select a phone type.\n"
		fld.style.color = "red";
    } else if (fld.value == "0") {
        error = "Please select a phone type.\n";
		fld.style.color = "red";
    } else {
    }
    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 = "You didn't enter a phone number.\n";
		fld.style.color = "red";
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
		fld.style.color = "red";
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
		fld.style.color = "red";
    } 
    return error;
}

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 == "") {
        error = "You didn't enter an email address.\n";
		fld.style.color = "red";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "Please enter a valid email address.\n";
		fld.style.color = "red";
    } else if (fld.value.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";
		fld.style.color = "red";
    } else {
		
    }
    return error;
}

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

   if (fld.value == "") {
        error = "You didn't enter a zip code.\n";
		fld.style.color = "red";
    } else if (isNaN(parseInt(stripped))) {
        error = "The zip code contains illegal characters.\n";
		fld.style.color = "red";
    } else if (!(stripped.length == 5)) {
        error = "The zip code appears invalid.\n";
		fld.style.color = "red";
    } 
    return error;
}

function validateLocation(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        error = "Please select a campus.\n"
		fld.style.color = "red";
    } else if (fld.value == "0") {
        error = "Please select a campus.\n";
		fld.style.color = "red";
    } else {
    }
    return error;   
}

