// JavaScript Document

function validateFormOnSubmit(theForm) {
var reason = "";
  reason += validateFirstname(theForm.firstname);
  reason += validateLastname(theForm.lastname);
  reason += validateEmail(theForm.email);
  reason += validatePhoneNumber(theForm.phonenumber);
  reason += validateZip(theForm.zip);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  // 2008.07.11 KJD: Add element to stop robots from submitting the form
  try{document.getElementById( 'passkey' ).value = 'authorized';}catch(e){}
  
  return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field(s) have not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateFirstname(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a first name.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


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

function validateZip(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a zip code.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 5)) {
        error = "The zip code is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The zip code contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The zip code must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } 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 = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
  var re = /^\(?[1-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
  //var re = /^(\([2-9]\d{2}\))|([2-9]\d{2})[^)][\.-]?\s?\d{3}[\s\.-]?\d{4}$/
  return re.test(str);
}


// phone number - strip out delimiters and check for 10 digits

function validatePhoneNumber(fld) {
	
	var error = "";
	
	if (fld.value == "") {
	   fld.style.background = 'Yellow';
	   error = "You didn't enter a phone number.\n";
	} else if (!isPhoneNumber(fld.value)) {
	   fld.style.background = 'Yellow';
	   error = "You didn't enter a valid U.S phone number.\n";
	}else {
	   fld.style.background = 'White';
	}
	return error;
}
