﻿var Login = new function () {

    this.RALABEL = 'RA: ';
    this.COACESSOLABEL = 'Senha: ';
    this.TextBoxRAClientID = '#ctl00_txtnuRA';
    this.TextBoxSBClientID = '#ctl00_txtcoAcesso';
    this.TextBoxBTClientID = '#ctl00_btnLogin';
    this.TextBoxBTErrorClientID = '#ctl00_btnOkErro';

    this.Inicializa = function () {

        var $textBoxRA = $(this.TextBoxRAClientID);
        var $textBoxSenha = $(this.TextBoxSBClientID);
        var $buttonLogin = $(this.TextBoxBTClientID);

        $textBoxRA.attr('style', 'width: 122px; height: 10px; background-color:#3c3b3b; border: solid 1px #808080; font-family:Verdana, Geneva, sans-serif; font-size:10px; color:#FFF;')
        $textBoxRA.attr('maxlength', '8');

        $textBoxSenha.attr('style', 'width: 81px; height: 10px; background-color:#3c3b3b; border: solid 1px #808080; font-family:Verdana, Geneva, sans-serif; font-size:10px; color:#FFF;')
        $textBoxSenha.attr('maxlength', '20');

        $textBoxRA.watermark(this.RALABEL);
        $textBoxSenha.watermark(this.COACESSOLABEL);

        $(this.TextBoxBTErrorClientID).click(function () {

            Login.ShowLogin();

        });

        $buttonLogin.click(function () {

            Login.Autentica();

        });
    }

    this.Autentica = function () {

        var $textBoxRA = $(this.TextBoxRAClientID);
        var $textBoxSenha = $(this.TextBoxSBClientID);

        if ($textBoxRA.val() == this.RALABEL) {
            $textBoxRA.focus();
            return;
        }
        else if ($textBoxSenha.val() == this.COACESSOLABEL) {
            $textBoxSenha.focus();
            return;
        }

        var prs = { 'nuRA': $textBoxRA.val(), 'coAcesso': $textBoxSenha.val() };

        $.ajax({
            type: 'POST',
            url: '/includes/LoginEspacoAluno.aspx',
            data: prs,
            cache: false,
            dataType: 'text/json',
            success: function (data) {

                if (data != null) {

                    var json = eval(data);

                    var result = json[0];

                    if (!result.icAutenticado)
                        Login.ShowErro();
                    else {
                        window.location.href = result.edUrlLogin;
                    }
                }
            },
            error: function (s, h) {

                alert(s.status);
            }
        });

    }

    this.ShowLogin = function () {

        $('#eaLoginErro').hide();
        $('#eaLoginForm').show();

    }

    this.ShowErro = function () {

        $('#eaLoginForm').hide();
        $('#eaLoginErro').show();

    }
}   
