<!--
/*  detect user inactivity . After 15 minutes issue a warning,
   after another minute execute call to no_activity.php to log them off

   time limit is changeable and controlled by variable "warnWait"
   
   first set up initial values
   
   warningText is used to determine if a warning
   message was already given. If not, the warning is issued. If
   it was given, then it executes a php function
   that logs them off
   
   Activity is determined by the "onmousemove" and "onkeypress" events
   If this is detected, the time interval is reset and
   it is set back to warning level (so if a warning was issued
   and then later activity occurs, you give another warning)
*/
// warnWait controls the time limit for inactivity before taking action
// it is in seconds (900 seconds = 15 minutes)
var warnWait = 1200;
var killWait = 60;
var warnOutDiv = "noActivity";
var oldWarnOutDiv = "";
var alreadyWarned = false;
//
window.onload = noActivitySetUp;
//
//      set up event handler and initiate timer
//
function noActivitySetUp() {
    document.onmousemove = activityDetected;
    document.onkeypress  = activityDetected;
//    document.getElementById(outDiv).innerHTML = "This is your original out div area";
    warnID           = setTimeout('warn()', warnWait * 1000); 
    
}
function activityDetected() {
// if activity was detected, reset time interval and warning level
//   and get rid of warning message if it was issued
//
    clearTimeout(warnID);
	if (alreadyWarned) {
		document.getElementById(warnOutDiv).innerHTML = oldWarnOutDiv;
	}
    alreadyWarned = false;
    warnID = setTimeout('warn()', warnWait * 1000);
//    document.getElementById(outDiv).innerHTML = "";
}

function warn() {
//
//    set up vaiables for warning message based on the time
//    limit that was set in "warnWait"
//
//
//  save the area before overlaying it so you can put it back later if
//  requested
//

    if (alreadyWarned == false) {
        oldWarnOutDiv = document.getElementById(warnOutDiv).innerHTML;
    }
    var inactiveTime = warnWait / 60 ;
    var inactiveUnit = " minute(s)";
    if (warnWait < 60) {
        inactiveTime = warnWait;
        inactiveUnit = " seconds"
    }
//     first level is a warning. Once warning issued, restart interval. If this has already been done then instead
//     execute php function to display input values
//
    if (alreadyWarned == false) {
        document.getElementById(warnOutDiv).innerHTML = "<span style='color:red;font-weight:bold;'>You have been inactive for "
                                                     + inactiveTime + inactiveUnit 
                                                     +  ". If no activity is detected in the next " + killWait
                                                     + " seconds you will be logged off.</span>";
        clearTimeout(warnID);
        warnID = setTimeout('warn()', killWait * 1000);
        alreadyWarned = true;
    }
    else {
        document.getElementById(warnOutDiv).innerHTML = "<span style='color:red;font-weight:bold;'>You are now being logged off</span>.";
        callNoActivity();
    }
}
function putBackDiv() {
    document.getElementById(warnOutDiv).innerHTML = oldWarnOutDiv;
}
//
//      this function uses AJAX to invoke a php function,
//      passes it the input fields on the screen and outputs
//      the results of this function which re-displays what was
//      input in a separate div on the screen
//
function callNoActivity() {
//
//   initialize output area and collect up all <input> tags
//
//    document.getElementById("output").innerHTML = "";
//    var allInput = document.getElementsByTagName("input");
    var postData = "";
//
//     generalized routine loops through all input fields
//     getting the name and value and creates a string
//     for the "POST" request to send to PHP function
//
//     only do this if the <input> tag has a name attribute
//     this is checked to avoid sending things like the "submit" button
//     which is also an <input> tag
//
//     after the first name/value pair is constructed, put "&" between
//     each value as required in "POST" data format
//
/*
    for (var i=0;i<allInput.length;i++) {
        if (allInput[i].name !== "") {
            if (i > 0) {
                postData += "&";
            }
            postData += allInput[i].name + "=" + allInput[i].value;
        }
    }
*/
//
//
//   define xmlhttp here so new copy is created each time
//   function is called so can handle multiple AJAX requests
//   overlapping without confusing the separate requests
//
    var xmlhttp;
//

    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }//
//     set up event handler for when asynchronous request completes
//     successfully to output results to screen element with ID of
//     "output"
//
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById(warnOutDiv).innerHTML += "<span style='color:red;font-weight:bold;>....." + xmlhttp.responseText + "</span>";
        }
      }
//
//      set up to send data to PHP fucntion using "POST" method
//
    xmlhttp.open("POST","/cart/php/route_request.php?request=logout",true);
	
	host = window.location.hostname;
	path= 'http://'+host+'/';
    window.location = path;
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(postData);
}
//-->

