/**************************************************/
/*START: Fix IE 6 Text Selection Bug */
if (window.createPopup && document.compatMode && document.compatMode=="CSS1Compat") {
	document.onreadystatechange = function fixIE6AbsPos()
	{
		if( !document.body ) {
			return;
		}
		if( document.readyState == "complete" ) {
			document.body.style.height = document.documentElement.scrollHeight + 'px';
		}
	}
}
/*END: Fix IE 6 Text Selection Bug */
/**************************************************/

/**************************************************/
/*START: Functions for displaying and hiding DIVs */

function showhidediv(div) {
	if (document.getElementById(div).style.display == 'none')
	{
		showdiv(div);
	}
	else
	{
		hidediv(div);
	}
}

function showdiv (div) 
{
	if (document.getElementById)
	{ // DOM3 = IE5, NS6
		document.getElementById(div).style.display = 'block';
	}
}

function hidediv(div)
{
	//alert('hidediv');
	if (document.getElementById)
	{ // DOM3 = IE5, NS6
		document.getElementById(div).style.display = 'none';
	}
}

/*END: Functions for displaying and hiding DIVs */
/**************************************************/


/**************************************************/
/*START: Functions to open popups */

function openPopup(url, popupName, width, height, posX, posY)
{
	var pos = getWindowPos(width, height, posX, posY);
	if (pos != undefined)
	{
		posX = pos[0];
		posY = pos[1];
	}
	popupwindow = window.open(url, popupName, 'width='+width+', height='+height+', toolbar=no, menubar=no, scrollbars=yes, resizable=no, top='+posY+', left='+posX);
	if (popupwindow)
	{
		popupwindow.focus();
	}
}

function openGallery(url, popupName, width, height, posX, posY)
{
	var pos = getWindowPos(width, height, posX, posY);
	if (pos != undefined)
	{
		posX = pos[0];
		posY = pos[1];
	}
	
	iAdjustment = navigator.appVersion.match(/MSIE/) ? 29 : 49;
	popupwindow = window.open(url, popupName, 'width='+width+', height='+height+', toolbar=no, menubar=no, scrollbars=yes, resizable=yes, top='+posY+', left='+posX);
	if (popupwindow)
	{
		popupwindow.resizeTo(width,height+iAdjustment);
		popupwindow.focus();
	}
}

function getWindowPos(width, height, posX, posY)
{
	var standardX = 255;
	var standardY = 70;
	
	if(posX == undefined)
	{
		if(window.screenLeft != undefined){
			//for microsoft ie
			if(window.screenLeft + standardX + width < screen.width - 100)
			{
				posX = window.screenLeft + standardX;
			} 
			else 
			{
				posX = screenLeft - 50;
			}
			
			if(window.screenTop + standardY + height < screen.height - 30)
			{
				posY = window.screenTop + standardY;
			}
			else
			{
				posY = standardY;
			}
		}
		else 
		{
			if(window.screenX + standardX + width < screen.width - 100)
			{
				posX = window.screenX + standardX;
			} 
			else 
			{
				posX = screenX - 50;
			}
			
			if(window.screenY + standardY + height < screen.height - 30)
			{
				posY = window.screenY + standardY;
			}
			else
			{
				posY = standardY;
			}
		}
	return [posX,posY]
	}
}
/*END: Functions to open popups */
/**************************************************/
/**************************************************/
/*START: Function to open new Window */

function openMaxWidthWindow(location, width, height, name, toolbar, scrollbars, status, resizable, otherAttrib)
{	
	//openMaxWidthWindow object properties
	this.location=(location != null) ? location:'#';
	this.width=(width != null) ? width: 1024;
	this.height=(height != null) ? height: 768;
	this.name=(name != null) ? name:'neu';
	this.locationbar='yes';
	this.menubar='yes';
	this.toolbar=(toolbar != null) ? toolbar:'1';
	this.scrollbars=(scrollbars != null) ? scrollbars:'1';
	this.status=(status != null) ? status:'1';
	this.resizable=(resizable != null) ? resizable:'yes';
	this.leftPos='0';
	this.topPos='0';

	//open method opens a window maximized the screen
	this.open=function()
	{
		this.name = window.open(this.location, this.name, 'location='+this.locationbar+', menubar='+this.menubar+', toolbar='+this.toolbar+',status='+this.status+',scrollbars='+this.scrollbars+',resizable='+this.resizable+','+this.otherAttrib+',left='+this.leftPos+',top='+this.topPos+',height='+this.height+',width='+this.width);
	}
}

function openNewWin(pUrl)
{
	if (navigator.appVersion.match(/MSIE/))
	{	
		return true;
	}
	var oPDF = new openMaxWidthWindow(pUrl, null, null, '');
	oPDF.open();
	return false;
}
/*END: Function to open new Window */
/**************************************************/
/**************************************************/
/*START: Functions to close popups */
function doClose()
{
	if (window.opener)
	{
		window.opener.location.reload();
	}
	window.close();
}


function doCloseReload(path)
{
	if (window.opener && path)
	{
		window.opener.location.href=path;
	}
	window.close();
}
/*END: Functions to close popups */
/**************************************************/

function doPrint(servletSrc)
{
	window.print();
	var trackingImage = new Image();
	trackingImage.src = servletSrc; 
}

/**************************************************/
/*START: Function to rewrite pwd-field */
function handlePassword()
{
	var inputPwd = document.getElementById('login-password');
	if (inputPwd) {
		var inputDummy = document.createElement("input");
		inputDummy.value ="Passwort";
		inputDummy.name="dummy";
		inputDummy.id="dummy";
		inputDummy.className = "input M";
		inputPwd.style.display = "none";
		inputPwd.parentNode.insertBefore(inputDummy,inputPwd.nextSibling);
		
		inputDummy.inputPwd = inputPwd;
		inputDummy.onfocus = function () {
			this.style.display="none";
			this.inputPwd.style.display="inline";
			this.inputPwd.value="";
			this.inputPwd.focus();
		}
		
		inputPwd.inputDummy = inputDummy;
		inputPwd.onblur = function () {
			if (this.value.length==0) {
				this.style.display="none";
				this.inputDummy.style.display="inline";
			}
		}
	}
}
/*END: Function to rewrite pwd-field */
/**************************************************/
/**************************************************/
/*START: helper functions */
function getWindowInnerSize()
{
	var objWin = new Object();

	if (self.innerHeight) {
	// all except Internet Explorer
		objWin.width = self.innerWidth;
		objWin.height = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
	// Explorer 6 Strict Mode
		objWin.width = document.documentElement.clientWidth;
		objWin.height = document.documentElement.clientHeight;
	} else if (document.body) {
	// other Explorers
		objWin.width = document.body.clientWidth;
		objWin.height = document.body.clientHeight;
	}
	return objWin;
}

function $() {
  var elements = new Array();
  
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }
  return elements;
}

function getPos(id)
{
	var obj = document.getElementById(id);
	var pos = {left:0, top:0};
	if(typeof obj.offsetLeft != 'undefined') {
		while (obj) {
			pos.left += obj.offsetLeft;
			pos.top += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else {
		pos.left = obj.left;
		pos.top = obj.top;
	}
	return pos;
}

function getHeight(element)
{
	element = $(element);
	return element.offsetHeight;
}

function setHeight(id, height)
{
	var obj = document.getElementById(id);
	obj.style.height = height + "px";
}
/*END: helper functions */
/**************************************************/

/**************************************************/
/*START: layout functions*/
function initLayout()
{
	var iWindowHeight = getWindowInnerSize().height;
	var iFooterHeight = ($("footer-zone")) ? getHeight("footer-zone") : 0;
	var iFooterTopPosition = ($("footer-zone")) ? getPos("footer-zone").top : 0;
	if ($("footer-position-placeholder")) {
		var iFooterPlaceholderHeight = getHeight("footer-position-placeholder");
		if (((iFooterTopPosition - iFooterPlaceholderHeight) + iFooterHeight) < iWindowHeight) {
			var height = iFooterPlaceholderHeight + (iWindowHeight-(iFooterTopPosition+ iFooterHeight));
			setHeight("footer-position-placeholder", height)
		} else {
			setHeight("footer-position-placeholder", 0)
		}
	}
}
/*END: layout functions*/
/**************************************************/


window.onload = function () {
	initLayout();
	handlePassword();
}

window.onresize = function () {
	initLayout();
}
