// Popup code
var gHomePath = document.getElementById("gHomePath").value;
var gImagePath = document.getElementById("gImagePath").value;
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gpopupTitle = null; 
var gReturnFunc;
var gPopupIsShown = false;
var gHideSelects = false; //IE has a problem with wanted select form tags to always be the topmost z-index or layer
var gTabIndexes = new Array();
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
var startingWidth;
var startingHeight;

function initPopUp() {
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	//gpopupTitle = document.getElementById("popupTitle");
	gHideSelects = !IE7Up();
}
addEvent(window, "load", initPopUp);

function showPopWin(url, width, height, returnFunc, TitlebarCaption) {
	gPopupIsShown = true;
	disableTabIndexes();

	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	startingWidth=width;
	startingHeight=height;
	centerPopWin(width, height);
	
	var titleBarHeight = 0;	//parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	gPopFrame.style.width = (width) + "px";	//parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame.style.height = (height) + "px";
	
	// set the url
	SetSrc(gPopFrame , url);
	gPopupMask.style.zIndex = 1001;
	gPopupContainer.style.zIndex = 1002;
	
	gReturnFunc = returnFunc;
	
	// for IE
	if (gHideSelects == true) {
		toggleObjects(false);
	}
	
	//setPopTitle(TitlebarCaption);
}

var gi = 0;
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		//When in strict compat mode, browser  updates properties in document.documentElement, otherwise document.body.
		var theBody = document.compatMode=='CSS1Compat' ? document.documentElement : document.body; 

		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		gPopupMask.style.height = fullHeight + "px";
		gPopupMask.style.width = fullWidth + "px";
		gPopupMask.style.top = scTop + "px";
		gPopupMask.style.left = scLeft + "px";
		window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
		
		var titleBarHeight = 0;	//parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}

addEvent(window, "resize", centerPopWin);
window.onscroll = centerPopWin;

function hidePopWin(callReturnFunc) {
	gPopupIsShown = false;
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(DanaGetFrames(window)["popupFrame"].returnVal);
	}
	SetSrc(gPopFrame, gHomePath+'blank.htm');
	// display all select boxes
	if (gHideSelects == true) {
		toggleObjects(true);
	}
}

function expandPopWin(imgObj) {
	var Height;
	var Width;
	
	if (imgObj.src.indexOf("maximize",0)>0) {
		Height = getViewportHeight()-80;
		Width = getViewportWidth()-40;
		if (Height<0 || Width<0) return;
		imgObj.src=gImagePath+"restore.gif";
		} 
	else {
		imgObj.src=gImagePath+"maximize.gif";
		Height=startingHeight;
		Width=startingWidth;
	}
	
	centerPopWin(Width, Height);
	var titleBarHeight = 0;	//parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	
	gPopupContainer.style.width = Width + "px";
	gPopupContainer.style.height = (Height+titleBarHeight) + "px";
	gPopFrame.style.width = (Width) + "px";	//parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame.style.height = (Height) + "px";
}

//function setPopTitle(TitleBarCaption) {
//	gpopupTitle.innerHTML = TitleBarCaption;
//}

function SetSrc(obj,url) {
if (IsIframe(obj)) obj.src=url;
}

function IsIframe(a)
{return (a!=null)&&(typeof(a.tagName)!="undefined")&&(a.tagName.toUpperCase()=="IFRAME");}

function toggleObjects(toOn) {// if toOn is true, make available, otherwise disable
	var visa = "visible"; //hidden default to on
	var disa = null;
	
	if (toOn==false){visa="hidden";disa=true;}
	var coll = document.all.tags("SELECT");
	if (coll!=null){
		for (i=0; i<coll.length; i++) 
		coll[i].style.visibility=visa;
	}
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}
