var popupWindow;
var dialogReturnId;
var dialogReturnFunctionName;

function newWindow(page, target, winLoc, h, w, scroll, menu, tool, resize, address, status) 
{
	
	// Clicking on a link opens a new window with specified properties
	var dw = 50;
	var dh = 60;

	// Determine location of window
	if (winLoc == 'center') 
	{
		var posx = (screen.width - (w+dw))/2;
		var posy = (screen.height - (h+dh))/2;
	}
	else if (winLoc == 'lowright') 
	{
		var posx = (screen.width - (w+dw));
		var posy = (screen.height - (h+dh));
	}
	else if (winLoc == 'upright') 
	{
		var posx = (screen.width - (w+dw));
		var posy = 0;
	}
	else if (winLoc == 'lowleft') 
	{
		var posx = 0;
		var posy = (screen.height - (h+dh));
	}
	else if (winLoc == 'upleft') 
	{
		var posx = 0;
		var posy = 0;
	}

	// Set window properties
	windowprops = "height=" + h + ",width=" + w + ",location=no, left=" + posx + ",screenx=" + posx + ",top=" + posy 
		+ ",screeny=" + posy + ",scrollbars=" + scroll + ",menubar=" + menu + ",toolbar=" + tool + ",resizable=" 
		+ resize + ",location=" + address + ",status=" + status;

	// Open new window
	popupWindow = window.open('', target, windowprops);
	popupWindow.opener = window;
	popupWindow.focus();
	popupWindow.location.href = page;
	return false;
	
}

// Have not been tested with multiple dialog windows.
function OpenDialogWindow(page, target, winLoc, h, w, scroll, menu, tool, resize, address, status)
{
	newWindow(page, target, winLoc, h, w, scroll, menu, tool, resize, address, status);
	KeepPopupWindowFocused();
	return false;
}

// Have not been tested with multiple dialog windows.
function OpenDialogWindowWithReturnValues(page, returnId, functionName, target, winLoc, h, w, scroll, menu, tool, resize, address, status)
{
	dialogReturnId = returnId;
	dialogReturnFunctionName = functionName;
	newWindow(page, target, winLoc, h, w, scroll, menu, tool, resize, address, status);
	KeepPopupWindowFocused();
	return false;
}

function SetReturnValue(returnValue)
{
	getById(dialogReturnId).value = returnValue;
}

function CallReturnFunction()
{
	eval(dialogReturnFunctionName+'();');	
}

function KeepPopupWindowFocused()
{
	if ( typeof( window.addEventListener ) != "undefined" ) 
	{
		window.addEventListener("focus", FocusPopUpWindow, true);
		document.addEventListener("mouseover", FocusPopUpWindow, true);
	}
	else if ( typeof( window.attachEvent ) != "undefined" )
	{ 
		window.attachEvent("onfocus", FocusPopUpWindow);
		document.attachEvent("onmouseover", FocusPopUpWindow);
	}
	else
	{
		window.onfocus = FocusPopUpWindow;
		document.onmouseover = FocusPopUpWindow;
	}
}

function FocusPopUpWindow()
{
	if (popupWindow != null && !popupWindow.closed)
	{
		popupWindow.focus();
	}
}
