/*
Graceful / customisable popup window routines
Windows will always re-focus if opened again, centered in screen.
*/

	var winObj = new Array();
	var winRef = new Array();
	var winCount = 0;
	// open or set focus to a window
	function openWindow(url, winName, id, width, height, toolbar, scrollbar, resize) {
		var i
		if(toolbar!='yes') toolbar = 'no'
		if(resize!='yes') resize = 'no'
		if(scrollbar!='no') scrollbar = 'yes'
		var properties = 'scrollbars=' + scrollbar + ',status=no,location=no,menubar=no,toolbar=' + toolbar + ',resizable=' + resize + ',width=' + width + ',height=' + height;
		var win=null;
		
		// search for an existing window of that name
		if (winCount>0) {
			for (i=0;i<winCount;i++) {
				if (winRef[i]==winName) {
					win=winObj[i];
					break;
				};
			};
		};

		//put the console in the middle of the screen
		if (window.screen) 
		{ 
			var ah = screen.availHeight - 30;
			var aw = screen.availWidth - 10;
			var xc = (aw - width) / 2;
			var yc = (ah - height) / 2;

			properties += ',innerHeight=' + height
			+ ',innerWidth=' + width
			+ ',left=' + xc + ',screenX=' + xc
			+ ',top=' + yc + ',screenY=' + yc;
		}
		
		
		// if found, show it or recreate it, otherwise add it to the list of popups and show it
		if (win) {
			if (!win.closed) {
				win.focus()
				win.location=url;
			}
			else {
				win=window.open(url, winName, properties);
				winObj[i]=win;
				win.focus()
			}
		}
		else {
			win = window.open(url, winName, properties);
			winObj[winCount]=win;
			winRef[winCount]=winName;
			winCount++;
			win.focus()
		};
	};
