/**
 * POPUP WINDOW CODE
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Contributions by Eric Angel (tab index code) and Scott (hiding/showing selects for IE users)
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/subModal
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

function showPopWinAsync(href, postdata)		
{
	var ajaxRequest = new CSAjax(href, postdata, null);
	getRef('popupFrame').ajaxRequest = ajaxRequest;
	ajaxRequest.setCallback(showPopWinAsyncRequestFinised);
	ajaxRequest.makeRequest();
}

function showPopWinAsyncRequestFinised(result)
{
	replaceTag('popupFrame', result);
	centerPopWin();
}

// Popup code
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gPopIFrame = null;
var gPopupTitleBar = null;
var gReturnFunc;
var gPopupIsShown = false;

var gHideSelects = false;


var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

/**
 * Initializes popup code on load.	
 */
function initPopUp() {
	if (gPopupContainer != null)
	{
		return;
	}

	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	gPopIFrame = document.getElementById("popupIFrame");	
	gPopupTitleBar = getRef('popupTitleBar');
	
	// 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;
	}
	try {
		if (ie7up)
			gHideSelects = false;
	}
	catch (e) {
	}
}
addEvent(window, "load", initPopUp);

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument url - url to display
	* @argument returnFunc - function to call when returning true from the window.
	*/

function showPopWin(url, width, height, returnFunc, useIFrame, formdata) {
	gPopupIsShown = true;
	disableTabIndexes();
	
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	
	//Replace with our XHTML injection code
	if ( useIFrame )
	{
		var titleBarHeight = 0;
		if (document.getElementById("popupTitleBar") != null )
		{
			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";
	
		gPopIFrame.src = url;

		gPopupMask.style.display = "block";
		gPopupContainer.style.display = "block";
	}
	else
	{
		if (formdata == undefined)
			formdata = null;

		gPopIFrame.style.display = "none";
		
		getRef('popupFrame').innerHTML = '';
		
		var response = retrieveXMLDoc(url, formdata, (formdata ? true : false));

		gPopupMask.style.display = "block";
		gPopupContainer.style.display = "block";

		var status = _loadXHTMLFragment(response, "popupFrame");
		
		if ((status != 200) && (status != 0))
		{
			setPopTitleTo("Error " + status);
			getRef("popupFrame").innerHTML = "<div><p style='margin:8px'>The server returned a status code of " + status + "</p></div><div id='popupButtons'><input type='button' class='stdButton' onclick='hidePopWin()' value='Close'/></div>";
		}
	}
		
	gReturnFunc = returnFunc;
	centerPopWin();
}

//
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();
		
		var scTop = window.pageYOffset != null ? window.pageYOffset : document.body.scrollTop;
		var scLeft = window.pageYOffset != null ? window.pageXOffset : document.body.scrollLeft;

		gPopupMask.style.height = (fullHeight - 20) + "px";
		gPopupMask.style.width = (fullWidth - 20) + "px";
		gPopupMask.style.top = scTop + "px";
		gPopupMask.style.left = scLeft + "px";

		var titleBarHeight = 0;
		if (document.getElementById("popupTitleBar") != null )
		{
			titleBarHeight = 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);
//addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc) {
	try {
	HideCalendar();
	} catch (e) {}

	gPopupIsShown = false;
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(window.frames["popupFrame"].returnVal);
	}
	gPopFrame.src = 'loading.html';
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}

/**
 * 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 == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
		//gPopupTitleBar.style.width = elementWidth(gPopFrame) + "px";
	}
}

function setPopTitleTo(title) {
	getRef("popupTitle").innerHTML = title  + "&nbsp;&nbsp;&nbsp;";
}

function hidePopTitleBar()
{
	if( document.getElementById("popupTitleBar") != null )
	{
		getRef('popupTitleBar').id = 'xpopupTitleBar';
	}
}

function showPopTitleBar()
{
	if( document.getElementById("xpopupTitleBar") != null )
	{
		getRef('xpopupTitleBar').id = 'popupTitleBar';
	}
	
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	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";
			tagElements[k].tabEnabled=false;
			i++;
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
	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++;
		}
	}
}


/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
	if (ie55up)
	{
		if (!ie7up)
		{
			for(var i = 0; i < document.forms.length; i++) {
				if (document.forms[i].id != "modalDialog") {
					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="hidden";
						}
					}
				}
			}
		}
		
		$$('div.clickHereToEdit').each(function(btn) {
			btn.style.display = 'none';
		});
	}
}

/**
* Makes all drop down form select boxes on the screen 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() {
	if (ie55up)
	{
		if (!ie7up)
		{
			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="visible";
					}
				}
			}
		}

		$$('div.clickHereToEdit').each(function(btn) {
			btn.style.display = '';
		});
	}
}
