var d = document;

// Modal code
var gModalMask 		= null;
var gModalContainer	= null;
var gModalFrame 	= null;
var gReturnFunc;
var gModalIsShown 	= 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");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	d.onkeypress = ftnKeyDownHandler;
}

/**
 * Initializes Modal code on load.	
 */
function initModal() {
	gModalMask = d.getElementById("modalMask");
	gModalContainer = d.getElementById("modalContainer");
	gModalFrame = d.getElementById("modalFrame");	
	
	// 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;
	}
}
ftnAddEvent(window, "load", initModal);

 /**
	* @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 ftnShowModal(url, width, height, returnFunc) {
	gModalIsShown = true;
	ftnDisableTabIndexes();
	gModalMask.style.display = "block";
	gModalContainer.style.display = "block";
	// calculate where to place the window on screen
	ftnCenterModal(width, height);
	
	var titleBarHeight = parseInt(d.getElementById("modalTitleBar").offsetHeight, 10);
	
	gModalContainer.style.width = width + "px";
	gModalContainer.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
	gModalFrame.style.width = parseInt(d.getElementById("modalTitleBar").offsetWidth, 10) + "px";
	gModalFrame.style.height = (height) + "px";
	
	// set the url
	gModalFrame.src = url;
	
	gReturnFunc = returnFunc;
	// for IE
	if (gHideSelects == true) {
		ftnHideSelectBoxes();
	}
	
	window.setTimeout("ftnSetModalTitle();", 600);
}

//
var gi = 0;
function ftnCenterModal(width, height) {
	if (gModalIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gModalContainer.offsetWidth;
		}
		if (height == null) {
			height = gModalContainer.offsetHeight;
		}
		
		var fullHeight = ftnGetViewableHeight();
		var fullWidth = ftnGetViewableWidth();
		
		var theBody;
		if (d.documentElement && d.documentElement.scrollTop) { 
			theBody = d.documentElement;
		}
		else if (document.body) {
			theBody = document.body;
		}
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		gModalMask.style.height = fullHeight + "px";
		gModalMask.style.width = fullWidth + "px";
		gModalMask.style.top = scTop + "px";
		gModalMask.style.left = scLeft + "px";
		
		window.status = gModalMask.style.top + " " + gModalMask.style.left + " " + gi++;
		
		var titleBarHeight = parseInt(d.getElementById("modalTitleBar").offsetHeight, 10);
		
		gModalContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gModalContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}
ftnAddEvent(window, "resize", ftnCenterModal);
window.onscroll = ftnCenterModal;

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function ftnHideModal(callReturnFunc) {
	gModalIsShown = false;
	ftnRestoreTabIndexes();
	if (gModalMask == null) {
		return;
	}
	gModalMask.style.display = "none";
	gModalContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(window.frames["modalFrame"].returnVal);
	}
	gModalFrame.src = "/include/loading.htm";
	// display all select boxes
	if (gHideSelects == true) {
		ftnDisplaySelectBoxes();
	}
}

/**
 * Sets the Modal title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
function ftnSetModalTitle() {
	if (window.frames["modalFrame"].document.title == null) {
		window.setTimeout("ftnSetModalTitle();", 10);
	} else {
		d.getElementById("modalTitle").innerHTML = window.frames["modalFrame"].document.title;
	}
}

// Tab key trap. iff Modal is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function ftnKeyDownHandler(e) {
    if (gModalIsShown && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function ftnDisableTabIndexes() {
	if (d.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = d.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 ftnRestoreTabIndexes() {
	if (d.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = d.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
*/
function ftnHideSelectBoxes() {
	for(var i = 0; i < d.forms.length; i++) {
		for(var e = 0; e < d.forms[i].length; e++){
			if(d.forms[i].elements[e].tagName == "SELECT") {
				d.forms[i].elements[e].style.visibility="hidden";
			}
		}
	}
}

/**
* 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 ftnDisplaySelectBoxes() {
	for(var i = 0; i < d.forms.length; i++) {
		for(var e = 0; e < d.forms[i].length; e++){
			if(d.forms[i].elements[e].tagName == "SELECT") {
			d.forms[i].elements[e].style.visibility="visible";
			}
		}
	}
}


/**
 * X-browser event handler attachment and detachment
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function ftnAddEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function ftnRemoveEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

function ftnGetViewableHeight() {
	if (window.innerHeight!=window.undefined) return (window.innerHeight-1);
	if (document.compatMode=='CSS1Compat') return (document.documentElement.clientHeight-1);
	if (document.body) return (document.body.clientHeight-1); 
	return window.undefined; 
}
function ftnGetViewableWidth() {
	if (window.innerWidth!=window.undefined) return (window.innerWidth-1); 
	if (document.compatMode=='CSS1Compat') return (document.documentElement.clientWidth-1); 
	if (document.body) return (document.body.clientWidth-1); 
	return window.undefined; 
}