/*
Instructions:
=============
This include file provides the function openDialog, which will open a second browser window with certain properties:
	1. No menu bar
	2. Centralized
	3. if modal the dialog must be closed before focus can go back to the parent window.
	4. If not independent, when the opening window is closed the dialog is closed as well.

NOTE that the onunload event for the opening window is captured - if this event is already being handled, take the window.onunload= line out of this file and include the call to closeDialogs in the onunload event handler.
*/


var dialogs = new Array();
var nDialogs = 0;


function openDialog(URL, name, width, height, modal, independent)
{
	// if this dialog already open then bring it to the front:
	if (dialogs[name] && dialogs[name].window && !dialogs[name].window.closed)
	{
		dialogs[name].window.focus();
		dialogs[name].window.location.href = URL;
	}
	else
	{
		// calculate the position of the dialog - we want it centralized:
		var left = (screen.availWidth - width)/2;
		var top = (screen.availHeight - height)/2;

		// open the dialog:
		// Note: in the features string:
			// for IE we set the "left" and "top" attributes
			// for Netscape we set the "screenX" and "screenY" attributes
		nDialogs++;
		// generate unique window name:
		windowName = 'window' + (new Date()).valueOf();
		dialogs[name] = new Object();
		dialogs[name].window = window.open(URL, windowName, "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top + ",scrollbars=yes,resizable=yes,location=no");

		// is it modal?
		if (typeof(modal) == 'undefined')
			modal = true;
		dialogs[name].modal = modal == modal;
		// is it dependent?
		if (typeof(independent) == 'undefined')
			independent = false;
		dialogs[name].independent = independent == true;
	}
	return dialogs[name];
}


function closeDialog(name)
{
	if (dialogs[name])
	{
		if (dialogs[name].window && !dialogs[name].window.closed)
			dialogs[name].window.close();
		dialogs[name] = null;
	}
}


function closeDependentDialogs()
{
	for (name in dialogs)
	{
		if (dialogs[name] && !dialogs[name].independent)
			closeDialog(name);
	}
}


function focusDialog(name)
{
	if (dialogs[name] && dialogs[name].window && !dialogs[name].window.closed)
		dialogs[name].window.focus();
}


function focusModalDialogs()
{
	for (name in dialogs)
	{
		if (dialogs[name] && dialogs[name].window && !dialogs[name].window.closed && dialogs[name].window.modal)
			focusDialog(name);
	}
}


function listDialogs()
{
	var str = "";
	for (name in dialogs)
		str += name + '\n';
	alert(str);
}


// if we close this window then we will close any dependent dialogs too:
window.onunload = closeDependentDialogs;

// if the main window gains focus, make sure any modal dialogs take focus:
window.onfocus = focusModalDialogs;

