// Popup code
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gPopupMask2 = null;
var gPopupContainer2 = null;
var gPopFrame2 = null;
var gReturnFunc;
var gReturnFunc2;
var gPopupIsShown = false;
var gPopup2IsShown = false;
var gDefaultPage = "about:blank";
var gHideSelects = false;
var gReturnVal = null;
var gReturnVal2 = null;

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;
}

/**
 * Initializes popup code on load.	
 */
function initPopUp() {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar">' +
				'<div id="popupTitle"></div>' +
				'<div id="popupControls">' +
					'<img src="Images/close.gif" onclick="hidePopWin(true);" id="popCloseBox" />' + 
				'</div>' +
			'</div>' +
			'<iframe src="'+ gDefaultPage +'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
		'</div>';
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);

	//Lägg till element för en andra nivå av popupfönster	
	popmask2 = document.createElement('div');
	popmask2.id = 'popupMask2';
	popcont2 = document.createElement('div');
	popcont2.id = 'popupContainer2';
	popcont2.innerHTML = '' +
		'<div id="popupInner2">' +
			'<div id="popupTitleBar2">' +
				'<div id="popupTitle2"></div>' +
				'<div id="popupControls2">' +
					'<img src="Images/close.gif" onclick="hidePopWin2(true);" id="popCloseBox2" />' +
				'</div>' +
			'</div>' +
			'<iframe src="' + gDefaultPage + '" style="width:100%;height:100%;background-color:transparent" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame2" name="popupFrame2" width="100%" height="100%"></iframe>' +
		'</div>';
	theBody.appendChild(popmask2);
	theBody.appendChild(popcont2);
	
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");
	
	gPopupMask2 = document.getElementById("popupMask2");
	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) {
		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) { 
			// var onclick = 'function (){showPopWin(\''+elms[i].href+'\','+width+', '+height+', null);return false;};';
			// elms[i].onclick = eval(onclick);
			elms[i].onclick = function(){
				// default width and height
				var width = 400;
				var height = 200;
				// Parse out optional width and height from className
				params = this.className.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);

 /**
	* @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(url, width, height, returnFunc, showCloseBox, title) {
    if (gPopupIsShown) {
        showPopWin2(url, width, height, returnFunc, showCloseBox, title);
    }
    else {
        // show or hide the window close widget
        if (showCloseBox == null || showCloseBox == true) {
            document.getElementById("popCloseBox").style.display = "block";
        } else {
            document.getElementById("popCloseBox").style.display = "none";
        }
        gPopupIsShown = true;
        disableTabIndexes();        
        gPopupMask.style.display = "block";
        gPopupContainer.style.display = "block";

        document.getElementById("popupTitle").innerHTML = title;

        // calculate where to place the window on screen
        centerPopWin(width, height);

        var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);


        gPopupContainer.style.width = width + "px";
        gPopupContainer.style.height = (height + titleBarHeight) + "px";

        setMaskSize();

        // 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 = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
        gPopFrame.style.height = (height) + "px";

        // set the url
        gPopFrame.src = url;

        gReturnFunc = returnFunc;
        // for IE
        if (gHideSelects == true) {
            hideSelectBoxes();
        }

        window.setTimeout("setPopTitle();", 600);
    }
}

/**
* @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 showPopWin2(url, width, height, returnFunc, showCloseBox, title) {
    // show or hide the window close widget
    if (showCloseBox == null || showCloseBox == true) {
        document.getElementById("popCloseBox2").style.display = "block";
    } else {
        document.getElementById("popCloseBox2").style.display = "none";
    }
    gPopup2IsShown = true;
    disableTabIndexes();
    gPopupMask2.style.display = "block";
    gPopupContainer2.style.display = "block";

    document.getElementById("popupTitle2").innerHTML = title;

    // calculate where to place the window on screen
    centerPopWin2(width, height);

    var titleBarHeight = parseInt(document.getElementById("popupTitleBar2").offsetHeight, 10);

    gPopupContainer2.style.width = width + "px";
    gPopupContainer2.style.height = (height + titleBarHeight) + "px";

    setMaskSize2();

    // 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
    gPopFrame2.style.width = parseInt(document.getElementById("popupTitleBar2").offsetWidth, 10) + "px";
    gPopFrame2.style.height = (height) + "px";

    // set the url
    gPopFrame2.src = url;

    gReturnFunc2 = returnFunc;
    // for IE
    if (gHideSelects == true) {
        hideSelectBoxes();
    }

    window.setTimeout("setPopTitle2();", 600);
}

//
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.getElementsByTagName("BODY")[0];
		
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		setMaskSize();
		
				
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();

		var popupTop = (scTop + ((fullHeight - (height + titleBarHeight)) / 2));
		var popupLeft = (scLeft + ((fullWidth - width) / 2));

		if (popupTop < 0) {
		    popupTop = 0;
		}

		if (popupLeft < 0) {
		    popupLeft = 0;
		}
		
		gPopupContainer.style.top = popupTop + "px";
		gPopupContainer.style.left =  popupLeft + "px";		
	}
}
addEvent(window, "resize", centerPopWin);

function centerPopWin2(width, height) {
    if (gPopup2IsShown == true) {
        if (width == null || isNaN(width)) {
            width = gPopupContainer2.offsetWidth;
        }
        if (height == null) {
            height = gPopupContainer2.offsetHeight;
        }


        var theBody = document.getElementsByTagName("BODY")[0];

        var scTop = parseInt(getScrollTop(), 10);
        var scLeft = parseInt(theBody.scrollLeft, 10);

        setMaskSize2();


        var titleBarHeight = parseInt(document.getElementById("popupTitleBar2").offsetHeight, 10);

        var fullHeight = getViewportHeight();
        var fullWidth = getViewportWidth();

        var popupTop = (scTop + ((fullHeight - (height + titleBarHeight)) / 2));
        var popupLeft = (scLeft + ((fullWidth - width) / 2));

        if (popupTop < 0) {
            popupTop = 0;
        }

        if (popupLeft < 0) {
            popupLeft = 0;
        }

        gPopupContainer2.style.top = popupTop + "px";
        gPopupContainer2.style.left = popupLeft + "px";
    }
}
addEvent(window, "resize", centerPopWin2);

//Borttaget... vill inte flytta fönstret när man skrollar
//addEvent(window, "scroll", centerPopWin);
//window.onscroll = centerPopWin;


/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() {
    var theBody = document.getElementsByTagName("BODY")[0];

    gPopupMask.style.height = "0px"
    gPopupMask.style.width = "0px"
			
	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 (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

function setMaskSize2() {
    gPopupMask2.style.top = gPopupContainer.style.top;
    gPopupMask2.style.left = gPopupContainer.style.left;
    gPopupMask2.style.width = gPopupContainer.style.width;
    gPopupMask2.style.height = gPopupContainer.style.height;    
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc) {
    if (gPopup2IsShown) {
        hidePopWin2(callReturnFunc);
    }
    else {
        gPopupIsShown = false;
        var theBody = document.getElementsByTagName("BODY")[0];
        theBody.style.overflow = "";
        restoreTabIndexes();
        if (gPopupMask == null) {
            return;
        }
        gPopupMask.style.display = "none";
        gPopupContainer.style.display = "none";
        if (callReturnFunc == true && gReturnFunc != null) {
            // Set the return code to run in a timeout.
            // Was having issues using with an Ajax.Request();
            gReturnVal = window.frames["popupFrame"].returnVal;
            window.setTimeout('gReturnFunc(gReturnVal);', 1);
        }
        gPopFrame.src = gDefaultPage;
        // display all select boxes
        if (gHideSelects == true) {
            displaySelectBoxes();
        }
    }
}

function hidePopWin2(callReturnFunc) { 
    gPopup2IsShown = false;
    var theBody = document.getElementsByTagName("BODY")[0];
    theBody.style.overflow = "";
    restoreTabIndexes();
    if (gPopupMask2 == null) {
        return;
    }
    gPopupMask2.style.display = "none";
    gPopupContainer2.style.display = "none";
    if (callReturnFunc == true && gReturnFunc2 != null) {
        // Set the return code to run in a timeout.
        // Was having issues using with an Ajax.Request();
        gReturnVal2 = window.frames["popupFrame2"].returnVal;
        window.setTimeout('gReturnFunc2(gReturnVal2);', 1);
    }
    gPopFrame2.src = gDefaultPage;
    // display all select boxes
    
    if (gHideSelects == true) {
        displaySelectBoxes();
    }
}

/**
 * Sets the popup title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
	}
}

function setPopTitle2() {
    return;
    if (window.frames["popupFrame2"].document.title == null) {
        window.setTimeout("setPopTitle2();", 10);
    } else {
        document.getElementById("popupTitle2").innerHTML = window.frames["popupFrame2"].document.title;
    }
}


// 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 x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++) {
    x[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 wanting select form tags to always be the 
 * topmost z-index or layer.
 */
function displaySelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++){
    x[i].style.visibility = "visible";
  }
}
