﻿var userAgent = navigator.userAgent.toLowerCase();   // Figure out what browser is being used   
var browser = {   
version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],   
safari: /webkit/.test( userAgent ),  
google: /chrome/.test( userAgent ), 
opera: /opera/.test( userAgent ),   
msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),   
mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ), 
macintoshOS:/macintosh/.test(userAgent) //macintoshOS for identifying the operating system of the end user -- this identify whether the user using AppleMAc or not
};   
var ver=userAgent.split('/');
    ver=ver[ver.length-1].split('.');
    var currentVersion=ver[0];    




var milisec = 0
var seconds = 10

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie() {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    //var check_name = 'VEVNUFVTRVJJRA..';
    var check_name = "COOKIETIME";
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    var currTime = new Date();
    var difference;
    var hrs;
    var dt;
    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            
           dt =new Date(cookie_value);
           var difference = currTime.getHours() - dt.getHours(); 
           alerttime=new Date();          
            if(difference==7)
            {
                if(confirm("Your session will be expired soon. Click 'OK' to keep the session"))
                {                     
                     expires_date = new Date();
                     var responsehour=expires_date.getHours()-alerttime.getHours(); 
                     var resopnseminute=expires_date.getMinutes()-alerttime.getMinutes();                      
                     if(responsehour>0 || resopnseminute>30)
                     {
                            logout(); 
                            return;
                     }  
                     if(browser.msie==true)         
                        document.cookie=cookie_name+"="+cookie_value+";expires="+expires_date.toGMTString()+";";                        
                     else
                        document.cookie=cookie_name+"="+cookie_value+";expires="+expires_date.toGMTString()+"; path=/"; 
                     
                     expires_date.setHours(expires_date.getHours()+8);
                     document.cookie = cookie_name+"=" + currTime +"; expires=" + expires_date.toGMTString()+"; path=/"; 
                          
                     xmlHttp=GetXmlHttpObject();
                     var url='/ajax/cookie-reset.aspx?cookie=reset';
                     xmlHttp.onreadystatechange=cookieReset;
                     xmlHttp.open('get',url,true);
                     xmlHttp.send(null);                     
                }
            }                
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
function cookieReset()
{
    if(xmlHttp.readyState==4)
    {         
       alert("Your sesion has been reset successfully");
    }
}

function Set_Cookie() {
    today = new Date();   
    expires_date = new Date();
    expires_date.setHours(expires_date.getHours()+8);
    var cookies=document.cookie;
    if(!cookies.match('COOKIETIME'))        
        document.cookie = "COOKIETIME" + "=" + today +"; expires=" + expires_date.toGMTString()+"; path=/";     
 
}

function display() {
Get_Cookie();
setTimeout("display()", 3600000);

} 
var xmlHttp;
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
	objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 
function logout()
{
     var cookies=document.cookie.split(';');    
     for (var j = 0; j <cookies.length; j++)
     {
       expires_date = new Date();
       expires_date.setHours(expires_date.getHours()-8);
       var cookie = cookies[j].split('=');
       var name=cookie[0];
       var value=cookie[1];
       if(browser.msie==true)         
            document.cookie=name+"="+value+";expires="+expires_date.toGMTString()+";";                        
       else
            document.cookie=name+"="+value+";expires="+expires_date.toGMTString()+"; path=/"; 
     }
    document.location="/";
}