var xScroll, yScroll, timerPoll, timerRedirect, timerClock;
var time_out = 3000;

function initRedirect(){
  if (typeof document.body.scrollTop != "undefined"){ //IE,NS7,Moz
    xScroll = document.body.scrollLeft;
    yScroll = document.body.scrollTop;

    clearInterval(timerPoll); //stop polling scroll move
    clearInterval(timerRedirect); //stop timed redirect

    timerPoll = setInterval("pollActivity()",1); //poll scrolling
    timerRedirect = setInterval("location.href='logout.php?Timer=1'",time_out*1000); //set timed redirect

    clearInterval(timerClock);
    clock = 0;
    timerClock=setInterval("(clock+1)",1000);
    
    //for tracking only
    ////// This is only to show the clock
    ////clearInterval(timerClock);
    ////document.getElementById("clock").innerHTML="0";
    ////timerClock=setInterval("document.getElementById('clock').innerHTML=parseInt(document.getElementById('clock').innerHTML,10)+1",1000);
    //end tracking
  }
  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("location.href='logout.php?Timer=1'",time_out*1000); //set timed redirect

    clearInterval(timerClock);
    clock = 0;
    timerClock=setInterval("(clock+1)",1000);
    
    //for tracking only
    ////// This is only to show the clock
    ////clearInterval(timerClock);
    ////document.getElementById("clock").innerHTML="0";
    ////timerClock=setInterval("document.getElementById('clock').innerHTML=parseInt(document.getElementById('clock').innerHTML,10)+1",1000);
    //end tracking
  }
  //else do nothing
}

function pollActivity(){
  if ((typeof document.body.scrollTop != "undefined" && (xScroll!=document.body.scrollLeft || yScroll!=document.body.scrollTop)) //IE/NS7/Moz
   ||
   (typeof window.pageYOffset != "undefined" && (xScroll!=window.pageXOffset || yScroll!=window.pageYOffset))) { //other browsers
      initRedirect(); //reset polling scroll position
  }
}

document.onmousemove=initRedirect;
document.onclick=initRedirect;
document.onkeydown=initRedirect;
window.onload=initRedirect;
window.onresize=initRedirect;
