
function getXmlHttp(){
  var xmlhttp;
  /** Special IE only code ... */
  /*@cc_on
    @if (@_jscript_version >= 5)
      try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
            xmlhttp = false;
        }
     }
    @else
      xmlhttp = false;
  @end @*/
  
  /** Every other browser on the planet */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
        xmlhttp = new XMLHttpRequest();
    }
    catch (e) {
       xmlhttp = false;
    }
  }
  return xmlhttp;
}


function initTimeout(check, to, f, bvTo) {
  var date = new Date();
  start = date.getTime();
  freq = f * 1000; //s->ms
  bvTimeout = bvTo * 1000 * 60; //min->ms
  timeout = to * 1000 * 60;
  runTimeout(check, timeout);
}

function runTimeout(check) {
  pingSession = check;
  var date = new Date();
  var dt = date.getTime() - start;
  xmlHttp = getXmlHttp();
  if((dt + bvTimeout)  >= timeout) {
    pingSession = false;
  }
  
  if(pingSession && xmlHttp) {
    setTimeout('keepSessionAlive()',freq);
  }
}

function keepSessionAlive() {
  if(xmlHttp) {
    xmlHttp.open('GET', pingUrl, true);
    // Force no cache on IE
    xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    xmlHttp.onreadystatechange = processPingRequest;
    xmlHttp.send(null);
  }
}

function processPingRequest() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      var result = xmlHttp.responseText;
      // The result would change the pingSession variable
      if(result.length>100 || result.indexOf("pingSession")==-1)
       pingSession = false;
      else
       eval(result);
    }
    else {
      pingSession = false;
    }
    runTimeout(pingSession, timeout);
  }
}
