var wndHandle_Arr = new Array();
var argsArr = null;

var featuresFooter = "left=135,top=75,screenX=135,screenY=75,width=450,height=500,toolbar=no,menubar=no,scrollbars=yes,resizable=no,status=no,directories=no,location=no";

// Initiates launch of the window.
// Any old window is closed before the new window is re-opened under the same name; this has been done for browser-compatibility purposes.
// Netscape runs this function once whereas IE runs this function twice. IE uses a two-step
// process because it needs to pause after closing the old window (before opening the new
// window), otherwise the new window may never open.
// Maybe this happens in IE because maybe the old window's thread is still closing while the
// JavaScript may be allowed to continue (unsure). What is known is the pause allows enough
// time that IE does not exhibit the weird behaviour after the pause is completed.
// Not all IEs suffer from this open/close window phenomena, but for brevity purposes I make
// all IEs go through the two-step process because all of them can without error.
// this function stors windowhandle in the global array of handles by window name

function LaunchPopup(wndName,wndTarget,wndFeatures,url,iePart2) 
{
	argsArr = arguments;
	ClosePopup(argsArr[0]);
    var hWndPopup = window.open(argsArr[3], argsArr[0], argsArr[2]);
	wndHandle_Arr[argsArr[0]] = hWndPopup;
	return false;
}


// Only closes the window if it appears to be opened.
function ClosePopup(wndName)
{
	// loooking foe window handle in the global array
	if (wndHandle_Arr[wndName] && (!wndHandle_Arr[wndName].closed))
	{
		wndHandle_Arr[wndName].close();
		wndHandle_Arr[wndName] = null;
		//removing window handle from the array
		delete wndHandle_Arr[wndName];
	}
	return false;
}


// function that close all open windows
function CloseAll()
{
	for (var i in wndHandle_Arr)
		ClosePopup(i);

	return false;
}

// When called, Iterate through the multi-action array and perform all actions.
function BeforePageUnload()
{
	for (var AllFunc=0;AllFunc < ArrayUnloadFunctions.length; AllFunc++)
		ArrayUnloadFunctions[AllFunc]();
}

// function
function AfterPageLoad()
{
	// Attach the CloseAll function to the beggining of array of unload function
	ArrayUnloadFunctions[ArrayUnloadFunctions.length] = CloseAll;
	
	// Attach previus window.onunload to the end of array ArrayUnloadFunctions
	// IMPORTANT: previus window.onunload event has to be the last in this array.
	if (window.onunload)
		ArrayUnloadFunctions[ArrayUnloadFunctions.length] = window.onunload;
	
	// Attach the BeforePageUnload function to the window.onunload event.
	window.onunload = BeforePageUnload;
}

// Define the multi-action array if it doesn't already exist.
if (!window.ArrayUnloadFunctions)
	window.ArrayUnloadFunctions = new Array();
	
	
function newWindow(contentsURL, winName, winProps, width, height) {
	if (!winName) var winName = "CALCULATOR";
	if (!winProps) var winProps = "menubar=0,toolbar=0,resizable=0,location=0,status=0,scrollbars=0";
	if (!width) var w = 620; 
	else w = width;
	if (!height) var h = 400;
	else h = height;
	var x = 100; y = 100;
	var args = "width=" + w + ",height=" + h + "," + winProps + ",screenx=" + x + ",screeny=" + y + ",left=" + x + ",top=" + y;
	window.open(contentsURL, winName, args);
}