﻿Type.registerNamespace('THBLogin');

THBLogin._Interface = function() {
    this._loginCompleteHandler = null;
    this._loginFailedHandler = null;
}

THBLogin._Interface.prototype = {
    set_loginCompleteHandler: function(value) { this._loginCompleteHandler = value; },
    get_loginCompleteHandler: function() { return this._loginCompleteHandler; },
    set_loginFailedHandler: function(value) { this._loginFailedHandler = value; },
    get_loginFailedHandler: function() { return this._loginFailedHandler; },

    pageLoad: function() {
        Sys.Services.AuthenticationService.set_path('/Authentication_JSON_AppService.axd');
        if (Sys.Services.AuthenticationService.get_isLoggedIn() == false) {
            var modalLogin = $find('ModalLogin');
            modalLogin.add_shown(function() { $get('tb-login').focus(); });
        }
    },
    dispose: function() {
        if (this._loginCompleteHandler) this._loginCompleteHandler = null;
        if (this._loginFailedHandler) this._loginFailedHandler = null;
    },
    showLogin: function() {
        $get('thb-error').style.display = 'none';
        
        $addHandler( document.body, 'keydown', this.eventKeyDown );

        var modalLogin = $find('ModalLogin');
        modalLogin.show();        
    },
    eventKeyDown: function(e) {
    
      if (e.keyCode ===  Sys.UI.Key.enter) {

        e.preventDefault();

        $get('loginButton').click();         

      }
      else if (e.keyCode ===  Sys.UI.Key.esc)
      {
        e.preventDefault();

        var modalLogin = $find('ModalLogin');
        modalLogin.hide();                
      }
    },
    hideLogin: function() {
        $removeHandler( document.body, 'keydown', this.eventKeyDown );

        var modalLogin = $find('ModalLogin');
        modalLogin.hide();                
    },
    hideWritePanel: function() {
        $get('thb-panel-write').style.display = 'none';
    },
    setCallBack: function(CallBack) {
        this.callBack = CallBack;
    },
    login: function(refresherClientID) {
        if (this._loginCompleteHandler == null) this._loginCompleteHandler = Function.createDelegate(this, this.onLoginComplete);
        if (this._loginFailedHandler == null) this._loginFailedHandler = Function.createDelegate(this, this.onLoginFailed);

        this.linkButtonRefreshedClientID = refresherClientID;

        if ($get('tb-login').value.trim() == '' || $get('tb-password').value.trim() == '') {
            $get('thb-error').style.visibility = 'visible';
        } else {
            Sys.Services.AuthenticationService.login($get('tb-login').value.trim(), $get('tb-password').value.trim(), $get('cbRememberMe').checked, null, null, this._loginCompleteHandler, this._loginFailedHandler, null);
        }
    },
    logout: function(clientId) {
        if (Sys.Services.AuthenticationService.get_isLoggedIn()) {
            Sys.Services.AuthenticationService.logout(null, null, null, null);
        }
    },
    onLoginComplete: function(validCredentials, userContext, methodName) {
        if (validCredentials == true) {
        
            this.hideLogin();
            
            if (this.callBack != null)
                this.callBack();

            var elmt = $get(this.linkButtonRefreshedClientID); 
            if (elmt.click)
                elmt.click();
        } else {
            $get('thb-error').style.display = 'block';
        }
    },
    onLoginFailed: function(error, userContext, methodName) {
        $get('thb-error').style.display = 'block';
   }
};
THBLogin._Interface.registerClass('THBLogin._Interface', null, Sys.IDisposable);

THBLogin.Interface = new THBLogin._Interface();

if (Sys && Sys.Application) {
    Sys.Application.notifyScriptLoaded();
}