﻿// JScript File
var nOffset = 0;
var dServerDate;
var dClientDate;
function UpdateClock() 
{
    try
    {
        if(clockID) 
        {
            clearTimeout(clockID);
            clockID  = 0;
        }

        var tDate=new Date();
        
        tDate.setTime(tDate.getTime() + nOffset);
        
        var nhours=tDate.getHours();
        var nmins=tDate.getMinutes();
        var nsecn=tDate.getSeconds();
            
        var AorP=" ";
        
        if (nhours>=12) AorP="PM";  else AorP="AM";
        if (nhours>=13) nhours-=12;
        if (nhours==0)  nhours=12;
        if (nsecn<10)   nsecn="0"+nsecn;
        if (nmins<10)   nmins="0"+nmins;

        $(document.getElementById("lblDate")).text(tDate.toDateString());
        $(document.getElementById("lblTime")).text("" + nhours + ":" + nmins + ":" + nsecn + " " + AorP);  
        clockID = setTimeout("UpdateClock()", 1000);
        /* On Every Time tick there would be check whether the time difference between the 
        current time and the time of last mouse move is greater than the console session timeout. 
        The timeout value is member level configurable. A check for document.lockConsole
        is done to avoid the function being called by the Login Page, as Server time would be present at 
        Login and the Base page*/
        if(document.lockConsole)
            lockConsole(); 
    }
    catch(e)
    {
        alert('UpdateClock' + e.message);
    }
}

function StartClock(isLocalTime)
{
    try
    {
        //var t = new Date()
        //alert(t.getDate());
        if(!isLocalTime)
        {
            GetServerDateTime();
        }    
        clockID = setTimeout("UpdateClock()", 500);
    }
    catch(e)
    {
    alert('StartClock' + e.message);
    }
}

function KillClock() 
{
    try
    {
        if(clockID) 
        {
            clearTimeout(clockID);
            clockID = 0;
        }
    }
    catch(e)
    {
        alert('KillClock' + e.message);
    }
}

//to get server date time through ajax call
function GetServerDateTime()
{
    
    var sRequest=CreateRequest(C_V_SERVERDATETIME,'');
    
    var strData = "param="+sRequest;
    $(document).ready(function(){
        $.ajax({
            type: "POST",
            url: AJAXDATAFETCHER_URL,
            cache: false,
            dataType: "text",
            data: strData,
            success: CalculateOffset,
            error: HandleAjaxError
        });
    });
}

function CalculateOffset(sResponse, status)
{
    //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(sResponse))
        return false;
    try
    {
        if(sResponse != "")
        {
            var arrTemp = sResponse.split('|');
            dServerDate = new Date(arrTemp[0],arrTemp[1]-1,arrTemp[2], arrTemp[3], arrTemp[4], arrTemp[5], arrTemp[6]);
            dClientDate = new Date();
            nOffset = dServerDate.getTime() -  dClientDate.getTime();
            //If server time and client time is not same intimate the user about the syncronization
            //The msg will be displayed in the Online Message UC
            if(nOffset != 0)
            {
                // DisplayMessage function present in Online Message UC
                DisplayMessage('Your terminal\'s clock has been synchronized with the server\'s clock');
            }
        }
    }
    catch(e)
    {
        DisplayMessage('Failed to synchronize terminal\'s clock with the server\'s clock, currently displaying terminal\'s time');
        nOffset=0;   
    }
}

function HandleAjaxError(sResponse)
{
    alert(sResponse);
}