function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.name);
  reason += validateEmail(theForm.email);
  reason += validateUsername(theForm.username);
  reason += validatePassword(theForm.password);
  reason += validateSecImage(theForm.securitycode);
  
  if (reason != "") {
    alert("Some required fields are not filled out:\n" + reason);
    return false;
  }

   
}
function validateSecImage(fld)
{
    var error = "";
    if (fld.value == "") {
    fld.style.background = '#fcbf03'; 
    error = "- Security code\n";
    }
    else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLoginOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.username);
  reason += validatePassword(theForm.password);
  
  
  if (reason != "") {
    alert("Some fields are incorrect:\n" + reason);
    return false;
  }

   
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
    var legalChars = /[0-9]/;  //make sure not only letters
    var legalChars2 = /[a-z]/; //make sure not only numbers
    if (fld.value == "") {
        fld.style.background = '#fcbf03';
        error = "- Password\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "- The password must be more than 7 and less than 15 characters long\n";
        fld.style.background = '#fcbf03';
    } else if (illegalChars.test(fld.value)) {
        error = "- The password must contain only letters and numbers\n";
        fld.style.background = '#fcbf03';
    } else if  (!(legalChars.test(fld.value))) {           
        error = "- The password must contain at least one numeral\n";
        fld.style.background = '#fcbf03';
    }
        else if  (!(legalChars2.test(fld.value))) {            
        error = "- The password must contain at least one letter\n";
        fld.style.background = '#fcbf03';
    }
     else {
        fld.style.background = 'White';
    }
   return error;
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#fcbf03';
        error = "- Username\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 25)) {
        error = "- The username must be more than 7 and less than 25 characters long\n";
        fld.style.background = '#fcbf03';
    } else if (illegalChars.test(fld.value)) {
        error = "- The username must contain only letters and numbers\n";
        fld.style.background = '#fcbf03';
    } 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 = '#fcbf03';
        error = "- Email address\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#fcbf03';
        error = "- Please enter a valid email address\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#fcbf03';
        error = "- The email address contains illegal characters\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateName(fld) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.background = '#fcbf03'; 
        error = "- Name\n"
    } 
    
    else {
        fld.style.background = 'White';
    }
    return error;  
}

