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

// Required Fields  
  reason += validateFirstname(theForm.fname);
  reason += validateLastname(theForm.lname);
	reason += validateEmail(theForm.email);
	reason += validateMessage(theForm.message);
	reason += validateCaptcha(theForm.recaptcha_response_field);
	
// Optional Fields
  reason += validatePhoneOpt(theForm.phone);
	        
  if (reason != "") {
    alert("Some fields are missing or incorrect:\n" + reason);
    return false;
  }

  return true;
}

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

function validateFirstname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a First Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The First Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateLastname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Last Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Last Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The Password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The Password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The Password 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;
}

function validateEmail2(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 Verification.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid Email Address Verification.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Email Address Verification contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function verifyEmail(fld1,fld2) {
    var error="";
    var tfld1 = trim(fld1.value);    // value of field with whitespace trimmed off
		var tfld2 = trim(fld2.value);    // value of field with whitespace trimmed off
    if ((tfld1 != "") && (tfld1 != tfld2)) {
		fld1.style.background = 'Yellow';
		fld2.style.background = 'Yellow';
    error = "Email Addresses not the same.\n";  
    }
    return error;
}

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.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The Phone Number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The Phone Number is the wrong length. Make sure you included an Area Code.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

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

   if (fld.value == "") {
        fld.style.background = 'White';
    } else if (isNaN(parseInt(stripped))) {
        error = "The Phone Number contains illegal characters.\n";
        fld.style.background = 'Yellow';
		}
		if (fld.value == "") {
        fld.style.background = 'White';
    } else if (!(stripped.length == 10)) {
        error = "The Phone Number is the wrong length. Make sure you included an Area Code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}

function validateMessage(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't include a Message.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateStreet(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Street Address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Street Address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCity(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a City.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The City contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateZip(fld) {  // 5 Character Numeric
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
		if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Zip Code.\n";
   } else if (IsNumeric(tfld) == false) 
      {
       fld.style.background = 'Yellow';
       error = "You didn't enter a Numeric Zip Code.\n";
	 } else if (fld.value.length != 5) {
        error = "The Zip Code must be 5 Characters. \n";
        fld.style.background = 'Yellow';	 
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function validateCaptcha(fld) {  // 
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
		if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a reCaptcha Code.\n";
    
    } else {
        fld.style.background = 'White';
    }
    return error;
}
