﻿// JScript File
var isAuthenticated;
var grUserName;

function pageLoad (sender, args)
{
    
    PageMethods.GetIdentity(gotIdentity);
}

function gotIdentity (result, userContext, methodName)
{
    isAuthenticated = result.IsAuthenticated;
        
    if (isAuthenticated == true)
    {
        grUserName = result.Name;
        
        hideLoginFields();
        validateUser = false;
        
        document.getElementById(loginLinkId).href = 'default.aspx?u=' + grUserName;
        
        $get('usernameloginlbl').innerHTML=grUserName;
        
        document.getElementById('mcm').style.display = (result.IsAdmin?"none":"inline");
        
        if (result.IsAdmin)
        {
            $get('adminlink').style.display='inline';
            jQuery('#mDealerAccount').hide();
        }
        else if (result.IsDealer)
        {
            $get('adminlink').style.display='none'; 
            jQuery('#mDealerAccount').show();
        }
        else
        {
            $get('adminlink').style.display='none';
            jQuery('#mDealerAccount').hide();
            
        }
        
    }
    else
    {
        showLoginFields();
        validateUser = true;
    }
}

function showLoginFields()
{
    jQuery('#mDealerAccount').hide();
    $get('login_display').style.display = "block";
    $get('login_status').style.display = "none";
    $get('welcome').style.display = "none";
if ($get('userdata1'))   $get('userdata1').style.visibility = "visible";
if ($get('userdata2'))    $get('userdata2').style.visibility = "visible";        
}

function hideLoginFields()
{
    
    $get('login_display').style.display = "none";
    $get('login_status').style.display = "block";
    $get('welcome').style.display = "block";
if ($get('userdata1'))   $get('userdata1').style.visibility = "hidden";
if ($get('userdata2'))    $get('userdata2').style.visibility = "hidden";        
}


// 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 LoginUser(username, password, closePop) 
{   
    // 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, false,null,null,null,null,"User Context");
    
    if (closePop == null || closePop == true)    
        closePopups();
}

// This function calls the logout method of the
// authentication service to clear the forms 
// authentication cookie.
function LogoutUser() 
{  
   // 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());			
	    */
	    alert ('login failed ' + error.get_message());
}


// 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) 
    {        
    
        
        // Clear the user name.
        //username.value = "";

       
        // Clear the feedback area.
        //DisplayInformation(""); 
    }
    else 
    {
        //textLoggedIn.style.visibility = "hidden";
        //textNotLoggedIn.style.visibility = "visible";
        //DisplayInformation(
        //    "Login Credentials Invalid. Could not login"); 
        alert("Invalid user or password");
    }
    
    PageMethods.GetIdentity(gotIdentity);
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{
    // Display login fields.
    showLoginFields();

}                   

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
