// Set browser version:
var ie = document.all;
var ns6 = document.getElementById&&!document.all;
var ns = document.layers;


/*	OPENED WINDOWS ARRAY
 *
 *	Private
 *
 * An array of named (not '_blank') windows that have been opened.
 *
 * Implementation Notes:
 * - Do not define as 'new Object', else '.length' is initially undefined.
 */
var LM_windowsOpenedArray=new Array();  // Array of openedWindow objects


/**	FN: OPENED WINDOW DATA STRUCTURE
 *
 *	Private
 *
 * Constructor for data structure to track the names and JS references of named (not '_blank') browser windows.
 *
 * Dependency: global var 'LM_windowsOpenedArray'.
 */
function LM_openedWindow(winName, winRef) {
	this.name=winName;  // 'target=' name of the window
	this.ref=winRef;  // browser's object reference to the window
}  //end, LM_openedWindow constructor


/**	FN: OPEN BROWSER WINDOW
 *
 *	Public
 *
 * Opens a browser window, with four cases.
 *   (a) If the window's name is '_blank' (viz., target="_blank") then the window gets opened simpliciter.
 *   (b) If the window is named (not '_blank') but has been closed by the user, then the window is re-opened on
 *     the default page (i.e., the address passed to this as a param).
 *   (c) If the window is named and is currently open (and the user might have navigated it to a particular page),
 *     then the window gets focus on whatever page is currently open there.
 *   (d) If the window is named but has never been opened in the current 'session' (i.e., since this Left Menu page
 *     loaded or was refreshed), then the window is opened on the default page (i.e., the address passed to this
 *     as a param) and the window is added to the Array of Opened Windows.
 *
 * Params: Caller passes String of the default URL (or empty) of the webpage, and String of the "target=" browser's
 *   name of the window.
 * Return: JavaScript's internal reference to the opened window.
 *
 * Implementation Notes:
 * - Dependencies: LM_openedWindow(); and LM_windowsOpenedArray.
 * - Un-named ('_blank') windows always open a new window (there can be many windows named '_blank').
 * - All windows are opened with the standard window properties (address bar, etc.).
 * - When user closes a browser window, its Window Object persists, but it's 'closed' property is set to _true_.
 */
function LM_openWindow(webAddress, windowName) {
	var windowRef;  // local window object ref; return value
	var newWindow;  // new LM_openedWindow data structure
	var i=0;  // index
	var j=0;  // limit
	var found=false;  // flag

	// Assertions:
	if (null == webAddress) {
		webAddress='';
	}
	if (null == windowName) {
		windowName='_blank';
	}
	if (windowName == '') {
		windowName='_blank';
	}
	//end, assertions

	if (windowName == '_blank') {
		// Open but don't add:
		windowRef=window.open(webAddress, windowName);
		// In most browsers, focus now switches to this window.
	// case, not '_blank':
	} else {
		// Look for the named window in the array of opened windows:
		i=0;  // init
		j=LM_windowsOpenedArray.length;
		found=false;  // flag, found
		while (i < j) {
			if (LM_windowsOpenedArray[i].name == windowName) {
				found=true;
				windowRef=LM_windowsOpenedArray[i].ref;
				break;
			}
			i++;
		}  //end, while
		if (found) {
			// Check whether window has been closed by user (but it still remains in the browser's list):
			if (windowRef.closed) {
				// Re-open with old handle and default Web address:
				windowRef=window.open(webAddress, windowName);
				// In most browsers, focus now switches to this window.
			// case, not closed:
			} else {
				// Get focus (on most platforms, moves new window to front):
				windowRef.focus();
			}
		// case, not found:
		} else {
			// Open new (or re-open old, if this LM_windowsOpenedArray was obliterated by a reload):
			windowRef=window.open(webAddress, windowName);
			// In most browsers, focus now switches to this window.
			newWindow=new LM_openedWindow(windowName, windowRef);
			// Add new member to the Array:
			LM_windowsOpenedArray[LM_windowsOpenedArray.length]=newWindow;  // add [starts at zero]
		}  //end, if found/not found
	}  // end, if/else '_blank'
	return windowRef;
}  //end, LM_openWindow()

