// JavaScript Document

/***************
 PopUp Code.
****************/

// initializing local-global variables.
var popUp_Window = null;
var defaultWinWidth = 600;
var defaultWinHeight = 450;
var reqdWidth;
var reqHeight;	// if width and/or height were specified.
var popUpPageMargin = 25;	// pixels around the table in the popup window.
var popUpPageBottomPadding = 8;	// pixels beneath the table in the popup window.
var parentWinWidth;
var parentWinHeight;


// ******* includes function getAbsoluteURL() in dom to map relative link. *******
function navToPopUpHTML(linkAddr, SizeW, SizeH)
{	
	linkAddr = getAbsoluteURL(linkAddr);	// getAbsoluteURL() is in dom.
	popUpHTML(linkAddr, SizeW, SizeH);
}

// ******* called from opener window *******
function popUpHTML(linkAddr, SizeW, SizeH)
{	
	// checking to see if default values need to set.
	var winWidth = (SizeW==undefined) ? defaultWinWidth : SizeW;
	var winHeight = (SizeH==undefined) ? defaultWinHeight : SizeH;
	// if user passed in requested width or height, save them.
	if (SizeW!=undefined){ reqdWidth = SizeW; }
	if (SizeH!=undefined){ reqHeight = SizeH; }
	// get the parent window's body and height.
	parentWinWidth = (BrowserIsIE) ? document.body.clientWidth : window.innerWidth;
	parentWinHeight = (BrowserIsIE) ? document.body.clientHeight : window.innerHeight;
	// get the parent window's body and height.
	var parentWinLeft = (BrowserIsIE) ? window.screenLeft : window.screenX;
	var parentWinTop = (BrowserIsIE) ? window.screenTop : window.screenY;
	// set popup position.
	var winLeft = Math.floor(parentWinLeft + (parentWinWidth/2) - (winWidth/2));
	var winTop = Math.floor(parentWinTop + (parentWinHeight/2) - (winHeight/2));
	
	// specify popup window params.
	var popUp_Params = "resizable=yes, scrollbars=yes, dependent=yes";
	popUp_Params = popUp_Params + ", height="+winHeight+", width="+winWidth+", left="+winLeft+", top="+winTop;
	
	popUp_Window = window.open(linkAddr, "popUp", popUp_Params);
	if (popUp_Window.blur) popUp_Window.blur();
}


// ******* called from popup window *******
function resizePopUp(evt, dontDoExisting){
	// from here we pass true for dontDoExisting after any event object.
	if (existingOnload && !dontDoExisting) existingOnload(evt, true);
	// note if user had requested a specific width and/or height.
	var reqdWidth = (BrowserIsIE && ComputerIsAMac) ? window.opener.document.reqdWidth : window.opener.reqdWidth;
	var reqdHeight = (BrowserIsIE && ComputerIsAMac) ? window.opener.document.reqdHeight : window.opener.reqdHeight;
	//
	var oldWidth = (BrowserIsIE) ? document.body.clientWidth : window.outerWidth;
	var oldHeight = (BrowserIsIE) ? document.body.clientHeight : window.outerHeight;
	// allow reqdWidth and reqdHeight to override any sizing.
	oldWidth = (reqdWidth!=undefined) ? reqdWidth : oldWidth;
	oldHeight = (reqdHeight!=undefined) ? reqdHeight : oldHeight;
	var newHeight = oldHeight;
	var newWidth = oldWidth;
	//
	if (window.sizeToContent != undefined){
		window.sizeToContent();	// custom function avail to some Netscape browsers.
		window.resizeBy(popUpPageMargin/2, popUpPageMargin/2);	// add in 1/4 our usual padding.
		newWidth = (reqdWidth!=undefined) ? reqdWidth : window.outerWidth;
		newHeight = (reqdHeight!=undefined) ? reqdHeight : window.outerHeight;
		//
		var newLeft = window.screen.availWidth/2 - oldWidth/2;
		var newTop = window.screen.availHeight/2 - oldHeight/2;
		window.moveTo(newLeft, newTop);
		
	} else if (BrowserIsIE || BrowserIsNN6x){
		var tableObj = getObjectRef("popUp_Table");
		newWidth = (reqdWidth!=undefined) ? reqdWidth : tableObj.scrollWidth + (2 * popUpPageMargin);
		newHeight = (reqdHeight!=undefined) ? reqdHeight : tableObj.scrollHeight + (2 * popUpPageMargin) + popUpPageBottomPadding;
	} else {
		alert("Resizing is not currently supported for your browser.");
	}
	// clip new window height to height of opener.
	var myParentWinHeight;
	var myParentWinWidth;
	if (BrowserIsIE && ComputerIsAMac){
		// if (window.opener.document.parentWinTop){
			myParentWinHeight = window.opener.document.parentWinHeight;
			myParentWinWidth = window.opener.document.parentWinWidth;
		// }
	} else {
		myParentWinHeight = window.opener.parentWinHeight;
		myParentWinWidth = window.opener.parentWinWidth;
	}
	if (myParentWinHeight!=undefined) newHeight = Math.min(newHeight, myParentWinHeight);
	if (myParentWinWidth!=undefined) newWidth = Math.min(newWidth, myParentWinWidth);
	// make sure the window stays on screen.
	var nudgeHoriz = Math.floor((oldWidth-newWidth)/2);
	var nudgeVert = Math.floor((oldHeight-newHeight)/2);
	window.moveBy(nudgeHoriz, nudgeVert);
	//
	if (newWidth != oldWidth || newHeight != oldHeight) {
		window.resizeTo(newWidth, newHeight);
	}
	// bring into focus.
	if (window.focus) window.focus();
}

function centerOverParentWin(){
	var reqdWidth = (BrowserIsIE && ComputerIsAMac) ? window.opener.document.reqdWidth : window.opener.reqdWidth;
	var reqdHeight = (BrowserIsIE && ComputerIsAMac) ? window.opener.document.reqdHeight : window.opener.reqdHeight;
	// get the parent window's body and height.
	parentWinWidth = (BrowserIsIE) ? window.opener.document.body.clientWidth : window.opener.innerWidth;
	parentWinHeight = (BrowserIsIE) ? window.opener.document.body.clientHeight : window.opener.innerHeight;
	// get the parent window's body and height.
	var parentWinLeft = (BrowserIsIE) ? window.opener.screenLeft : window.opener.screenX;
	var parentWinTop = (BrowserIsIE) ? window.opener.screenTop : window.opener.screenY;
	// set popup position.
	var winLeft = Math.floor(parentWinLeft + (parentWinWidth/2) - (reqdWidth/2));
	var winTop = Math.floor(parentWinTop + (parentWinHeight/2) - (reqdHeight/2));
	//
	window.moveTo(winLeft, winTop);
	window.resizeTo(reqdWidth, reqdHeight);
}


if (window.opener!=undefined && window.opener!=null){
	if (window.opener.popUp_Window==window){
		// insert our functionality into onload and onresize events, while maintaining whatever may already be there.
		var existingOnload = function(){};
		if (window.onload!=null) { existingOnload = window.onload };
		//alert("in popup.js -- window.opener: " + window.opener);
		window.onload=resizePopUp;
	}
}