﻿/// register namespace Nammedia.Security.Login
Type.registerNamespace("Nammedia.Security.Login");
///
/// constructor
///
Nammedia.Security.Login.Login=function(){
    Nammedia.Security.Login.Login.initializeBase(this);
    // Define global variables.    
    // user name control
    this._username=null;
    // password control
    this._password=null;  
    // isPersistent control
    this._persistent=null; 
    /// redirect url used to redirect after user logined  
    this._redirectUrl="";
}
Nammedia.Security.Login.Login.prototype={
    get_mgslbl:function(){return this._mgslbl;},
    set_mgslbl:function(val){this._mgslbl=val;},
    	
    /// set user name
    set_username:function(val){
        this._username=val;
    },
    /// get user name
	get_username:function(val){
		return this._username;
	},
	/// set password	
	set_password:function(val){
		this._password=val;
	},	
	get_password:function(){
	    return this._password;
    },
     /// set Persisten 
    set_persistent:function(val){
        this._persistent=val;
    },
    /// get Persisten
    get_persistent:function(){
        return this._persistent;
    },
    /// set redirect url
   
     set_redirectUrl:function(val){
        this._redirectUrl=val;
    },
    /// get redirect url
    get_redirectUrl:function(){
        return this._redirectUrl;
    },
    _key_press:function(evn){
    	if(evn.charCode==Sys.UI.Key.enter){
    		this.login();
    	}
    },
    initialize:function(){
    	$addHandler(this._username,"keypress",Function.createDelegate(this,this._key_press));
    	$addHandler(this._password,"keypress",Function.createDelegate(this,this._key_press));
    },
    /// This function sets and gets the default
    /// login completed callback function.
    _SetDefaultLoginCompletedCallBack : function()
    {
        // Set the default callback function.
        Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(Function.createDelegate(this,this._loginCompleteHandler));

        // Get the default callback function.
        var callBack =     
            Sys.Services.AuthenticationService.get_defaultLoginCompletedCallback();
    },

    /// This function sets and gets the default
    /// logout completed callback function.
    _SetDefaultLogoutCompletedCallBack : function()
    {
        // Set the default callback function.
        Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(Function.createDelegate(this,this._logoutCompleteHandler));

        // Get the default callback function.
        var callBack =     
            Sys.Services.AuthenticationService.get_defaultLogoutCompletedCallback();
    },


    // This function sets and gets the default
    // failed callback function.
    _SetDefaultFailedCallBack : function()
    {
        // Set the default callback function.
        Sys.Services.AuthenticationService.set_defaultFailedCallback(Function.createDelegate(this,this._failedHandler));

        // Get the default callback function.
        var callBack =     
            Sys.Services.AuthenticationService.get_defaultFailedCallback();  
    },


    // This function calls the login method of the
    // authentication service to verify 
    // the credentials entered by the user.
    // If the credentials are authenticated, the
    // authentication service issues a forms 
    // authentication cookie. 
    login : function() 
    {   

        // Set the default callback functions.
        this._SetDefaultLoginCompletedCallBack();
        this._SetDefaultLogoutCompletedCallBack();
        this._SetDefaultFailedCallBack();
        
        // Call the authetication service to authenticate
        // the credentials entered by the user.
        
        if(this._username!="" || this._password!="")
            Sys.Services.AuthenticationService.login(this._username.value.toLowerCase(), 
                this._password.value, this._persistent.checked,null,null,null,null,"User Context");
    },

    // This function calls the logout method of the
    // authentication service to clear the forms 
    // authentication cookie.
    logout : function() 
    {  
       // Clear the forms authentication cookie. 
       Sys.Services.AuthenticationService.logout(null, 
            null, null, null); 
    }, 
    // This is the callback function called 
    // if the authentication fails.      
    _failedHandler : function(error, 
        userContext, methodName)
    {			
        // Display feedback message.
	    this._DisplayInformation("error:message = " + error.get_message());
	  //  _DisplayInformation("error:timedOut = " + error.get_timedOut());
	  //  _DisplayInformation("error:statusCode = " + error.get_statusCode());			
    },


    // The callback function called 
    // if the authentication completed successfully.
    _loginCompleteHandler : function(validCredentials, 
        userContext, methodName)
    {
	    // GetPageElements();
    	
        // Clear the user password.
        this._password.value = "";

        // On success there will be a forms 
        // authentication cookie in the browser.
        if (validCredentials == true) 
        {

            // Clear the user name.
            this._username.value = "";

            // Clear the feedback area.
            this._DisplayInformation(""); 
            // login
            window.location=this._redirectUrl;
        }
        else 
        {
            this._DisplayInformation("Tên truy cập và mật khẩu không hợp lệ!"); 
        }    	
    },

    // This is the callback function called 
    // if the user logged out successfully.
    _logoutCompleteHandler : function(result) 
    {
        window.location=this._redirectUrl;
    },           

    // This function displays feedback
    // information for the user.    
    _DisplayInformation : function(text)
    {
        // Display authentication service information.
        var userLoggedIn =Sys.Services.AuthenticationService.get_isLoggedIn();    	
        var authServiceTimeout =Sys.Services.AuthenticationService.get_timeout();   
        this._mgslbl.innerHTML=text;
        
    }
}
Nammedia.Security.Login.Login.registerClass("Nammedia.Security.Login.Login",Sys.Component);