
/* check if the used browser is an Internet Explorer */
var ie = true;
if (self.innerHeight) ie = false;

// avoid IE6 flicker bug
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

// ADD EVENT FUNCTION - USED FOR ADDING EVENTS TO HTML ELEMENTS
/**
* Name: addEvent (elm, evType, fn, useCapture)
* Description: adds an event to a passed HTML element.
* Params: elm - the HTML element object where the event needs to be attached to, 
* 			evType - the type of event without 'on' prefix (e.g. pass 'load' if you want to add an onload event)
*			fn - the function to be called when the event occurs without brackets (e.g. myFunction)
*			useCapture - value 'true' (default) if the event needs to become active in the capture phase
**/
function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 
// helper function: gets the absolute position of an element (coordinates of the upper left corner of the element). It returns the coordinates as an object containing the "x" and "y" coordinate as properties
function getAbsolutePosition (element) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(element.tagName);
	if (is_div && element.scrollLeft) SL = element.scrollLeft;
	if (is_div && element.scrollTop) ST = element.scrollTop;
	var r = { x: element.offsetLeft - SL, y: element.offsetTop - ST };
	if (element.offsetParent) {
		var tmp = getAbsolutePosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

// RETURNS A CONTAINER BY ID
function getContainer(aContainerId)
{
	var aContainer = document.getElementById(aContainerId);
	if (aContainer != null)
	{
		return aContainer;
	}
	else
	{
		alertMissingId(aContainerId);
		return false;
	}
}

// DISPLAY A CONTAINER BY ITS  ID
/**
* Name: showContainer (aId)
* Description: shows a container by accessing it by its ID
* Params: aId - a unique ID as a string
**/
function showContainer(aId)
{
	var containerToShow = getContainer(aId);
	if (!containerToShow) return false;
	containerToShow.style.display = 'block';
}
// HIDE A CONTAINER BY ITS  ID
/**
* Name: hideContainer (aId)
* Description: hides a container by accessing it by its ID
* Params: aId - a unique ID as a string
**/
function hideContainer(aId)
{
	var containerToHide = getContainer(aId);
	if (!containerToHide) return false;
	containerToHide.style.display = 'none';
}
// DETERMINE TABLES WHICH HAVE ALTERNATING ROWS
addEvent(window, "load", alternatingTables_init); // an onload event is added to the loaded page
function alternatingTables_init()
{
	// Find all tables where the rows must be in alternating color
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if ((' '+thisTbl.className+' ').indexOf("alternatingRows") != -1) {
            setAlternatingRows(thisTbl);
        }
		// check if this table needs a row highlighting
		if ((' '+thisTbl.className+' ').indexOf("addHighlightRowEvent") != -1) {
            setHighlightRows(thisTbl);
        }
    }
}
function setAlternatingRows(tableElement)
{
	var currentRowToBeHighlighted = 0;
	if (tableElement.rows && tableElement.rows.length > 0) {
		
		for ( var i=0; i<tableElement.rows.length; i++)
		{
			//determine sort row
			if (tableElement.rows[i].cells[0].tagName.toLowerCase() == 'td') // highlight only rows which are not used as headline ( no <th> ) 
			{
			
				if (currentRowToBeHighlighted%2 == 0) tableElement.rows[i].className = tableElement.rows[i].className+' brightRow';
				else tableElement.rows[i].className = tableElement.rows[i].className+' darkRow';
				currentRowToBeHighlighted+=1;
			
			}	
		}
        
    }
}
function setHighlightRows (tableElement)
{
	if (tableElement.rows && tableElement.rows.length > 0) {
		
		for ( var i=0; i<tableElement.rows.length; i++)
		{
			if (tableElement.rows[i].cells[0].tagName.toLowerCase() == 'td') // highlight only rows which are not used as headline ( no <th> ) 
			{
				tableElement.rows[i].onmouseover=function() { highlightRow(this); }
			
			}	
		}
        
    }
}
// HIGHLIGHT A TABLE ROW
/**
* Name: highlightRow (rowElement)
* Description: highlightes a table row when passing the cursor over it. This method is called in a onMouseover event of a <tr> element.
* Params: rowElement - the <tr> HTML element
**/
function highlightRow(rowElement)
{
	// check if a valid rowElement has been passed
	if (typeof rowElement != 'object' || rowElement.nodeName != "TR")
	{
		alert("ERROR: passed property 'rowElement' is not a <tr> HTML element");
		return false;
	}
	/* highlightedRow must be added additional to a possible old className of the <tr>. When removing the "highlightedRow" class name, the old class name must remain */
	rowElement.className = rowElement.className + ' highlightedRow';
	rowElement.onmouseout = function()
	{
		rowElement.className = rowElement.className.replace(/highlightedRow/, '');
	}
}
// ADVANCED TAB CONTAINER
/**
* Name: prepareAdvancedTabContainer ()
* Description: 
* Params: none
**/
addEvent(window, "load", determineAdvancedTabContainers); // an onload event is added to the loaded page
var tabContainerIndex = 0; //for each tab-container-div the amount will be increased by 1. Needed to specify unique IDs for the main-tabs.
function determineAdvancedTabContainers()
{
	var allDivs = document.getElementsByTagName('DIV');
	for (var i = 0; i < allDivs.length; i++)
	{
		var currentDiv = allDivs[i];
		if (currentDiv.className.match(/\badvancedTabContainer\b/))
		{
			tabContainerIndex++;
			prepareAdvancedTabContainer(currentDiv);
		}
	}
}
function prepareAdvancedTabContainer(tabContainer)
{
	// determine main tabs
	var tabs = tabContainer.getElementsByTagName('LI');
	for (i=0; i<tabs.length; i++)
	{
		var tab = tabs[i];
		// check if it is a main tab (LI-element)
		if (tab.tagName == "LI" && tab.getElementsByTagName('A')[0])
		{
			// provide li-element with an id if not already defined by the developer
			if(trimString(tab.id).length == 0)
			{
				tab.id = "tab_" + tabContainerIndex + "_" + i;
			}
			// provide <a> element with onclick event. The <li>-element could also be provided with the event, but then the functions whould also be invoked when clicking on sub-li-elements.
			tab.getElementsByTagName('A')[0].onclick=function() { chooseTab(this.parentNode); }
		}
	}
}
function chooseTab(liElement)
{
	if ( typeof liElement == 'string' )
	{
		liElement = getContainer(liElement);
	}
	// return false if the li element is not an HTML element in order to prevent errors
	if (!liElement) return false;
	// check if the provided element is an li-element. if not, a anchor element may have been provided. take the parent element.
	if (liElement.nodeName != 'LI')
	{
		if (liElement.className.match(/\badvancedTabContainer\b/)) return; // in this case the tab container has been left. No further processing
		chooseTab(liElement.parentNode);
		return;
	}
	// reset all tabs of this tab container
	resetTabs(liElement);
	// mark the new selected tab as selected
	liElement.className += " selectedTab";
	if (liElement.parentNode.parentNode.nodeName == 'LI') 
	{
		liElement.parentNode.parentNode.className += " selectedTab";
	}
	// if selected tab has sub-tabs, the tab container div needs a margin bottom else the border is 0px.
	var root = determineRoot(liElement);
	root.style.marginBottom = "18px";
	if (liElement.getElementsByTagName('UL').length == 0 && liElement.parentNode.parentNode.className.match(/\badvancedTabContainer\b/))
	{
		root.style.marginBottom = "0px";
	}
}
function resetTabs(element)
{
	// get the root-ul for this container, get all li-elements for the root and remove the selectedTab class from all li-elements
	var root = determineRoot(element);
	var liElements = root.getElementsByTagName('LI');
	for (var i=0; i<liElements.length; i++)
	{
		liElements[i].className = liElements[i].className.replace(/ ?selectedTab/,'')
	}
}
function determineRoot(element)
{
	if(element.parentNode.className.match(/\badvancedTabContainer\b/))	
	{
		return element.parentNode; 
	}
	else
	{
		return determineRoot(element.parentNode);
	}
}
/**
* Name:			openNewWindowLink(uri, newWindowName) 
* Description: 		opens a new window
* Params: 			uri -> the URI
* 				newWindowName -> the name of the new window
**/
var newWin;
function openNewWindowLink(uri, newWindowName){
	// if current window has no name, provide window with an name
	if (window.name == "")
	{
		window.name = "currentWin";
	}
	// determine width and height of the new window (available screen width and height minus some space)
	var width = parseInt(screen.availWidth)-100;
	var height = parseInt(screen.availHeight)-150;
	// open new window
	newWin = window.open(uri, newWindowName, "dependent=yes,width="+width+",height="+height+",left=50,top=50,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,modal=yes");
	newWin.focus();
	// close new window if opener window is unloaded
	window.onunload = function(){newWin.close();}
	return false;
}
// CHECK ALL CHECKBOXES INSIDE A CONTAINER
/**
* Name: manageCheckboxes (aContainerId, isChecked)
* Description: checks or clears all checkboxes which are situated inside the container specified by the passed ID
* Params: 	aContainerId - a unique ID as a string
*			isChecked - possible values: true, false. Indicates whether the checkboxes should be checked or cleared
**/
function manageCheckboxes(aContainerId, isChecked)
{
	var containerElement = document.getElementById(aContainerId);
	// return false if the Element with the passed ID does not exist
	if ( containerElement == null) return false;
	
	// get all input elements of the container
	var inputElements = containerElement.getElementsByTagName('INPUT');
	for (var i=0; i<inputElements.length; i++)
	{
		var inputElement = inputElements[i];
		if (inputElement.type == 'checkbox')
		{
			inputElement.checked = isChecked;
		}
	}
	
}
// CENTER A BOX HORIZONTALLY AND/OR VERTICALLY
/**
* Name: centerBox (boxId, centerHoriz, centerVert)
* Description: centers a given box horizontally and vertically (as good as possible, since the measures of the box are not always known)
* Params: boxId - a unique ID as a string, centerHoriz - boolean, indicates whether the box should be centered horizontally, 
*			centerVert - boolean, indicates whether the box should be centered vertically
**/
function centerBox(boxId, centerHoriz, centerVert)
{	
	// 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;
	// get the box element by its ID. If not available, return false.
	var boxElement = getContainer(boxId);
	if ( !boxElement )
	{
		return false;
	}
	// if box is not absolute positioned it can not be centered
	if (boxElement.style.position != 'absolute')
	{
		alert("ERROR: the box with the ID \"" + boxId + "\" is not absolutely positioned. \nThe box MUST be absolutely positioned in order to be centered.");
		return false;
	}
	// determine vertical and horizontal scroll position
	var scrollPosTop = determineScrollPosTop();
	var scrollPosLeft = determineScrollPosLeft();
	// determine vertical and horizontal viewport measures
	var innerHeight = determineInnerHeight();
	var innerWidth = determineInnerWidth();
	// determine width of the box (if available)
	var boxWidth = 400; // standard width of a popin - see intranetStyle.css
	if (boxElement.style.width != '') 
	{
		boxWidth = (boxElement.style.width).replace(/px/,"");
	}
	// calculate position values of the box
	var confirmBoxPositionX = ( scrollPosLeft+innerWidth/2-boxWidth/2 ); // horizontal position (CSS: left)
	var confirmBoxPositionY = ( scrollPosTop+innerHeight/2 - 100 ); // horizontal position (CSS: top)
	// convert non static ancestors into static ones in order that the box is displayed properly
	convertNonStaticAncestors(boxElement);
	// set the position of the box and the display property
	if(centerHoriz)
	{
		boxElement.style.left = confirmBoxPositionX+'px';
	}
	if (centerVert)
	{
		boxElement.style.top = confirmBoxPositionY+'px';
	}
}

// This function determines and returns the upper (vertical) scroll position in the browser window, depending on IE browser or non IE browser
function determineScrollPosTop()
{
	var aScrollPosTop;
	// determine scroll position
	if (self.pageYOffset) // all except Explorer
	{
		aScrollPosTop = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Standard
	{
		aScrollPosTop = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		aScrollPosTop = document.body.scrollTop;
	}
	return aScrollPosTop;
}
// This function determines and returns the left (horizontal) scroll position in the browser window, depending on IE browser or non IE browser
function determineScrollPosLeft()
{
	var aScrollPosLeft;
	// determine scroll position
	if (self.pageXOffset) // all except Explorer
	{
		aScrollPosLeft = self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Standard
	{
		aScrollPosLeft = document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		aScrollPosLeft = document.body.scrollLeft;
	}
	return aScrollPosLeft;
}
// This function determines and returns the inner height (viewport) of a browser window, depending on IE browser or non IE browser
function determineInnerHeight()
{
	if (self.innerHeight) // all except Explorer
	{
		aInnerHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Standard Mode
	{
		aInnerHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		aInnerHeight = document.body.clientHeight;
	}
	return aInnerHeight;
}
// This function determines and returns the inner height (viewport) of a browser window, depending on IE browser or non IE browser
function determineInnerWidth()
{
	if (self.InnerWidth) // all except Explorer
	{
		aInnerWidth = self.InnerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
		// Explorer 6 Standard Mode
	{
		aInnerWidth = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		aInnerWidth = document.body.clientWidth;
	}
	return aInnerWidth;
}
// CONVERTS ALL NON STATIC ANCESTOR ELEMENTS TO STATIC ONES
var originalElements = new Array();
var newElements = new Array();
function convertNonStaticAncestors(htmlElement)
{	
	var parentElement = htmlElement.parentNode;
	var posValue = getCurrentStyle(parentElement, "position") ;
	if (posValue != 'static')
	{
		originalElements.push(posValue);
		parentElement.style.position = 'static';
		newElements.push(parentElement);
	}
	if (htmlElement.parentNode.nodeName == 'HTML') return;
	convertNonStaticAncestors(parentElement);
}
function revertConvertedAncestors (htmlElement)
{
	for (var i=0; i<newElements.length; i++)
	{
		newElements[i].style.position = originalElements[i];
	}
	originalElements = new Array();
	newElements = new Array();
}

function getCurrentStyle(htmlElement, property) 
{
	if (!htmlElement || !property) return;
	if (window.getComputedStyle) 
	{
		return window.getComputedStyle(htmlElement, null)[property];
	}
	else if (htmlElement.currentStyle)
	{
		return htmlElement.currentStyle[property];
	}
	return undefined; // Oder was auch immer beliebt.
}

function alertMissingId(aId)
{
	// uncomment the following line for development time in order to get a warning if a ID does not exist
	// alert("ERROR: The ID \"" + aId + "\" does not exist. Please specify a valid ID.");
}

//  -----   feel free to use this stuff   ---   play with it   ---   copy as You  like   -----
//  -----   but leave this authors 2 lines untouched: pseliger@gmx.net [september 2007]  -----
//
Array.indexOf = function (obj, objLookingFor, idx) {var k,i=-1,l=(((obj instanceof Array)||((typeof obj.length=="number")&&((typeof obj.item=="function")||(typeof obj.item=="object")||(typeof obj.item=="string")||(obj instanceof window.NodeList)||(obj instanceof window.HTMLCollection))))?(obj.length):(0));idx=((idx&&isNaN(Number(idx)))?(parseInt(Number(idx),10)):(0));idx=((idx<0)?(Math.max(0,(l+idx))):(idx));for(k=idx;k<l;++k){if((obj[k]||obj.item(k))===objLookingFor){i=k;break;}}return i;};
Array.contains = function (obj, objLookingFor) {return(Array.indexOf(obj,objLookingFor)>=0);};
function trimString(aString)
{
	// trim left side of the string
	while (aString.substring(0,1) == ' ')
	{
		aString = aString.substring(1, aString.length);
	}
	// trim right side of the string
	while (aString.substring(aString.length-1, aString.length) == ' ')
	{
		aString = aString.substring(0,aString.length-1);
	}
	return aString;
}



