﻿// Authentication.js

//var usernameEntry;
//var passwordEntry;
//var username;
//var password;
//var textLoggedIn;
//var textNotLoggedIn;
//var buttonLogin;  
//var buttonLogout; 

//function pageLoad()
//{
//// 	usernameEntry = $get("NameId");
////	passwordEntry = $get("PwdId");
////	username = $get("username");
////	password = $get("password");
////	textLoggedIn = $get("loggedin");
////	textNotLoggedIn = $get("notloggedin");
////	buttonLogin = $get("ButtonLogin");  
////	buttonLogout = $get("ButtonLogout"); 
//}            

// 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 doClientLogin(username,password,rememberme) 
{   
    // Set the default callback functions.
    SetDefaultLoginCompletedCallBack();
    SetDefaultLogoutCompletedCallBack();
    SetDefaultFailedCallBack();
   
    // Call the authetication service to authenticate
    // the credentials entered by the user.
    Sys.Services.AuthenticationService.login(username, 
        password, rememberme,null,null,null,null,"User Context");
        
closeMessage();
return false;

}

// 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)
{		
   var messageHTML = "<p style='text-align:center;width:100%;padding-top:5px;padding-bottom:5px;margin:0px;background-color:#0033FF;color:white;font-family:Verdana,Arial;font-weight:bold;font-size:Medium;'>Login Failed!</p><br/>";
         messageHTML += "<p style='text-align:center;font-size:small;font-family:Verdana,Arial;color:red'><img src='Images/redX.png' style='width:25px;height:25px;vertical-align:middle;'/>&nbsp;The system is having trouble logging you on, please try again later.</p>";
         messageHTML += "<br/>";
         messageHTML+= "<div style='text-align:center;'><input type='button' value='OK' onclick='closeMessage();return false;' class='cssTextButton'/></div>";
         //validateHTML+= "</div>";
         displayStaticMessage(messageHTML,false,370,200);	
    // 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)
{
	
    // Clear the user password.
    //password.value = "";
    
    // On success there will be a forms 
    // authentication cookie in the browser.
    if (validCredentials == true) 
    {
     window.location.reload();
//        
//        // Clear the user name.
//        username.value = "";
//     
//        // Hide login fields.
//        buttonLogin.style.visibility = "hidden";
//        usernameEntry.style.visibility = "hidden";
//        passwordEntry.style.visibility = "hidden";
//        textNotLoggedIn.style.visibility = "hidden";  
//   
//        // Display logout fields.
//        buttonLogout.style.visibility = "visible";
//        textLoggedIn.style.visibility = "visible";
//        
//        // Clear the feedback area.
//        DisplayInformation(""); 
    }
    else 
    {
       var messageHTML = "<p style='text-align:center;width:100%;padding-top:5px;padding-bottom:5px;margin:0px;background-color:#0033FF;color:white;font-family:Verdana,Arial;font-weight:bold;font-size:Medium;'>Credentials Incorrect!</p><br/>";
         messageHTML += "<p style='text-align:center;font-size:small;font-family:Verdana,Arial;color:red'><img src='Images/redX.png' style='width:25px;height:25px;vertical-align:middle;'/>&nbsp;Your login attempt has failed, please check your credentials and try again.</p>";
         messageHTML += "<br/>";
         messageHTML+= "<div style='text-align:center;'><input type='button' value='OK' onclick='closeMessage();return false;' class='cssTextButton'/></div>";
         //validateHTML+= "</div>";
         displayStaticMessage(messageHTML,false,370,200);
   // alert("bad credentials");
//        textLoggedIn.style.visibility = "hidden";
//        textNotLoggedIn.style.visibility = "visible";
//        DisplayInformation(
//            "Login Credentials Invalid. Could not login"); 
    }
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{
alert("logged out");
//alert("logging out");
//    // 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";
}                   

// This function displays feedback
// information for the user.    
function DisplayInformation(text)
{
//    document.getElementById("FeedBackID").innerHTML = 
//        "<br/>" + text;

//    // Display authentication service information.
//    
//    
//	var userLoggedIn =
//	    Sys.Services.AuthenticationService.get_isLoggedIn();
//	
//    var authServiceTimeout =       
//        Sys.Services.AuthenticationService.get_timeout();
//   
//    var userLoggedInfo = 
//        "<br/> User logged in:                 " + userLoggedIn;
//        
//    var timeOutInfo = 
//        "<br/> Authentication service timeout: " + authServiceTimeout;
//        
//    document.getElementById("FeedBackID").innerHTML = 
//        userLoggedInfo + timeOutInfo; 
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
