/*
	Popup Window Code (PopupWindows.js)
	Copyright © 2000-2002, ActiveDynamics - http://www.activedynamics.com/
	Author: James Ritchie Carroll - ritchie@carrollhome.net
	
	Usage Restriction:	
	
		You may use this code freely on any site so long as the 
		above copyright notice remains unchanged.

	Created: September 4, 2000
	Updated: November 27, 2001 - Fixed URL reload issue on non-IE broswers
	Updated: May 10, 2002 - Added titleBar property, IE4 or better only

	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
		
	Notes:
		Remember to provide the user with a way to close the popup
		window when hiding the title bar, for example:
		
			<A HREF="javascript:window.close()">Close</A>
		
		Also, you cannot hide the title bar from a popup window for
		URL's accessing FTP sites (e.g.: ftp://ftp.microsoft.com/).
*/

// 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, titleBar)
{
	// Properties
	this.name = name;
	this.URL = URL;
	this.options = options;
	this.height = height;
	this.width = width;
	this.top = top;
	this.left = left;
	this.titleBar = titleBar;
	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, (!this.titleBar && isIE40 ? "fullscreen," : "") + "height=" + this.height + ",width=" + this.width + ",top=" + this.top + ",left=" + this.left + (this.options.length ? "," + this.options : ""));
	
	if (!this.titleBar && isIE40)
	{
		this.windowRef.blur();
		window.focus();
		this.windowRef.resizeTo(this.width, this.height);
		this.windowRef.moveTo(this.left, this.top);
    		this.windowRef.focus();
	}
	
	// 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]
//	hideTitleBar 	hide popup window title bar	[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, hideTitleBar)
{
	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, !hideTitleBar);
	
	// 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]
//	hideTitleBar 	hide popup window title bar	[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();
//
//	This function is also handy if the URL for the popup window changes frequently.
//	For example, if you have a glossary in a defined popup window with bookmarked
//	terms, you could use this function from links in your parent page:
//
//		<a href="javascript:ShowPopupWindow('Glossary', 'Glossary.html#Term')">
//
function ShowPopupWindow(name, URL, options, height, width, top, left, hideTitleBar)
{
	var objPopupWindow = eval("popupWindows." + name);
	
	// See if popup window has been defined
	if (objPopupWindow)
	{
		// Redefine any popup window properties provided
		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;
		if (URL)
		{
			objPopupWindow.URL = URL;
			if (objPopupWindow.windowRef)
			{
				if (!objPopupWindow.windowRef.closed)
				{
					if (isIE40)
					{
						// Load new URL in existing window on IE browsers
						objPopupWindow.windowRef.document.URL = URL;
					}
					else
					{
						// Close and reopen window on non-IE browsers
						objPopupWindow.unload();
					}
				}
			}
			
		}
	}
	else
		objPopupWindow = DefinePopupWindow(name, URL, options, height, width, top, left, hideTitleBar);
	
	// Show new popup window
	return objPopupWindow.show();
}

// Unload all defined popup windows
//
// Note:
//	If you want your popup windows to close when the parent page closes, then
//      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 three ways:
//
// 		1) In the HTML BODY tag:
//			<BODY ONUNLOAD="UnloadPopupWindows()">
//
//		2) Assigned to window unload event through script:
//			<script>
//			<!--
//				// Make sure any popup windows are unloaded when this window unloads
//				window.onunload = UnloadPopupWindows();
//			// -->
//			</script>
//
//		3) Declared dynamically in a script tag (IE only):
//			<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();
	}
}