﻿// Authentication.js

function AjaxAuthentication()
{

    // This function sets and gets the default
    // login completed callback function.
    this.SetDefaultLoginCompletedCallBack = function()
    {
        // Set the default callback function.
        Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(this.OnLoginCompleted);
       
        // Get the default callback function.
        var callBack =     
            Sys.Services.AuthenticationService.get_defaultLoginCompletedCallback();
    }

    // This function sets and gets the default
    // logout completed callback function.
    this.SetDefaultLogoutCompletedCallBack = function()
    {
        // Set the default callback function.
        Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(this.OnLogoutCompleted);
       
        // Get the default callback function.
        var callBack =     
            Sys.Services.AuthenticationService.get_defaultLogoutCompletedCallback();
    }

    // This function sets and gets the default
    // failed callback function.
    this.SetDefaultFailedCallBack = function()
    {
        // Set the default callback function.
        Sys.Services.AuthenticationService.set_defaultFailedCallback(this.OnFailed);
       
        // 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. 
    this.Login = function(username, password)
    {   
        // Set the default callback functions.
        this.SetDefaultLoginCompletedCallBack();
        this.SetDefaultLogoutCompletedCallBack();
        this.SetDefaultFailedCallBack();
       
        // Call the authetication service to authenticate
        // the credentials entered by the user.
        Sys.Services.AuthenticationService.login(username, 
            password, false,null,null,null,null,"User Context");
    }

    // This function calls the logout method of the
    // authentication service to clear the forms 
    // authentication cookie.
    this.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.      
    this.OnFailed = function(error, userContext, methodName)
    {			
    }


    // The callback function called 
    // if the authentication completed successfully.
    this.OnLoginCompleted = function(validCredentials, 
    userContext, methodName)
    {	   
    }
    
    // This is the callback function called 
    // if the user logged out successfully.
    this.OnLogoutCompleted = function(result) 
    {
    }                   

    // This function displays feedback
    // information for the user.    
    this.DisplayInformation = function(text)
    {
	    var userLoggedIn =
	        Sys.Services.AuthenticationService.get_isLoggedIn();
    	
        var authServiceTimeout =       
            Sys.Services.AuthenticationService.get_timeout();
       
        var userLoggedInfo = 
            "| User logged in:                 " + userLoggedIn;
            
        var timeOutInfo = 
            "| Authentication service timeout: " + authServiceTimeout;

        alert(text + userLoggedInfo + timeOutInfo); 
    }
    
}


if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

var AjaxAuthenticationProxy = new AjaxAuthentication();