// javascript file to keep ASP.NET session alive
// this file works together with keepAlive.aspx
// (c) 2009 I.P.Services - Peter Vandijck
// versie 1.18 | 2008-02-16 9:54
//
// Place this file, along with the ASP.NET file
// into a folder in the root named //keepAlive/


// parameters voor session-timer
var interval = 300;          // om de x seconden sessie refreshen
var unreachable = 7;         // na x seconden het wachten op de response afbreken
var manualAbort = 0;     // na x seconden wordt het keepAlive-proces actief beëindigd [0 = disable]
var logStatusCodes = false   // true/false > logt onderaan de pagina de AJAX-communicatie (witte tekst)
// parameters voor message-box
var opac = 90;
var bgColorScreen = '#003311';
var bgColorDialog = '#99b7a3';
var linkColor = '#006633';
var homepage = 'http://www.logis-opleidingscentrum.be/';



function KeepAliveProcess() {
  var httpRequestResponse = null;
  var unreachableTimerId = 0;
  // eerste maal opstarten met init=true om sessie actief te starten in ASP.NET-page
  ServerClientComm('keepAlive/keepAlive.aspx?init=true');
  // vanaf de 2e keer: ASP.NET-page gewoon aanroepen om de x-aantal sec
  keepAliveIntervalId = setInterval("ServerClientComm('keepAlive/keepAlive.aspx')", interval*1000);
  if (!(isNaN(manualAbort) || manualAbort < interval)) {
    setTimeout("sessionIsLost()", manualAbort*1000);
    }
  // na het uitvoeren van het hele keepAlive gebeuren: indien nodig nog een opstart-functie in de ASP.NET pagina aanroepen
  // dit is nodig in geval er behoefte is aan een "body onload". "Body onload" werkt namelijk niet samen met de window.onload uit dit script > vandaar!
  if(typeof init == 'function') {
    init();
    }
  }


function ServerClientComm(url) {
  httpRequestResponse = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
  httpRequestResponse.onreadystatechange = requestResponse;
  httpRequestResponse.open("HEAD", url, true);
  httpRequestResponse.send(null);
  unreachableTimerId = setTimeout("httpRequestResponse.abort()", unreachable*1000);
  }

function requestResponse() {
  if (httpRequestResponse.readyState == 4) {  // geeft aan dat de communicatie die terugkomt van de server volledig is afgerond
    // indien de log-optie bovenaan dit document op TRUE staat > witte tekst wegschrijven onderaan document
    if (logStatusCodes) {
      // object aanmaken via DHTML...
      if (document.getElementById('outputKeepAlive')==null) {
        var logP = document.createElement("p");
        logP.id = 'outputKeepAlive';
        logP.style.fontSize = '8px';
        logP.style.color = 'white';
        document.body.appendChild(logP);
        }
      document.getElementById('outputKeepAlive').innerHTML += Date() + ' ------ ' + httpRequestResponse.status + '<br />';
      }
    clearTimeout(unreachableTimerId);
    if (httpRequestResponse.status == 599) {
      // er werd een nieuwe sessie gedetecteerd > originele sessie is niet meer actief!
      sessionIsLost();
      }
    }
  }

function sessionIsLost() {
  // > de cyclische communicatie server-client (opgestart via setInterval) stoppen
  clearInterval(keepAliveIntervalId);
  // > melding geven
  showSessionLostMessage();
  }


function showSessionLostMessage() {
  var ie = (navigator.appName == 'Microsoft Internet Explorer');
  var ie6 = (navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.substring(22,25) == '6.0');
  var doctype = getDoctype();
  //bg = background half transparent
  var bg = document.createElement("div");
  bg.id = 'keepAliveBackground';
  bg.style.position = 'fixed';
  bg.style.top = '0px';
  bg.style.width = '100%';
  bg.style.height = '100%';
  bg.style.backgroundColor = bgColorScreen;
  bg.style.filter = 'alpha(opacity:'+opac+')';
  bg.style.MozOpacity = opac/100;
  bg.style.KHTMLOpacity = opac/100;
  bg.style.opacity = opac/100;
  if (ie6 || (ie && doctype==null)) {
    bg.style.position = 'absolute';
    bg.style.height = '1000px';
    }
  document.body.appendChild(bg);
  //infoBox = kader met melding timeout
  var infoBox = document.createElement("div");
  infoBox.id = 'keepAliveInfoBox';
  infoBox.style.position = 'fixed';
  infoBox.style.top = '100px';
  infoBox.style.left = '50%';
  infoBox.style.marginLeft = '-175px';
  infoBox.style.width = '350px';
  infoBox.style.padding = '20px 0px 30px 0px';
  infoBox.style.textAlign = 'center';
  infoBox.style.borderStyle = 'solid';
  infoBox.style.borderWidth = '3px';
  infoBox.style.borderColor = 'white';
  infoBox.style.backgroundColor = bgColorDialog;
  infoBox.style.fontFamily = 'verdana,arial,sans-serif';
  infoBox.style.fontSize = '13px';
  infoBox.style.backgroundImage = 'url(http://www.ipservices.be/images/sessionTimeout/clock_timeout.png)';
  infoBox.style.backgroundPosition = '15px 15px';
  infoBox.style.backgroundRepeat = 'no-repeat';
  infoBox.innerHTML = '<b>De verbinding is verbroken...</b><br /><br />Ga terug naar <a style="color: '+linkColor+'; font-weight: bold;" href="'+homepage+'">onze homepage</a>.'
  if (ie6 || (ie && doctype==null)) {
    infoBox.style.position = 'absolute';
    }
  document.body.appendChild(infoBox);
  document.getElementById('keepAliveInfoBox').focus();
  }

function getDoctype() {
  var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
  if(typeof document.namespaces != "undefined") {
    if(document.all[0].nodeType==8)
      return re.exec(document.all[0].nodeValue)[3];
    else
      return null;
    } else {
    if(document.doctype != null)
      return re.exec(document.doctype.publicId)[3];
    else
      return null;
    }
  }

var keepAliveIntervalId = 0;

window.onload = KeepAliveProcess;
