// TWindow Routines
// Requires Browser.js to be included in page.

// Default object & function to open a window
var oWindow1	= null;
function openWindow(sURL, sWindowName, nFeatures, sDimensions, vDialogArguments)
{	// no return value
	openWindow2(sURL, sWindowName, nFeatures, sDimensions, vDialogArguments);
}
function openWindow2(sURL, sWindowName, nFeatures, sDimensions, vDialogArguments)
{
	if ("string" != typeof(sWindowName))	sWindowName	= "popupWindow";
	if ("number" != typeof(nFeatures))		nFeatures	= null;
	if ("string" != typeof(sDimensions))	sDimensions	= null;
	oWindow1	= new TWindow("oWindow1", sURL, sWindowName);
	if (sDimensions != null)
	{
		// sDimensions format = "X,Y,X2,Y2"
		var aDimensions	= sDimensions.split(",");
		if (aDimensions.length == 4)
		{
			oWindow1.setLeft(aDimensions[0]);
			oWindow1.setTop(aDimensions[1]);
			oWindow1.setWidth(aDimensions[2]);
			oWindow1.setHeight(aDimensions[3]);
		}
	}
	oWindow1.setFeatures(nFeatures);
	if (oWindow1.isDialog())
		oWindow1.setArguments(vDialogArguments);
	return oWindow1.open();
}

// TWindow 'features' bitwise variables
var TwAutoCenter	= 1;
var TwAutoClose		= 2;
var TwAutoSize		= 4;
var TwDirectories	= 8;
var TwFullScreen	= 16;
var TwLocation		= 32;
var TwMenubar		= 64;
var TwResizable		= 128;
var TwScrollbars	= 256;
var TwStatus		= 512;
var TwToolbar		= 1024;
var TwDialog		= 2048;
var TwModeless		= 4096;
var TwChromeLess	= 8192;
var TwPopUnder		= 16384;

// [TWindow Object Prototype] Create new window object
function TWindow(sObjectName, sWinUrl, sWinName, nWinLeft, nWinTop, nWinWidth, nWinHeight)
{
//	Private Properties
	var oWindow			= null;
	var oWindow2		= null;
	var bAutoCenter		= false;
	var bAutoClose		= false;
	var bAutoSize		= false;
	var nDelay			= 100;
	var nDelayCount		= 0;
	var nDependent		= 0;
	var bDialog			= false;
	var dialogArguments	= null;
	var nDirectories	= 0;
	var nFeatures		= 0;
	var nHeight			= 440;
	var nHotkeys		= 0;
	var nLeft			= 20;
	var nLocation		= 0;
	var bModeless		= false;
	var nMenubar		= 0;
	var sName			= null;
	var bPopUnder		= false;
	var nResizable		= 0;
	var nScrollbars		= 0;
	var nStatus			= 0;
	var nToolbar		= 0;
	var nTop			= 20;
	var sURL			= "";
	var nWidth			= 600;
	var sWindowName		= "_blank";
//	Internet Explorer specific
	var nFullscreen		= 0;
	var nChromeLess		= 0;

//	Public Method Pointers
	this.close			= close;
	this.closeAll		= closeAll;
	this.closeWindow	= closeWindow;
	this.isDialog		= isDialog;
	this.open			= open;
	this.open2			= open2;
//	this.openDialog		= openDialog;

//	Public Property Method Pointer
	this.setAutoCenter	= setAutoCenter;
	this.setAutoClose	= setAutoClose;
	this.setAutoSize	= setAutoSize;
	this.setArguments	= setArguments;
	this.setFeatures	= setFeatures;
	this.setHeight		= setHeight;
	this.setLeft		= setLeft;
	this.setTop			= setTop;
	this.setURL			= setURL;
	this.setWidth		= setWidth;
	this.setWindowName	= setWindowName;

//	Initialization
	if (is.ns4)
	{
		setArguments(dialogArguments);
		setFeatures(nFeatures);
		setHeight(nHeight);
		setLeft(nLeft);
		setTop(nTop);
		setURL(sURL);
		setWidth(nWidth);
		setWindowName(sWindowName);
		initNetscape4(sName, nDelay, nDelayCount, nDependent, nHotkeys);
	}
	setObjectName(sObjectName);
	setURL(sWinUrl);
	setWindowName(sWinName);
	setHeight(nWinHeight);
	setLeft(nWinLeft);
	setTop(nWinTop);
	setWidth(nWinWidth);

//	Private Property Methods (No public pointers)
	function initNetscape4(name, delay, delayCount, dependent, hotkeys)
	{
		oWindow		= null;
		oWindow2	= null;
		sName		= name;
		nDelay		= delay;
		nDelayCount	= delayCount;
		nDependent	= dependent;
		nHotkeys	= hotkeys;
	}

//	Public Methods (They have public pointers)
	function close()
	{
		TWindow_closeWindow(oWindow, sWindowName, sURL);
	}

	function closeAll()
	{
		TWindow_doCloseAll();
	}

	function closeWindow(sWinName)
	{
		if ("string" != typeof(sWinName))
			return;
		var sWinURL;
		for (var idx = oTwWindows.length - 1; idx >= 0; idx--)
		{
			if ("object" != typeof(oTwWindows[idx]))
			{
				oTwWindows.splice(idx, 1);
				oTwWinNames.splice(idx, 1);
				oTwWinURLs.splice(idx, 1);
				continue;
			}
			if (oTwWinNames[idx].toLowerCase() == sWinName.toLowerCase())
			{
				oWindow2	= oTwWindows[idx];
				sWinURL		= oTwWinURLs[idx];
				TWindow_closeWindow(oWindow2, sWinName, sWinURL);
				oTwWindows.splice(idx, 1);
				oTwWinNames.splice(idx, 1);
				oTwWinURLs.splice(idx, 1);
			}
		}
	}

	function isDialog()
	{
		return bDialog;
	}

	function open()
	{
		if (bDialog && is.ieDOM)
		{
			closeWindow(sWindowName);
			return openDialog(true);
		}
		// If PDF plug-in not present, set bAutoClose to false to prevent JS error.
		if (sURL.toLowerCase().indexOf(".pdf") != -1 && !hasPlugin("application/pdf") && bAutoClose)
			setAutoClose(false);
		closeWindow(sWindowName);
		nDelayCount	= 1;
		open2(true);
		return null;
	}

	function open2()
	{
		if (!getArgument(arguments, 0, "boolean", false))
			return;
		if (oWindow2 != null)
		{
			if (!oWindow2.closed && nDelayCount < 10)
			{
				nDelayCount++;
				setTimeout(sName + ".open2(true);", nDelay);
				return;
			}
			oWindow2	= null;
		}
		var sFeatures	= "";
		sFeatures	+= "dependent=" + nDependent;
		sFeatures	+= ",directories=" + nDirectories;
		sFeatures	+= ",height=" + nHeight;
		sFeatures	+= ",hotkeys=" + nHotkeys;
		sFeatures	+= ",left=" + nLeft;
		sFeatures	+= ",location=" + nLocation;
		sFeatures	+= ",menubar=" + nMenubar;
		sFeatures	+= ",resizable=" + nResizable;
		sFeatures	+= ",scrollbars=" + nScrollbars;
		sFeatures	+= ",status=" + nStatus;
		sFeatures	+= ",toolbar=" + nToolbar;
		sFeatures	+= ",top=" + nTop;
		sFeatures	+= ",width=" + nWidth;
		sFeatures	+= ",fullscreen=" + nFullscreen;
		oWindow	= window.open(sURL, sWindowName, sFeatures);
		if (nChromeLess == 1)
		{
			oWindow.blur();
			window.focus();
			oWindow.resizeTo(nWidth, nHeight);
			oWindow.moveTo(nLeft, nTop);
		}
		oTwWindows.push(oWindow);
		oTwWinNames.push(sWindowName);
		oTwWinURLs.push(sURL);
		if (is.DOM2)
			eval("try{ if (bPopUnder && oWindow.blur) oWindow.blur(); else if (!bPopUnder && oWindow.focus) oWindow.focus(); } catch(oError) {/*Do Nothing*/}");
		else
		{
			if (bPopUnder && oWindow.blur)
				oWindow.blur();
			else if (!bPopUnder && oWindow.focus)
				oWindow.focus();
		}
	}

	function openDialog()
	{
		if (!getArgument(arguments, 0, "boolean", false))
			return null;
		var sFeatures	= (bAutoCenter) ? "center:yes;" : "center:no;dialogLeft:" + nLeft + "px;dialogTop:" + nTop + "px;";
		sFeatures	+= "dialogHeight:" + nHeight + "px;";
		sFeatures	+= "dialogWidth:" + nWidth + "px;";
		sFeatures	+= "edge:sunken;";	// or "edge:raised;";
		sFeatures	+= "help:no;";
		sFeatures	+= "resizable:" + nResizable + ";";
		sFeatures	+= "scroll:" + nScrollbars + ";";
		sFeatures	+= "status:" + nStatus + ";";
		sFeatures	+= "unadorned:" + nChromeLess;
		if (bModeless)
		{
			oWindow	= window.showModelessDialog(sURL, dialogArguments, sFeatures);
			oTwWindows.push(oWindow);
			oTwWinNames.push(sWindowName);
			oTwWinURLs.push(sURL);
			return oWindow;
		}
		// Default Modal Dialog Windows - waits until window is closed - returns a value
		return window.showModalDialog(sURL, dialogArguments, sFeatures);
	}

//	Public Property Methods (They have public pointers)
	function setArguments(vValue)
	{
		if ("undefined"	== typeof(vValue))
			return;
		dialogArguments	= vValue;
	}

	function setAutoCenter(bValue)
	{
		if ("boolean" != typeof(bValue) || !is.DOM2 && !is.ns4)
			return;
		bAutoCenter	= bValue;
		if (!bAutoCenter)
			return;
		setLeft((screen.availWidth - nWidth) / 2);
		setTop((screen.availHeight - nHeight) / 2);
	}

	function setAutoClose(bValue)
	{
		if ("boolean" != typeof(bValue) || !is.DOM2 && !is.ns4)
			return;
		bAutoClose	= bValue;
		if (is.ns4)
		{
			if (bAutoClose)
				window.captureEvents(Event.FOCUS);
			else
				window.releaseEvents(Event.FOCUS);
		}
		window.onfocus	= (bAutoClose) ? TWindow_doCloseAll : null;
	}

	function setAutoSize(bValue)
	{
		if ("boolean" != typeof(bValue) || !is.DOM2 && !is.ns4)
			return;
		bAutoSize	= bValue;
		if (!bAutoSize)
			return;
		setLeft(screen.availWidth * .10);
		setTop(screen.availHeight * .05);
		setWidth(screen.availWidth - (nLeft * 2));
		setHeight(screen.availHeight - (nTop * 5));
	}

	function setFeatures(nValue)
	{
		if ("number" != typeof(nValue))
			return;
		nFeatures	= nValue;

		setAutoCenter((TwAutoCenter & nFeatures) > 0);
		setAutoClose((TwAutoClose & nFeatures) > 0);
		setAutoSize((TwAutoSize & nFeatures) > 0);
		bDialog			= ((TwDialog & nFeatures) > 0);
		nDirectories	= (TwDirectories & nFeatures) > 0 ?	1 : 0;
		nLocation		= (TwLocation & nFeatures) > 0 ?	1 : 0;
		nMenubar		= (TwMenubar & nFeatures) > 0 ?		1 : 0;
		bModeless		= ((TwModeless & nFeatures) > 0);
		bPopUnder		= ((TwPopUnder & nFeatures) > 0);
		nResizable		= (TwResizable & nFeatures) > 0 ?	1 : 0;
		nScrollbars		= (TwScrollbars & nFeatures) > 0 ?	1 : 0;
		nStatus			= (TwStatus & nFeatures) > 0 ?		1 : 0;
		nToolbar		= (TwToolbar & nFeatures) > 0 ?		1 : 0;
		nChromeLess		= (TwChromeLess & nFeatures) > 0 && is.ieDOM ?	1 : 0;
		if (nChromeLess == 1)
			nFullscreen	= 1;
		else
			nFullscreen	= (TwFullScreen & nFeatures) > 0 && is.ieDOM ?	1 : 0;
	}

	function setHeight(nValue)
	{
		nValue	= parseInt(nValue, 10);
		if ("number" == typeof(nValue) && nValue > 0)
			nHeight	= nValue;
	}

	function setLeft(nValue)
	{
		nValue	= parseInt(nValue, 10);
		if ("number" == typeof(nValue))
			nLeft	= nValue;
	}

	function setObjectName(sValue)
	{
		if ("string" != typeof(sValue) || sValue.trim() == "")
		{
			alert("TWindow Error: Create Object :: Missing or invalid sValue argument.");
			return;
		}
		sName	= sValue;
		setTimeout("TWindow_checkObjectName('" + sName + "')", 10);
	}

	function setTop(nValue)
	{
		nValue	= parseInt(nValue, 10);
		if ("number" == typeof(nValue))
			nTop	= nValue;
	}

	function setURL(sValue)
	{
		if ("string" == typeof(sValue))
			sURL	= sValue;
	}

	function setWidth(nValue)
	{
		nValue	= parseInt(nValue, 10);
		if ("number" == typeof(nValue) && nValue > 0)
			nWidth	= nValue;
	}

	function setWindowName(sValue)
	{
		if ("string" == typeof(sValue))
			sWindowName	= sValue;
	}
}

// TWindow global variables
oTwWindows	= new Array();
oTwWinNames	= new Array();
oTwWinURLs	= new Array();

// Standalone support functions - not a member of the TWindow object.
function TWindow_checkObjectName(sObjectName)
{
	eval("var sObjectNameType = typeof(" + sObjectName + ");");
	if (sObjectNameType != "object")
		alert("TWindow Error: Create Object :: The object '" + sObjectName + "' specified in sObjectName was not found.\n\nPlease set sObjectName to the name of the new TWindow object you are creating.");
	else
	{
		eval("var oObject = " + sObjectName + ";");
		if ("function" != typeof(oObject.setWindowName))
			alert("TWindow Error: Create Object :: The object '" + sObjectName + "' specified in sObjectName is not a TWindow object.\n\nPlease set sObjectName to the name of the new TWindow object you are creating.");
	}
}

function TWindow_doCloseAll()
{
	var oWindow, sWinName, sWinURL;
	for (; oTwWindows.length > 0;)
	{
		oWindow		= oTwWindows.pop();
		sWinName	= oTwWinNames.pop();
		sWinURL		= oTwWinURLs.pop();
		TWindow_closeWindow(oWindow, sWinName, sWinURL);
	}
}

function TWindow_closeWindow(oWindow, sWinName, sWinURL)
{
	if (oWindow && "object" == typeof(oWindow))
	{
// IE 5+ sets the 'closed' flag to true when it opens a PDF file. This work
// around sets the flag back to false by opening a blank page in the existing window.
		if (is.ie && !is.ie3 && !is.ie4 && sWinURL.toLowerCase().indexOf(".pdf") != -1 &&
		    "boolean" == typeof(oWindow.closed) && oWindow.closed &&
		    "object" == typeof(oWindow.location) && "string" == typeof(oWindow.location.href))
			oWindow	= window.open("", sWinName);
		if (is.DOM2)
			eval("try{ if (!oWindow.closed) oWindow.close();} catch(oError) {/*Do Nothing*/}");
		else
			eval("if (!oWindow.closed) oWindow.close();");
	}
}
// End of file
