﻿// Authentication.js


// 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");
    
    try {
    
	    var e = window.event;
	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();    
    } catch (exception) {
    }
    return false;
}

// This function calls the logout method of the
// authentication service to clear the forms 
// authentication cookie.
function OnClickLogout() 
{  
    // Set the default callback functions.
    SetDefaultLoginCompletedCallBack();
    SetDefaultLogoutCompletedCallBack();
    SetDefaultFailedCallBack();

    // Clear the forms authentication cookie. 
    Sys.Services.AuthenticationService.logout(null, 
        null, null, null); 

    try {
	    var e = window.event;
	    e.cancelBubble = true;
	    if (e.stopPropagation) e.stopPropagation();    
    } catch (exception) {
    }
    return false;
} 

// 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());    			
}



var username;
var password;
var buttonLogin;  
var welcomeLabel;
var rowUserName;
var rowPassword;
var rowWelcome;
var linkRegister;
var linkVendorList;
var linkForgotPassword;
var linkMyAccount;
var linkLoginStatus;
var menuLoggedOnSep1;

// 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) 
    {            
        // Hide login fields.
        rowUserName.style.display = "none";
        rowPassword.style.display = "none";
        linkRegister.style.display = "none";
        linkForgotPassword.style.display = "none";

               
        // Display logout fields.
        if (welcomeLabel.innerText == undefined) {
            //Firefox
            welcomeLabel.textContent = "Welcome, " + username.value;
        } else {
            //IE
            welcomeLabel.innerText = "Welcome, " + username.value;
        }

        rowWelcome.style.display = "inline";
        linkVendorList.style.display = "inline";
        linkNotifyList.style.display = "inline";
        linkMyAccount.style.display = "inline";
        linkLoginStatus.style.display = "inline";
        menuLoggedOnSep1.style.display = "inline";
        menuLoggedOnSep2.style.display = "inline";
        
        // Clear the feedback area.
        DisplayInformation(""); 
        
        try {
            SetNavLogonState(true);
        } catch (exception) {
        }
        try {
            SetContentLogonState(true);
        } catch (exception) {
        }
    }
    else 
    {
        DisplayInformation(
            "Login Credentials Invalid. Could not login"); 
    }
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{


    // Display login fields.
    rowUserName.style.display = "inline";
    rowPassword.style.display = "inline";
    linkRegister.style.display = "inline";
    linkForgotPassword.style.display = "inline";
   
    // Hide logout fields.
    rowWelcome.style.display = "none";
    linkVendorList.style.display = "none";
    linkMyAccount.style.display = "none";
    linkLoginStatus.style.display = "none";
    menuLoggedOnSep1.style.display = "none";
}                   

// 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;
                
    if (!userLoggedIn) {
        alert("Sign in failed.  Please check your email address and password, or use the Forgot Password link to reset your password.");
    }
    //document.getElementById("FeedBackID").innerHTML = 
    //    userLoggedInfo + timeOutInfo; 
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
