﻿SignInPage =
{
    Init: function () {
        var oDiv = document.getElementById("divMasterContent");
        if (oDiv) {
            if (window.screen.height <= 768)
                oDiv.style.height = '350px';
            else
                oDiv.style.height = '450px';
        }

        if (typeof(btnSubmit) != 'undefined')
            Ext.EventManager.on(btnSubmit, 'click', SignInPage.OnBtnSubmitClicked);

        // enter key
        Ext.get(txtUsername).on('keydown', function (e) {
            if (e.getKey() === e.ENTER) {
                Ext.get(txtPassword).focus();
            }
        });
        Ext.get(txtPassword).on('keydown', function (e) {
            if (e.getKey() === e.ENTER) {
                Ext.get(txtValidateNumber).focus();
            }
        });
        Ext.get(txtValidateNumber).on('keydown', function (e) {
            if (e.getKey() === e.ENTER) {
                SignInPage.OnBtnSubmitClicked();
            }
        });

        // cookie
        var cp = new Ext.state.CookieProvider();
        var lastUser = cp.get('LastLogonUsername', '');
        ExtPage.forms[0].findField(txtUsername).setValue(lastUser);
        if (lastUser != '') {
            ExtPage.forms[0].findField(txtPassword).focus();
            ExtPage.forms[0].findField(btnRememberMe).setValue(true);
        }
    },

    OnBtnSubmitClicked: function () {
        if (!ExtPage.forms[0].isValid()) return;

        //MasterPage.ShowLoading(true);
        PageMethods.AjaxLogin(ExtPage.forms[0].findField(txtUsername).getValue()
            , ExtPage.forms[0].findField(txtPassword).getValue()
            , ExtPage.forms[0].findField(txtValidateNumber).getValue()
            , SignInPage.OnAjaxLogin
            );
    },


    OnAjaxLogin: function (result) {
        // save cookie
        if (result.success) {
            var cp = new Ext.state.CookieProvider();
            cp.set('LastLogonUsername', ExtPage.forms[0].findField(btnRememberMe).getValue() ?
                ExtPage.forms[0].findField(txtUsername).getValue() : ''
                );
            (new Ext.state.CookieProvider()).set('LastSelectItem', 1);
            window.location = 'PartnerSystemSelection.aspx';
        }
        else {
            Ext.MessageBox.show({
                msg: result.error,
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.ERROR,
                draggable: false,
                closable: false
            });
            //MasterPage.ShowLoading(false);
        }
    }
};

Ext.EventManager.onDocumentReady(SignInPage.Init);
