﻿// Popup code
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 __theoldDoPostBack; //Record __doPostBack function to be reinstated later.
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
var startingWidth;
var startingHeight;
var gDefaultURL;

function initPopUp() {
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	gpopupTitle = document.getElementById("popupTitle");
	gDefaultURL=GetSrc(gPopFrame);
	
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	gHideSelects = !IE7Up();
	//testing -- showPopWin("loading.htm",200,200,null,"Testing");
}
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 = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame.style.height = (height) + "px";
	
	// set the url
	SetSrc(gPopFrame , url);
	
	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 = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}

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 , gDefaultURL);
	// display all select boxes
	if (gHideSelects == true) {
		toggleObjects(true);
	}
}

function expandPopWin(imgObj) {
	var Height;
	var Width;
	
	var imgidx=imgObj.src.indexOf("checkout",0);
	if (imgidx>0) {
		Height = getViewportHeight()-80;
		Width = getViewportWidth()-40;
		if (Height<0 || Width<0) return;
		imgObj.src=imgObj.src.substr(0,imgidx)+"checkin.gif"; //"../images/checkin.gif";
		} 
	else {imgidx=imgObj.src.indexOf("checkin",0);
		imgObj.src=imgObj.src.substr(0,imgidx)+"checkout.gif"; //"../images/checkout.gif";
		Height=startingHeight;
		Width=startingWidth;
	}
	
	centerPopWin(Width, Height);
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	
	gPopupContainer.style.width = Width + "px";
	gPopupContainer.style.height = (Height+titleBarHeight) + "px";
	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopFrame.style.width = 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 GetSrc(obj) {
if (IsIframe(obj)) return obj.src; else return "../Forms_Kernel/Loading.htm";
}

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++;
			}
		}
	}
}
