/**
 * POPUP WINDOW CODE v1.3
 * 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
 * 	Scott - hiding/showing selects for IE users
 *	Todd Huss - inserting modal dynamically and anchor classes
 *
 * 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.
 */

// Popup code
var lockedParent=true;
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gPopupIsShown = false;
var gHideSelects = false;
var defaultWidth;
var defaultHeight;
var topBarH=10;
var botBarH=10;
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","DIV");	

/**
 * Initializes popup code on load.	
 */
function initPopUp(closable,maximizable,html) {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	if(document.getElementById('popupMask')){
	var o = document.getElementById('popupMask').parentNode;
	o.removeChild(document.getElementById('popupMask'));
	}
	if(document.getElementById('popupContainer')!=null){
	var o = document.getElementById('popupContainer').parentNode;
	o.removeChild(document.getElementById('popupContainer'));
	}
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.className='resizeMe';
	
	
	var body='<tr>';
	if(html){
				body='<td align="center">';
				body=body+html;
	}else
					body=body+'<td align="center"><iframe onresize="onPopMove()"  style="width:98%;height:100%;background-color:transparent;" scrolling="no" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="98%" height="100%"></iframe>';
	body=body+'</td></tr>';
	

	popcont.innerHTML='<div><table style="border: none;background-image:none;background-color:transparent;" cellpadding="0" cellspacing="0">'+body+'</table></div>';	
	
	theBody.appendChild(popmask);

	theBody.appendChild(popcont);
	
	gPopupMask = getElementById("popupMask");
	gPopupContainer = getElementById("popupContainer");
	if (getElementById("popupFrame"))
	gPopFrame = 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;
	}
	
}



function isClosable(){
		return (parent.getElementById('isClosable')&&	parent.getElementById('isClosable').value=='true')
}
function startDragging(){
	if (gHideSelects == true){
							xEnableDrag(getElementById("popupContainer"),null,null,onPopMove);//IE won't lock the pop up window inside the parent page
	}else{
							xEnableDrag(getElementById("popupContainer"),null,onMouseDrag,onPopMove);
	}
}
function stopDragging(){
if(window._xDrgMgr){
		var tmp=getElementById('popupContainer');
		xRemoveEventListener(tmp,'mousedown',_xOMD,false);
		_xDrgMgr.mm=false;
		xRemoveEventListener(tmp,'mousemove',_xOMM,false);
		tmp.xDraggable=false;
		tmp.xODS=null;
		tmp.xOD=null;
		tmp.xODE=null;
}
}
function maximize(){
if(gPopFrame==null)
		gPopFrame=getElementById("popupFrame");
if (gPopFrame){
   gPopFrame.style.width=getViewportWidth()+ 'px';
   gPopFrame.style.height=(getViewportHeight()-(topBarH+botBarH))+'px';
   centerPopWin();
  
   var max_restore_but=getElementById("max_restore_but");
   var max_restore_img=getElementById("max_restore_img");
   
   if(max_restore_but!=null && max_restore_img!=null){
   			max_restore_but.href="javascript:restore()";
   				max_restore_img.src="./restore.gif";
   }
   xMoveTo(gPopupContainer,0,0);
			onPopMove();
}

}
function restore(){
if(gPopFrame==null)
		gPopFrame=getElementById("popupFrame");
if (gPopFrame){
   gPopFrame.style.width=defaultWidth;
   gPopFrame.style.height=defaultHeight-topBarH+botBarH;
   centerPopWin();
   var max_restore_but=getElementById("max_restore_but");
   var max_restore_img=getElementById("max_restore_img");
   if(max_restore_but!=null && max_restore_img!=null){
   			max_restore_but.href="javascript:maximize()";
   				max_restore_img.src="./maximize.png";
   }
}
}

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument url - url to display
	*	@argument scrolling - yes,no,auto
	*	@argument closable - null or false when the frame won't have a close button
	* @argument maximizable - false when the frame won't have maximize button
	*	@argument html - if the url is ignored and we prefer a custom html
	* @argument lockParent - false if parent won't be locked
	*/
function showPopWin(url, width, height,scrolling,par,closeCauseRefr,closable,maximizable,html,isLocked) {
		if(isLocked==false)
					lockedParent=isLocked;
					
		lockParent();
					
		//If the given height is longer than the view
		if (getViewportHeight()<height)
					height=getViewportHeight();
		//If the given width is longer than the view
		if (getViewportWidth()<width)
					width=getViewportWidth();
	
	this.defaultHeight=height;
	this.defaultWidth=width;
	initPopUp(closable,maximizable,html);
	gPopupIsShown = true;
	if(lockedParent!=false){
				gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";}
	// calculate where to place the window on screen
	centerPopWin(width, height);
	gPopFrame=getElementById('popupFrame');
	if(gPopFrame){
	if(scrolling){
			gPopFrame.setAttribute('scrolling',scrolling);
	}
	
	// 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 = (width) + "px";
	gPopFrame.style.height = (height-topBarH+botBarH) + "px";
	// set the url
	gPopFrame.src = url;
	}
	addPopEvent(window, "resize", onMoveResize);
	addPopEvent(window, "scroll", onMoveResize);
	window.onscroll = onMoveResize;
	// If using Mozilla or Firefox, use Tab-key trap.
	if (!document.all) {
			document.onkeypress = keyDownHandler;
	}
	displayElementById("popupMask");
	displayElementById("popupContainer");
 displayElementById("popupFrame");	
 
 	
 	gPopupContainer.style.visibility='visible';
		gPopupMask.style.visibility='visible';
}
function hidePopWin(shouldRefresh) {

	gPopupIsShown = false;

	var theBody = document.getElementsByTagName("BODY")[0];
	
	if(gPopupMask==null)	
				gPopupMask=getElementById("popupMask");
	if(gPopupContainer==null)
				gPopupContainer=getElementById("popupContainer");
	
			gPopupMask.style.display='none';
				gPopupContainer.style.display='none';
	removePopEvent(window, "resize", onMoveResize);
	removePopEvent(window, "scroll", onMoveResize);
	
	theBody.style.overflow = "";
	
	window.onscroll = '';
	// If using Mozilla or Firefox, use Tab-key trap.
	if (!document.all) {
			document.onkeypress = '';
	}
	
	unlockParent();
	
	if(shouldRefresh==true){
					form_action('refresh');
	}		
}

function lockParent(){
			//hideTree();
			hideMenu();
			//lockKeys();
			disableTabIndexes();
			setMaskSize();
			//if (gHideSelects == true) {
							hideSelectBoxes();
			//}
}
function unlockParent(){
			displayMenu();
	//displayTree();
			unlockKeys();
			restoreTabIndexes();
	// display all select boxes
	//if (gHideSelects == true) {
		displaySelectBoxes();
	//}
}


/**
*http://cross-browser.com/x/examples/key_events.php
* Code for locking the keyboard
*/
function lockKeys()
{
  xAddEventListener(document, 'keypress', onKeypress, false);
  xAddEventListener(document, 'keyup', onKeyup, false);
  xAddEventListener(document, 'keydown', onKeydown, false);
}
function unlockKeys(){/*There is no code needed,yet, to unlock keys!*/}
var lockedKeys = new Array(112,113,114,115,116,117,118,119,120,121,122,123);
function isKeyLocked(keyCode){
for (var i=0;i<lockedKeys.length;i++){
				if (lockedKeys[i] == keyCode){
							return true;
				}
}
return false;
}
function onKeypress(evt){var e = new xEvent(evt);if (isKeyLocked(e.keyCode))xPreventDefault(evt);}
function onKeyup(evt){var e = new xEvent(evt);if (isKeyLocked(e.keyCode))xPreventDefault(evt);}
function onKeydown(evt){var e = new xEvent(evt);if (isKeyLocked(e.keyCode))xPreventDefault(evt);}
//End of Keyboard Lock
function onMoveResize(){
	if (gPopupContainer.offsetWidth>getViewportWidth() || gPopupContainer.offsetHeight>getViewportHeight()){
				maximize();
	}
							lockParent();
}

function onMouseDrag(ele, mdx, mdy){
if (gPopupContainer==null)
			gPopupContainer=getElementById("popupContainer");
xMoveTo(gPopupContainer,xLeft(gPopupContainer)+mdx,xTop(gPopupContainer)+mdy);
											onPopMove();
}

function onPopMove(){
if (xLeft(gPopupContainer)<0 )
			xMoveTo(gPopupContainer,0,xTop(gPopupContainer));
if (xLeft(gPopupContainer)>(getViewportWidth()-10))
			xMoveTo(gPopupContainer,(getViewportWidth()-10),xTop(gPopupContainer));
			
if (xTop(gPopupContainer)<0)
			xMoveTo(gPopupContainer,xLeft(gPopupContainer),0);
if (xTop(gPopupContainer)>(getViewportHeight()-topBarH))
			xMoveTo(gPopupContainer,xLeft(gPopupContainer),(getViewportHeight()-topBarH));
			lockParent();
}
//
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 theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		theBody.style.overflow = "hidden";
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		gPopupMask.style.top = scTop + "px";
		gPopupMask.style.left = scLeft + "px";
	
		setMaskSize();
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		gPopupContainer.style.top = (scTop + ((fullHeight - height) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}



function displayElementById(id) {
 if(getElementById(id)!=null)
					getElementById(id).style.visibility="visible";		
}

function hideElementById(id) {
		if(getElementById(id)!=null)
					getElementById(id).style.visibility="hidden";
}

function getElementById(id){
/*						var tmp =window.top.document.getElementById(id); */
						var tmp =this.document.getElementById(id);
						if(tmp!=null)
													return tmp;
						return document.getElementById(id);
}


var menuDisplay;
/**
* Hopping that there is only one menu..
*/
function hideMenu(){
if(typeof  gmobj!="undefined"){
			var menu=gmobj('menu0');
			if(menu){
											menu.style.display="none";
					}
}
			if(getElementById('menu0'))
								getElementById('menu0').style.visibility="hidden";
}
function displayMenu(){				
if(typeof  gmobj!="undefined"){
			var menu=gmobj('menu0');
			if(menu){
														menu.style.display="";
					}
}
					if(getElementById('menu0'))
									getElementById('menu0').style.visibility="visible";
}

function hideTree()
{
								if(getElementById('tree_td')){
												getElementById('tree_td').style.display='none'; 
								}
}
function displayTree()
{
								if(getElementById('tree_td')){
												getElementById('tree_td').style.display=''; 
								}
}

function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight
	}
	if(!gPopupMask)
								gPopupMask=getElementById("popupMask");
	if(gPopupMask){
								gPopupMask.style.height = popHeight + "px";
								gPopupMask.style.width = theBody.scrollWidth + "px";
	}
}
// 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() {
	var els = document.getElementsByTagName('select');
		if(els){
			for(i=0;i<els.length;i++){
				els[i].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() {
var els = document.getElementsByTagName('select');
		if(els){
			for(i=0;i<els.length;i++){
				els[i].style.visibility='visible';
			}
		}	
}
