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

reason += validateName(tucf.name);
reason += validateEmail(tucf.email);
reason += validateComments(tucf.comments);
reason += validateCheckboxes(tucf.generalcheck,tucf.appcheck,tucf.bencheck);

  if (reason != "") {
    alert("Form not complete:\n" + reason);
    return false;
  }

  return true;
}

function validateName(fld) {
    var error = "";
	document.getElementById(fld);
	if (fld.value=="Name"){
		fld.style.background='yellow';
		error="Please enter a valid name.\n";
	} else
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The name field has not been filled in.\n";
   } 
    return error;   
}

function validateComments(fld) {
    var error = "";
  if (fld.value == "Comments") {
        fld.style.background = 'Yellow'; 
        error = "This is not a valid comment, please enter your comments.\n"
    } else
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Please remember to include your comments.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateCheckboxes(fld1,fld2,fld3){
	var error ="";
	
	if ((fld1.checked==false)&&(fld2.checked==false)&&(fld3.checked==false)){
		fld1.style.background = 'Yellow';
		fld2.style.background = 'Yellow';
		fld3.style.background = 'Yellow';
		error="Please check a box regarding the subject of your message. This will ensure it is routed to the correct department.\n"
	} else {
		error ="";
	}
	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= /[\(\)\<\>\,\;\:\\\"\[\]\s]/ ;
    
	if (fld.value == "email@email.com") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a valid email address.\n";
    } else
    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;
}