function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.nome);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.telefone);
  reason += validateAssunto(theForm.assunto);
  reason += validateMsg(theForm.msg);
      
  if (reason != "") {
    alert("Por favor, preencha os campos obrigatórios:\n" + reason);
    return false;
  }


}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#CCCCCC'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateAssunto(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#CCCCCC'; 
        error = "Você não informou o assunto.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateMsg(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#CCCCCC'; 
        error = "Você não informou o assunto.\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 = '#CCCCCC'; 
        error = "Você não informou o seu nome:\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 25)) {
        fld.style.background = '#CCCCCC'; 
        error = "Nome inválido.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#CCCCCC';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#CCCCCC';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#CCCCCC';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = '#CCCCCC';
    } 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 = '#CCCCCC';
        error = "Você não informou o seu e-mail.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#CCCCCC';
        error = "E-mail informado inválido.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#CCCCCC';
        error = "E-mail informado inválido.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Você não informou seu telefone.\n";
        fld.style.background = '#CCCCCC';
    } else if (isNaN(parseInt(stripped))) {
        error = "O número informado contém caracteres inválidos.\n";
        fld.style.background = '#CCCCCC';
    } else if (!(stripped.length == 10)) {
        error = "Inclua o código de área de sua região.\n";
        fld.style.background = '#CCCCCC';
    }
    return error;
}
