var gModalDialogWindow;

//hasScroll - determines whether the window to have scrolling
function openModalDlg(sURL, iWidth, iHeight, hasScroll) {
	var iLeft = (screen.width - iWidth) / 2;
	var iTop = (screen.height - iHeight) / 2;
	var sFeatures = "modal=1, dialog=1, width=" + iWidth + ", height=" + iHeight + ", top=" + iTop + ", left=" + iLeft;
	if (hasScroll){
		sFeatures = sFeatures + ", scrollbars=1";
	}

	var idx = sURL.indexOf('?');
	if (idx < 0) {
		sURL += "?";
	} else if (idx < sURL.length - 1) {
		sURL += "&";
	}
	sURL += "justOpened=1";

	gModalDialogWindow = window.open(sURL, null, sFeatures);
	gModalDialogWindow.focus();
}

function addEvent(oWnd, sEventName, func) {
	if (window.addEventListener){
		oWnd.addEventListener(sEventName, func, false);
	} else if (window.attachEvent) {
		oWnd.attachEvent("on" + sEventName, func);
	}
}

addEvent(window, 'unload', function(){
	if (gModalDialogWindow != null && !gModalDialogWindow.closed) {
		gModalDialogWindow.close();
	}
});

if (document.all) {
	addEvent(window, 'focus', function(){
		if (gModalDialogWindow != null && !gModalDialogWindow.closed) {
			gModalDialogWindow.focus();
		}
	});
}
