﻿/// <reference path="../jquery-1.4.1-vsdoc.js" />

var formIsOk = null;
var email;
var senha;

$(document).ready(function () {

    $('#txtSenhaCab').keypress(function (e) {
        if (JSA.event.checkEnter(e)) {
            $('#btnEntrar').click();
        }
    });

    // Botão Cadastrar
    $('#btnEntrar').click(function () {
        formIsOk = true;

        email = $('#txtEmailCab');
        senha = $('#txtSenhaCab');

        if (!email.val()) {
            alert('Preencha o campo Email.');
            return false;
        }
        else if (!JSA.validation.isMail(email.val())) {
            alert('Email inválido.');
            return false;
        }

        if (!senha.val()) {
            alert('Preencha o campo Senha.');
            return false;
        }

        //passa os values dos campos para data
        if (formIsOk) {
            senha = $('#txtSenhaCab').val();
            email = $('#txtEmailCab').val();

            //chamada ajax da função
            UI_Cabecalho.Logar(email, senha, function (res) {
                if (res.value.isAuthenticated) {
                    if (document.location.toString().split('/')[document.location.toString().split('/').length - 1].split('?')[0] == 'Busca.aspx') {
                        $('#areaCliente').html('');
                        $('#areaCliente').html('<img src="/images/btnSair.png" class="btnSair" onclick="javascript:Deslogar();" style="cursor:pointer;" />' +
                    '<h2>Área restrita do cliente</h2><h3>Olá,' + res.value.userName +
                    '</h3><p>Seja bem-vindo(a) ao Portal do Cliente do Vale Saúde.</p>');

                        $('#areaCliente').attr('class', 'areaCliente');
                        //location.reload(true);

                        if($('#hLogado') != null)
                            $('#hLogado').attr('Value', '1');
                    } else {
                        document.location = '/VerificaUsuario.aspx';
                    }

                } else {
                    alert('Usuário ou senha inválidos.');
                }
            });

        }

        return false;
    });

});

function Deslogar() {
    UI_Cabecalho.Deslogar(function (res) {
        if (!res.error) {
            if (res.value != '') {
                $('#containerUsuario').html(res.value);
                //window.location = '/Index_Cliente.aspx';
                location.reload(true);
            } else {
                //window.location = '/Index_Cliente.aspx';
                location.reload(true);
            }
        }
    });
}

function Logar() {
// Botão Cadastrar
    formIsOk = true;

    email = $('#txtEmailCab');
    senha = $('#txtSenhaCab');

    if (!email.val()) {
        alert('Preencha o campo Email.');
        formIsOk = false;
    }
    else if (!JSA.validation.isMail(email.val())) {
        alert('Email inválido.');
        formIsOk = false;
    }

    if (!senha.val()) {
        alert('Preencha o campo Senha.');
        formIsOk = false;
    }

    //passa os values dos campos para data

    if (formIsOk) {
        senha = $('#txtSenhaCab').val();
        email = $('#txtEmailCab').val();

        //chamada ajax da função
        UI_Cabecalho.Logar(email, senha, function (res) {
            if (res.value.isAuthenticated) {

                $('#areaCliente').html('');
                $('#areaCliente').html('<img src="/images/btnSair.png" class="btnSair" onclick="javascript:Deslogar();" style="cursor:pointer;" />' +
                '<h2>Área restrita do cliente</h2><h3>Olá,' + res.value.userName +
                '</h3><p>Seja bem-vindo(a) ao Portal do Cliente do Vale Saúde.</p>');

                $('#areaCliente').attr('class', 'areaCliente');

            } else {
                alert('Usuário ou senha inválidos.');
            }
        });

    }

    return false;

};
