function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.username);
 
  reason += validateEmail(theForm.email);
 
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  
  window.location.url('index.php');
  return false;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#fff9e8'; 
        error = "The required field has not been filled in.\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 = '#fff9e8'; 
        error = "Enter Your Name.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#fff9e8'; 
        error = "The Name is the Wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#fff9e8'; 
        error = "The 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 = '#fff9e8';
        error = "Enter Your Email Address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#fff9e8';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#fff9e8';
        error = "The Email Address Contains illegal Characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

