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

  reason += validateUsername(theForm.username);
  reason += validateEmail(theForm.email_from);
  reason += validateCompany(theForm.company);
  reason += validateEmpty1(theForm.comments);

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

  return true;
}



function validateEmpty1(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '9298a3'; 
        error = "You didn't enter a Comments.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '9298a3'; 
        error = "You didn't enter a User Name.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '9298a3'; 
        error = "The User Name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '9298a3'; 
        error = "The User Name contains illegal characters.\n";
    } 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 = '9298a3';
        error = "You didn't enter an Email Address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '9298a3';
        error = "Please enter a valid Email Address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '9298a3';
        error = "The Email Address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}





function validateCompany(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '9298a3'; 
        error = "You didn't enter a Company.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '9298a3'; 
        error = "The Company is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '9298a3'; 
        error = "The Company contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}





