//================================================================================================================
// Função que valida o formulário Fale Conosco
//================================================================================================================
function valida_fale()
{

var nome = document.getElementById("xnome").value;
var email = document.getElementById("xemail").value;
var telefone = document.getElementById("xddd").value + document.getElementById("xtel1").value + document.getElementById("xtel2").value;
var mensagem = document.getElementById("xmensagem").value;

    if(nome == "")
    {
    alert("Preencha o campo \"Nome\".");
    document.getElementById("xnome").focus();
    return false;
    }
    
    if(email == "")
    {
    alert("Preencha o campo \"E-mail\".");
    document.getElementById("xemail").focus();
    return false;
    }
    
    if(valida_email_reg_exp(email) == false)
    {
    alert("O \"E-mail\" informado é inválido.");
    document.getElementById("xemail").focus();
    return false;
    }
    
    if(telefone.length != 0 && telefone.length != 10)
    {
    alert("O campo \"Telefone\" deve conter ao todo 10 caracteres.");
    document.getElementById("xddd").focus();
    return false;
    }
    
    if(valida_numero(telefone) == false)
    {
    alert("O \"Telefone\" informado é inválido.");
    document.getElementById("xddd").focus();
    return false;
    }
    
    if(mensagem == "")
    {
    alert("Preencha o campo \"Mensagem\".");
    document.getElementById("xmensagem").focus();
    return false;
    }

return true;

}

//================================================================================================================
// Função que valida o formulário de Oportunidades
//================================================================================================================
function valida_oportunidade()
{

var nome = document.getElementById("xnome").value;
var email = document.getElementById("xemail").value;
var telefone = document.getElementById("xddd").value + document.getElementById("xtel1").value + document.getElementById("xtel2").value;
var mensagem = document.getElementById("xmensagem").value;

    if(nome == "")
    {
    alert("Preencha o campo \"Nome\".");
    document.getElementById("xnome").focus();
    return false;
    }
    
    if(email == "")
    {
    alert("Preencha o campo \"E-mail\".");
    document.getElementById("xemail").focus();
    return false;
    }
    
    if(valida_email_reg_exp(email) == false)
    {
    alert("O \"E-mail\" informado é inválido.");
    document.getElementById("xemail").focus();
    return false;
    }
    
    if(telefone.length != 0 && telefone.length != 10)
    {
    alert("O campo \"Telefone\" deve conter ao todo 10 caracteres.");
    document.getElementById("xddd").focus();
    return false;
    }
    
    if(valida_numero(telefone) == false)
    {
    alert("O \"Telefone\" informado é inválido.");
    document.getElementById("xddd").focus();
    return false;
    }
    
    if(mensagem == "")
    {
    alert("Preencha o campo \"Mensagem\".");
    document.getElementById("xmensagem").focus();
    return false;
    }
    
    if(document.getElementById("xanexo").value == "")
    {
    alert("Selecione o seu \"Currículo\".");
    document.getElementById("xanexo").focus();
    return false;
    }
    
    var anexo = document.getElementById("xanexo").value;
    var ponto = anexo.lastIndexOf(".");
    var extensao = anexo.substr(ponto + 1);

    if(anexo != "")
    {
        if(extensao != "doc" && extensao != "pdf")
        {
        alert("Somente são permitidos arquivos do tipo doc ou pdf.");
        document.getElementById("xanexo").focus();
        return false;
        }
    }

return true;

}

//================================================================================================================
// Função que passa de um campo para outro no formulário automaticamente
//================================================================================================================
function preenchendo(preenchido, proximo, tamanho)
{
    if(document.getElementById(preenchido).value.length == tamanho)
    {
    document.getElementById(proximo).focus();
    }
}

//================================================================================================================
//Função boleana que valida um número
//================================================================================================================
function valida_numero(numero)
{

var numero_tratado = numero;

numero_tratado = numero_tratado.toString();
numero_tratado = numero_tratado.replace(",",".");

    if (isNaN(numero_tratado) == true)
    {
    return false;
    }

return true;
}

//================================================================================================================
// Função que valida um endereço de e-mail (copiada e adaptada por mim para JavaScript)
//================================================================================================================
function valida_email_reg_exp(email)
{

var email_min = email.toLowerCase();

var tlds = "(BIZ|CAT|COM|EDU|GOV|INT|MIL|NET|ORG|PRO|TEL)|(AERO|ARPA|ASIA|COOP|INFO|JOBS|MOBI|MUSEUM|NAME|TRAVEL)";
var conta = "[a-z0-9]+([0-9a-z._-]+)?";
var dominio = "([a-z0-9]{1,63}([0-9a-z._-]+)?){2,63}\.(([a-z0-9]{2})|" + tlds + ")";

var regular = conta.toLowerCase() + "@" + dominio.toLowerCase();
var regular = "^" + regular + "$";
var regular = new RegExp(regular);

    //A função no PHP funciona melhor não sei porque. Aqui há um problema que ele não está verificando a ausência do primeiro ponto no domínio, então eu iclui esta condição manualmente.
    if (email_min.match(regular) != null && email_min.indexOf(".", email_min.indexOf("@")) != -1)
    {
    return true;
    }
    else
    {
    return false;
    }
}
