// Authentication.js

var usernameEntry;
var passwordEntry;
var username;
var password;
var textLoggedIn;
var textNotLoggedIn;
var ButtonLogin;  
var ButtonLogout; 

function pageLoad()
{
    var userLoggedIn = Sys.Services.AuthenticationService.get_isLoggedIn();
    
    if(userLoggedIn)
    {
            //alert(userLoggedIn);
//            document.getElementById("registerid").style.visibility = "hidden";
//            document.getElementById("signid").style.visibility = "hidden";
//            
//            document.getElementById("NameId").style.visibility = "hidden";
//            document.getElementById("PwdId").style.visibility = "hidden";
//            document.getElementById("username").style.visibility = "hidden";
//            document.getElementById("password").style.visibility = "hidden";
//            
//            document.getElementById("loginid").style.visibility = "hidden";
//            
//            document.getElementById("forgotid").style.visibility = "hidden";
//            
//            document.getElementById("myaccountid").style.visibility = "visible";
//            document.getElementById("ButtonLogout").style.visibility = "visible";
//            document.getElementById("logoutid").style.visibility = "visible";

              document.getElementById("withoutlogin").style.display = "none";
              document.getElementById("withlogin").style.display = "";
              
            
    }
    else if (!userLoggedIn)
    {
        //alert("else");
	    
//            document.getElementById("registerid").style.visibility = "visible";
//            document.getElementById("signid").style.visibility = "visible";
//            
//            document.getElementById("NameId").style.visibility = "visible";
//            document.getElementById("PwdId").style.visibility = "visible";
//            document.getElementById("username").style.visibility = "visible";
//            document.getElementById("password").style.visibility = "visible";
//            
//            document.getElementById("loginid").style.visibility = "visible";
//            
//            document.getElementById("forgotid").style.visibility = "visible";
//            
//            document.getElementById("myaccountid").style.visibility = "hidden";
//            document.getElementById("ButtonLogout").style.visibility = "hidden";
//            document.getElementById("logoutid").style.visibility = "hidden";   

              document.getElementById("withoutlogin").style.display = "";	
              document.getElementById("withlogin").style.display = "none";                  
            
            
	   
	    username = $get("username");
    	password = $get("password");
    	
    } 
}            

// This function sets and gets the default login completed callback function.
function SetDefaultLoginCompletedCallBack()
{
    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()
{
    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()
{
    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 OnClickLogin() 
{   
    SetDefaultLogoutCompletedCallBack();
    SetDefaultLoginCompletedCallBack();
    
    Sys.Services.AuthenticationService.set_timeout(50000);
    var authTimeout = Sys.Services.AuthenticationService.get_timeout(); 
    
    // Call the authetication service to authenticate the credentials entered by the user.
    Sys.Services.AuthenticationService.login(username.value, password.value, true,null,null,null,null,authTimeout);
     //Sys.Services.AuthenticationService._setAuthenticated(true);
}

// 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)
{	
	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)
{
   
    //Sys.Services.AuthenticationService._setAuthenticated(true);
    //password.value = "";
    
    // On success there will be a forms authentication cookie in the browser.
    if (validCredentials == true) 
    {
        username.value = "";
        
//            document.getElementById("registerid").style.visibility = "hidden";
//            document.getElementById("signid").style.visibility = "hidden";
//            
//            document.getElementById("NameId").style.visibility = "hidden";
//            document.getElementById("PwdId").style.visibility = "hidden";
//            document.getElementById("username").style.visibility = "hidden";
//            document.getElementById("password").style.visibility = "hidden";
//            
//            document.getElementById("loginid").style.visibility = "hidden";
//            
//            document.getElementById("forgotid").style.visibility = "hidden";
//            
//            document.getElementById("myaccountid").style.visibility = "visible";
//            document.getElementById("ButtonLogout").style.visibility = "visible";
//            document.getElementById("logoutid").style.visibility = "visible";

              document.getElementById("withoutlogin").style.display = "none";
              document.getElementById("withlogin").style.display = "";
    }
    else 
    {
        DisplayInformation("Login Credentials Invalid. Could not login"); 
    }
}

// This is the callback function called if the user logged out successfully.
function OnLogoutCompleted(result) 
{
//            document.getElementById("registerid").style.visibility = "visible";
//            document.getElementById("signid").style.visibility = "visible";
//            
//            document.getElementById("NameId").style.visibility = "visible";
//            document.getElementById("PwdId").style.visibility = "visible";
//            document.getElementById("username").style.visibility = "visible";
//            document.getElementById("password").style.visibility = "visible";
//            
//            document.getElementById("loginid").style.visibility = "visible";
//            
//            document.getElementById("forgotid").style.visibility = "visible";
//            
//            document.getElementById("myaccountid").style.visibility = "hidden";
//            document.getElementById("ButtonLogout").style.visibility = "hidden";
//            document.getElementById("logoutid").style.visibility = "hidden";   

              document.getElementById("withoutlogin").style.display = "";	
              document.getElementById("withlogin").style.display = "none";  
}                   

// This function displays feedback information for the user.    
function DisplayInformation(text)
{
    alert(text);
    
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


