﻿// Authentication.js

var anonView;
var loginView;
var username;
var password;
var textLoggedIn;
var textNotLoggedIn;

function pageLoad()
{
 	anonView = $get("AnonymousView");
	loginView = $get("loginView");
	userid = $get("username");	
	username = $get("LoginView1_Login1_UserName");
	password = $get("LoginView1_Login1_Password");
	usernameid2 = $get("usernameid");
	
}            

// 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 OnClickLogin() 
{   
    // 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.value, 
        password.value, 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)
{
	
    // Clear the user password.
    password.value = "";
    
    // On success there will be a forms 
    // authentication cookie in the browser.
    if (validCredentials == true) 
    {
        usernameid2.innerHTML = username.value ;
        
        // Clear the user name.
        username.value = "";
     
        // Hide login fields.
        anonView.style.visibility = "hidden";
        
   
        // Display logout fields.
        loginView.style.visibility = "visible";        
        
        // Clear the feedback area.
        DisplayInformation(""); 
        usrAuthenticated = 1;
        
    }
    else 
    {
        loginView.style.visibility = "hidden";
        anonView.style.visibility = "visible";
        DisplayInformation("Login Credentials Invalid. Could not login"); 
            usrAuthenticated = 0;
    }
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{
    //alert("This is logout Complete");
    // Display login fields.
    loginView.style.visibility = "visible";    
   
    // Hide logout fields.
    anonView.style.visibility = "hidden";
    usrAuthenticated = 0;
    return false;    
}                   

// This function displays feedback
// information for the user.    
function DisplayInformation(text)
{
    document.getElementById("FeedBackID").innerHTML = text;

    // Display authentication service information.
    
    
	var userLoggedIn = Sys.Services.AuthenticationService.get_isLoggedIn();
	
    var authServiceTimeout = Sys.Services.AuthenticationService.get_timeout();
   
    var userLoggedInfo = 
        ",Logged in: " + userLoggedIn;
        
    var timeOutInfo = 
        ",Authentication service timeout: " + authServiceTimeout;
        
    document.getElementById("FeedBackID").innerHTML = text;
    //userLoggedInfo + timeOutInfo;     
    setTimeout(HideFeeback, 5000);       
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


// This is the callback function called 
// if the authentication failed.
function OnAuthenticationFailed(error_object, 
    userContext, methodName)
{	
    DisplayInformation("Authentication failed with this error: " +
	    error_object.get_message());
}


// Loads the profile of the current
// authenticated user.
function LoadProfile()
{
	Sys.Services.ProfileService.load(null, 
	    OnLoadCompleted, OnProfileFailed, null);
}

// Saves the new profile
// information entered by the user.
function SaveProfile()
{

	Sys.Services.ProfileService.properties.Backgroundcolor = 
	    GetElementById("bgcolor").value;
	    // document.getElementById('bgcolor').value;
	Sys.Services.ProfileService.properties.Foregroundcolor =
	    GetElementById("fgcolor").value; 
	   // document.getElementById('fgcolor').value;
	Sys.Services.ProfileService.save(null, 
	    OnSaveCompleted, OnProfileFailed, null);
}

// Reads the profile information and displays it.
function OnLoadCompleted(numProperties, userContext, methodName)
{
	//document.bgColor = 
	//    Sys.Services.ProfileService.properties.Backgroundcolor;

    //document.fgColor =   
	//    Sys.Services.ProfileService.properties.Foregroundcolor;			
}

// This is the callback function called 
// if the profile was saved successfully.
function OnSaveCompleted(numProperties, userContext, methodName)
{
	LoadProfile();
	// Hide the area that contains 
	// the controls to set the profile properties.
    SetProfileControlsVisibility("hidden");
}

// This is the callback function called 
// if the profile load or save operations failed.
function OnProfileFailed(error_object, userContext, methodName)
{
	alert("Profile service failed with message: " + 
	        error_object.get_message());
}


// Utility functions.

// This function sets the visibilty for the
// area containing the page elements for settings
// profiles.
function SetProfileControlsVisibility(currentVisibility)
{
    GetElementById("setProfileProps").style.visibility = 
        currentVisibility; 
}

// Utility function to display user's information.
//function DisplayInformation(text)
//{
//	document.getElementById('FeedBackID').innerHTML = text;
//	setTimeout(HideFeeback, 5000);
	//alert("hello");
//}

function HideFeeback() {
    document.getElementById('FeedBackID').innerHTML = "";
}

   
function GetElementById(elementId)
{
    var element = document.getElementById(elementId);
    return element;
}
    
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

