// this script needs the commons.js

// GLOBAL VARIABLES
var width,height;
var scrollLeft,scrollTop;
var pageWidth, pageHeight;
var dBoxIframeElement;
var dBoxDivElement;
var dBoxIframeId = 'dBoxIframe';
var dBoxDivId = 'dBoxDiv';
// CREATES A DIALOG BOX WITH A SEMITRANSPARENT BACKGROUND
/**
* Name: createDialogBox (aBoxId)
* Description: creates a dialog box with a semitransparent background in order to lock all other links or buttons on the page
* Params: aBoxId - the ID of the box which will be shown as a dialog box
**/
function createDialogBox (aBoxId, centerHoriz, centerVert, isMovable){
	// if the function call does not contain  values for the center-params , the params will be "true".
	if ( centerHoriz != false ) centerHoriz = true;
	if ( centerVert != false ) centerVert = true;
	if ( isMovable != false ) isMovable = true;
	if ( !document.getElementById(aBoxId) ) return false;
	var dialogBox = getDbox(aBoxId);
	// create necessary node(s) - only if not already there
	if ( !document.getElementById(dBoxDivId) )
	{
		var newDialogBoxDiv = document.createElement("DIV");
		newDialogBoxDiv.setAttribute("id", dBoxDivId);
		document.getElementsByTagName('BODY')[0].appendChild(newDialogBoxDiv);
		dBoxDivElement = newDialogBoxDiv;
	}
	if ( ie && !document.getElementById(dBoxIframeId))
	{
		// additionally a iFrame node must be created
		var newDialogBoxIframe = document.createElement("IFRAME");
		newDialogBoxIframe.setAttribute("id", dBoxIframeId);
		newDialogBoxIframe.setAttribute("src", "javascript:false");
		newDialogBoxIframe.setAttribute("frameBorder", "100");
		newDialogBoxIframe.setAttribute("scrolling", "no");
		document.getElementsByTagName('BODY')[0].appendChild(newDialogBoxIframe);
		dBoxIframeElement = newDialogBoxIframe;
	}
	
	// set dialog box properties
	dialogBox.style.display = 'block';
	dialogBox.style.position = 'absolute';
	dialogBox.style.zIndex = '999';
	// set dialog box background properties (div)
	dBoxDivElement.style.display = 'block';
	dBoxDivElement.style.zIndex = '998';
	// set dialog box background properties (additionally iframe for IE)
	if (ie) 
	{
		dBoxIframeElement.style.display = 'block';
		dBoxIframeElement.style.zIndex = '997';
		// set transparency
		dBoxIframeElement.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		dBoxDivElement.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=75)';
	}
	// resize the dialog box background according to the screen
	resizeDialogBackground();
	
	// finally centralize the box according to the center-params
	centerBox(aBoxId, centerHoriz, centerVert);
	
	// implementation of movable box
	if(isMovable)
	{
		var elements = dialogBox.getElementsByTagName('P');
		for (var i=0; i<elements.length; i++)
		{
			var aElement = elements[i];
			if ( aElement.className.match(/\bmessageOk\b/)
				|| aElement.className.match(/\bmessageWarning\b/)
				|| aElement.className.match(/\bmessageProcessing\b/))
			{
				aElement.style.cursor='move';
				addEvent(aElement, "mousedown", dragMouseDown);
				break;
			}
		}
	}
}
// CLOSES A DIALOG BOX
/**
* Name: closeDialogBox (aBoxId)
* Description: closes a dialog box
* Params: aBoxId - the ID of the box which will be closed
**/
function closeDialogBox (aBoxId)
{
	getDbox(aBoxId).style.display = 'none';
	getDboxDiv().style.display = 'none';
	if (ie) getDboxIframe().style.display = 'none';
	revertConvertedAncestors( getDbox(aBoxId) );
}
// RESIZE THE DIALOG BACKGROUND 
/**
* Name: resizeDialogBackground ()
* Description: resizes th iFrame and iFrame-Div according to the determined measures
**/
function resizeDialogBackground() {
	// store metrics in global vars
	determineMetrics();
	/*
	alert(pageWidth);
	alert(viewPortWidth);
	alert(document.body.scrollWidth);
	alert(document.body.offsetWidth);
	*/
	dBoxDivElement = getDboxDiv();
	dBoxDivElement.style.width = '100%';
	dBoxDivElement.style.height = height + 'px';
	
	// IE needs an iFrame additionaly to the background-div and a different sizing of the background-div
	if (ie)
	{
		// set width and height
		dBoxDivElement.style.width = width + 'px';
		dBoxDivElement.style.height = height + 'px';
		dBoxIframeElement = getDboxIframe();
		dBoxIframeElement.style.width = width + 'px';
		dBoxIframeElement.style.height = height + 'px';
		// additionally the position of the backgrounds must be set. In real browsers this is not necessary since the CSS property positioning-value 'fixed' can be used
		dBoxDivElement.style.top = scrollTop;
		dBoxDivElement.style.left = scrollLeft;
	}
}
// DETERMINE METRICS OF A PAGE
/**
* Name: determineMetrics ()
* Description: determines viewport and page metrics
**/
function determineMetrics () {
	// determine width and height of the viewport
	if (self.innerHeight) // all but IE
	{
		viewPortWidth = self.innerWidth;
		viewPortHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	 // Explorer 6 standard compliance Mode
	{
		viewPortWidth = document.documentElement.clientWidth;
		viewPortHeight = document.documentElement.clientHeight;
		scrollLeft = document.documentElement.scrollLeft;
		scrollTop = document.documentElement.scrollTop;
	}
	else if (document.body) // other IE (quirks Mode)
	{
		viewPortWidth = document.body.clientWidth;
		viewPortHeight = document.body.clientHeight;
		scrollLeft = document.body.scrollLeft;
		scrollTop = document.body.scrollTop;
	}
	
	// determine width and height of the whole page
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Mac Explorer
	{
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}
	else // Mac Explorer
	// Explorer 6 Strict, Mozilla and Safari
	{
		pageWidth = document.body.offsetWidth;
		pageHeight = document.body.offsetHeight;
	}
	
	// determine height and width which will be finally set
	width = pageWidth;
	height = pageHeight;
	if (viewPortWidth > pageWidth) width = viewPortWidth;
	if (viewPortHeight > pageHeight) height = viewPortHeight;
}

// RESIZE DIALOG BACKGROUND WHEN THE WINDOW IS RESIZED
window.onresize = function()
{
	if ( getDboxIframe() || getDboxDiv() )
	{
		resizeDialogBackground();
	}
}
window.onscroll = function()
{
	if ( ie && ( getDboxIframe() || getDboxDiv() ) )
	{
		resizeDialogBackground();
	}
}
// RETURNS THE DIALOG BOX
function getDbox(aBoxId)
{
	if ( document.getElementById(aBoxId) ) // do not use the getContainer() function instead
	{
		return document.getElementById(aBoxId);
	}
	return false;
	
}
// RETURNS THE IFRAME ELEMENT
function getDboxIframe()
{
	if ( document.getElementById(dBoxIframeId) ) // do not use the getContainer() function instead
	{
		return document.getElementById(dBoxIframeId);
	}
	return false;
}
// RETURNS THE IFRAME DIV ELEMENT
function getDboxDiv() 
{
	if ( document.getElementById(dBoxDivId) ) // do not use the getContainer() function instead
	{
		return document.getElementById(dBoxDivId);
	}
	return false;
}
// DRAG FUNCTIONALITY
var elToDrag;
var elX;
var elY;
function dragMouseDown(ev)
{
	// get the element to drag
	if (ie) { elToDrag = window.event.srcElement.parentNode;	}
	else { elToDrag = ev.currentTarget.parentNode; }
	// avoid IE bug: if not set to 'absolute' here, relative positioned elements inside this element will not be moved when the element is moved
	elToDrag.style.position = 'absolute';
	// determine the position of the cursor
	var cursorPos = determineCursorPosition(ev);
	// set new position of elToDrag depending on the cursor position
	elX = cursorPos.x - (elToDrag.style.left).replace(/px/,"");
	elY = cursorPos.y - (elToDrag.style.top).replace(/px/,"");
	// add events
	addEvent(document, "mousemove", moveElement);
	addEvent(document, "mouseup", stopDragging);
}
function moveElement(ev)
{
	var cursorPos = determineCursorPosition(ev);
	var st = elToDrag.style;
	// set new position of the element
	st.left = (cursorPos.x - elX) + "px";
	st.top = (cursorPos.y - elY) + "px";
}
function stopDragging()
{
	removeEvent(document, "mousemove", moveElement);
	removeEvent(document, "mouseup", stopDragging);
}
function removeEvent(el, evname, func) 
{
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	}
	else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, false);
	}
	else {
		el["on" + evname] = null;
	}
}
function determineCursorPosition(ev)
{
	var pos;
	if (ie) {
		pos = {x:window.event.clientX + document.body.scrollLeft, y:window.event.clientY + document.body.scrollTop};
	} else {
		pos = {x:ev.pageX, y:ev.pageY}
	}
	return pos;
}





