// JavaScript Document to validate general email forms\ 

var wColor = '#960';//Change color of highlight
var rColor = '#666';//Same color of the normal field, so if a field that was previously wrong turns right colored.

function validateFormOnSubmit(theForm) {
	var reason = "";
	var fColor = theForm.style.background;
	
	reason += validateName(theForm.name);
  	reason += validateEmail(theForm.email);
  	reason += validatePhone(theForm.phone);
	reason += validateList(theForm.selection);
	reason += validateMessage(theForm.my_text);
	
 	 if (reason != "") {
    	alert("Some fields need correction:\n" + reason + fColor);
    	return false;
  	}

  	return true;
}
function validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = wColor; 
        error = "You didn't enter a name.\n"
    } else {
        fld.style.background = rColor;
    }
    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 = wColor;
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {//test email for illegal characters
        fld.style.background = wColor;
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = wColor;
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = rColor;
    }
    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 = wColor;
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = wColor;
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = wColor;
    }
	else {
		fld.style.background = rColor;
	}
    return error;
}
function validateList(fld) {
    var error = "";
 
    if (fld.value == "default") {
        fld.style.background = wColor; 
        error = "You didn't select a category.\n"
    } else {
        fld.style.background = rColor;
    }
    return error;  
}
function validateMessage(fld) {
    var error = "";
 
    if (fld.value.length >= 1000) {
        fld.style.background = wColor; 
        error = "You entered too many characters.\n"
    } else {
        fld.style.background = rColor;
    }
    return error;  
}

//For reference http://www.webcheatsheet.com/javascript/form_validation.php
