$(document).ready(function(){
    $('#cpfTopo').click(function(){
        if($(this).val() == 'CPF / CNPJ'){
           $(this).val('');
        }
    });
    $('#cpfTopo').blur(function(){
        if($(this).val() == ''){
            $(this).val('CPF / CNPJ');
        }
    });

    $('.abrelogin').click(function(){
        if($('#areaclientes_aberta').css('display') == 'none'){
            $('#areaclientes_aberta').slideDown();
        }else{
            $('#areaclientes_aberta').slideUp();
        }
    });

    $('#erro1').hide();
    $('#erro2').hide();
    $('#erro3').hide();
    $('#box_erro').hide();
    $('#respostaEsqueci').hide();

    /*$('.esqueceusenha').click(function(){
        $('#erro2').hide();
        $('#erro3').hide();
        $('#respostaEsqueci').hide();
        $('#erro1').show();
        $('#box_erro').slideDown();
    });
    $('.naorecebeu').click(function(){
        $('#erro2').hide();
        $('#erro3').hide();
        $('#respostaEsqueci').hide();
        $('#erro1').show();
        $('#box_erro').slideDown();
    });*/

    $('#logout').click(function(){
        $.ajax({
            type: "POST"
            ,dataType: "json"
            ,cache: true
            ,async: false
            ,data: 'logout=sair'
            ,url: 'login_xp.php'
            ,error: function(data) {
                alert('Não foi possivel enviar as informações. Tente novamente.');
            }
            ,success: function(data) {
                if(data.send == 'ok') {
                    $(location).attr('href', data.msg);
                } else {
                    alert(data.msg);
                }
            }
        });
        return false;
    });

    $('#formLoginTopo').submit(function(){
        $.ajax({
            type: "POST"
            ,dataType: "json"
            ,cache: true
            ,async: false
            ,data: $(this).serialize()
            ,url: $(this).attr('action')
            ,beforeSend: function(){
                var text = "";

                if($("#cpfTopo").val() == "CPF / CNPJ" || $("#cpfTopo").val() == ''){
                    text += "Informe o seu CPF / CNPJ.\n";
                }
                if($("#senhaTopo").val() == "Senha" || $("#senhaTopo").val() == ''){
                    text += "Informe a sua senha.\n";
                }

                if(text != ''){
                    alert(text);
                    return false;
                }
            }
            ,error: function(data) {
                alert('Não foi possivel enviar as informações. Tente novamente.');
            }
            ,success: function(data) {
                if(data.send == 'ok') {
                    $(location).attr('href', data.msg);
                } else {
                    alert(data.msg);
                }
            }
        });
        return false;
    });
    
    $('#formLogin').submit(function(){
        $.ajax({
            type: "POST"
            ,dataType: "json"
            ,cache: true
            ,async: false
            ,data: $(this).serialize()
            ,url: $(this).attr('action')
            ,beforeSend: function(){
                var text = "<h3>";

                if($("#cpf").val() == "CPF / CNPJ" || $("#cpf").val() == ''){
                    text += "Informe o seu CPF / CNPJ.<br/>";
                }
                if($("#senha").val() == "Senha" || $("#senha").val() == ''){
                    text += "Informe a sua senha.";
                }
                text += "</h3>";

                if(text != '<h3></h3>'){
                    $('#erro1').hide();
                    $('#erro2').hide();
                    $('#erro3').html(text);
                    $('#erro3').show();
                    $('#box_erro').slideDown();
                    return false;
                }else{
                    $('#erro1').hide();
                    $('#erro2').hide();
                    $('#erro3').html('<h3>Aguarde...</h3>');
                    $('#erro3').show();
                    $('#box_erro').slideDown();
                }
            }
            ,error: function(data) {
                alert('Não foi possivel enviar as informações. Tente novamente.');
            }
            ,success: function(data) {
                if(data.send == 'ok') {
                    $(location).attr('href', data.msg);
                } else {
                    $('#erro1').hide();
                    $('#erro2').hide();
                    $('#erro3').html('<h3>'+data.msg+'</h3>');
                    $('#erro3').show();
                    $('#box_erro').slideDown();
                }
            }
        });
        return false;
    });
});
