﻿var interval = 5 * 60 * 1000;
var errorInterval = 3 * 60 * 1000;
var normInterval = interval;
var http = null;

function sessionKeepAlive() {
    if (http == null) {
        http = getXMLHTTPObject();
    }
    try {
        //(window.status = (new Date().toLocaleTimeString());
        http.onreadystatechange = sessionKeepAlive_CallBack;
        http.open("POST", "/SessionKeepAlive.aspx", true);
        http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        http.setRequestHeader('Content-length', '0');
        http.setRequestHeader('Connection', 'close');
        http.send(null);
    } catch (e) { }
    window.setTimeout(function() { sessionKeepAlive() }, interval);
}
function sessionKeepAlive_CallBack() {
    if (http != null) {
        if (http.readyState == 4) {
            if ((http.status == 302 || http.status == 502) || (http.status == 200 && http.responseText=='false')) {//Session has terminated
                alert(TimeoutAlert);
                Sys.Services.AuthenticationService.logout(null, null, null, null);
            }
            if (http.status == 200) {//OK                
                try {
                    http.onreadystatechange = null;
                    http.open("POST", "/SessionKeepAlive.asp", true);
                    http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                    http.setRequestHeader('Content-length', '0');
                    http.setRequestHeader('Connection', 'close');
                    http.send(null);
                } catch (e) { }
                interval = normInterval;
            } else {
                interval = errorInterval;
            }
        }
    }
}
function getXMLHTTPObject() {
    var objhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    if (!objhttp) { return };
    return objhttp;
}

window.setTimeout(function() { sessionKeepAlive() }, interval);  

 


