/**
 * SUBMODAL v1.4
 * 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 gPopupMask = null;
var gReturnFunc;
var gHideSelects = false;
var gPopUp = null;
var gPopUpHeight = 0;
var gPopUpWidth = 0;
var gContainerWidth = 587;

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 initPopupMask()
{
	// Add the HTML to the body
	var theForm = document.getElementsByTagName('FORM')[0];
	
	if(!gPopupMask)
	{
		var popMask = document.createElement('div');
		popMask.id = 'popupMask';
		theForm.appendChild(popMask);
		
		gPopupMask = document.getElementById("popupMask");
		
		// 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;
		}
	}
}

addEvent(window, "load", initPopupMask);

 /**
	* @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.
	* @argument showCloseBox - show the close box - default true
	*/

function showPopWin(width, height, popup, returnFunc)
{	
	gPopUp = popup;
	gPopUpHeight = height;
	gPopUpWidth = width;
	
	disableTabIndexes();
	gPopupMask.style.display = "block";

	//Få lagerpfunktionen att fungera även i IE6
	var theForm = document.getElementsByTagName('FORM')[0];
	gPopUp.parentNode.removeChild(gPopUp);
	theForm.appendChild(gPopUp);
		
	// calculate where to place the window on screen
	centerPopWin();
		
	gPopUp.style.width = width + "px";
	gPopUp.style.height = height + "px"; //(height+titleBarHeight) + "px";
	
	gReturnFunc = returnFunc;
	
	gPopUp.style.display = "block";
	
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}	
}

function centerPopWin() {
	if (gPopupMask && gPopUp)
	{
		if (gPopUpWidth == null || isNaN(gPopUpWidth)) {
			gPopUpWidth = gPopUp.offsetWidth;
		}
		if (gPopUpHeight == null || isNaN(gPopUpHeight)) {
			gPopUpHeight = gPopUp.offsetHeight;
		}
		
		setMaskSize();
			
		var scTop = 0;
		var scLeft = 0;
		
		if (document.documentElement && document.documentElement.scrollTop)
			scTop = document.documentElement.scrollTop;
		else if (document.body)
			scTop = document.body.scrollTop;
		
		if (document.documentElement && document.documentElement.scrollLeft)
			scLeft = document.documentElement.scrollLeft;
		else if (document.body)
			scLeft = document.body.scrollLeft;
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
	
		var theBody = document.getElementsByTagName("BODY")[0];
	
		var objs = xGetElementsByClassName("BannerTop", theBody, "div", null);
		
		var bannerheight = 0;
		
		if (objs.length > 0)
		{
			if (document.all)
				bannerheight = objs[0].offsetHeight+35;
			else
				bannerheight = objs[0].childNodes[1].offsetHeight+35;
		}
		
		gPopUp.style.top = (scTop + ((fullHeight - gPopUpHeight - bannerheight) / 2) - 25) + "px";
		gPopUp.style.left =  185 + ((gContainerWidth - gPopUpWidth)/2) + "px";
	}
}

addEvent(window, "resize", setMaskSize);
addEvent(window, "scroll", setMaskSize);

//addEvent(window, "resize", setMaskSize);

/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() 
{
	var theBody = document.getElementsByTagName("BODY")[0];
	
	var objs = xGetElementsByClassName("BannerTop", theBody, "div", null);
	
	var bannerheight = 0;
	
	if (objs.length > 0)
	{
		if (document.all)
			bannerheight = objs[0].offsetHeight+35;
		else
			bannerheight = objs[0].childNodes[1].offsetHeight+35;
	}
	
	var scTop = 0;
	var scLeft = 0;
	
	if (document.documentElement && document.documentElement.scrollTop)
		scTop = document.documentElement.scrollTop;
	else if (document.body)
		scTop = document.body.scrollTop;
	
	if (document.documentElement && document.documentElement.scrollLeft)
		scLeft = document.documentElement.scrollLeft;
	else if (document.body)
		scLeft = document.body.scrollLeft;

	gPopupMask.style.top = (scTop-bannerheight) + "px";
		
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	//Special för edit. Annars krachar wabblasaren
	if(window.top != window) 
	{
		if (fullHeight > theBody.scrollHeight) 
		{
			popHeight = fullHeight;
		} else {
			popHeight = fullHeight;// theBody.scrollHeight-fullHeight;
		}
	}	
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc) {	
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	
	gPopupMask.style.display = "none";
	gPopUp.style.display = "none";
	
	gPopUp = null;
	gPopUpHeight = 0;
	gPopUpWidth = 0;
	
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}

function hidePopWin2(callReturnFunc) {	
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexes();
		
	gPopUp.style.display = "none";
	
	gPopUp = null;
	gPopUpHeight = 0;
	gPopUpWidth = 0;
	
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}

// 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 (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";
			}
		}
	}
	
	//Visa de selectboxar som ligger i popupen
	var selectBoxes = gPopUp.getElementsByTagName("SELECT");
	for(var i = 0; i < selectBoxes.length; i++)
	{
		selectBoxes[i].style.visibility="visible";	
	}
}

/**
* 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";
			}
		}
	}
}