/**
 * 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 gPopupMask2 = null;
var gPopupContainer2 = null;
var gPopFrame2 = null;
var gReturnFunc2;
var gPopupIsShown2 = false;

var gHideSelects2 = false;


var gTabIndexes2 = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags2 = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandler2;
}



/**
 * Initializes popup code on load.	
 */
function initPopUp2() {
	// Add the HTML to the body
	theBody2 = document.getElementsByTagName('BODY')[0];
	popmask2 = document.createElement('div');
	popmask2.id = 'popupMask2';
	popcont2 = document.createElement('div');
	popcont2.id = 'popupContainer2';
	popcont2.innerHTML = '' +
			'<div id="popupInner2" style="background:none;">' +
			'</div>' +
				'<iframe scrolling="no" src="/_modal_windows/test2.php" style="width:100%;height:100%;background:none;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame2" name="popupFrame2" width="100%" height="100%"></iframe>' +

		'</div>';
	theBody2.appendChild(popmask2);
	theBody2.appendChild(popcont2);
	
	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 brsVersion2 = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion2 <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects2 = true;
	}
	
	// Add onclick handlers to 'a' elements of class submodal or submodal-width2-height2
	var elms2 = document.getElementsByTagName('a');
	for (i = 0; i < elms2.length; i++) {
		if (elms2[i].className.indexOf("submodal") == 0) { 
			// var onclick = 'function (){showPopWin2(\''+elms2[i].href+'\','+width2+', '+height2+', null);return false;};';
			// elms2[i].onclick = eval(onclick);
			elms2[i].onclick = function(){
				// default width2 and height2
				var width2 = 140;
				var height2 = 140;
				// Parse out optional width2 and height2 from className
				params2 = this.className.split('-');
				if (params2.length == 3) {
					width2 = parseInt(params[1]);
					height2 = parseInt(params[2]);
				}
				showPopWin2('/_modal_windows/test2.php',width2,height2,null); return false;
			}
		}
	}
}
addEvent2(window, "load", initPopUp2);

 /**
	* @argument width2 - int in pixels
	* @argument height2 - int in pixels
	* @argument url2 - url2 to display
	* @argument returnFunc - function to call when returning true from the window.
	* @argument showCloseBox - show the close box - default true
	*/

function showPopWin2(url2, width2, height2, returnFunc2, showCloseBox2) {
	// show or hide the window close widget
	if (showCloseBox2 == null || showCloseBox2 == true) {
	//	document.getElementById("popCloseBox").style.display = "block";
	} else {
//		document.getElementById("popCloseBox").style.display = "none";
	}
	gPopupIsShown2 = true;
	disableTabIndexes2();
	gPopupMask2.style.display = "block";
	gPopupContainer2.style.display = "block";
	// calculate where to place the window on screen
	centerPopWin2(width2, height2);
	
	// var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);


	gPopupContainer2.style.width = width2 + "px";
	gPopupContainer2.style.height = (height2) + "px";
	
	setMaskSize2();

	// need to set the width2 of the iframe to the title bar width2 because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	// gPopFrame2.style.width2 = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame2.style.height = (height2) + "px";
	
	// set the url2
	gPopFrame2.src = '/_modal_windows/test2.php';
	
	gReturnFunc2 = returnFunc2;
	// for IE
	if (gHideSelects2 == true) {
		hideSelectBoxes2();
	}
	
	// window.setTimeout("setPopTitle2();", 600);
}

//
var gi = 0;
function centerPopWin2(width2, height2) {
	if (gPopupIsShown2 == true) {
		if (width2 == null || isNaN(width2)) {
			width2 = gPopupContainer2.offsetWidth;
		}
		if (height2 == null) {
			height2 = gPopupContainer2.offsetHeight;
		}
		
		//var theBody2 = document.documentElement;
		var theBody2 = document.getElementsByTagName("BODY")[0];
		theBody2.style.overflow = "hidden";
		
		var scTop2 = parseInt(theBody2.scrollTop,10);
		var scLeft2 = parseInt(theBody2.scrollLeft,10);
		
		gPopupMask2.style.top = scTop2 + "px";
		gPopupMask2.style.left = scLeft2 + "px";
	
		setMaskSize2();
		
		//window.status = gPopupMask2.style.top + " " + gPopupMask2.style.left + " " + gi++;
		
		var titleBarHeight = 0;
		
		var fullHeight2 = getViewportHeight2();
		var fullWidth2 = getViewportWidth2();
		
		gPopupContainer2.style.top = (scTop2 + ((fullHeight2 - (height2)) / 2)) + "px";
		gPopupContainer2.style.left =  (scLeft2 + ((fullWidth2 - width2) / 2)) + "px";
		//alert(fullWidth + " " + width2 + " " + gPopupContainer2.style.left);
	}
}
addEvent2(window, "resize", centerPopWin2);
//addEvent2(window, "scroll", centerPopWin2);
window.onscroll = centerPopWin2;

/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize2() {
	var theBody2 = document.getElementsByTagName("BODY")[0];
			
	var fullHeight2 = getViewportHeight2();
	var fullWidth2 = getViewportWidth2();
	
	// Determine what's bigger, scrollHeight or fullHeight / width2
	if (fullHeight2 > theBody2.scrollHeight) {
		popHeight2 = fullHeight2;
	} else {
		popHeight2 = theBody2.scrollHeight;
	}
	
	if (fullWidth2 > theBody2.scrollWidth) {
		popWidth2 = fullWidth2;
	} else {
		popWidth2 = theBody2.scrollWidth;
	}
	
	gPopupMask2.style.height = popHeight2 + "px";
	gPopupMask2.style.width = popWidth2 + "px";
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin2(callReturnFunc2) {
	gPopupIsShown2 = false;
	var theBody2 = document.getElementsByTagName("BODY")[0];
	theBody2.style.overflow = "";
	restoreTabIndexes2();
	if (gPopupMask2 == null) {
		return;
	}
	gPopupMask2.style.display = "none";
	gPopupContainer2.style.display = "none";
	if (callReturnFunc2 == true && gReturnFunc2 != null) {
		gReturnFunc2(window.frames["popupFrame2"].returnVal);
	}
	gPopFrame2.src = '/_modal_windows/test2.php';
	// display all select boxes
	if (gHideSelects2 == true) {
		displaySelectBoxes2();
	}
}

/**
 * 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 setPopTitle2() {
	return;
	if (window.frames["popupFrame2"].document.title == null) {
		// window.setTimeout("setPopTitle2();", 10);
	} else {
	//	document.getElementById("popupTitle").innerHTML = 'Hinweis';
	}
}

// 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 keyDownHandler2(e) {
    if (gPopupIsShown2 && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes2() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags2.length; j++) {
			var tagElements2 = document.getElementsByTagName(gTabbableTags2[j]);
			for (var k = 0 ; k < tagElements2.length; k++) {
				gTabIndexes2[i] = tagElements2[k].tabIndex;
				tagElements2[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes2() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags2.length; j++) {
			var tagElements2 = document.getElementsByTagName(gTabbableTags2[j]);
			for (var k = 0 ; k < tagElements2.length; k++) {
				tagElements2[k].tabIndex = gTabIndexes2[i];
				tagElements2[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 hideSelectBoxes2() {
	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 displaySelectBoxes2() {
	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";
			}
		}
	}
}

function addEvent2(obj2, evType2, fn2){
 if (obj2.addEventListener){
    obj2.addEventListener(evType2, fn2, false);
    return true;
 } else if (obj2.attachEvent){
    var r2 = obj2.attachEvent("on"+evType2, fn2);
    return r2;
 } else {
    return false;
 }
}

function getViewportHeight2() {
	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 getViewportWidth2() {
	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; 
}
