﻿
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gPopupIsShown = false;

// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	
var gTabIndexes = new Array();

var gHideSelects = false;

AttachEvent(window, "load", initPopUp, false);
AttachEvent(window, "resize", centerPopWin, false);
AttachEvent(window, "scroll", centerPopWin, false);
AttachEvent(document, "keypress", keyDownHandler, false);


// X-browser event handler attachment
function AttachEvent(obj, evt, fnc, useCapture)
{
	if (!useCapture)
		useCapture = false;
	if (obj.addEventListener)
	{
		obj.addEventListener(evt, fnc, useCapture);
		return true;
	}
	else
	if (obj.attachEvent)
		return obj.attachEvent("on" + evt, fnc);
	else
	{
		MyAttachEvent(obj, evt, fnc);
		obj["on" + evt] = function() { MyFireEvent(obj, evt) };
	}
} 

// The following are for browsers like NS4 or IE5Mac which don't support either
// attachEvent or addEventListener
function MyAttachEvent(obj, evt, fnc)
{
	if (!obj.myEvents)
		obj.myEvents = {};

	if (!obj.myEvents[evt])
		obj.myEvents[evt] = [];

	var evts = obj.myEvents[evt];
	evts[evts.length] = fnc;
}

function MyFireEvent(obj,evt)
{
	if (!obj || !obj.myEvents || !obj.myEvents[evt])
		return;

	var evts = obj.myEvents[evt];
	for (var i = 0, len = evts.length; i < len; i++)
		evts[i]();
}

// Tab key trap. if popup is shown and key was [TAB], suppress it.
function keyDownHandler(e)
{
	var ev = (document.all)? event:e;

	if (gPopupIsShown && ev.keyCode == 27)
		hidePopWin(false);

	if (gPopupIsShown)
		ev.preventDefault();
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes(disable)
{
	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++)
			{
				if (disable)
				{
					gTabIndexes[i++] = tagElements[k].tabIndex;
					tagElements[k].tabIndex = "-1";
				}
				else
				{
					tagElements[k].tabIndex = gTabIndexes[i++];
					tagElements[k].tabEnabled = true;
				}
			}
		}
	}
}

// Makes all drop down form select boxes on the screen hidden/visible so they 
// do not reappear after the dialog is closed. IE has a problem with wanted 
// select form tags to always be the topmost z-index or layer
function displaySelectBoxes(display)
{
	for (var i = 0; i < document.forms.length; i++)
		for (var e = 0; e < document.forms[i].length; e++)
			if (document.forms[i].elements[e].tagName == "SELECT")
				document.forms[i].elements[e].style.visibility = (display)? "visible":"hidden";
}

// Gets the full height because it's different for most browsers.
function getViewportHeight()
{
	if (window.innerHeight != window.undefined)
		return window.innerHeight;

	if (document.compatMode == 'CSS1Compat')
		return document.documentElement.clientHeight;

	if (document.body)
		return document.body.clientHeight; 

	return window.undefined; 
}

// Gets the full width because it's different for most browsers.
function getViewportWidth()
{
	if (window.innerWidth != window.undefined)
		return window.innerWidth; 

	if (document.compatMode == 'CSS1Compat')
		return document.documentElement.clientWidth; 

	if (document.body)
		return document.body.clientWidth; 

	return window.undefined; 
}

// Calculate where to place the window on screen
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();

		var theBody = (document.documentElement.scrollTop)? 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";
		
		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";
	}
}

// Sets the popup title based on the title of the html document it contains.
// Uses a timeout to keep checking until the title is valid.
function setPopTitle()
{
	if (window.frames["popupFrame"].document.title == "")
		window.setTimeout("setPopTitle();", 10);
	else
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
}

// Initializes popup code on load.	
function initPopUp()
{
	// Add the HTML to the body
	body = document.getElementsByTagName('body')[0];

	popmask = document.createElement('div');
	popmask.id = 'popupMask';

	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML =	'<div id="popupInner">' +
							'<div id="popupTitleBar">' +
								'<div id="popupTitle"></div>' +
								'<div id="popupControls">' +
									'<a href="JavaScript:hidePopWin(false);"><img src="styles/close.gif" alt="Close"/></a>' +
								'</div>' +
							'</div>' +
							'<iframe src="styles/loading.html" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
						'</div>';

	body.appendChild(popmask);
	body.appendChild(popcont);

	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	

	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1)
		gHideSelects = true;

/*	
	// Add onclick handlers to 'a' elements of class submodal or submodal-width-height
	var elms = document.getElementsByTagName('a');
	for (i = 0; i < elms.length; i++) {
		if (elms[i].className.indexOf("submodal") == 0) { 
			// var onclick = 'function (){showPopWin(\''+elms[i].href+'\','+width+', '+height+', null);return false;};';
			// elms[i].onclick = eval(onclick);
			elms[i].onclick = function(){
				// default width and height
				var width = 400;
				var height = 200;
				// Parse out optional width and height from className
				params = this.className.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWin(this.href,width,height,null); return false;
			}
		}
	}
*/
}

// Show popup window
function showPopWin(url, width, height, returnFunc)
{
	gPopupIsShown = true;
	disableTabIndexes(true);

	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";

	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
	gPopFrame.src = url;
	
	gReturnFunc = returnFunc;

	// for IE
	if (gHideSelects == true)
		displaySelectBoxes(false);

	document.getElementById("popupTitle").innerHTML = "";
	window.setTimeout("setPopTitle();", 600);
}

// Hide popup window
function hidePopWin(callReturnFunc)
{
	gPopupIsShown = false;
	disableTabIndexes(false);

	if (gPopupMask == null)
		return;

	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";

	if (callReturnFunc == true && gReturnFunc != null)
		gReturnFunc(window.frames["popupFrame"].returnVal);

	gPopFrame.src = 'styles/loading.html';

	// display all select boxes
	if (gHideSelects == true)
		displaySelectBoxes(true);
}

