﻿var username;
var password;
var valUser; 
var valPassword;
var errorRow;
var errorMsg;
var redirectUrl;
var passwordBox;
var usernameBox;
var uContext;

function isAuth()
{
    var result = Sys.Services.AuthenticationService.get_isLoggedIn();
    return result;    
}

function pageLoad()
    {
        // Display login or logout depending on the user's status
        ShowLoginState();
        
        var lerr = $get('loginErr');
        var lerrRow = $get('loginErrRow');
        
        if (lerr != null) {
            lerr.innerHTML = '';
            lerr.style.fontSize = '11px';
        }

        if (lerrRow != null) {
            lerrRow.style.display = 'none';
        }
    }
    
    function login(user, userBox, pwd, pwdBox, alertUser, alertPwd, errRow, errMsg, redirect, mContext)
    {
        // Set the login variables
        username = user;
        password = pwd;
        valUser = alertUser;
        valPassword = alertPwd;
        errorRow = errRow;
        errorMsg = errMsg;
       redirect = redirectUrl;
       usernameBox = userBox;
       passwordBox = pwdBox;
       uContext = mContext;
        
        // Call the login handler
        loginHandler();
    }
    
    
    // Handles the login event 
    function loginHandler()
    {
       
       //alert("Username = " + username + "\r\nPassword = " + password);
       
        if (loginFormValid(username, password, valUser, valPassword, errorRow, errorMsg))
        {
            var isPersistent = false;
            var customInfo = null;
            var redirectUrl = null;
            Sys.Services.AuthenticationService.set_path('/Authentication_JSON_AppService.axd');
               
        Sys.Services.AuthenticationService.set_defaultUserContext(uContext);
            Sys.Services.AuthenticationService.login(username, password,
                isPersistent,
                customInfo,
                redirectUrl,
                loginCompleted,
                loginFailed);
        }
            
            return false;
    }
    
    // Handles the logout event
    function logoutHandler()
    {
        var userContext = null;
        Sys.Services.AuthenticationService.set_path('/Authentication_JSON_AppService.axd');
        Sys.Services.AuthenticationService.logout(redirectUrl, logoutCompleted, loginFailed, userContext);
    }
   
   // Handles successful logout
    function logoutCompleted(result, context, methodName)
    {
         ShowLoginState();
    }
    
    // Handles the callback from the login attempt
    function loginCompleted(result, context, methodName)
    {
    

         if (result)
         {
            // Login successful
             ShowLoginState();
             Sys.Services.ProfileService.set_path('/Profile_JSON_AppService.axd');
            Sys.Services.ProfileService.load(null, onLoadSuccess, onError);
            if (context === 'ttlogin')
            {
                myProjectsAuth();
            }
            else if (context == 'smlogin')
            {
                HideActiveToolTip();
            }
         }
         else
         {
            // Login Failed
            errorRow.style.display = 'block'
            errorMsg.innerHTML = "Your login attempt was not successful. Please try again.";
         }
         
         return false;
     }

     function onLoadSuccess(obj) {

         var u = $get('spanUserName');
         if (u != null) {
             u.innerHTML = Sys.Services.ProfileService.properties.MyName;
         }
     }

     function onError(error) {
     }
    
    function loginFailed(error, context, methodName)
    {
        alert(error.get_message());
        return false;
    }
    
    // Determines wether or not to show the login or logout state depending
    // on the current users authenticated value
    function ShowLoginState()
    {
        // Check if user is logged in
        Sys.Services.AuthenticationService.set_path('/Authentication_JSON_AppService.axd');
        var result = Sys.Services.AuthenticationService.get_isLoggedIn();
        var smLogIn = $get('smallLoggedIn');
        var smLogOut = $get('smallLoggedOut');

        if (smLogIn != null & smLogOut != null) {
            if (result === true) {
                // hide the login and show the logout
                smLogIn.style.display = 'none';
                smLogOut.style.display = 'block';

                // Load the profile to get the username to display
                Sys.Services.ProfileService.set_path('/Profile_JSON_AppService.axd');
                Sys.Services.ProfileService.load(null, onLoadSuccess, onError);
            }
            else {

                // show the login and hide the logout
                smLogIn.style.display = 'block';
                smLogOut.style.display = 'none';
            }
        }
    }
    
    function loginFormValid(username, password, valUser, valPassword, errorRow, errorMsg)
    {
       
       var strMessage = '';
       
       var isValid = true;
       
       if (username === '')
       {
           valUser.style.display = 'inline';
           usernameBox.style.backgroundColor = '#FFC';
           isValid = false;
           strMessage = '- Please enter your Email.<br />';
       }
       else
       {
           valUser.style.display = 'none';
           usernameBox.style.backgroundColor = '#FFF';
       }
       
       if (password === '')
       {        
           valPassword.style.display = 'inline'
           passwordBox.style.backgroundColor = '#FFC';
           isValid = false;
           strMessage =  strMessage + '- Please enter your password.';
       }
       else
       {
           valPassword.style.display = 'none'
           passwordBox.style.backgroundColor = '#FFF';
       }
       
       if (!isValid)
       {
            errorRow.style.display = 'block';
            errorMsg.innerHTML = strMessage;
       }
       
       
       return isValid;
    }
    
    // On password field regaining focus 
    // delete the contents of the password field.
    function onPwdFocus() {
        var pwd = $get('txtPassword');
        if (pwd != null) {
            pwd.value = '';
        }
    }
