
function onlyNumbers(evt)
{
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;

}

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.name);
  reason += validateAddress(theForm.address);
  reason += validateEmail(theForm.email);
  reason += validateCity(theForm.city);
  reason += validateState(theForm.state);
  reason += validateZip(theForm.zip);
  reason += validatePhone1(theForm.phone1);
  reason += validatePhone2(theForm.phone2);
  reason += validatePhone3(theForm.phone3);
  reason += validateEmpty(theForm.zip);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  //alert("All fields are filled correctly");
  //return false;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#E2E7EB'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateName(fld) {
    var error = "";
    var illegalChars = /\W /; // allow letters, numbers, spaces and underscores
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter a name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 35)) {
        fld.style.background = '#E2E7EB'; 
        error = "The name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#E2E7EB'; 
        error = "The name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAddress(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter an address.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 55)) {
        fld.style.background = '#E2E7EB'; 
        error = "The address is the wrong length.\n";
    } //else if (illegalChars.test(fld.value)) {
        //fld.style.background = '#E2E7EB'; 
        //error = "The address contains illegal characters.\n";
    //} 
	else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCity(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter a city.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 35)) {
        fld.style.background = '#E2E7EB'; 
        error = "The city is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#E2E7EB'; 
        error = "The city should be all letters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateState(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter a state.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 25)) {
        fld.style.background = '#E2E7EB'; 
        error = "The state is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#E2E7EB'; 
        error = "The state should be all letters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateZip(fld) {
    var error = "";
    //var illegalChars = /\d/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter a zip.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 25)) {
        fld.style.background = '#E2E7EB'; 
        error = "The zip is the wrong length.\n";
    } //else if (illegalChars.test(fld.value)) {
        //fld.style.background = '#E2E7EB'; 
        //error = "The zip should be all numbers.\n";
    //} 
	else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone1(fld) {
    var error = "";
    //var illegalChars = /\d/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter an area code.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 3)) {
        fld.style.background = '#E2E7EB'; 
        error = "The area code is the wrong length.\n";
    } //else if (illegalChars.test(fld.value)) {
        //fld.style.background = '#E2E7EB'; 
        //error = "The zip should be all numbers.\n";
    //} 
	else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone2(fld) {
    var error = "";
    //var illegalChars = /\d/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter a phone suffix.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 3)) {
        fld.style.background = '#E2E7EB'; 
        error = "The phone suffix is the wrong length.\n";
    } //else if (illegalChars.test(fld.value)) {
        //fld.style.background = '#E2E7EB'; 
        //error = "The zip should be all numbers.\n";
    //} 
	else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone3(fld) {
    var error = "";
    //var illegalChars = /\d/; // allow letters
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter a phone number.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 4)) {
        fld.style.background = '#E2E7EB'; 
        error = "The phone number is the wrong length.\n";
    } //else if (illegalChars.test(fld.value)) {
        //fld.style.background = '#E2E7EB'; 
        //error = "The zip should be all numbers.\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 = '#E2E7EB';
        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 = '#E2E7EB';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#E2E7EB';
    } 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 = '#E2E7EB';
    } 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 = '#E2E7EB';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#E2E7EB';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#E2E7EB';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    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 = '#E2E7EB';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#E2E7EB';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#E2E7EB';
    }
    return error;
}

