var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gPopupContainer2 = null;
var gPopFrame2 = null;

var gReturnFunc;
var gPopupIsShown = false;
var gPopupIsShown2 = false;

var gHideSelects = false;
var gHideSelects2 = 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) {
	document.onkeypress = keyDownHandler;
}

function addEvent(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 removeEvent(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");
    return false;
  }
}


function showPopWin(url, width, height, returnFunc) {
	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;
	}
	
	gPopupIsShown = true;
	//disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = height + "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 = width + "px";
	gPopFrame.style.height = (height) + "px";
	if (returnFunc == "scrollingYes") {
		// for main page's notice layer
		// 2010.08.11 by ilmano
		gPopFrame.scrolling = "yes";
	}
	
	// set the url
	centerPopWin(width, height);
	
	if (navigator.userAgent.indexOf('Gecko') !=-1) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		window.setTimeout('setIFrameSRC(\'' + url + '\')', 10);
		return false;
    } else {
		gPopFrame.src = url;	
    }

	if (returnFunc != "scrollingYes") {
		gReturnFunc = returnFunc;
	}

	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
}	

function goMyRoom(url) 
{
	gPopFrame = document.getElementById("popupFrame");
	
	if (navigator.userAgent.indexOf('Gecko') !=-1)
	{
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		window.setTimeout('setIFrameSRC(\'' + url + '\')', 10);
		return false;
    }
    else
    {
		gPopFrame.src = url;	
    }

	gReturnFunc = returnFunc;
}	



function showPopWin2(url, width, height, returnFunc)
{
	gPopupContainer2 = document.getElementById("popupContainer2");
	gPopFrame2 = document.getElementById("popupFrame2");
	
	// 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) {
		gHideSelects2 = true;
	}
	
	gPopupIsShown2 = true;

	gPopupContainer2.style.display = "block";

	
	gPopupContainer2.style.width = width + "px";
	gPopupContainer2.style.height = height + "px";

	gPopFrame2.style.width = width + "px";
	gPopFrame2.style.height = (height) + "px";
	

	centerPopWin2(width, height);
	
	if (navigator.userAgent.indexOf('Gecko') !=-1)
	{
		window.setTimeout('setIFrameSRC2(\'' + url + '\')', 10);
		return false;
    }
    else
    {
		gPopFrame2.src = url;	
    }
	
	gReturnFunc2 = returnFunc;
	// for IE
	if (gHideSelects2 == true)
	{
		hideSelectBoxes();
	}
}	


function setIFrameSRC(ifurl)
{
	gPopFrame.src = ifurl;
}

function setIFrameSRC2(ifurl)
{
	gPopFrame2.src = ifurl;
}



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;

		var scTop
		if (document.documentElement && document.documentElement.scrollTop)
		{
			scTop = parseInt(theBody.scrollTop,10);
		}
		else if (document.body)
		{
			scTop = parseInt(document.body.scrollTop,10)
		}
		else
		{
			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";
		
		
		if(height <fullHeight)
		{
			gPopupContainer.style.top = (scTop + ((fullHeight - height) / 2)) + "px";
		}
		else
		{
			gPopupContainer.style.top = "-5px";
		}
		
		if(width <fullWidth)
		{
			gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		}
		else
		{
			gPopupContainer.style.left =  "-2px";				
		}
	}
}

function centerPopWin2(width, height) {

	if (gPopupIsShown2 == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer2.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer2.offsetHeight;
		}
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();

		var theBody = document.documentElement;

		var scTop
		if (document.documentElement && document.documentElement.scrollTop)
		{
			scTop = parseInt(theBody.scrollTop,10);
		}
		else if (document.body)
		{
			scTop = parseInt(document.body.scrollTop,10)
		}
		else
		{
			scTop = parseInt(theBody.scrollTop,10);
		}

		var scLeft = parseInt(theBody.scrollLeft,10);				
		
		if(height <fullHeight)
		{
			gPopupContainer2.style.top = (scTop + ((fullHeight - height) / 2)) + "px";
		}
		else
		{
			gPopupContainer2.style.top = "-5px";
		}
		
		if(width <fullWidth)
		{
			gPopupContainer2.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		}
		else
		{
			gPopupContainer2.style.left =  "-2px";				
		}
	}
}

addEvent(window, "resize", centerPopWin);
addEvent(window, "scroll", centerPopWin);


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; 
}

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++;
			}
		}
	}
}

function hidePopWinDirect(failMessage) {
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	
	gPopupIsShown = false;
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	

	gPopFrame.src = "about:blank";
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
	
	focus();
	alert(failMessage);
}

function hidePopWin(callReturnFunc) {
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	
	gPopupIsShown = false;
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(window.frames["popupFrame" + templateID].returnVal);
	}
	gPopFrame.src = "about:blank";
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}




function hidePopWin2(callReturnFunc)
{
	gPopupContainer2 = document.getElementById("popupContainer2");
	gPopFrame2 = document.getElementById("popupFrame2");	
	
	gPopupIsShown2 = false;

	gPopupContainer2.style.display = "none";
	if (callReturnFunc == true && gReturnFunc2 != null) {
		gReturnFunc2(window.frames["popupFrame2" + templateID].returnVal);
	}
	gPopFrame2.src = "about:blank";

	if (gHideSelects == false && gHideSelects2 == true) {
		displaySelectBoxes();
	}
}


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";
			}
		}
	}
	
	var oSelectBox = document.getElementById("totalSearchFlag");
	
	if(oSelectBox != null)
	{
		oSelectBox.style.visibility = "hidden";
	}
	
}

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";
			}
		}
	}
	
	var oSelectBox = document.getElementById("totalSearchFlag");
	
	if(oSelectBox != null)
	{
		oSelectBox.style.visibility = "visible";
	}
}

