﻿/* 
<PTMETAINFO> 
<PROJECT>Musannif</PROJECT>
<OBJECT>SessionTimerCookie.js</OBJECT> 
<VERSIONHISTORY> 
<VERSION ver="1.0" date="7/19/2011" modifiedby="ParachaTech"/> 
</VERSIONHISTORY> 
<COMPANY>ParachaTech Copy Rights 2011. All rights reserved.</COMPANY> 
</PTMETAINFO> 
*/
// JScript File
/******************************************************************************************************************
// This script is being used for SessionTimer and its controlling.
/******************************************************************************************************************/

var m_blnEnterFromMultiline = false; // Variable used to allow the enter in multiline
var m_blnTimerRunning = false;  // Check whether the timer of 1 second is already attached.
var m_strCookie;
var m_blnRedirectToLogOut = true;

// Method to create cookie
function CreateCookie(name,value,days) 
{
	if (days) {
		var l_date = new Date();
		l_date.setTime(l_date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + l_date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}
// Method to erase cookie
function EraseCookie(name) 
{
	CreateCookie(name, "", -1);
}


// Method to trim the string value (Left/right trim)
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
// Method that actually calculates time. If its less then a minute then shows TimerPopup
// If time becomes equal to or less a sec then redirect user to logout page.
function ShowTimer() {
    
    //debugger;
    var l_oParent = window.opener;
   	var l_strCookieArray = document.cookie.split(';');
   	var l_strCookieName = "SessionTimeCookie" + m_strSessionID;
   	var l_intCookieValue;
    var l_intSessionTimeNow;
   	 
   	for (var i=0; i < l_strCookieArray.length; i++)
    {
        var l_strElm = l_strCookieArray[i].substring(0, l_strCookieArray[i].indexOf('='));
        var l_oTrim = new String(l_strElm);
       
        if (  l_oTrim.trim() == l_strCookieName )     // Note space before Cookie's name
        {   //debugger;
            m_strCookie = l_strCookieArray[i].trim();
            break;
        }
    }
	
    if (typeof m_strCookie != 'undefined' ) 
    {
        l_intCookieValue = parseInt(m_strCookie.substring((l_strCookieName + '=').length, m_strCookie.length));
          
        if ( !l_oParent )   
            l_intSessionTimeNow = l_intCookieValue - 1;  // After every minute session time reduces by one
        else        // If Parent exists, DO NOT subtract Cookie's value by 1. Parent will do this.
            l_intSessionTimeNow = l_intCookieValue;
             
        EraseCookie(l_strCookieName);                           // Delete existing SessionTime Cookie
        CreateCookie(l_strCookieName, l_intSessionTimeNow, 1);  // Create it again with new value
        
        var l_oDivSession = document.getElementById('divSessionTimer');
        if ( l_intSessionTimeNow <= 0 && l_intSessionTimeNow ) {
	        //debugger;
	        if ( window.l_oSessionIntervalID )
	            window.clearInterval(l_oSessionIntervalID);
	        
	        var l_oHiddenVar = document.getElementById("SessionTimerIndicator");
	        if ( l_oHiddenVar != null )
	            l_oHiddenVar.value = 1;
	        // 
    	    var l_oModal = window.dialogArguments;      // Check if the ModalDialog is opened
	        if ( l_oParent || l_oModal ) {
	            window.close();
	        }
	        else {
	            //debugger;
	            if ( m_blnRedirectToLogOut == true )
	            {
	                window.location.href = m_urlLogout;     // m_urlLogout is a global param, set at Footer's load
	                //window.focus();
	            }
	        }
	    }
	    else if ( l_intSessionTimeNow < 60 && l_intSessionTimeNow )	{
	       //document.getElementById("lblTimeOut").innerHTML = l_intSessionTimeNow;
	    //if ( document.getElementById('SessionTimer').style.display == 'none' )
	    
	       if (l_oDivSession){
	            if ( l_oDivSession.style.display == 'none' && l_intSessionTimeNow > 50 )
	                l_oDivSession.style.display = 'block';	        
	            document.getElementById("lblSessionTitle").className = 'bodytext';
	            document.getElementById("lblSessionTitle").innerHTML = "&nbsp;Session Timer";
	            document.getElementById("lblSessionContent").className = 'bodytext';
	            if ( l_intSessionTimeNow < 10 ) 
	                document.getElementById("lblSessionContent").innerHTML = "Time Remaining in Session Timeout<br>" + "0:0"  + l_intSessionTimeNow + "</br>";
	            else
	                document.getElementById("lblSessionContent").innerHTML = "Time Remaining in Session Timeout<br>" + "0:"  + l_intSessionTimeNow + "</br>";
	       }
        }
	    else if ( l_oDivSession ) {
	        if ( l_oDivSession.style.display == 'block' )
	            l_oDivSession.style.display = 'none';
	    }
	}
	
   // If currently timer is not running, then start it.
    if ( !m_blnTimerRunning )
	{
	    l_oSessionIntervalID = window.setInterval("ShowTimer()",1000);
	    m_blnTimerRunning = true;
	}
}

window.setInterval("MakeContrlFloatable()",500);
//window.onscroll = makeContrlFloatable;
// Method to move the timer controll when user does scrolling
function MakeContrlFloatable()
{
	//debugger;
	var l_oParent = window.opener;
	var l_oModal = window.dialogArguments;      // Check if the ModalDialog is opened
	var l_oSessionTimer = document.getElementById('divSessionTimer');
	if (l_oSessionTimer != null)
	{
		if (typeof l_oSessionTimer != 'undefined')
		{
			var l_intDocHeight;
			var l_intDocWidth;
			var l_intCtrlPos;
			try
			{
				//docHeight = document.body.offsetHeight;
				//docHeight = window.screenTop;
				if ( ie ) {
				    l_intDocHeight = document.documentElement.scrollHeight;
				    l_intDocWidth = document.documentElement.scrollWidth;
				}
				else {
				    l_intDocHeight = window.outerHeight;
				    l_intDocWidth = window.outerWidth;
				}
				    
    				l_intCtrlPos = document.body.scrollTop + (l_intDocHeight - l_oSessionTimer.offsetHeight);
    				l_intCtrlPosLeft = document.body.scrollLeft + (l_intDocWidth - l_oSessionTimer.offsetWidth);
    				
				if ( ie ) {
				    if ( l_oParent || l_oModal ) {
				       l_oSessionTimer.style.pixelTop = l_intCtrlPos - 20; 
				       l_oSessionTimer.style.pixelLeft = l_intCtrlPosLeft - 25;
				    }
				    else {
				       l_oSessionTimer.style.pixelTop = l_intCtrlPos - 20; 
				       l_oSessionTimer.style.pixelLeft = l_intCtrlPosLeft - 30;
				    }
			    }
				else {
				    if ( l_oParent || l_oModal ) {
				        l_oSessionTimer.style.top = l_intCtrlPos - 60;
				        l_oSessionTimer.style.left = l_intCtrlPosLeft - 25;
				    }
				    else {
				        l_oSessionTimer.style.top = l_intCtrlPos - 155;
				        l_oSessionTimer.style.left = l_intCtrlPosLeft - 35;
				    }
				}    
				
			}
			catch(exception)
			{
                // do nothing
			}
			
		}
	}
}
///////////

