/*
    #############################################################################
    # Nome: contato.js                                                          #
    # Sistema: Planet Business                                                  #
    # Criado por: Breno Henrique Vivarelli                                      #
    # Empresa: Netnigro                                                         #
    # E-mail: breno@netnigro.com.br                                             #
    # DescriÃ§Ã£o: ValidaÃ§Ãµes do formulÃ¡rio de contato de clientes                #
    # HistÃ³rico:                                                                #
    #    . 10/06/2008 - Breno Henrique Vivarelli                                #
    #            CriaÃ§Ã£o da PÃ¡gina                                              #
    #############################################################################
*/

    function GravaContato()
    {
        var dados = $("#frmContato").formSerialize();
        $.ajax(
        {
            type: "POST",
            url: "contato_grava.php",
            data: dados,   
            success: function(msg)
            {                      
                alert(msg);
                $("#frmContato").resetForm(); 
            }
        });
    }
    
    function ValidaFormulario()
    {
        //Nome
        if($("#txtNome").val() == "")
        {
            alert("Favor preencher o campo Nome.");
            $("#txtNome").focus();
            return false;
        }
        
        //E-mail
        if(($("#txtEmail").val() == "") && ($("#txtTelefone").val() == ""))
        {
            alert("Favor preencher o campo Email ou Telefone para que seja possível o retorno do contato.");
            $("#txtEmail").focus();
            return false;
        }
        
        //Mensagem
        if($("#txtMensagem").val() == "")
        {
            alert("Favor preencher o campo Mensagem.");
            $("#txtMensagem").focus();
            return false;
        }
        
        return true;
    }
    
    $(function()
    {         
         //Adiciona mÃ¡scaras aos campos                  
         $("#txtTelefone").mask("(99) 9999-9999",{placeholder:" "});  
              
         //Email
         $("#txtEmail").blur(
            function()
            {    
                if(this.value != "")
                {        
                    var retorno = ValidaEmail(this);   
                    if(!retorno)
                    {
                        this.focus();
                        alert("E-mail inválido!");
                    }
                }
            }
         ); 
        
        $("#frmContato").bind("submit", 
            function() 
            { 
                if(ValidaFormulario())
                {
                    GravaContato();
                }
                return false;           
            } 
        );
        
        $("#txtNome").focus();        
    });
