// JavaScript Document

/***************
 PopUp Code.
****************/

// initializing local-global variables.
var popUp_Window = null;
var defaultWinWidth = 200;
var defaultWinHeight = 200;
var popUpPageMargin = 25;	// pixels around the table in the popup window.
var parentWinWidth;
var parentWinHeight;


// ******* called from opener window *******
function popUpHTML(linkAddr, SizeW, SizeH)
{	
	// checking to see if default values need to set.
	var winWidth = (SizeW==null) ? defaultWinWidth : SizeW;
	var winHeight = (SizeH==null) ? defaultWinHeight : 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=no dependent=yes";
	popUp_Params = popUp_Params + ", height="+winHeight+", width="+winWidth+", left="+winLeft+", top="+winTop;
	
	popUp_Window = window.open(linkAddr, "popUp", popUp_Params);
	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);
	//
	var oldWidth = (BrowserIsIE) ? document.body.clientWidth : window.outerWidth;
	var oldHeight = (BrowserIsIE) ? document.body.clientHeight : window.outerHeight;
	var newHeight = oldHeight;
	var newWidth = oldWidth;
	//
	if (BrowserIsIE){
		var tableObj = getObjectRef("popUp_Table");
		newWidth = tableObj.clientWidth + (2 * popUpPageMargin);
		newHeight = tableObj.clientHeight + (2 * popUpPageMargin) + 8;
	} else {
		if (window.sizeToContent != undefined){
			window.sizeToContent();	// custom function avail to some Netscape browsers.
		}
		newWidth = window.outerWidth;
		newHeight = window.outerHeight;
	}
	//
	if (newWidth != oldWidth && newHeight != oldHeight) { 
		window.resizeTo(newWidth, newHeight);
	}
	//
	keepOnScreen(oldHeight, newHeight, oldWidth, newWidth);
}

// nudge window if any part of it has run off the screen.
// NOTE: don't try to simplify the verbose code -- IE Mac can't handle it.
function keepOnScreen(oldHeight, newHeight, oldWidth, newWidth){
	// clip new window height to height of opener.
	var myParentWinHeight = 0;
	var myParentWinWidth = 0;
	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;
	}
	newHeight = Math.min(newHeight, myParentWinHeight);
	newWidth = Math.min(newWidth, myParentWinWidth);
	var nudgeHoriz = 0;
	var nudgeVert = 0;
	nudgeHoriz = Math.floor((oldWidth-newWidth)/2);
	nudgeVert = Math.floor((oldHeight-newHeight)/2);
	window.moveBy(nudgeHoriz, nudgeVert);
	// bring into focus.
	// if (window.forward) window.forward();
	if (window.focus) window.focus();
}


if (window.opener!=undefined){
	// 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 -- existingOnload: " + existingOnload);
	window.onload=resizePopUp;
}