
/**
 * This derivative version of subModal can be downloaded from http://gabrito.com/files/subModal/
 * Original By Seth Banks (webmaster at subimage dot com)  http://www.subimage.com/
 * Contributions by Eric Angel (tab index code), Scott (hiding/showing selects for IE users), Todd Huss (submodal class on hrefs, moving div containers into javascript, phark method for putting close.gif into CSS), Thomas Risberg (safari fixes for scroll amount), Dave Campbell (improved parsing of submodal-width-height class)
 */

// Popup code
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gReturnFalseFunc;
var gPopupIsShown = false;
var gHideSelects = false;
var gLoading = "loading.html";
var gDefaultURL = "/TracWeb40/js/popwin.4sp"	// What to use if none provided

var gStyleCode = 1;
var gCenter = true;
var allowDrag = false;

var colorCode;
var helpCode;
var closeCode;
var theHeight;
var theWidth;

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) {
	document.onkeypress = keyDownHandler;
}

function is_iPhone() {
	var agent=navigator.userAgent.toLowerCase();
	return (agent.indexOf('iphone')!=-1);
}

function is_iPad() {
	var agent=navigator.userAgent.toLowerCase();
	return (agent.indexOf('ipad')!=-1);
}

/* functions to aid in moving the window around */
var DragHandler = {
 
 
	// private property.
	_oElem : null,
 
 
	// public method. Attach drag handler to an element.
	attach : function(oElem) {
		oElem.onmousedown = DragHandler._dragBegin;
 
		// callbacks
		oElem.dragBegin = new Function();
		oElem.drag = new Function();
		oElem.dragEnd = new Function();
 
		return oElem;
	},
 
 
	// private method. Begin drag process.
	_dragBegin : function(e) {
		//alert(e.currentTarget.id);
		if (allowDrag) {
			var oElem = DragHandler._oElem = this;
	 		if (oElem != null) {
				if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
				if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }
		 
				var x = parseInt(oElem.style.left);
				var y = parseInt(oElem.style.top);
		 
				e = e ? e : window.event;
				oElem.mouseX = e.clientX;
				oElem.mouseY = e.clientY;
		 
				oElem.dragBegin(oElem, x, y);
		 
				document.onmousemove = DragHandler._drag;
				document.onmouseup = DragHandler._dragEnd;
			}
		}
		return false;
	},
 
 
	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;
		if (oElem != null) {
			var x = parseInt(oElem.style.left);
			var y = parseInt(oElem.style.top);
	 
			e = e ? e : window.event;
			oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
			oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';
	 
			oElem.mouseX = e.clientX;
			oElem.mouseY = e.clientY;
	 
			oElem.drag(oElem, x, y);
 		}
		return false;
	},
 
 
	// private method. Stop drag process.
	_dragEnd : function() {
		var oElem = DragHandler._oElem;
 
 		if (oElem != null) {
			var x = parseInt(oElem.style.left);
			var y = parseInt(oElem.style.top);
 
			oElem.dragEnd(oElem, x, y);
 
			document.onmousemove = null;
			document.onmouseup = null;
			DragHandler._oElem = null;
		}
	}
 
}
/**/


/**
 * Override the loading page from loading.html to something else
 */
function setPopUpLoadingPage(loading) {
	gLoading = loading;
}

function initPopData() {
	popData = [["Are you sure?","Confirmation","alert2.jpg"], ["OK",'ok',true],["Cancel",'cancel',false]];
	returnVal = "";
}

var returnVal = "";
var popdata = null;
initPopData();

function getPopData() {
	return(popData);
}

// this is called by the confirmation dialog popwin.4sp
function doConfirmAction(action,retBool) {
	// reset the values of the popData for next time
	initPopData();
	returnVal = action;
	hidePopWin(retBool); //window.parent. or window.top (dnw)
}


function popStyle1() {
	return '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar" class="popupTitleBar" >' +
				'<div id="popupTitle" style="position: relative; float:left;" onmousedown="javascript:allowDrag=true;" ></div>' +
				'<div id="popupControls">' +
					'<a onclick="hidePopWin(false);"><span>Close</span></a>' +
				'</div>' +
			'</div>' +
			'<iframe src="'+gLoading+'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
		'</div>';
}

function popStyle2(colorCode, baseURL, theID) {
	//what is theID?
	//textCol = "";
	var shadowCol = "";
	
	if (colorCode != null) {
		var tCols = colorCode.split("-");
		if (tCols.length == 0) {
			colorCode = "b";
			textCol = "";
		} else if (tCols.length>1) {
			colorCode = tCols[0];
			textCol = tCols[1];
		} else {
			colorCode = tCols[0];
			textCol = "";
		}
		if (tCols.length == 3) {
			shadowCol = tCols[2];
		}
		if (tCols.length == 4) {
			wshadowCol = tCols[3];
		}
	}
	if ((colorCode==null) || (colorCode=="")) {
		colorCode='b';
	} else {
		if(colorCode=="") colorCode='b';
	}
	
	if (theID==null) theID = 'pop';
	if (baseURL==null) baseURL = '';
	
	closeCode = 'javascript:hidePopWin(false);';
	
	textStyleCode="";
	if (textCol != null) {
		if (textCol!="") textStyleCode = "style='position: relative; color:"+textCol+";"
		if (shadowCol != "") textStyleCode += "text-outline: 1px 1px "+shadowCol+";"+"text-shadow: "+shadowCol+" 0px 0px 2px;";
		if (textStyleCode != "") textStyleCode += "'";
	} else {
		//no color
		//textStyleCode = 
		textCol = "style='position: relative;'";
	}
	
	return "" +
		"<table class='dlog' width='100%' height='100%' border=0 cellspacing=0 cellpadding=0>" +
			"<tr height='27'><td width=8 class='topleft "+colorCode+"-topleft' style='padding-top: 0pt; padding-left: 0pt; padding-right: 0pt;' >" +
				"<div id='helpArea' style='position: relative; top: 10pt; left: 10pt;'>"+
						iftext((helpCode!='')&(helpCode != null),"<a id='"+theID+"help' href='"+helpCode+"'><img src='"+baseURL+"images/question.gif'></a>","&nbsp;&nbsp;&nbsp;")+
				"</div></td>"+
				"<td class='dtop "+colorCode+"-top'><table width='100%'><tr><td width=40 class='innertop col"+colorCode+"'>"+
				"</td>"+
				"<td NOWRAP align=center height='26' class='innertop col"+colorCode+"'><div id='popupTitleBar'><div style='position: relative;' onmousedown='javascript:allowDrag=true;'  id='popupDrag"+theID+"' ><div id='popupTitle'"+
				textStyleCode+
				">&nbsp;</div></div></div></td>"+
				"<td align=right width=40></td></tr></table></td>"+
				"<td align=left class='dtopright "+colorCode+"-topright' style='padding-top:0; padding-bottom:0;'>"+
				"<div style='position: relative; top: 10pt; left: -16pt;'>"+
					iftext((closeCode!='')&(closeCode != null),"<a id='"+theID+"close' href='"+closeCode+"'><img src='"+baseURL+"images/closebox.jpg'></a>","&nbsp;&nbsp;&nbsp;")+
				"</div>"+
				"</td></tr><tr>"+
				"<td class='dleft' width=8></td><td class='dbody'"+
 				//iftext(theHeight!=""," height="+theHeight)+
				//iftext(theWidth!=""," width="+theWidth)+
				">" + //content goes here
					"<iframe src='"+gLoading+"' style='width:100%;height:100%;background-color:transparent;' scrolling='auto' frameborder='0' allowtransparency='true' id='popupFrame' name='popupFrame' width='100%' height='100%'></iframe>" +
				"</td><td class='dright'></td></tr><tr><td class='dbottomleft'></td>"+
				"<td class='dbottom'>&nbsp;</td><td class='dbottomright'></td>"+
			"</tr>"+
		"</table>";
}


function popStyle3(colorCode, baseURL, theID) {
	//what is theID?
	//textCol = "";
	var shadowCol = "";
	var wshadowCol = "#ccc";
	
	if (colorCode != null) {
		var tCols = colorCode.split("-");
		if (tCols.length == 0) {
			colorCode = "b";
			textCol = "";
		} else if (tCols.length>1) {
			colorCode = tCols[0];
			textCol = tCols[1];
		} else {
			colorCode = tCols[0];
			textCol = "";
		}
		if (tCols.length == 3) {
			shadowCol = tCols[2];
		}
		if (tCols.length == 4) {
			wshadowCol = tCols[3];
		}
		
	}
	if ((colorCode==null) || (colorCode=="")) {
		colorCode='b';
	} else {
		if(colorCode=="") colorCode='b';
	}
	
	if (theID==null) theID = 'pop';
	if (baseURL==null) baseURL = '';
	
	closeCode = 'javascript:hidePopWin(false);';
	
	textStyleCode="";
	if (textCol != null) {
		if (textCol!="") textStyleCode = "style='position: relative; color:"+textCol+";"
		if (shadowCol != "") textStyleCode += "text-outline: 1px 1px "+shadowCol+";"+"text-shadow: "+shadowCol+" 0px 0px 2px;";
		if (textStyleCode != "") textStyleCode += "'";
	} else {
		//no color
		//textStyleCode = 
		textCol = "style='position: relative;'";
	}
	
	theTitle="&#160;"
		//iftext(theHeight!=""," height="+theHeight)+
		//iftext(theWidth!=""," width="+theWidth)+
	
	return "<table class='' style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' >"+
				"<tr>"+
					"<td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;'>"+
						"<div class='dlogframe' >"+
							"<table border='0' cellspacing='0' cellpadding='0' class='dlogtable' style='border-collapse: separate;' >"+
								"<tr height='26'>"+
									"<td width=8 class='dlogtopleft' style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' >"+
										"<div style='position: relative; top: 2pt; left: 5pt;'>"+
											iftext (((helpCode!='')&(helpCode != null)),"<a id='"+theID+"help' href='"+helpCode+"'><img src='"+baseURL+"images/question.gif'></a>","&nbsp;")+
										"&#160;</div>"+
									"</td>"+
									"<td class='dlogtop'>"+
										"<table width='100%'>"+
											"<tr>"+
												"<td width=40 class='innertop'>"+"</td>"+
												"<td NOWRAP align=center height='26' class='innertop'>"+
													"<div id='popupTitleBar'><div style='position: relative;' onmousedown='javascript:allowDrag=true;'  id='popupDrag"+theID+"' ><div id='popupTitle'"+
													textStyleCode+
													">&nbsp;</div></div></div>"+
												"</td>"+
												"<td align=right width=40></td>"+
											"</tr>"+
										"</table>"+
									"</td>"+
									"<td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' align=left class='dlogtopright' >"+
										"<div style='position: relative; top: 2pt; left: -3pt;'>"+
											iftext((closeCode!='')&(closeCode != null),"<a id='"+theID+"close' href='"+closeCode+"'><img src='"+baseURL+"images/closebox.jpg'></a>")+
										"</div>"+
									"</td>"+
								"</tr>"+
								"<tr>"+
									"<td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' class='dlogleft' width=8></td>"+
									"<td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' class='dlogbody' >"+
										"<iframe src='"+gLoading+"' style='width:100%;height:100%;background-color:transparent;' scrolling='auto' frameborder='0' allowtransparency='true' id='popupFrame' name='popupFrame' width='100%' height='100%'></iframe>"+

									"</td>"+
									"<td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' class='dlogright'></td></tr><tr><td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' class='dlogbottomleft'></td>"+
									"<td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' class='dlogbottom' >&nbsp;</td><td style='padding-top:0; padding-bottom:0; padding-left: 0; padding-right: 0;' class='dlogbottomright'></td>"+
								"</tr>"+
							"</table>"+
						"</div>"+
					"</td>"+
				"</tr>"+
			"</table>";

}

/*
	assuming we have initialized the popup already, adjust it's look
	to match the style code we pass in
*/
function setPopStyle(styleCode, colorCode, baseURL, theID) {
	var body = document.getElementsByTagName('body')[0];
	if (gPopupContainer==null) {
		initPopUp();
	}
	popcont = gPopupContainer;
	if ((styleCode =="")||(styleCode=='1')) {
		popcont.innerHTML = popStyle1();
	} else {
		if (styleCode=='3') {
			popcont.innerHTML = popStyle3(colorCode, baseURL, theID);
			//gPopupContainer = document.getElementById("popupContainer");
			//gPopupContainer.className = "dlogframe";
		} else {
			popcont.innerHTML = popStyle2(colorCode, baseURL, theID);
		}
	}
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");

	popDrag = DragHandler.attach(gPopupContainer);
	popDrag.dragBegin = pop_dragBegin;
	popDrag.dragEnd = pop_dragEnd;

	gStyleCode = styleCode;
	gCenter = true;
}

/**
 * Initializes popup code on load.	
 */
 
var popDrag = null;
function pop_drag(element, x, y) {
	;
}

function myAddClassName(elem,theStyle) {
	var s = "";
	if (elem != null) {
		s = elem.className;
		if ((s == null) || (s == "")) {
			s = theStyle;
		} else {
			s = s + theStyle;
		}
		elem.className = s;
	}
	return s
}

function myRemClassName(elem,theStyle) {
	var s = "";
	var re = new RegExp(' '+theStyle,'g');
	var re2 = new RegExp(theStyle,'g');
	if (elem != null) {
		s = elem.className;
		s = s.replace(re, "");
		s = s.replace(re2, "");
		elem.className = s;
	}
	return s
}


function addchildstyle(element,theStyle) {
	//element.addClassName(theStyle);
	myAddClassName(element,theStyle)
}
function remchildstyle(element,theStyle) {
	//element.removeClassName(theStyle);
	myRemClassName(element,theStyle)
}

function pop_dragBegin(element, x, y) {
	addchildstyle(element,'transp');
}

function pop_dragEnd(element, x, y) {
	//element.className = element.storeClass;
	allowdrag = false;
	remchildstyle(element,'transp');
}

function initPopUp( styleCode ) {
	// Add the HTML to the body
	var body = document.getElementsByTagName('body')[0];

	var popmask = document.createElement('div');
	popmask.id = 'popupMask';

	var popcont = document.createElement('div');
	popcont.id = 'popupContainer';

	popcont.innerHTML = popStyle1();
	
	/*
	'' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar">' +
				'<div id="popupTitle"></div>' +
				'<div id="popupControls">' +
					'<a onclick="hidePopWin(false);"><span>Close</span></a>' +
				'</div>' +
			'</div>' +
			'<iframe src="'+gLoading+'" 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");	
	
	popDrag = DragHandler.attach(gPopupContainer);
	popDrag.dragBegin = pop_dragBegin;
	//popDrag.drag = pop_drag;
	popDrag.dragEnd = pop_dragEnd;
	
	// 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) { 
			elms[i].onclick = function(){
				// default width and height
				var width = 400;
				var height = 200;
				// Parse out optional width and height from className
				var startIndex = this.className.indexOf("submodal");
				var endIndex = this.className.indexOf(" ", startIndex);
				if (endIndex < 0) {
					endIndex = this.className.length;
				}
				var clazz = this.className.substring(startIndex, endIndex);
				params = clazz.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWin(this.href,width,height,null); return false;
			}
		}
	}
}
addEvent(window, "load", initPopUp);
//addEvent(window, "mouseup", DragHandler._drag);

/**
	Custom confirmation with up to 3 options
	smText	Title of Window
	bigText	Description of Question
	btn1	array of Title of Button1, value to pass
	btn2	Title of Button2 (or null)
	btn3	Title of Button3 (or null)
	okFunc	Call this when button 1 and possibly 2 is clicked
	noFunc	Call this when button 3 and possibly 2 is clicked
	g		name of graphic icon or null for alert2.jpg
	w		width of window or null for 400
	h		height of window or null for 150
	
	Title of button clicked is passed to the function.
*/
function doConfirm(smText, bigText, btn1, btn2, btn3, okFunc, noFunc, g, w, h) {
		if (w == null) w = 400;
		if (h == null) h = 150;
		if (g == null) g = "alert2.jpg";
		var dlgHeight = 150;
		popData[0][0] = bigText;
		popData[0][1] = smText;
		popData[0][2] = g;
		if (btn2 == null) {
			if (btn1[1]==null) {btn1val = btn1[0];} else {btn1val = btn1[1];}
			popData[1] = [btn1[0],btn1val,true];
		} else if (btn3 == null) {
			// only 2 choices
			if (btn1[1]==null) {btn1val = btn1[0];} else {btn1val = btn1[1];}
			if (btn2[1]==null) {btn2val = btn2[0];} else {btn2val = btn2[1];}
			popData[1] = [btn1[0],btn1val,true];
			popData[2] = [btn2[0],btn2val,false];
		} else {
			// only 1 choice
			if (btn1[1]==null) {btn1val = btn1[0];} else {btn1val = btn1[1];}
			if (btn2[1]==null) {btn2val = btn2[0];} else {btn2val = btn2[1];}
			if (btn3[1]==null) {btn3val = btn3[0];} else {btn3val = btn3[1];}
			popData[1] = [btn1[0],btn1val,true];
			popData[2] = [btn2[0],btn2val,true];
			popData[3] = [btn3[0],btn3val,false];
		}
		showPopWin("" , w, h, okFunc, noFunc,"", '#fff');
}



 /**
	* @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.
	*/
var options = "";

function showPopWin(url, width, height, returnFunc, returnFalseFunc, optionalText, maskColor) {
	if (optionalText != null) options = optionalText;
	gPopupIsShown = true;
	disableTabIndexes();
	if (maskColor != null) {
		gPopupMask.style.backgroundColor = maskColor;
	} else {
		gPopupMask.style.backgroundColor = '#000'
	}
	gPopupMask.style.display = "block";

	gPopupContainer.style.display = "block";
	if (!is_iPhone() && !is_iPad()) {
		centerPopWin(width, height);
	} else {
		topleftPopWin(width, height);
	}
	
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	gPopupContainer.style.width = parseInt(width) + "px";
	gPopupContainer.style.height = (parseInt(height)+titleBarHeight) + "px";
	if (gStyleCode == 1) {
		gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	} else {
		gPopFrame.style.width = parseInt(width) +"px"; //parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	}
	gPopFrame.style.height = parseInt(height) + "px";
	if (url == "") url = gDefaultURL;

	gPopFrame.src = url;
	gReturnFunc = returnFunc;
	gReturnFalseFunc = returnFalseFunc;
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	window.setTimeout("setPopTitleAndRewriteTargets();", 100);
}

//
var gi = 0;
function centerPopWin(width, height) {
	if ((gPopupIsShown == true) & (gCenter)) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		// scLeft and scTop changes by Thomas Risberg
		var scLeft,scTop;
		if (self.pageYOffset) {
			scLeft = self.pageXOffset;
			scTop = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			scLeft = document.documentElement.scrollLeft;
			scTop = document.documentElement.scrollTop;
		} else if (document.body) {
			scLeft = document.body.scrollLeft;
			scTop = document.body.scrollTop;
		} 
		gPopupMask.style.height = parseInt(fullHeight) + "px";
		gPopupMask.style.width = parseInt(fullWidth) + "px";
		gPopupMask.style.top = parseInt(scTop) + "px";
		gPopupMask.style.left = parseInt(scLeft) + "px";
		
		window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		var topMargin = scTop + ((fullHeight - (height+titleBarHeight)) / 2);
		if (topMargin < 0) { topMargin = 0; }
		if (isNaN(topMargin)) { topMargin = 0; }
		gPopupContainer.style.top = topMargin + "px";
		gPopupContainer.style.left =  (scLeft + ((parseInt(fullWidth) - parseInt(width)) / 2)) + "px";
	}
}

function topleftPopWin(width, height) {
	if ((gPopupIsShown == true) & (gCenter)) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		// scLeft and scTop changes by Thomas Risberg
		var scLeft,scTop;
		if (self.pageYOffset) {
			scLeft = self.pageXOffset;
			scTop = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			scLeft = document.documentElement.scrollLeft;
			scTop = document.documentElement.scrollTop;
		} else if (document.body) {
			scLeft = document.body.scrollLeft;
			scTop = document.body.scrollTop;
		} 
		gPopupMask.style.height = parseInt(fullHeight) + "px";
		gPopupMask.style.width = parseInt(fullWidth) + "px";
		gPopupMask.style.top = "0px"; //parseInt(scTop) + "px";
		gPopupMask.style.left = "0px"; //parseInt(scLeft) + "px";
		
		window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		//var topMargin = scTop + ((fullHeight - (height+titleBarHeight)) / 2);
		var topMargin = 0 + ((fullHeight - (height+titleBarHeight)) / 2);

		if (topMargin < 0) { topMargin = 0; }
		if (isNaN(topMargin)) { topMargin = 0; }
		gPopupContainer.style.top = "0px"; //topMargin + "px";
		gPopupContainer.style.left = "0px";// (scLeft + ((parseInt(fullWidth) - parseInt(width)) / 2)) + "px";
	}
}
if (!is_iPhone() && !is_iPad()) {
	addEvent(window, "resize", centerPopWin);
	//addEvent(window, "scroll", centerPopWin);

	//don't do this if not a iphone
	window.onscroll = centerPopWin;
} else {
	//
}


/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc) {
	gPopupIsShown = false;
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(returnVal);
		//gReturnFunc(window.frames["popupFrame"].returnVal);
	}
	if (callReturnFunc != true && gReturnFalseFunc != null) {
		gReturnFalseFunc(returnVal);
		//gReturnFalseFunc(window.frames["popupFrame"].returnVal);
	}

	gPopFrame.src = gLoading;
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}



/**
 * Sets the popup title based on the title of the html document it contains.
 * Also adds a base attribute so links and forms will jump out out of the iframe
 * unless a base or target is already explicitly set.
 * Uses a timeout to keep checking until the title is valid.
 */
/* MODIFIED 11/3/08, fixed a bug that caused the title to not get loaded
*/

function setPopTitleAndRewriteTargets() {

	var theDoc = window.frames["popupFrame"].document;

	if (theDoc==null) {
		//window.setTimeout("setPopTitleAndRewriteTargets();", 10);
	} else if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitleAndRewriteTargets();", 10);
	} else if (window.frames["popupFrame"].document.title == "Loading Modal Content") {
		window.setTimeout("setPopTitleAndRewriteTargets();", 10);
	} else {
		var popupDocument = window.frames["popupFrame"].document;
		document.getElementById("popupTitle").innerHTML = popupDocument.title + " "+ options;
		
		// Set the title based on the popData variable
		if (popData[0].length > 1) {
			document.getElementById("popupTitle").innerHTML = popData[0][1] + " "+ options;
		}
		//
		
		if (popupDocument.getElementsByTagName('base').length < 1) {
			var aList  = window.frames["popupFrame"].document.getElementsByTagName('a');
			for (var i = 0; i < aList.length; i++) {
				if (aList.target == null) aList[i].target='_parent';
			}
			var fList  = window.frames["popupFrame"].document.getElementsByTagName('form');
			for (i = 0; i < fList.length; i++) {
				if (fList.target == null) fList[i].target='_parent';
			}
		}
	}
}

// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

// 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++;
			}
		}
	}
}

/**
* 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() {
	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="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 displaySelectBoxes() {
	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";
			}
		}
	}
}

/**
 * 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
 */
/* 

	Code commented-out because we use our version in Common.js

*/
/*
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
*/
/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/ *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 * Gets the full width/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; 
}

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; 
}

