/*
Invalid scenario:
Open popupA with name 'foo'. Within popupA you open popupB also with name 'foo'.
Because they have the same name popupA will also be popupB.
And due to the onunload event in each popup window, this will cause popupA/B to close.
*/
if (typeof DWindow == "function") {
	DWindow.minimizeImageSource = '/shared/icons/window-icons/minimize_white.gif';
	DWindow.maximizeImageSource = '/shared/icons/window-icons/maximize_white.gif';
	DWindow.closeImageSource = '/shared/icons/window-icons/close_white.gif';
	DWindow.resizeImageSource = '/shared/icons/window-icons/resize_white.gif';
}

if (typeof Object.addEventListener != "function") {
	throw new Error('Popup.js:\nYou must load the file Object.js!');
}
if (typeof Timer != "function") {
	throw new Error('Popup.js:\nYou must load the file Timer.js!');
}
if (typeof PopupLanguage != "function") {
	throw new Error('Popup.js:\nYou must load the file PopupLanguage.js!');
}
/*	Class for keeping track of popups. They're closed if the opener window is closed.
	If your popup is used to submit user sensitive data, you should give it a unpredictable name to
	prevent hijacking. Or just specify the empty string and the class will do it for you.
	Or call the constructor like this: new Popup(url, Popup.scrambleName(name), features, replace)	*/
function Popup(url, name, features, replace) {
	if (typeof url != "string" && typeof name != "string" && typeof features != "string" && typeof replace != "boolean") {
		return;
	}
	this.url = (typeof url == "string") ? url : "";
	this.name = (typeof name == "string" && name != "") ? name : Popup.scrambleName(Popup.defaultName);
	if (typeof features != "string") {
		var x = (screen.availWidth - Popup.defaultWidth) / 2;
		var y = (screen.availHeight - Popup.defaultHeight) / 2;
		features = 'left=' + x + ',top=' + y + ',width=' + Popup.defaultWidth + ',height=' + Popup.defaultHeight;
		features += Popup.defaultFeatures;
	}
	this.features = features;
	this.replace = (typeof replace == "boolean") ? replace : false;
	this.popupWin = null;
	Popup.popups.push(this);
}

// must be done before Popup.getMotherWindow is called
if (typeof Document != "undefined" && typeof Document.setParentDomain == "function") {
	//Document.setParentDomain();	// no need for this if mother window does not change domain
}

Popup.getMotherWindow = function() {
	var mother = top; // Popup.js may not be loaded in parent
	try {
		while (mother.opener != null && !mother.opener.closed && mother.opener.Popup != null) {
			mother = mother.opener;
		}
	}
	catch (ex) {
		// do nothing
	}
	return (mother.Popup != null) ? mother : window;
}

Popup.getLifetime = function() {
	if (typeof Cookie == "function") {
		var sessionKey = Cookie.readCookie("SESSION-KEY");
		if (sessionKey != null) {
			return (8 * 60 * 1000); // JNP
		}
	}
	return (28 * 60 * 1000); // JNE
}

/*	Since this window is closed when a opener (or the mother) window is closed, this reference is safe to use.	*/
Popup.motherWindow = Popup.getMotherWindow();
Popup.x = NaN;
Popup.y = NaN;
Popup.dWindowStackOrder = 10000;
Popup.debug = (location.hostname.indexOf(".") == -1);
Popup.defaultName = "Popup";
Popup.defaultWidth = 600;
Popup.defaultHeight = 450;
Popup.defaultFeatures = ",toolbar,menubar,location,resizable,status,scrollbars";
Popup.lastPopup = null;
Popup.closeAllOnUnload = true;
Popup.doConfirm = false;
Popup.confirmLimit = 2 * 60 * 1000; // measured in milliseconds
Popup.keepSessionAliveURL = "/keepAlive"; // If the source refers to an image, the onerror event won't fire! But the onload event will.		
Popup.keepSessionAliveImage = new Image();
// Safari does not support the onerror event for an image
Popup.keepSessionAliveImage.onerror = function() { Popup.sessionProlonged(); };
Popup.killSessionURL = "";
Popup.killSessionImage = new Image();
Popup.killSessionImage.onerror = function() { if (typeof Popup == "function") { Popup.sessionKilled(); }; };
Popup.loginURL = "";
Popup.cookiePath = "/";
Popup.cookieExcludes = new Array("SESSION-KEY", "PI-ID", "SPROG", "HOSTNAME", "PROTOCOL", "PD-S-SESSION-ID", "JNE.Cookie", "JNE.Cookie.Locale");
Popup.popups = new Array(); // all popups are stored in this array - do not add to it manually!
Popup.activeWindow = window;
Popup.timer = (Popup.motherWindow == window) ? new Timer(NaN, Popup.getLifetime()) : null;
Popup.dWindow = null;//(Popup.motherWindow != null && document.createElement != null && typeof DWindow == "function") ? new DWindow('timerWindow', window, parent.document.title) : null;
Popup.countDownContainerId = "parCountDownContainer";
Popup.extraTimer = null; // is set, when Popup.timer times out
Popup.checkUserId = true;

Popup.scrambleName = function(name) {
	if (typeof name == "string") {
 		name += (new Date()).getTime() * Math.random();
		name = name.replace(".", "");
	}
	return name;
}

Popup.test = function(listAllCookies) {
	var msg = "";
	msg += "No of cookies:\t" + document.cookie.split("; ").length + "\n";
	msg += "Mother window:\t" + Popup.motherWindow + "\n";
	msg += "Is this window the mother window?\t" + (Popup.motherWindow == window) + "\n";
	var hasTimer = (Popup.motherWindow != null && Popup.motherWindow.Popup != null && Popup.motherWindow.Popup.timer != null);
	msg += "Is there a timer?\t" + hasTimer + "\n";
	if (hasTimer) {
		var isRunning = (hasTimer && Popup.motherWindow.Popup.timer.isRunning());
		msg += "Is it running?\t" + isRunning + "\n";
	}
	if (typeof listAllCookies == "boolean" && listAllCookies) {
		msg += "Cookies:\n" + document.cookie;
	}
	alert(msg);
}

/* IE's Security Zone must be 'Local Intranet' or 'Trusted Sites' for the JScript/ActiveX to work!	*/
Popup.getUserId = function() {
	var jbUserId = null;
	if (typeof Popup.checkUserId == "boolean" && Popup.checkUserId && document.getElementById != null) {
		var code = 'try	{';
		code += '	var jbWScript = (window.ActiveXObject != null) ? new ActiveXObject("WScript.Shell") : null;';
		code += '	if (jbWScript != null) {';
		code += '		var wshUserEnvironment = jbWScript.Environment("Process");';
		code += '		jbUserId = wshUserEnvironment.Item("Username").toLowerCase();';
		code += '	}';
		code += '}';
		code += 'catch(e) {';
		code += '}';
		eval(code);
	}
	return jbUserId;
}

Popup.askForConfirmation = function() {
	if (typeof Cookie == "function") {
		var sessionKey = Cookie.readCookie("SESSION-KEY");
		if (sessionKey != null) {
			return (Popup.getUserId() == null);
		}
		return true;
	}
	return false;
}

Popup.createFromMotherWindowOnclick = function(link) {
	if (link != null && typeof link.href == "string") {
		Popup.createFromMotherWindow(link.href, link.target);
	}
	return false;
}

Popup.createFromMotherWindow = function(url, name, features, replace) {
	if (typeof url == "string" && url != "" && typeof name == "string" && name != "") {
		if (Popup.motherWindow != null) {
			var popup, win;
			for (var i = 0; i < Popup.motherWindow.Popup.popups.length; i++) {
				popup = Popup.motherWindow.Popup.popups[i];
				if (popup != null && popup.name == name) {
					win = popup.popupWin;
					if (win != null && !win.closed) {
						alert(PopupLanguage.budgetAlreadyOpened);
						popup.focus();
						return;
					}
				}
			}
			Popup.motherWindow.Popup.create(url, name, features, replace);
		}
	}
}

// Safari does not support the onerror event for an image, so this method should not contain any logic/functionality
Popup.sessionProlonged = function() {
	if (Popup.debug) {
		window.status = "Session prolonged at " + new Date().toString() + " ...";
	}
}

// Safari does not support the onerror event for an image, so this method should not contain any logic/functionality
Popup.sessionKilled = function() {
	if (Popup.debug) {
		window.status = "Session killed at " + new Date().toString() + " ...";
	}
}

// alert user if requested popups are blocked
Popup.alertUser = function() {
	alert(PopupLanguage.popupsBlockedMessage);
}

Popup.get = function(name) {
	if (typeof name == "string") {
		var popup;
		for (var i = 0; i < Popup.popups.length; i++) {
			popup = Popup.popups[i];
			if (popup != null && popup.name == name) {
				return popup;
			}
		}	
	}
	return null;
}

/* Note that the last created popup is saved as a property 'lastPopup' on the Popup class. */
Popup.createOnclick = function(url, name, features, replace) {
	Popup.lastPopup = Popup.create(url, name, features, replace);
	return false;
}

Popup.create = function(url, name, features, replace) {
	if (Popup.motherWindow == null || Popup.motherWindow.closed || Popup.motherWindow.Popup == null || Popup.motherWindow.Popup.timer == null) {
		alert(PopupLanguage.sessionTerminatedOrNeverCreated);
		if (Popup.motherWindow != null && !Popup.motherWindow.closed) {
			Popup.motherWindow.location.replace("/");
		}
		return null;
	}
	var popup = Popup.get(name);
	if (popup == null) {
		popup = new Popup(url, name, features, replace);
	}
	popup.load(url, replace);
	popup.focus();
	return popup;
}

// deletes reference to mother window - should never be called in mother window or frame
Popup.deleteMother = function() {
	if (window.opener != null) {
		Popup.motherWindow = null;
	}
	var popup, win;
	for (var i = 0; i < Popup.popups.length; i++) {
		popup = Popup.popups[i];
		win = (popup != null) ? popup.popupWin : null;
		if (win != null && !win.closed) {
			if (win.Popup != null) {
				win.Popup.deleteMother();
			}
		}
	}
}

// does not kill session in this window, only in popups
Popup.closeAll = function(doConfirm) {
	if (typeof doConfirm != "boolean") {
		doConfirm = Popup.doConfirm;
	}
	if (window != Popup.motherWindow) {
		doConfirm = false;
	}
	var doClose = (typeof Popup.closeAllOnUnload == "boolean" && Popup.closeAllOnUnload);
	var popup, win;
	var first = true;
	for (var i = 0; i < Popup.popups.length; i++) {
		popup = Popup.popups[i];
		win = (popup != null) ? popup.popupWin : null;
		if (win != null && !win.closed) {
			if (first && doClose && doConfirm) {
				first = false;
				doClose = window.confirm(PopupLanguage.confirmCloseMessage);
			}
			if (doClose) {
				popup.close();
			} else {
				try {
					if (win.Popup != null) {
						win.Popup.deleteMother();
					}
				}
				catch (ex) {
					// do nothing
				}
			}
		}
	}
}

/* Is to be called by the onsubmit handler in a form.	*/
Popup.handleFuldmagt = function() {
	var hasOpen = false;
	var popup;
	for (var i = 0; i < Popup.popups.length; i++) {
		popup = Popup.popups[i];
		if (popup != null && popup.popupWin != null && !popup.popupWin.closed) {
			hasOpen = true;
			break;
		}
	}
	if (hasOpen) {
		var answer = window.confirm(PopupLanguage.confirmFuldmagtMessage);
		if (answer) {
			Popup.closeAll(false);
			return true;
		}
		return false; // Stop the submit action.
	}
	return true;
}

Popup.notifyOpener = function() {
	// must set active window when this window is unloaded
	if (window.opener != null && !window.opener.closed) {
		var activeWindow = window.opener;
		// detect frameset possible frameset in mother window
		if (Popup.motherWindow == window.opener) {
			if (window.opener.frames != null && window.opener.frames.length > 0) {
				activeWindow = window.opener.frames[window.opener.frames.length - 1];
			}
		}
		if (activeWindow.Popup != null) {
			activeWindow.Popup.setActiveWindow();
		}
	}
	/*
	if (window.opener != null && !window.opener.closed && window.opener.focus != null) {
		window.opener.focus(); // if a popup is opened just as the timer times out to prompt the user, this line may throw an error!
	}*/
}

// called when a document is loaded/unloaded in popup and when a window receives focus
Popup.setActiveWindow = function() {
	if (Popup.motherWindow != null && !Popup.motherWindow.closed && Popup.motherWindow.Popup != null) {
		Popup.motherWindow.Popup.activeWindow = window;
	}
}

/*	When a page/document which includes Popup.js is loaded, this method is called from Popup.loadPage in the mother window.
	It prolongs descendant sessions.
	This method is also invoked when user confirms to keep session alive (Popup.doKeepSessionAlive).	*/
Popup.keepDescendantSessionsAlive = function() {
	if (Popup.motherWindow != null && !Popup.motherWindow.closed && Popup.motherWindow.Popup != null) {
		// TODO: if mother window is the active window the session will not be kept alive (rådgivere fra WebTop)!
		if (Popup.motherWindow.Popup.activeWindow != null && !Popup.motherWindow.Popup.activeWindow.closed && Popup.motherWindow.Popup.activeWindow != window) {
			Popup.keepSessionAlive();
		}
		if (Popup.motherWindow == window) {
			// detect frameset possible frameset in mother window
			if (window.frames != null && window.frames.length > 0) {
				var contentFrame = window.frames[window.frames.length - 1];
				if (contentFrame.Popup != null && contentFrame != Popup.activeWindow) {
					contentFrame.Popup.keepSessionAlive();
				}
			}
			Popup.setTimers();
			/* In Opera access is denied to Popup.activeWindow.Popup even though document.domain is set to parent domain! */
			if (Popup.activeWindow != null && !Popup.activeWindow.closed && Popup.activeWindow.Popup != null && Popup.activeWindow.Popup.dWindow != null) {
				Popup.activeWindow.Popup.dWindow.close();
			}
		}
	}
	var popup;
	for (var i = 0; i < Popup.popups.length; i++) {
		popup = Popup.popups[i];
		if (popup.popupWin != null && !popup.popupWin.closed && popup.popupWin.Popup != null) {
			popup.popupWin.Popup.keepDescendantSessionsAlive();
		}
	}
}

// invoked when user presses OK to keep session alive
Popup.doKeepSessionAlive = function() {
	if (Popup.motherWindow != null && Popup.motherWindow.Popup != null) {
		/*	Popup will always be loaded in active window at first, but the user may have changed the
			URL in the window so the last check in the following if is needed. Note that access may be denied!		*/
		if (Popup.motherWindow.Popup.activeWindow != null && !Popup.motherWindow.Popup.activeWindow.closed && Popup.motherWindow.Popup.activeWindow.Popup != null) {
			Popup.motherWindow.Popup.activeWindow.Popup.keepSessionAlive(); // access may be denied
		}
		Popup.motherWindow.Popup.keepDescendantSessionsAlive();
	}
}

Popup.keepSessionAlive = function() {
	if (typeof Popup.keepSessionAliveURL == "string" && Popup.keepSessionAliveURL != "") {
		/*	Make the URL dynamic. Otherwise the browser will use the cache and then the server isn't hit.	*/
		var sep = (Popup.keepSessionAliveURL.indexOf("?") == -1) ? "?" : "&";	
		Popup.keepSessionAliveImage.src = Popup.keepSessionAliveURL + sep + Popup.scrambleName(window.name);
	}
}

Popup.setTimers = function() {
	if (Popup.timer != null) {
		if (Popup.timer.isRunning()) {
			Popup.timer.reset();
		} else {
			Popup.timer.start();
		}
	}
	if (Popup.extraTimer != null) {
		Popup.extraTimer.stop();
		Popup.extraTimer = null;
	}
}

Popup.stopTimers = function() {
	if (Popup.timer != null) {
		Popup.timer.stop();
		Popup.timer = null;
	}
	if (Popup.extraTimer != null) {
		Popup.extraTimer.stop();
		Popup.extraTimer = null;
	}
}

Popup.removeTimer = function() {
	if (Popup.timer != null) {
		Popup.timer.stop();
		Popup.timer = null;
	}
}

Popup.replaceTimer = function(newTimer) {
	if (newTimer != null && newTimer.constructor == Timer) {
		var wasRunning = false;
		if (Popup.timer != null) {
			wasRunning = Popup.timer.isRunning();
			Popup.timer.stop();
		}
		Popup.timer = newTimer;
		if (wasRunning) {
			Popup.timer.start();
		}
	}
}

Popup.createLogoutImage = function() {
	var ifrm = document.createElement("IMAGE");
	ifrm.setAttribute("src", "/pkmslogout");
	ifrm.style.display = 'none';
	ifrm.style.width = 2 + "px";
	ifrm.style.height = 2 + "px";
	document.body.appendChild(ifrm);
}

Popup.createLogoutIFrame = function() {
	ifrm = document.createElement("IFRAME");
	ifrm.setAttribute("src", "/pkmslogout");
	ifrm.style.display = 'none';
	ifrm.style.width = 2 + "px";
	ifrm.style.height = 2 + "px";
	document.body.appendChild(ifrm);
}

// Called when user presses CANCEL to kill session and when session is
// automatically killed due to timeout
Popup.doKillSession = function() {
	if (Popup.motherWindow != null && !Popup.motherWindow.closed && Popup.motherWindow.Popup != null) {
		Popup.motherWindow.Popup.killSession();
		Popup.motherWindow.Popup.createLogoutIFrame(); // Firefox
		Popup.motherWindow.Popup.createLogoutImage(); // IE
		Popup.motherWindow.Popup.closeAll(false);
	}
}

// kill session in every window including possible content frame in mother window
Popup.killSession = function() {
	if (typeof Popup.killSessionURL == "string" && Popup.killSessionURL != "") {
		/*	Make the URL dynamic. Otherwise the browser will use the cache and then the server isn't hit.	*/
		var sep = (Popup.killSessionURL.indexOf("?") == -1) ? "?" : "&";
		Popup.killSessionImage.src = Popup.killSessionURL + sep + Popup.scrambleName(window.name);
	}
	if (window == Popup.motherWindow) {
		if (typeof Cookie == "function" && typeof Popup.cookiePath == "string" && Popup.cookiePath != "") {
			Cookie.removeAll(window, Popup.cookiePath, null, Popup.cookieExcludes);
		}
		// detect frameset possible frameset in mother window
		if (window.frames != null && window.frames.length > 0) {
			var contentFrame = window.frames[window.frames.length - 1];
			if (contentFrame.Popup != null) {
				contentFrame.Popup.killSession();
			}
		}
		if (Popup.activeWindow != null && !Popup.activeWindow.closed && Popup.activeWindow.Popup != null && Popup.activeWindow.Popup.dWindow != null) {
			Popup.activeWindow.Popup.dWindow.close();
		}
		Popup.stopTimers();
		if (typeof Popup.loginURL == "string" && Popup.loginURL != "") {
			location.replace(Popup.loginURL);
		}
	}
}

// for use in JNP Classic login and error pages
Popup.loadInMotherWindow = function() {
	if (Popup.motherWindow != null && !Popup.motherWindow.closed && Popup.motherWindow.Popup != null) {
		if (window.opener != null && Popup.motherWindow != window) {
			Popup.motherWindow.Popup.doConfirm = false;
			Popup.motherWindow.location.replace(location.href); // remember to call Popup.removeTimer in document/window calling this method
		}
		else if (Popup.motherWindow.Popup.timer != null) {
			Popup.motherWindow.Popup.timer.stop();
		}
	}
}

Popup.loadPage = function() {
	if (Popup.motherWindow != null && !Popup.motherWindow.closed && Popup.motherWindow.Popup != null) {
		if (Popup.dWindow != null) {
			if (isNaN(Popup.motherWindow.Date.SUNDAY) || Popup.motherWindow.Date.months == null) {
				throw new Error('Popup.js:\nYou must load the file Date.js and DateLanguage.js when using a dynamic window!');
				return;
			}
			Popup.dWindow.init();
		}
	}
}

Popup.initPage = function() {
	Object.addEventListener(window, "unload", function() { Popup.closeAll(); });
	if (window.opener != null) {
		Object.addEventListener(window, "unload", function() { Popup.notifyOpener(); });
	}
	// Opera 9 does support the onfocus event in a (popup) window, but window.focus() in Popup.loadPage does not fire this handler! Timing issue?
	Object.addEventListener(window, "load", function() { Popup.loadPage(); });
	if (Popup.motherWindow != null && !Popup.motherWindow.closed && Popup.motherWindow.Popup != null) {
		if (Popup.motherWindow == window) {
			Popup.setTimers(); // important for Popup.loadInMotherWindow that Popup.setTimers is called here and not in Popup.loadPage
		} else {
			Object.addEventListener(window, "focus", function() { Popup.setActiveWindow(); });
			Popup.setActiveWindow(); // this is for the sake of Opera 9 - see comment above
			Popup.motherWindow.Popup.keepDescendantSessionsAlive(); // must be called AFTER active window is set
		}
	}
	//Popup.doConfirm = (Popup.motherWindow == window);
	//document.title = window.name; // just for debugging
}

Popup.createHtmlContent = function() {
	var htm = '<p>' + PopupLanguage.confirmProlongSession + '<\/p>';
	htm += '<p id="' + Popup.countDownContainerId + '">';
	var date = new Date(2000, 0, 1);
	date.setMilliseconds(Popup.confirmLimit);
	htm += date.format(PopupLanguage.timeLeftPattern);
	htm += '<\/p>';
	htm += '<p class="ButtonContainer">';
	var capObj = Object.getCaptionObject(PopupLanguage.cancelButton, "Accesskey");
	htm += '<button type="button" class="Button" value="Cancel" onclick="Popup.doKillSession();" accesskey="' + capObj.accesskey + '">' + capObj.caption + '<\/button>';
	capObj = Object.getCaptionObject(PopupLanguage.okButton, "Accesskey");
	htm += '<button id="btnKeepAlive" type="button" class="Button" value="Ok" onclick="Popup.doKeepSessionAlive();" accesskey="' + capObj.accesskey + '">' + capObj.caption + '<\/button>';
	htm += '<\/p>';
	return htm;
}

Popup.prototype.open = function(url, replace) {
	if (typeof url == "string") {
		this.url = url;
	}
	if (typeof replace == "boolean") {
		this.replace = replace;
	}
	// for some reason the next line may throw an exception in Firefox - but only in Firefox!?
	this.popupWin = window.open(this.url, this.name, this.features, this.replace); // Opera stops executing after this line if popups are blocked
	if (this.popupWin == null) {
		Popup.alertUser();
	}
}

Popup.prototype.close = function() {
	// access to this.popupWin.Popup may be denied (across domains)
	if (this.popupWin != null && !this.popupWin.closed) {
		try {
			if (this.popupWin.Popup != null) {
				this.popupWin.Popup.killSession();
			}
		}
		catch (ex) {
			// do nothing
		}
		try {		
			if (this.popupWin.close != null) {
				this.popupWin.close();
			}
		}
		catch (ex) {
			// do nothing
		}
	}
}

Popup.prototype.load = function(url, replace) {
	if (this.popupWin != null && !this.popupWin.closed) {
		if (typeof url == "string") {
			if (typeof replace == "boolean" && replace) {
				this.popupWin.location.replace(url);
			} else {
				this.popupWin.location.href = url;
			}
		}
	} else {
		this.open(url, replace);
	}
}

Popup.prototype.remove = function() {
	var newPopups = new Array();
	var popup;
	for (var i = 0; i < Popup.popups.length; i++) {
		popup = Popup.popups[i];
		if (popup != this) {
			newPopups.push(popup);
		}
	}
	Popup.popups = newPopups;
}

Popup.prototype.focus = function() {
	if (this.popupWin != null && !this.popupWin.closed) {
		this.popupWin.focus();
	}
}

Timer.prototype.keepRunning = function() {
	if (this == Popup.extraTimer) {
		var countDownContainer = (document.getElementById != null) ? Popup.activeWindow.document.getElementById(Popup.countDownContainerId) : null;
		if (countDownContainer != null) {
			/*
			var minutesLeft = parseInt(countDownContainer.innerHTML, 10);
			minutesLeft -= parseInt(this.delay / 1000, 10);
			countDownContainer.innerHTML = '' + minutesLeft;*/
			// In Konqueror innerHTML is actually outerHTML. This causes an error in Date.parseDate!
			var date = Date.parseDate(countDownContainer.innerHTML, PopupLanguage.timeLeftPattern);
			date.setMilliseconds(date.getMilliseconds() - this.delay);
			countDownContainer.innerHTML = date.format(PopupLanguage.timeLeftPattern);
		}
	}
	return true;
}

// Customize timer behavior - will only be called in mother window!
Timer.prototype.timeout = function() {
	if (this == Popup.timer) {
		var activeWindow = Popup.activeWindow;
		if (activeWindow.closed || activeWindow.Popup == null) {
			activeWindow = Popup.motherWindow;
		}
		if (activeWindow != null && activeWindow.Popup != null) {
			if (!Popup.askForConfirmation()) {
				Popup.doKeepSessionAlive();
				return;
			}
			if (activeWindow.Popup.dWindow != null && activeWindow.Popup.dWindow.initialized) {
				activeWindow.Popup.dWindow.write(Popup.createHtmlContent());
				activeWindow.Popup.dWindow.setStackOrder(Popup.dWindowStackOrder);
				activeWindow.Popup.dWindow.setDisplay('block');
				if (window.ActiveXObject == null) { // TODO: test in IE7
					/*	This should not be done in IE because the CSS right property is used which is (wrongfully) automatically
						adjusted for possible horizontal scrolling. */
					if (isNaN(activeWindow.Popup.x)) {
						activeWindow.Popup.x = activeWindow.Popup.dWindow.getLeft();
					}
					activeWindow.Popup.dWindow.setLeft(activeWindow.Popup.x + Window.getScrollX(activeWindow));
				}
				if (isNaN(activeWindow.Popup.y)) {
					activeWindow.Popup.y = activeWindow.Popup.dWindow.getTop();
				}
				activeWindow.Popup.dWindow.setTop(activeWindow.Popup.y + Window.getScrollY(activeWindow));
				var button = (document.getElementById != null) ? activeWindow.document.getElementById("btnKeepAlive") : null;
				if (button != null && button.focus != null) {
					button.focus();
				} else {
					activeWindow.focus();
				}
				Popup.extraTimer = new Timer(1000, Popup.confirmLimit);
				Popup.extraTimer.start();
			} else {
				var start = new Date();
				// Opera 8/9 cannot prompt in a popup window
				var prevWin = (!activeWindow.closed) ? activeWindow : window; // must remember previous active window before setting focus which will change the active window
				// Both Konqueror and Opera supports document.all!
				var cWin = (document.all != null && window.ActiveXObject == null) ? window : prevWin;
				cWin.focus();
				var keepAlive = cWin.confirm(PopupLanguage.confirmProlongSession);
				var end = new Date();
				var passed = end.getTime() - start.getTime();
				if (keepAlive) {
					keepAlive = (passed < Popup.confirmLimit);
					if (!keepAlive) {
						cWin.alert(PopupLanguage.sessionAlreadyKilled);
					}
				}
				if (keepAlive) { // keep all sessions alive - remember this method is only invoked in mother window
					prevWin = (prevWin != null && prevWin.Popup != null && !prevWin.closed && prevWin.focus != null) ? prevWin : window; // must query activeWindow because user may have closed it before answering the prompt
					prevWin.focus();
					Popup.doKeepSessionAlive();
				} else { // kill sessions now or later
					if (passed < Popup.confirmLimit) { // start extra timer which kills all sessions on timeout
						Popup.extraTimer = new Timer(NaN, (Popup.confirmLimit - passed));
						Popup.extraTimer.start();
					} else {
						Popup.doKillSession();
					}
				}
			}
		}
	}
	else if (this == Popup.extraTimer) {
		Popup.doKillSession();
	}
}

Popup.initPage();

/*
According to the book JavaScript: The Definitive Guide the window.opener property is a read/write string.
Check if this is true in the major browsers.
Alternatively Popup.create could tie every popup to the mother window?!
Or simply create them from the mother window.
*/


