﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Ajax");

Ajax.login = function(btnOk, txtLogin, txtSenha, pnlLogin, pnlLoading) {
    Ajax.login.initializeBase(this, [btnOk]);
    Ajax.login.initializeBase(this, [txtLogin]);
    Ajax.login.initializeBase(this, [txtSenha]);
    Ajax.login.initializeBase(this, [pnlLogin]);
    Ajax.login.initializeBase(this, [pnlLoading]);

    this._btnOk = btnOk;
    this._txtLogin = txtLogin;
    this._txtSenha = txtSenha;
    this._pnlLogin = pnlLogin;
    this._pnlLoading = pnlLoading;
    
    this.initialize();
}

Ajax.login.prototype = {
    initialize: function() {
        Ajax.login.callBaseMethod(this, 'initialize');

        this._btnOk_Click_Delegate = Function.createDelegate(this, this._btnOk_Click);
        this._btnOk_Click_Retorno_Delegate = Function.createDelegate(this, this._btnOk_Click_Retorno);


        this._press_Key_Delegate = Function.createDelegate(this, this._press_Key);
        this._press_Key_Callback = Function.createCallback(this._press_Key_Delegate, "");

        $addHandlers(this._pnlLogin, {
            keyup: this._press_Key_Callback,
            keydown: this._press_Key_Callback,
            keypress: this._press_Key_Callback
        }, this);
        $addHandlers(this._txtSenha, {
            keyup: this._press_Key_Callback,
            keydown: this._press_Key_Callback,
            keypress: this._press_Key_Callback
        }, this);

        $addHandler(this._btnOk, "click", this._btnOk_Click_Delegate);
        // Add custom initialization here
    },
    dispose: function() {
        //Add custom dispose actions here
        Ajax.login.callBaseMethod(this, 'dispose');
    },

    _press_Key: function(event) {
        if (event.keyCode == 13) {
            this._btnOk_Click();
            return false;
        }
    },

    _btnOk_Click: function() {
        var msg = "";
        if (this._txtLogin.value == "") { msg = "Campo <b>E-mail</b> é obrigatório"; }
        else if (this._txtSenha.value == "") { msg += "Campo <b>Senha</b> é obrigatório"; }
        if (msg != "") { this._exibir_modal(msg); }
        else {
            var x = this._pnlLoading;
            var y = this._pnlLogin;
            $(y).fadeOut(
                function() { $(x).fadeIn(); }
            );
            PageMethods.logar($(this._txtLogin).val(), $(this._txtSenha).val(), this._btnOk_Click_Retorno_Delegate); //, this._exibir_modal("Erro ao conectar ao servidor.<br/> Por favor tente mais tarde."));
        }
    },

    _btnOk_Click_Retorno: function(retorno) {
       
        if (retorno.match(".aspx")) {
            setTimeout(
            function() {
                window.location.href = retorno;
            }, 3000);
        }
        else {
            this._exibir_modal(retorno, true);
        }
    },

    _exibir_modal: function(texto, erro) {
        var x = this._pnlLoading;
        var y = this._pnlLogin;
        $get("pnlAlerta").innerHTML = texto;
        $("#pnlAlerta").dialog('destroy');
        $("#pnlAlerta").dialog({ modal: true, width: 250,
            buttons: {
                Ok: function() {
                    $(this).dialog('destroy');
                    if (erro) {
                        $(x).fadeOut(
                            function() { $(y).fadeIn(); }
                        );
                    }
                }
            }
        });
    }
}
Ajax.login.registerClass('Ajax.login', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
