﻿//(Sys.Services.AuthenticationService.get_isLoggedIn() == true)

function getUserInfo() {
    // If the user isPersistant, load the data
    var cookie = getCookie("wtUser")
    if (cookie != null) {
        var index = cookie.indexOf("userName");
        var str = cookie.substring(index, cookie.length);
        index = str.indexOf("=");
        var index2 = str.indexOf("&");
        var user = str.substring(index + 1, index2);
        str = str.substring(index2 + 1, str.length);
        index2 = str.indexOf("=");
        var password = str.substring(index2 + 1, str.length);
        
        if ((user.length > 0) && (password.length > 0)) { 
            var unTxt = $get('userId');
            unTxt.value = user;
	        var pwdTxt = $get('userPwd');
	        pwdTxt.value = password;
	        var ckBox = $get('chkRemember');
            ckBox.checked = 'true';
        }
    }
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start!=-1)
            { 
                c_start=c_start + c_name.length+1; 
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                    return unescape(document.cookie.substring(c_start,c_end));
            } 
        }
    return "";
}

function OnClickLogin()
{	
	var usernameTxt = $get('userId');
	var passwordTxt = $get('userPwd');
	var chkBox = $get('chkRemember');

    var userInfo = new Array();
    userInfo[0] = usernameTxt.value;
    userInfo[1] = passwordTxt.value;
    userInfo[2] = chkBox.checked;
    
	Sys.Services.AuthenticationService.login(usernameTxt.value, passwordTxt.value, chkBox.checked, null, null, OnLoginComplete, OnAuthenticationFailed, userInfo);
	    
	// Resets textboxes
    usernameTxt.value = "";
    passwordTxt.value = "";
}

function OnClickLogout()
{
	//Sys.Services.AuthenticationService.logout(null, OnLogoutComplete, OnAuthenticationFailed,null);
	Sys.Services.AuthenticationService.logout(null, null, null,null);
}

function changePwdLogout()
{   
    Sys.Services.AuthenticationService.logout("./default.aspx", null, null,null);
} 

function OnLogoutComplete(result, userContext, methodName)
{

}		

function OnLoginComplete(validCredentials, userContext, methodName)
{
    if (validCredentials == true)
	{
	    DisplayInformation("Login Successfull!");
	    	    
        // Set UserId authentication ticket
        wsUser.SetAuthTicket(userContext[1], userContext[2], OnSetAuthenticationTicketSuccess);
	}
	else
	{
		DisplayInformation("Login Failure.");
		$get('userId').focus();
	}
}

function OnSetAuthenticationTicketSuccess()
{
    this._logPopup.hide();
    var url = window.location.href;
    window.location.href = url;
    //__doPostBack(this._source.name, '');
}

function OnAuthenticationFailed(error_object, userContext, methodName)
{	
    DisplayInformation("Authentication failed: " + error_object.get_message());
    
    // Keep the popup visible.
    this._logPopup.show();
}

function DisplayInformation(text)
{
	document.getElementById('placeHolder').innerHTML = text;
}
  
function GetElementById(elementId)
{
    var element = document.getElementById(elementId);
    return element;
}

function showLogin(source) 
{
    this._source = source;
    this._logPopup = $find('logPopup');
    //  show the popup
    this._logPopup.show();
    $get('userId').focus();
}         

function cancelLogin() {
    //  find the confirm ModalPopup and hide it    
    this._logPopup.hide();
    //  clear the event source
    this._source = null;
    this._logPopup = null;
} 

/*
function showLogin()
{
    // displays div loginWrapper around login panel
    document.getElementById('loginWrapper').style.display="block";
    
    // calls modalpopupextender to open panel
    $find('mpxLoginBehavior').show();
    
    // call for modalOpen animation
    var behavior  = $find("modalOpenBehavior");   
    var onClickAnimation = behavior.get_OnClickBehavior();
    onClickAnimation.play();    
}

function closeLogin()
{
    var behavior  = $find("modalOutBehavior");   
    var onClickAnimation = behavior.get_OnClickBehavior();
    onClickAnimation.play();
}
*/

function setFocus()
{
    $get('placeHolder').innerHTML = "";
    $get('userId').focus();
    //usernameEntry.focus();
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

