﻿namespace('nnet');

nnet.Security = $j.klass({
    initialize: function(config) {
        try {
            this.LoginButton = $j(sVars.LOGON_LogonButton);
            this.ForgottonPasswordMessage = $j(sVars.LOGON_ForgottonPasswordMessage);
            this.ErrorLabel = $j(sVars.LOGON_ErrorLabelID);
            this.MessageLabel = $j(sVars.LOGON_MessageLabelID);
            this.PasswordMessage = $j(sVars.LOGON_PasswordMessage);

            this.UserNameField = $j(sVars.LOGON_UserNameID);
            this.PassWordField = $j(sVars.LOGON_PassWordID);

            this.PostLoginFunction = null;

        } catch (e) { logE("nnet.Security.initialize", e); }
    },

    logout: function() {
        try {
            SecCallback.Logout(this.onLogoutCallback, OnPageError);
            document.body.style.cursor = 'wait';
        } catch (e) { logE("nnet.Security.logout", e); }
    },

    onLogoutCallback: function(result) {
        try {
            NNet.HeaderToolBar.showLogin();
            document.body.style.cursor = 'default';
            onLogOut.fire(result)
        } catch (e) { logE("nnet.Security.onLogoutCallback", e); }
    },

    getUserName: function() { return this.UserNameField.val(); },
    getPassWord: function() { return this.PassWordField.val(); },

    onLoginButtonClick: function(button) {
        try {
            var sec = NNet.Security;
            sec.ForgottonPasswordMessage.hide();
            sec.ErrorLabel.html('');
            document.body.style.cursor = 'wait';
            this.LoginButton.disabled = true;
            sec.MessageLabel.html('Logging On ......');
            SecCallback.Logon(sec.getUserName(), sec.getPassWord(), sec.onLoginCallback, OnPageError);
        } catch (e) { logE("nnet.Security.onLoginButtonClick", e); }
    },

    onLoginCallback: function(result) {
        try {
            var sec = NNet.Security;

            sec.LoginButton.disabled = false;
            sec.MessageLabel.html('');
            sec.ErrorLabel.html('');
            document.body.style.cursor = 'default';
            if (result.Success) {
                sec.hideLogin();
                sec.ErrorLabel.html('');
                if (this.PostLoginFunction != null) {
                    window.setTimeout(this.PostLoginFunction, 1);
                    this.PostLoginFunction = null;
                }
                onLogIn.fire(result)
            } else if (result.AuthenticationFailed) {
                sec.ForgottonPasswordMessage.show();
                sec.PasswordMessage.hide();
                sec.ErrorLabel.html('Could not login, username or password invalid');
            } else if (result.SiteAuthenticationFailed) {
                sec.hideLogin();
                sec.ErrorLabel.html('');
                NNet.CurrentUser.showSiteSignUp();
                onLogIn.fire(result)
            } else if (result.Error) {
                sec.ErrorLabel.html('Error while loging in, please try again');
                log('Error while loging in - '+result.ErrorText)
            }
        } catch (e) { logE("nnet.Security.onLoginCallback", e); }
    },


    showLogin: function(message) {
        try {
            if (message != null) this.MessageLabel.html(message);
            this.ForgottonPasswordMessage.hide();
            this.PasswordMessage.show();
            this.ErrorLabel.html('');
            try {
                NNet.HeaderToolBar.OptionsContentPanel.hide();
                NNet.HeaderToolBar.OptionsContentPanel.show();
            } catch (e) { }
            this.UserNameField.focus();
            this.LoginButton.disabled = false;
            HideAd();
        } catch (e) { logE("nnet.Security.showLogin", e); }
    },

    hideLogin: function() {
        try {
            this.MessageLabel.html('');
            NNet.HeaderToolBar.OptionsContentPanel.hide();
            NNet.HeaderToolBar.OptionsPanel.show();
            this.LoginButton.disabled = true;
            ShowAd();
        } catch (e) { logE("nnet.Security.hideLogin", e); }
    },

    showLoginAlert: function(message, alertMessage) {
        try {
            this.showLogin(message);
            alert(alertMessage);
        } catch (e) { logE("nnet.Security.showLoginAlert", e); }
    },

    // if userid present will try to auto login, only works if no password.
    showWithLogon: function(postFunction, message, userID) {
        try {
            if (userID != null) {
                this.PostLoginFunction = postFunction;
                SecCallback.Logon(userID, null, this.onShowWithLogonCallback, OnPageError);
            } else {
                if (NNet == null) window.setTimeout(postFunction, 1); // assume called during startup
                else {
                    if (NNet.CurrentUser.LoggedIn) window.setTimeout(postFunction, 1);
                    else {
                        this.PostLoginFunction = postFunction;
                        this.showLogin(message);
                        alert(message);
                    }
                }
            }
        } catch (e) { logE("nnet.Security.showWithLogon", e); }
    },

    onShowWithLogonCallback: function(Result) {
        try {
            if (Result == 0 || Result == 1) {
                if (NNet.Security.PostLoginFunction != null) {
                    window.setTimeout(NNet.Security.PostLoginFunction, 1);
                    NNet.Security.PostLoginFunction = null;
                }
            } else if (Result == -1) {
                NNet.Security.showLogin("Please Login to the Site");
                alert("Please Login to the Site");
            }
        } catch (e) { logE("nnet.Security.onShowWithLogonCallback", e); }
    }



});






