﻿//http://bytes.com/groups/net-c/224158-session-timeout
var xScroll, yScroll, timerPoll, timerRedirect, timerPopUp;
var TimerRediredtValue = 5400000;
var TimerPopUpValue = 1080000;
var TimeOutLocation = "Default.aspx?Page=TimeOut";
        
function SetAutoLogoutValues(id)
{
    if(id != "undefined")
    {
        var values = id.toString().split(",");
        
        TimerRediredtValue = values[0];
        TimeOutLocation = values[1];
        
        initRedirect();
    }
}  
      
function initRedirect()
{
    if (typeof $(document).attr("body").scrollTop != "undefined")
    { //IE,NS7,Moz
        xScroll = $(document).attr("body").scrollLeft;
        yScroll = $(document).attr("body").scrollTop;

        clearInterval(timerPoll); //stop polling scroll move
        clearInterval(timerRedirect); //stop timed redirect

        timerPoll = setInterval("pollActivity()",1); //poll scrolling
        timerRedirect = setInterval("RedirectDocument();",TimerRediredtValue);
        //timerPopUp = setInterval("PopUpDocument();",TimerPopUpValue);
    }
    else if (typeof window.pageYOffset != "undefined")
    { //other browsers that support pageYOffset/pageXOffset instead
        xScroll = window.pageXOffset;
        yScroll = window.pageYOffset;

        clearInterval(timerPoll); //stop polling scroll move
        clearInterval(timerRedirect); //stop timed redirect

        timerPoll = setInterval("pollActivity()",1); //poll scrolling
        timerRedirect = setInterval("RedirectWindow();",TimerRediredtValue);
        //timerPopUp = setInterval("PopUpWindow();",TimerPopUpValue);
    }
    //else do nothing
}

function pollActivity()
{
  if ((typeof $(document).attr("body").scrollTop != "undefined" && (xScroll!=$(document).attr("body").scrollLeft || yScroll!=$(document).attr("body").scrollTop)) //IE/NS7/Moz
   ||
   (typeof window.pageYOffset != "undefined" && (xScroll!=window.pageXOffset || yScroll!=window.pageYOffset))) 
   { //other browsers
      initRedirect(); //reset polling scroll position
   }
}
function PopUpDocument()
{
   confirm('Due to inactivity, your session is about to time out.');
}
function RedirectDocument()
{
    document.location = TimeOutLocation;
}
function PopUpWindow()
{
   confirm('Due to inactivity, your session is about to time out.');
}
function RedirectWindow()
{
    window.location = TimeOutLocation;
}

var OnLoad = 'initRedirect()';
$(document).ready(function(){
window.onload =         function() {eval(OnLoad);};
window.onresize=        function() {eval(OnLoad);};
document.onmousemove=   function() {eval(OnLoad);};
document.onclick=       function() {eval(OnLoad);};
document.onkeydown=     function() {eval(OnLoad);};
});
