﻿// JScript File
iXPrevMousePos = 0; // Previous Horizontal position of the mouse on the screen
iYPrevMousePos = 0; // Previous Vertical position of the mouse on the screen
iXMousePos = 0; // Horizontal position of the mouse on the screen
iYMousePos = 0; // Vertical position of the mouse on the screen
var tPrevDate=new Date(); 
var bConsoleLock = false;
var bSessionExpire = false;
var bReady = false;
//Default Console Session Timeout value as 3 min. The value is in ms
var iConsoleSessionTimeout = 180000;
icounter = 0;
var bLogOffClicked = false;

function setConsoleSessionTimeOut(iConSessionTimeout)
{     
  iConsoleSessionTimeout=iConSessionTimeout;  
}

function SetReady()
{
    bReady = true;
}

/* Following function is invoked on any mouse move event. It compares 
the x & y positions of mouse. if the mouse movement is after session timeout,
then it locks the console else it updates the timestamp 
*/
function UpdateClientSession(e)
{
    if (!bConsoleLock && !bSessionExpire && bReady)
    {    
        var IsKeyEvent = false;
        if (GetKeyCode(e) != 0)
        {
            IsKeyEvent = true;
        }       
        else
        {
            if(document.all)
            {
                iXMousePos = e.x;
                iYMousePos = e.y;
            }
            else
            {
                iXMousePos = e.layerX;
                iYMousePos = e.layerY;
            }
        }
        
        lockConsole();
        if (iXMousePos != iXPrevMousePos ||  iYMousePos != iYPrevMousePos || IsKeyEvent == true)
        {   
            tPrevDate = new Date();
            iXPrevMousePos = iXMousePos;
            iYPrevMousePos = iYMousePos;             
            //icounter = icounter + 1;
            //window.status = "counter = " + icounter; 
   
                  
        }  
    }   
}

/*The following function will check whether the time difference between the 
  current time and the time of last mouse move is greater than the console session timeout.
  If so then it will lock the console by sliding an opaque div and will also slide the div 
  for Password. Also a flag would be set when the console gets locked. The value of this 
  flag would be checked while opening any popup. This is just to avoid any pop up opening 
  after console lock by tab and then enter click*/
function lockConsole()
{    
    var tCurrDate=new Date();
    var nTimeDiff = tCurrDate - tPrevDate;
    if(nTimeDiff > iConsoleSessionTimeout)
    {
        centerPopup();
        loadPopup(); 
        closePopUps(); 
        bConsoleLock = true;
        HandleConsoleLock();//To check for consolelock timeout
        document.getElementById("ctl00_txtPassword").focus();                  
    } 
}

// this function is called when the user presses the HotKey for locking the console
function lockConsoleOnDemand()
{
    centerPopup();
    loadPopup(); 
    closePopUps(); 
    bConsoleLock = true;
    HandleConsoleLock();//To check for consolelock timeout
    document.getElementById("ctl00_txtPassword").focus();  
}

//loading popup with jQuery magic!
function loadPopup(){
//loads popup only if it is disabled

$(".sessionDiv").css({
"opacity": "0.5"
});
$(".sessionDiv").fadeIn("fast");
$(".popupPassword").fadeIn("slow");

}
 

//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
{
$(".sessionDiv").fadeOut("slow");
$(".popupPassword").fadeOut("slow");
}
}
 

//centering popup - Positions the opaque div and password div
function centerPopup(){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $(".popupPassword").height(); 
var popupWidth = $(".popupPassword").width(); //centering 
$(".popupPassword").css({
"position": "absolute",
"top": window.parent.document.getElementById('frOE').parentNode.offsetTop,//Modified for crossbrowser compatibility
"left": 0,
"height": window.parent.document.getElementById('frOE').clientHeight
});
//only need force for IE6
$(".sessionDiv").css({
"height": windowHeight
});
}

/* This function will have synchronous AJAX implementation for Authentication.
After Console lock, user can unlock by keying the password. On successful authentication
of password and checking the session validity at server end, the console gets unlocked
and the timestamp of mouse move is updated
*/
function AuthenticatePwd()
{
    //Ajax Implementation 
    document.getElementById("ctl00_lblPwdMessage").style.display='none';
    var strPwd = document.getElementById("ctl00_txtPassword").value;
    var strData = "param=" + C_V_TAG_MSGCODE + ND + C_V_TAG_VALIDATEPWD + FD + C_V_TAG_PWD + ND + strPwd;
    $.ajax({
           async: false, 
           type: "POST",
            url: AJAXDATAFETCHER_URL,
           cache: false,
           dataType: "text",
           data: strData,
           success: PwdValidated,
           error: HandleAjaxError
     });
    return false;
}

// called when the AJAX call is executed successfully
function PwdValidated(strResponse)
{
    //Following function is invoked for Session Maangement.
    //In case Session has expired then the AJAX page would return Session Expiry code
    //as response
    if(!ValidateSession(strResponse))
        return false;
    if(strResponse == C_S_YES)
    {
        disablePopup();
        tPrevDate = new Date();
        bConsoleLock = false;
        document.getElementById("ctl00_txtPassword").value = '';
        HandleConsoleUnlock();//To start touchline request if it was stopped
    }
     if(strResponse == C_S_NO)
    {
        document.getElementById("ctl00_txtPassword").value = '';
        document.getElementById("ctl00_lblPwdMessage").style.display='block';
        //Implementation Pending
    }
}

function EnterKeyforPwd()
{
    if(event.keyCode == 13)
    {
        AuthenticatePwd();
        return false;       
    }  
    
}

//Following Function would close all the pop ups and would replace to Session Expire Page
function ExpireSession()
{
    if(!bSessionExpire)
    {
        bSessionExpire = true;
        closePopUps();     
        OpenWindow(SESSIONEXPIRE_URL + '?SessionExpired=0','');
    }
}


