
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 += validateFName(theForm.fname);
  reason += validateLName(theForm.lname);
  reason += validateAddress(theForm.address);
  reason += validateEmail(theForm.email);
  reason += validateCity(theForm.city);
  reason += validateState(theForm.state);
  reason += validateZip(theForm.zip);
  reason += validatePhone(theForm.phone);
  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 validateFName(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 first name.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 15)) {
        fld.style.background = '#E2E7EB'; 
        error = "The first name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#E2E7EB'; 
        error = "The first name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateLName(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 last name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 20)) {
        fld.style.background = '#E2E7EB'; 
        error = "The last name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#E2E7EB'; 
        error = "The last 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 < 5) || (fld.value.length > 5)) {
        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 validatePhone(fld) {
    var error = "";
    //var illegalChars = /\- /; //no dashes
 
    if (fld.value == "") {
        fld.style.background = '#E2E7EB'; 
        error = "You didn't enter a phone number.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 10)) {
        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 phone number 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;
//}

