﻿// This function sets and gets the default
// login completed callback function.
function SetDefaultLoginCompletedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(OnLoginCompleted);

    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultLoginCompletedCallback();
}

// This function sets and gets the default
// logout completed callback function.
function SetDefaultLogoutCompletedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(OnLogoutCompleted);

    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultLogoutCompletedCallback();
}


// This function sets and gets the default
// failed callback function.
function SetDefaultFailedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultFailedCallback(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. 
function Login() 
{   
    var uid = $get("txtUserName").value;
    var pwd = $get("txtPassword").value;

    // Set the default callback functions.
    SetDefaultLoginCompletedCallBack();
    SetDefaultLogoutCompletedCallBack();
    SetDefaultFailedCallBack();

    // Call the authetication service to authenticate
    // the credentials entered by the user.
    Sys.Services.AuthenticationService.login(uid, 
        pwd, false,null,null,null,null,"User Context");
}

// This function calls the logout method of the
// authentication service to clear the forms 
// authentication cookie.
function OnClickLogout() 
{  
   // Clear the forms authentication cookie. 
   Sys.Services.AuthenticationService.logout(null, 
        null, null, null);                   
} 



// This is the callback function called 
// if the authentication fails.      
function OnFailed(error, 
    userContext, methodName)
{			
    // Display feedback message.
	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.
function OnLoginCompleted(validCredentials, 
    userContext, methodName)
{
	// GetPageElements();
	
    // Clear the user password.
    $get("txtPassword").value = "";

    // On success there will be a forms 
    // authentication cookie in the browser.
    if (validCredentials == true) 
    {
       //PageMethods.LoggedIn();
       alert("Login Succeeded"); 
       location.reload();
    }
    else 
    {
        alert("Login Credentials Invalid. Could not login"); 
    }
	
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{        
    //document.cookie = "TkMaDj9101433";//=0;expires=" + d.toGMTString() + ";" + ";";
    //alert(document.cookie);      
    /*// Display login fields.
    usernameEntry.style.visibility = "visible";
    passwordEntry.style.visibility = "visible";
    textNotLoggedIn.style.visibility = "visible";  
    buttonLogin.style.visibility = "visible";

    // Hide logout fields.
    buttonLogout.style.visibility = "hidden";
    textLoggedIn.style.visibility = "hidden";
    */
}








