﻿// *********************
// AJAX authentification
// *********************

/// <reference name="MicrosoftAjax.js"/>
/// <reference name="MicrosoftAjaxWebForms.js"/>


// Define global variables
var _userNameTextBox = null;
var _passwordTextBox = null;
var _loginCloseImageButton = null;
var _rememberMeCheckBox = null;
var _loginCancelButton = null;
var _loginOkButton = null;
var _loginMenuItem = null;
var _accountMenuItem = null;
var _userNameLabel = null;


// 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();
}

function showLoginAnimation(value) {
    var indicator = $get('indicator');
    if (value == true)
        indicator.style.display = "block";
    else
        indicator.style.display = "none";
}

function disableLoginControls(value) {
    var loginDialog = $get('LoginDialog')
    loginDialog.disabled = value;

    _loginOkButton.disabled = value;
    _loginCancelButton.disabled = value;
    _userNameTextBox.disabled = value;
    _passwordTextBox.disabled = value;
    _loginCloseImageButton.disabled = value;
    _rememberMeCheckBox.disabled = value;
}

function checkLoginForm() {
    returnValue = true;

    var userNameMissing = $get('UserNameMissing');
    var passwordMissing = $get('PasswordMissing');

    if (_userNameTextBox.value.trim() == '') {
        userNameMissing.style.color = 'Red';
        userNameMissing.style.visibility = 'visible';
        _userNameTextBox.focus();
        returnValue = false;
    }
    else userNameMissing.style.visibility = 'hidden';

    if (_passwordTextBox.value.trim() == '') {
        passwordMissing.style.color = 'Red';
        passwordMissing.style.visibility = 'visible';
        if (returnValue) _passwordTextBox.focus();
        returnValue = false;
    }
    else passwordMissing.style.visibility = 'hidden';

    return returnValue;
}

// Login
function login(userName, password, cancel, ok, rememberMe, close, login, account, userNameLabel) {
    _userNameTextBox = $get(userName);
    _passwordTextBox = $get(password);
    _loginCloseImageButton = $get(close);
    _rememberMeCheckBox = $get(rememberMe);
    _loginCancelButton = $get(cancel);
    _loginOkButton = $get(ok);
    _loginMenuItem = $get(login);
    _accountMenuItem = $get(account);
    _userNameLabel = $get(userNameLabel);

    // Check if form is complete
    if (!checkLoginForm()) return;

    // Show animation
    showLoginAnimation(true);

    // Disable controls
    disableLoginControls(true);

    // Set the default callback functions
    setDefaultLoginCompletedCallBack();
    setDefaultLogoutCompletedCallBack();
    setDefaultFailedCallBack();

    // Call the authetication service to authenticate
    // the credentials entered by the user
    Sys.Services.AuthenticationService.login(_userNameTextBox.value.trim(), _passwordTextBox.value.trim(), _rememberMeCheckBox.checked, null, null, null, null, "User Context");
}

// This function calls the logout method of the
// authentication service to clear the forms 
// authentication cookie
function onClickLogout() {
    enableShoppingCart(false);
    // Clear the forms authentication cookie
    Sys.Services.AuthenticationService.logout('default.aspx', null, null, null);
}

// This is the callback function called 
// if the authentication fails      
function onFailed(error, userContext, methodName) {
    // Display feedback message
    var error = "error:message = " + error.get_message();
    error += "error:timedOut = " + error.get_timedOut();
    error += "error:statusCode = " + error.get_statusCode();

    var loginError = $get('LoginError');
    loginError.innerHTML = error;
    loginError.style.display = 'block';
}

// The callback function called 
// if the authentication completed successfully
function onLoginCompleted(validCredentials, userContext, methodName) {
    // On success there will be a forms 
    // authentication cookie in the browser
    if (validCredentials == true) {
        _userNameLabel.innerHTML = 'Inloggad som ' + _userNameTextBox.value;
        _loginMenuItem.innerHTML = 'Logga ut';
        _accountMenuItem.innerHTML = 'Mitt konto';
        enableShoppingCart(true);
        closeLoginDialog();
        Onyx.Web.Services.OnyxWebService.GetShoppingCart(updateShoppingCart);
    }
    else {
        // Hide animation
        showLoginAnimation(false);

        // Enable controls
        disableLoginControls(false);

        var loginError = $get('LoginError');
        loginError.innerHTML = 'Felaktigt användarnamn och/eller lösenord!<br /><br />Om du gjort fler än 5 inloggningsförsök, spärras ditt konto. Se vidare under menyvalet \'Support\'!';
        loginError.style.display = 'block';
    }
}

// This is the callback function called 
// if the user logged out successfully
function onLogoutCompleted(result) {
    _loginMenuItem.innerHTML = 'Logga in';
    _accountMenuItem.innerHTML = 'Ansök om konto';
}

function isLoggedIn() {
    return (Sys.Services.AuthenticationService.get_isLoggedIn() == true)
}

// Notify ScriptManager that this is the end of the script
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


