// JavaScript Document
// JavaScript Document
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.Name);
    reason += validateStreet(theForm.Street);
	  reason += validateCity(theForm.City);
	    reason += validateState(theForm.State);
		  reason += validateZip(theForm.Zip);
	reason += validateEmailAddress(theForm.EmailAddress);

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

//alert("We will get back to you shortly. Thanks!");
//theForm.submit();
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#FF4B4B'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateName(fld) {
    var error = "";
    var illegalChars = /^[a-z] [-a-z0-9_ ]{0,49}$/i; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't enter a Name.\n";
    }  else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FF4B4B'; 
        error = "Name field contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateStreet(fld) {
    var error = "";
    var illegalChars = /^[a-z] [-a-z0-9_ ]{0,49}$/i; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't enter a Street address.\n";
    }  else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FF4B4B'; 
        error = "Street address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCity(fld) {
    var error = "";
    var illegalChars = /^[a-z] [-a-z0-9_ ]{0,49}$/i; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't enter a City.\n";
    }  else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FF4B4B'; 
        error = "City contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateState(fld) {
    var error = "";
    var illegalChars = /^[a-z] [-a-z0-9_ ]{0,49}$/i; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't enter a State.\n";
    }  else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FF4B4B'; 
        error = "State contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    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.background = '#FF4B4B';
    } 
	else if (isNaN(parseInt(stripped))) {
        error = "The Zip coder contains illegal characters.\n";
        fld.style.background = '#FF4B4B';
    } else if (!(stripped.length == 5)) {
        error = "The Zip code is the wrong length. Make sure you included 5 digit zip.\n";
        fld.style.background = '#FF4B4B';
    }
		else {
        fld.style.background = 'White';
    }
    return error;
}



function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmailAddress(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 = '#FF4B4B';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FF4B4B';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FF4B4B';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

