///////////////////////////////////////////////////////
// JavaScript form validation functions script.  
///////////////////////////////////////////////////////
function isBlank(Ctrl) {  // returns true if blank   
   if (Ctrl.value.length < 1) return true;
   else if (isEmpty(Ctrl.value)) return true;
   else return false; }
function isEmpty(s) { // prevents entering empty strings
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if ((c != ' ') && (c != '\n') && (c != '\t')) return false; 
   }
   return true; }
function isSelected(Ctrl, index){  // returns true if the index indicated is selected
   if (Ctrl.options[index].selected) return true;
   else return false; }   
function isNotANumber(Ctrl) {  // returns true if not a number
	if (isNaN(Ctrl.value)) return true;
	else return false; }	

function checkLength(Ctrl) {
	numericString = Ctrl.value;
	if (numericString.length == 10) return true;
	else return false;
} 

function checkPhoneOne(Ctrl) {
	numericString = Ctrl.value;
	if (numericString.length == 3) return true;
	else return false;
} 

function checkPhoneTwo(Ctrl) {
	numericString = Ctrl.value;
	if (numericString.length == 4) return true;
	else return false;
} 

function isChecked(Ctrl) {
	if (Ctrl.checked) return true;
	else return false; }   
function checkZip(Ctrl) {  // returns true if not properly formatted zip code
   zipString = Ctrl.value;
   if (zipString.length == 5) {
      if (isNaN(zipString)) return true;
   } else if (zipString.length < 5) {
      return true;
   } else if ( 
      zipString.length < 10 ||
      isNaN( zipString.substring(0,5) ) || 
      isNaN( zipString.substring(6,10) ) ||
      (zipString.substring(5,6) != '-') ) {
         return true;
   } 
   else return false;
}   // end checkZip()
function testSimpleEmail(Ctrl){  // returns true if invalid email
   var err=0;
   emailString = Ctrl.value;
   if (window.RegExp) {
      var regexEmail = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([a-zA-Z]{2,3})|(aero|coop|info|museum|name|biz))$/;
      return !regexEmail.test(emailString);
   } else {
      if (emailString.indexOf("@",1) == -1) err=1;  // need @ symbol
      if (emailString.indexOf("@",1) != emailString.lastIndexOf("@")) err=1;  // only one @ symbol
      if (emailString.indexOf(".",3) == -1) err=1;  // need at least one "."
      if (emailString.lastIndexOf(".") == (emailString.length-1)) err=1;  // can't end with a "."
      // check length
      if (err==0) {
         var at = (emailString.indexOf("@"))+1;
         var lastDot = (emailString.lastIndexOf("."))+1;
         // test to make sure there's at least one character between "at" and "lastDot"
         if (lastDot - at == 1) err=1;
      }
   }
   if (err==1) return true;
   else return false;
}   // end testSimpleEmail()
function Verify(f) {
   // set up params
   var ErrorString  = "";

   // form validation
   if (isBlank(f.txtLastName)) ErrorString += "\n - Your last name is required";
   if (isBlank(f.txtFirstName)) ErrorString += "\n - Your first name is required";
   if (isBlank(f.txtStreetAddress)) ErrorString += "\n - Your address is required";
   if (isBlank(f.txtCity)) ErrorString += "\n - Your city name is required";
   if (isBlank(f.txtZipCode)) {    ErrorString += "\n - Your zip code is required";
   } else if (checkZip(f.txtZipCode)) {    ErrorString += "\n - Your zip code is in an improper format";
   }
   if (isBlank(f.txtEmailAdd)) {    ErrorString += "\n - Your e-mail address is required";
   } else if (testSimpleEmail(f.txtEmailAdd)) {    ErrorString += "\n - Your e-mail address is in an improper format";
   }

   return errorAlert(ErrorString);
}
function errorAlert(e) {
    // If ErrorString "e" has content, there was at least one error; let them know.
   if (e.length > 0) {
      msg  = "____________________________________________________\n\n";
      msg += "  The form was not submitted for the following reason(s): \n";
      msg += "____________________________________________________\n";
      alert(msg + e);
      return false;
   } else {
        return true;
    }
} // end errorAlert()

