/*
 * Hensikten er � holde i live en session gjennom � jevnlig kj�re ett AJAX kall mot serveren
 * url: en (helst dummy) side hvor requesten sendes
 * interval: intervall mellom hvert kall i millisekund
 *
 * Er testet og fungerer i Opera/Firefox/Safari/IE6/IE7
 */




var getQueryParam = function(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

var keepAlive = function(url, interval){
	if(!url) {
		keepAlive.defer(1000, this, [document.location.href, 60000]);
		return;
	}
	var fail = function() {
		Ext.TaskMgr.stop(t);
		alert('You have been logged out..\nPress OK if you wish to be redirected to the login page.');
		var artId = getQueryParam('ARTICLE_ID');
		if((!artId) || (artId == "")) {
			artId = "100";
		}
		window.location = "/ecim/openIndex?ARTICLE_ID=" + artId;
	}
	
	var failToContactServer = function() {
		Ext.TaskMgr.stop(t);
		var msg = "Connection to the server is interrupted."
			   + "Press OK to try and establish a new connection with the server. "
			   + "Press Cancel to close this message.";
		if(confirm(msg)) {
			window.location = "/ecim/openIndex?ARTICLE_ID=100";			
		}
	}
	
	var t = {
		run: function() {
			Ext.Ajax.request({
				url: url,
				success: function(res, o) {
					if (res.responseText.indexOf('not authorized') > 0) {
						fail();
					}else {
						url = document.location.href;
					}
				},
				failure: function() {
					failToContactServer();
				}
			});
		},
		interval: interval
	}
	Ext.TaskMgr.start(t);
}

/*
 * Kaller opp "blank.html" hvert minutt
 */

Ext.onReady(function(){
	keepAlive.defer(2000, this, [document.location.href, 60000]);
})

//sessionKeepAlive("/ecim/openPage/blank.html", 60000);

