/*
	Popup Window Code (PopupWindows.js)
	James Ritchie Carroll
	
	Created: September 4, 2000
	Updated: November 27, 2001 - Fixed URL reload issue on non-IE broswers

	External Dependencies:
		Common Functions (CommonFunctions.js)

	Consumer Objects:
		popupWindows		- associative collection of popupWindow objects
		
	Consumer Functions:
		DefinePopupWindow	- defines a new popup window
		ShowPopupWindow		- show a popup window, defines if it doesn't exist
		UnloadPopupWindows	- unloads all defined popup windows
*/

// Associative array for popup window objects
var popupWindows = new popupWindowArray();

function popupWindowArray()
{
	// All properties dynamically added
}

// Popup window object constructor
function popupWindow(name, URL, options, height, width, top, left)
{
	// Properties
	this.name = name;
	this.URL = URL;
	this.options = options;
	this.height = height;
	this.width = width;
	this.top = top;
	this.left = left;
	this.windowRef = null;
	
	// Methods
	this.show = popupWindow_show;
	this.load = popupWindow_load;
	this.unload = popupWindow_unload;
}

// popupWindow::show()
function popupWindow_show()
{
	// If popup window is already open, bring it into focus, 
	// else load new popup window
	if (this.windowRef)
	{
		if (this.windowRef.closed)
			this.load();
		else
			this.windowRef.focus();
	}
	else
		this.load();
	
	// Return window reference
	return this.windowRef;
}

// popupWindow::load()
function popupWindow_load()
{
	// Open new popup window
	this.windowRef = window.open(this.URL, this.name, "height=" + this.height + ",width=" + this.width + ",top=" + this.top + ",left=" + this.left + (this.options.length ? "," + this.options : ""));
	
	// Return window reference
	return this.windowRef;
}

// popupWindow::unload()
function popupWindow_unload()
{
	// Close popup window if it is open
	if (this.windowRef)
		if (!this.windowRef.closed)
			this.windowRef.close();
	
	// Clear window reference
	this.windowRef = null;
}

// Define popup window
// 		name	unqiue name of window			[required]
// 		URL 	URL of popup window				[required]
// 		options	window opening options string	[optional]
// 		height	height of popup window			[optional]
// 		width	width of popup window			[optional]
// 		top		top position of popup window	[optional]
// 		left	left position of popup window	[optional]
//
// Note:
//		This function will define a popup window.  Window names must be unique.
//		You should only call this function once per window.
//
function DefinePopupWindow(name, URL, options, height, width, top, left)
{
	var objPopupWindow = null;
	
	if (!name || !URL) return null;
	if (!options) options = "status=0,toolbar=0,menubar=0,location=0,resizable=0";
	if (!height) height = screen.height / 3;
	if (!width) width = screen.width / 3;
	if (!top) top = (screen.height - height) / 2 - 27;
	if (!left) left = (screen.width - width) / 2;			
	
	// Create new popup window object
	objPopupWindow = new popupWindow(name, URL, options, height, width, top, left);
	
	// Create associative index for new popup window object
	eval("popupWindowArray.prototype." + name + " = objPopupWindow");

	// Return new popup window object
	return objPopupWindow;
}

// Show popup window
// 		name	unqiue name of window			[required]
// 		URL 	URL of popup window				[required when window is not defined]
// 		options	window opening options string	[optional]
// 		height	height of popup window			[optional]
// 		width	width of popup window			[optional]
// 		top		top position of popup window	[optional]
// 		left	left position of popup window	[optional]
//
// Note:
//		This function is just provided for convenience to define and show a popup window
//		in one step.  If you have already defined your popup window you can always reference
//		it directly as "popupWindows.<name>". For example, if you defined a popup window
//		called  "DetailDialog", you could show the window in script using:
//
//				popupWindows.DetailDialog.show();
//
function ShowPopupWindow(name, URL, options, height, width, top, left)
{
	var objPopupWindow = eval("popupWindows." + name);
	
	// See if popup window has been defined
	if (objPopupWindow)
	{
		// Redefine any popup window properties provided
		if (URL)
		{
			objPopupWindow.URL = URL;
			if (objPopupWindow.windowRef)
			{
				if (isIE40)
				{
					// Load new URL in existing window on IE browsers
					if (!objPopupWindow.windowRef.closed)
						objPopupWindow.windowRef.document.URL = URL;
				}
				else
				{
					// Close and reopen window on non-IE browsers
					objPopupWindow.unload();
					objPopupWindow.show();
				}
			}
			
		}
		if (options) objPopupWindow.options = options;
		if (height) objPopupWindow.height = height;
		if (width) objPopupWindow.width = width;
		if (top) objPopupWindow.top = top;
		if (left) objPopupWindow.left = left;
	}
	else
		objPopupWindow = DefinePopupWindow(name, URL, options, height, width, top, left);
	
	// Show new popup window
	return objPopupWindow.show();
}

// Unload all defined popup windows
//
// Note:
//		This function should be called in the ONUNLOAD event of any HTML form 
//		that references this code.  The function can be declared in one of two ways:
//
// 			1) In the HTML BODY tag:
//				<BODY ONUNLOAD="UnloadPopupWindows()">
//
//			2) Declared dynamically in a script tag:
//				<script for="window" event="onunload" language="JavaScript">
//				<!--
//					// Make sure any popup windows are unloaded when this window unloads
//					UnloadPopupWindows();
//				// -->
//				</script>
//
function UnloadPopupWindows()
{
	var x;

	// Unload each defined popup window
	for (x in popupWindows)
	{
		popupWindows[x].unload();
	}
}