// Application: cemex_de
// Wenn DEBUGGING = true, dann wird durch openCenteredDialog() im IE nicht mehr der
// modale Dialog, sondern ein herkömmliches Browserfenster geöffnet, da beim modalen
// Dialog das Kontextmenü nicht verfügbar ist.
// Ausserdem werden dann immer Menü-, Symbol-, Adress- und Statusleiste dargestellt.

var DEBUGGING = false;



/* Öffnet ein neues Fenster mit den übergebenen Eigenschaften */
function openWindow(url, name, width, height, scrollbar, resizable, statusbar) {
	if(!url) return;
	if(!scrollbar) scrollbar = "yes";
	if(!resizable) resizable = "yes";
	if(!statusbar) statusbar = "yes";
	if(width == null || width == "")	width = screen.availWidth;
	if(height == null || height == "")	height = screen.availHeight;
	if (width > screen.availWidth-10) width = screen.availWidth-10;
	if (height > screen.availHeight-30) height = screen.availHeight-30;
	var left = Math.round((screen.availWidth-10 - width) / 2);
	var top = Math.round((screen.availHeight-30 - height) / 2);
	var options = 'width='+width+',height='+height+',';
		options += 'screenX='+left+',screenY='+top+',left='+left+',top='+top+',';
		options += 'scrollbars=' + scrollbar + ',';
		options += 'resizable=' + resizable + ',';
		options += 'status=' + statusbar;
	//url = escape(url);
	newWindow = window.open(url, name, options);
	if(newWindow) {
		newWindow.focus();
	} else {
		alert("Das Fenster konnte nicht geöffnet werden.\nVermutlich ist bei Ihrem Browser ein Popup-Blocker aktiviert.\nBitte deaktivieren Sie ggf. Ihren Popup-Blocker für die Website:\n\n" + window.location.hostname);
	}
}

var calendarField = null;
var popupWindow = null;

function popUpCalendar(thisField, contextpath) {
	if(!contextpath || contextpath == "") {
		contextpath = "../";
	}
	if(!strEndsWith(contextpath, "/")) {
		contextpath += "/";
	}
	closePopupWindow();
	calendarField = thisField;
	popupWindow = openCenteredDialog(contextpath + 'js/calendar/calendar.html', 'calpopup', 240, 180, false, true, false, window);
//	popupWindow = openCenteredDialog(contextpath + 'js/calendar/calendar.html', 'calpopup', 264, 208, false, true, false, window);
}

function closePopupWindow() {
	if(popupWindow!=null && popupWindow.close) {
		popupWindow.close();
	}
	popupWindow = null;
}

window.onunload = closePopupWindow;


/**
 * Prüft den übergebenen Wert auf korrekte Zahl und kürzt diese ggf
 * auf übergebene Anzahl von Nachkommastellen. Prüft dabei ggf auch auf den
 * übergebenen Zahlenbereich. Rückgabe ist der ggf. korrigierte/formatierte
 * Wert. Ist der übergebene Wert keine gültige Zahl, dann wird null zurückgegeben.
 */
function getValidNumber(source, decimals, minVal, maxVal) {
	
	// Wert aus Formularfeld holen:
	var value = ""+source;
	
	// Format "veredeln":
	value = value.replace(/ /g, "");
	value = value.replace(/\./g, "");
	if(value=="") {
		value = "0";
	}
	
	// JS rechnet mit "." als Dezimaltrennzeichen:
	value = value.replace(/,/g, ".");
	
	// Falls unbrauchbarer Wert: und Tschüss
	if(isNaN(value)) {
		return null;
	}
	
	// Dezimalstellenangabe gültig machen:
	if(isNaN(decimals)) {
		decimals = 0;
	}
	decimals = parseInt(decimals, 10);
	
	// Überzählige Nachkommastellen entfernen:
	var floatVal = parseFloat(value);
	var testFactor = Math.pow(10, decimals);
	floatVal *= testFactor;
	floatVal = parseFloat(Math.round(floatVal));
	floatVal = parseFloat(getIntegerLimited(floatVal));
	floatVal /= testFactor;
	
	// Min- und Max-Werte berücksichtigen:
	if(!isNaN(minVal)) {
		minVal = parseFloat(minVal);
		if(floatVal < minVal) {
			floatVal = minVal;
		}
	}
	if(!isNaN(maxVal)) {
		maxVal = parseFloat(maxVal);
		if(floatVal > maxVal) {
			floatVal = maxVal;
		}
	}
	
	// "," als Dezimaltrennzeichen wiederherstellen
	value = (""+floatVal).replace(".", ",");
	
	// fehlende Nachkommastellen mit Nullen auffüllen
	if(decimals > 0) {
		if(value.indexOf(",") < 0) {
			value += ",";
		}
		while((value.length - value.lastIndexOf(",") - 1) < decimals) {
			value += "0";
		}
	}
	return value;
}

/**
 * Konvertiert den übergebenen Wert in eine Ganzzahl. Falls der Wert nicht als
 * Zahl interpretierbar ist, wird defaultVal zurückgegeben. Falls der Wert 
 * ausserhalb der übergebenen Grenzen liegt, wird er auf die nächstliegende
 * Grenze geändert.
 */
function getValidInt(source, minVal, maxVal, defaultVal) {
	if(isNaN(source)) {
		return defaultVal;
	}
	var num = parseInt(source, 10);
	if(num+"" == "NaN") { // wird trotz Prüfung mit isNaN() manchmal zurückgegeben
		return defaultVal;
	}
	if(num < minVal) {
		return minVal;
	}
	if(num > maxVal) {
		return maxVal;
	}
	return num;
}

/**
 * Gibt ein auf den Integer-Wertebereich begrenztes Ergebnis zurück.
 */
function getIntegerLimited(srcInt) {
	if(srcInt > 2147483647) {
		srcInt = 2147483647;
	} else if(srcInt < -2147483648) {
		srcInt = -2147483648;
	}
	return srcInt;
}

/**
 * Prüft die Eingabe auf maxLength Länge und beschneidet sie ggf mit entsprechender
 * Fehlermeldung.
 */
function checkLength(thisField, maxLength) {
	maxLength = parseInt(maxLength, 10);
	var value = ""+thisField.value;
	if(value.length > maxLength) {
		thisField.value = value.substring(0, maxLength);
		alert("Eingabe musste auf die maximale Länge von "+maxLength+" Zeichen abgeschnitten werden.");
	}
	return true;
}

/**
 * Prüft die Eingabe auf korrekte Zeitangabe. Format: "HH:mm"
 */
function checkTimeHourMinute(thisField, allowEmpty) {
	
	if(isEmpty(thisField.value)) {
		if(allowEmpty == true) {
			thisField.value = "";
			return true;
		} else {
			return false;
		}
	}
	
	var tokensTime = trim(thisField.value+"").split(":");
	if(tokensTime.length < 1) {
		return false;
	}
	var hours = getValidNumber(tokensTime[0], 0, 0, 23);
	if(hours == null) {
		return false;
	}
	if(hours < 10) {
		hours = "0" + hours;
	}
	
	var minutes = "00";
	if(tokensTime.length > 1) {
		minutes = getValidNumber(tokensTime[1], 0, 0, 59);
		if(minutes == null) {
			return false;
		}
		if(minutes < 10) {
			minutes = "0" + minutes;
		}
	}
	
	// Ergebnis zurückschreiben
	thisField.value = hours + ":" + minutes;
	return true;
}

/**
 * Konvertiert den übergebenen Datumsstring in ein Date-Objekt. Mögliche Formate:
 * "dd.MM.yyyy", "dd.MM.yyyy HH:mm", "dd.MM.yyyy HH:mm:ss"
 */
function parseDate(source) {
	var tokensAll = trim(source+"").split(" ");
	if(tokensAll.length < 1) {
		return null;
	}
	var tokensDate = tokensAll[0].split(".");
	if(tokensDate.length < 3) {
		return null;
	}
	for(var i=0; i<3; i++) {
		if(isNaN(tokensDate[i])) {
			return null;
		}
	}
	var date = new Date();
	date.setDate(getValidInt(tokensDate[0], 1, 31, 1));
	date.setMonth(getValidInt(tokensDate[1], 1, 12, 1) - 1);
	date.setYear(parseInt(tokensDate[2], 10));
	if(tokensAll.length < 2) {
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
	} else {
		var tokensTime = tokensAll[1].split(":");
		date.setHours(tokensTime.length>0 ? getValidInt(tokensTime[0], 0, 23, 0) : 0);
		date.setMinutes(tokensTime.length>1 ? getValidInt(tokensTime[1], 0, 59, 0) : 0);
		date.setSeconds(tokensTime.length>2 ? getValidInt(tokensTime[2], 0, 59, 0) : 0);
	}
	date.setMilliseconds(0);
	return date;
}



/************************************************************************************************************/
/***************************************** Std Library ******************************************************/
/************************************************************************************************************/


function ListOption(szText,szValue)
	{
	this.text = szText;
	this.value = szValue;
	}

function opt(szText,szValue)
	{
	return new ListOption(""+szText,""+szValue);
	}

// ******** list functions **************************

function setListItem(listControl, newText, index)
	{ 
	if(index<0)index = listControl.selectedIndex;
	if(index<0)return;
	
	listControl.options[index].text = newText;
	}

function deleteListItem(listControl, index)
	{ 
	if(index<0)index = listControl.selectedIndex;
	if(index<0)return;
	
	if(document.all)
		{
		arrOpts = new Array();
		for(i=0; i<listControl.length; i++)
			{
			if(i != index)
				{
				arrOpts = arrOpts.concat(new Array(listControl.options[i]));
				}
			}
		document.all[listControl.name].innerHTML = "";
		for(i=0; i<arrOpts.length; i++)
			{
			listControl.options[i]=arrOpts[i];
			}
		}
	else{
		listControl.options[index] = null;
		//history.go(0);
		}
	}

function getListText(listControl)
	{
	return listControl.options[listControl.selectedIndex].text;
	}

function getListValue(listControl)
	{
	return listControl.options[listControl.selectedIndex].value;
	}

function setListValue(listControl, value)
	{
	listControl.selectedIndex = findListValue(listControl, value);
	}

function selectListText(listControl, text)
	{
	listControl.selectedIndex = findListText(listControl, text);
	}

function selectMultiselectValues(multiselectControl, values, separator)
	{
	values = separator + values + separator;
	for(i = 0; i < multiselectControl.length; i++)
		{
		multiselectControl.options[i].selected = (values.indexOf(separator + multiselectControl.options[i].value + separator) >= 0);
		}
	}

function selectMultiselectTexts(multiselectControl, texts, separator)
	{
	texts = separator + texts + separator;
	for(i = 0; i < multiselectControl.length; i++)
		{
		multiselectControl.options[i].selected = (texts.indexOf(separator + multiselectControl.options[i].text + separator) >= 0);
		}
	}

function getMultiselectValues(multiselectControl, separator)
	{
	var values = '';
	for(i = 0; i < multiselectControl.length; i++)
		{
		if(multiselectControl.options[i].selected)
			values += separator + multiselectControl.options[i].value;
		}
	if(values.length > 0)
		values = values.substring(separator.length);
	return values;
	}

function insertListItem(listControl, newOption, before)
	{
	if(before<0)before = listControl.selectedIndex;
	if(before<0)before = listControl.length;
	if(before>=listControl.length)
		{
		listControl.options[listControl.length] = newOption;
		return;
		}
	
	var arrOpts = new Array();
	var arrVals = new Array();
	
	for(i=0; i<listControl.length; i++)
		{
		if(before == i)
			{
			arrOpts = arrOpts.concat(new Array(newOption.text));
			arrVals = arrVals.concat(new Array(newOption.value));
			}
		arrOpts = arrOpts.concat(new Array(listControl.options[i].text));
		arrVals = arrVals.concat(new Array(listControl.options[i].value));
		}
	
	clearList(listControl);
	
	for(i=0; i < arrOpts.length; i++)
		{
		var newOption = new Option(arrOpts[i], arrVals[i]);
		listControl.options[i] = newOption;
		}
	}

function clearList(listControl)
	{
	if(document.all)
		{
		document.all[listControl.name].innerHTML = "";
		}
	else{
		for(i=listControl.length; i>0; i--)
			{
			listControl.options[i-1] = null;
			}
		}
	}

function findListValue(listControl, value)
	{
	for(i = 0; i < listControl.length; i++)
		{
		if(listControl.options[i].value == value)
			return i;
		}
	return -1;
	}

function findListText(listControl, text)
	{
	for(i = 0; i < listControl.length; i++)
		{
		if(listControl.options[i].text == text)
			return i;
		}
	return -1;
	}

// ******** radio control functions **************************

function getRadioValue(radioControl)
	{
	if(radioControl.length)
		{
		for(var i=0; i<radioControl.length; i++)
			{
			if(radioControl[i].checked)
				return radioControl[i].value;
			}
		}
	else{
		if(radioControl.checked)
			return radioControl.value;
		}
	return '';
	}

function setRadioValue(radioControl, newValue)
	{
	for(var i=0; i<radioControl.length; i++)
		{
		radioControl[i].checked = (radioControl[i].value == newValue);
		}
	}

// ******** array functions **************************

function arrayDelete(arr, first, last)
	{
	var retArr = new Array();
	var arrLength = arr.length;
	if(first<0)first=0;
	if(first>=arrLength)first=ArrLength-1;
	if(last<0)last=ArrLength-1;
	if(last>=arrLength)last=ArrLength-1;
	
	if(first>0)retArr = arr.slice(0, first);
	if(last>=first)retArr = retArr.concat(arr.slice(last+1, arrLength));
	return retArr;
	}

function arrInsert(arr, newValue, before)
	{
	var arrLength = arr.length;
	if(!isNaN(newValue))
		newValue = String(newValue);
	if(before<0 || before>=arrLength)
		return arr.concat(new Array(newValue));
	
	var retArr = new Array();
	if(before>0)
		retArr = arr.slice(0, before);
	retArr = retArr.concat(new Array(newValue));
	retArr = retArr.concat(arr.slice(before, arrLength));
	return retArr;
	}

// ******** string functions **************************

function isWhiteSpace(c)
	{
	return ("\t\n\f\r ".indexOf((c+"-").charAt(0))>=0);
	}

function rTrim(str) 
	{
	str = ""+str;
	for(i=str.length-1; i>=0; i--) 
		{
		if(!isWhiteSpace(str.charAt(i)))
			return str.substr(0,i+1);
		}
	return "";
	}

function lTrim(str) 
	{
	str = ""+str;
	for(i=0; i<str.length; i++) 
		{
		if(!isWhiteSpace(str.charAt(i)))
			return str.substr(i);
		}
	return "";
	}

function trim(str) 
	{
	return lTrim(rTrim(str));
	}
	
function isEmpty(str) 
	{
	if(str==null)return true;
	str = ''+str;
	for(var i=str.length-1; i>=0; i--) 
		{
		if(!isWhiteSpace(str.charAt(i)))
			return false;
		}
	return true;
	}

function startsWith(str, prefix) {
	if(str && prefix) {
		return (str+"").indexOf(prefix+"") == 0;
	}
	return false;
}

function endsWith(str, suffix) {
	if(str && suffix) {
		var pos = (str+"").lastIndexOf(suffix+"");
		return pos >= 0 && pos == (str+"").length - (suffix+"").length;
	}
	return false;
}

function removePrefix(str, prefix) {
	if(startsWith(str, prefix)) {
		return str.substring((prefix+"").length);
	}
	return str;
}

function removeSuffix(str, suffix) {
	if(endsWith(str, suffix)) {
		return str.substring(0, (str+"").length - (suffix+"").length);
	}
	return str;
}

function strReplace(strSource, strWhat, strWith, forwardOnly)
	{
	if(!(forwardOnly==false))
		forwardOnly=true;
	while(true)
		{
		var strDest = "";
		var start = -1;
		var end = 0;
		var found = false;
		while((start = strSource.indexOf(strWhat, start+1)) >= 0)
			{
			found = true;
			strDest += strSource.substring(end, start) + strWith;
			end = start + strWhat.length;
			start = end-1;
			}
		strDest += strSource.substring(end);
		if(forwardOnly || !found)break;
		strSource = strDest;
		}
	return strDest;
	}

function strListEquals(strList1, strList2, separator) {
	strList1 = ""+strList1;
	strList2 = ""+strList2;
	separator = ""+separator;
	
	if(!strStartsWith(strList1, separator))strList1 = separator + strList1;
	if(!strStartsWith(strList2, separator))strList2 = separator + strList2;
	if(!strEndsWith(strList1, separator))strList1 += separator;
	if(!strEndsWith(strList2, separator))strList2 += separator;
	
	if(strList1.length!=strList2.length)
		return false;
	
	var sepLegth = separator.length;
	var itemStart = strList1.indexOf(separator);
	while(true) {
		var itemEnd = strList1.indexOf(separator, itemStart+sepLegth);
		if(itemEnd<0)
			return true;
		var item = strList1.substring(itemStart, itemEnd) + separator;
		if(strList2.indexOf(item)<0)
			return false;
		itemStart = itemEnd;
	}
	return true; // nur der form halber, wird eigentlich eh nie erreicht.
}

function strStartsWith(str, prefix) {
	str = ""+str;
	prefix = ""+prefix;
	return str.substring(0, prefix.length) == prefix;
}

function strEndsWith(str, suffix) {
	str = ""+str;
	suffix = ""+suffix;
	var start = str.length - suffix.length;
	if(start<0)return false;
	return str.substring(start) == suffix;
}

	
// ******** form functions **************************

function checkRequiredControl(ctrl, label)
	{
	if(!label)label='';
	if(ctrl.options)
		{
		for(var i=0; i<ctrl.length; i++)
			{
			var option = ctrl.options[i];
			if(option.selected && !isEmpty(option.text))
				return true;
			}
		if(label!='')
			alert("Bitte wählen Sie in Feld '" + label + "' einen Wert aus.");
		ctrl.focus();
		return false;
		}
	else{
		var value = trim(ctrl.value);
		ctrl.value = value;
		if(value == "")
			{
			if(label!='')
				alert("Bitte geben Sie in Feld '" + label + "' einen Wert ein.");
			ctrl.focus();
			return false;
			}
		return true;
		}
	}

function checkImgFile(ctrl, label)
	{
	if(!label)label='';
	var value = trim(ctrl.value);
	ctrl.value = value;
	if(value == '')return true;
	var pos = value.lastIndexOf('.');
	if(pos>0)
		{
		var ext = value.substring(pos+1).toLowerCase();
		if(ext!='jpg' && ext!='jpeg' && ext!='gif')pos=0;
		}
	if(pos>0)return true;
	if(label!='')
		alert("##The file type entered in field## '" + label + "' ##is not allowed.\n Only files with extension '.jpg', '.jpeg' & '.gif' are available.##");
	ctrl.focus();
	return false;
	}

function checkInputLength(ctrl, label, maxsize) {
	var value = trim(ctrl.value);
	ctrl.value = value;
	
	l=value.length;
	if(l<=maxsize)return true;
	alert("##The input in field## '" + label + "' ##is limited to## "+ maxsize +" ##characters. \nYour current input consists of## " + l + " ##characters##.");
	ctrl.focus();
	return false;
}

// ******** general functions **************************

function noop(){}

function breakpoint() // creates an error once for every call
	{
	dummy = new Array('');
	var a=2;
	var b=dummy[--a].length;
	}

function getBool(option, defaultVal)
	{
	if(defaultVal != true)
		defaultVal = false;
	
	if(option==null)return defaultVal;
	
	if(option==true)return true;
	if(option==false)return false;
	
	if(!isNaN(option))
		{
		var intOption = parseInt(option);
		if(intOption!=0)return true;
		if(intOption==0)return false;
		}
	
	option = "" + option; // convert to string
	option = option.toLowerCase();
	
	if(option=='ns')
		{
		if(document.layers)return true;
		return false;
		}
	if(option=='ie')
		{
		if(document.all)return true;
		return false;
		}
	
	if(option=='yes')return true;
	if(option=='true')return true;
	if(option=='on')return true;
	if(option=='y')return true;
	if(option=='j')return true;
	if(option=='no')return false;
	if(option=='false')return false;
	if(option=='off')return false;
	if(option=='n')return false;
	
	return defaultVal;
	}

function getBoolString(option, defaultVal)
	{
	if(getBool(option, defaultVal))return 'yes';
	return 'no';
	}

function openCenteredDialog(url, name, width, height, showScrollbars, isResizable, showStatusbar, openerWnd, isModeless, asNormalWindow)
	{
	if (width>screen.availWidth-10)width=screen.availWidth-10;
	if (height>screen.availHeight-30)height=screen.availHeight-30;
	var left = Math.round((screen.availWidth-10-width)/2);
	var top = Math.round((screen.availHeight-30-height)/2);
	var newWindow=null;
	//if(modal!=false)modal=true;
	if((getBool(isModeless) ? window.showModelessDialog : window.showModalDialog) && !getBool(asNormalWindow) && !DEBUGGING)
		{
		// Firefox unterstuetzt zwar offiziell die Option 'center', kriegts aber in Wirklichkeit nicht gebacken -> left+top explizit setzen.
		var options = 'dialogWidth: ' + width + 'px; dialogHeight: ' + height + 'px; center: yes; dialogleft: '+left+'; dialogtop: '+top+'; edge: raised; help: no;'
		options += ' scroll: ' + getBoolString(showScrollbars) + ';';
		options += ' resizable: ' + getBoolString(isResizable, true) + ';';
		options += ' status: ' + getBoolString(showStatusbar) + ';';
		if(!openerWnd)openerWnd = window.top;
		if(getBool(isModeless))
			// Als Opener-Window für Modeless-Dialog muss ein Fenster verwendet werden, das 
			// das nicht geschlossen wird, während der Dialog offen bleiben soll.
			newWindow = openerWnd.showModelessDialog(url, openerWnd, options);
		else
			openerWnd.showModalDialog(url, openerWnd, options);
		}
	else{
		var options = 'width='+width+',height='+height+',screenX='+left+',screenY='+top+',left='+left+',top='+top+',';
		if(DEBUGGING)
			options += 'toolbar=yes,location=yes,directories=no,menubar=yes,';
		else
			options += 'toolbar=no,location=no,directories=no,menubar=no,';
		
		showScrollbars = getBoolString(showScrollbars);
		if(showScrollbars=='yes')showScrollbars='auto';
		options += 'scrollbars=' + showScrollbars + ',';
		options += 'resizable=' + getBoolString(isResizable) + ',';
		if(DEBUGGING)
			options += 'status=yes';
		else
			options += 'status=' + getBoolString(showStatusbar);
		if(name==null || !name)name='dlg'+ parseInt(Math.random() * 1000000);
		//alert(options);
		//alert(url + " name: " + name);
		if(!openerWnd)openerWnd = window;
		newWindow = openerWnd.open(url, name, options);
		if(newWindow)
			{
			newWindow.focus();
			}
		else{
			alert("Das Fenster konnte nicht geöffnet werden.\nVermutlich ist bei Ihrem Browser ein Popup-Blocker aktiviert.\nBitte deaktivieren Sie ggf. Ihren Popup-Blocker für die Website:\n\n" + window.location.hostname);
			}
		}
	return newWindow;
	}

function getOpener()
	{
	if(top.dialogArguments)
		return top.dialogArguments;
	if(top.opener)
		return top.opener;
	return null;
	}

var windowError = null;
function showErrorWindow()
	{
	closeErrorWindow();
	var wnd = openCenteredDialog('/xRed/dialog.jsp?&dlgtitle=Error', '', 600, 350, true, false, false);
	if(wnd!=null)
		{
		top.windowError = wnd;
		}
	}

function closeErrorWindow()
	{
	if(top.windowError!=null && top.windowError.close)
		top.windowError.close();
	top.windowError = null;
	}

// ******** layer functions **************************

function writeContent(targetId, content) {
	if(document.all) {
		document.all[targetId].innerHTML = content;
	} else if(document.layers) {
		var layerdoc = document.layers[targetId].document;
		layerdoc.open();
		layerdoc.write('<nobr>'+content+'</nobr>');
		layerdoc.close();
	} else if(document.getElementById) {
		var element = document.getElementById(targetId);
		if(element!=null) {
			element.firstChild.nodeValue = content;
		}
	}
}

function initNescapeLayer(name) {
	var newLayer = document.layers[name];
	var tempName = 'xredtemp_' + name;
	var tempImage = document.images[tempName];
	if(tempImage) {
		newLayer.pageX = tempImage.x;
		newLayer.pageY = tempImage.y;
	} else {
		var tempAnchor = document.anchors[tempName];
		if(tempAnchor) {
			newLayer.pageX = tempAnchor.x;
			newLayer.pageY = tempAnchor.y;
		} else {
			var tempLayer = document.layers[tempName];
			newLayer.pageX = tempLayer.pageX;
			newLayer.pageY = tempLayer.pageY;
			tempLayer.visibility = "hide";
		}
	}
	newLayer.visibility = 'show';
}


// ******** window size functions **************************



function resizeWindowToFitMin(srcWindow, isCentered, isScrollable) {
	var offset = isScrollable ? 20 : 0;
	if(document.all) {
		offset = 0;
	}
	
	var dX = offset + getPageWidth(srcWindow.document) - getInnerWidth(srcWindow);
	var dY = offset + getPageHeight(srcWindow.document) - getInnerHeight(srcWindow);
	
	if(dX < 0) {
		dX = 0;
	}
	if(dY < 0) {
		dY = 0;
	}
	if(dX > 20 || dY > 20) {
//		srcWindow.top.resizeBy(dX, dY);
		resizeWindowBy(srcWindow, dX, dY, isCentered);
	} else {
		srcWindow.scrollTo(800, 600);
		dX = getScrollOffsetX(srcWindow);
		dY = getScrollOffsetY(srcWindow);
//		alert("dX="+dX);
		if(dX > 0 || dY > 0) {
			srcWindow.scrollTo(0, 0);
//			srcWindow.top.resizeBy(dX, dY);
			resizeWindowBy(srcWindow, dX, dY, isCentered);
//			alert("dX="+getScrollOffsetX(srcWindow));
		}
	}
}

function resizeWindowBy(srcWindow, dX, dY, isCentered) {
	
	if(isCentered != true) {
		isCentered = false;
	}
	
	if(srcWindow.top.dialogArguments && document.all) { // nur im ModalDialog des IE, nicht Firefox
		isCentered = false; // Sonst nervige Effekte
		var dlgWidth = srcWindow.top.dialogWidth;
		var dlgHeight = srcWindow.top.dialogHeight;
		dlgWidth = (parseInt(removePx(dlgWidth), 10) + dX);
		dlgHeight = (parseInt(removePx(dlgHeight), 10) + dY);
		srcWindow.top.dialogWidth = dlgWidth + "px";
		srcWindow.top.dialogHeight = dlgHeight + "px";
		if(isCentered) {
			var left = Math.round((screen.availWidth-10-dlgWidth)/2);
			var top = Math.round((screen.availHeight-30-dlgHeight)/2);
			srcWindow.top.dialogLeft = left + "px";
			srcWindow.top.dialogTop = top + "px";
		}
		
	} else {
		srcWindow.top.resizeBy(dX, dY);
		if(isCentered) {
			if(srcWindow.top.outerHeight) {
				var left = Math.round((screen.availWidth-10-srcWindow.top.outerWidth)/2);
				var top = Math.round((screen.availHeight-30-srcWindow.top.outerHeight)/2);
				srcWindow.top.moveTo(left, top);
			} else {
				srcWindow.top.moveBy(Math.round(dX/-2), Math.round(dY/-2));
			}
		}
	}
}

function removePx(src) {
	if((""+src).indexOf("px") >= 0) {
		return (""+src).replace("px","");
	}
	return src;
}



/**
 * Inner width.
 * The inner dimensions of the window or frame.
 */
function getInnerWidth(srcWindow) {
	if (self.innerHeight) { // all except Explorer
		return srcWindow.innerWidth;
	} else if (srcWindow.document.documentElement && srcWindow.document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		return srcWindow.document.documentElement.clientWidth;
	} else if (srcWindow.document.body) { // other Explorers
		return srcWindow.document.body.clientWidth;
	}
}

/**
 * Inner height.
 * The inner dimensions of the window or frame.
 */
function getInnerHeight(srcWindow) {
	if (self.innerHeight) { // all except Explorer
		return srcWindow.innerHeight;
	} else if (srcWindow.document.documentElement && srcWindow.document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		return srcWindow.document.documentElement.clientHeight;
	} else if (srcWindow.document.body) { // other Explorers
		return srcWindow.document.body.clientHeight;
	}
}

/**
 * Scrolling offset X.
 * How much the page has scrolled to the right.
 */
function getScrollOffsetX(srcWindow) {
	if (srcWindow.pageYOffset) { // all except Explorer
		return srcWindow.pageXOffset;
	} else if (srcWindow.document.documentElement && srcWindow.document.documentElement.scrollTop) {
		// Explorer 6 Strict
		return srcWindow.document.documentElement.scrollLeft;
	} else if (srcWindow.document.body) { // all other Explorers
		return srcWindow.document.body.scrollLeft;
	}
}

/**
 * Scrolling offset Y.
 * How much the page has scrolled down.
 */
function getScrollOffsetY(srcWindow) {
	if (srcWindow.pageYOffset) { // all except Explorer
		return srcWindow.pageYOffset;
	} else if (srcWindow.document.documentElement && srcWindow.document.documentElement.scrollTop) {
		// Explorer 6 Strict
		return srcWindow.document.documentElement.scrollTop;
	} else if (srcWindow.document.body) { // all other Explorers
		return srcWindow.document.body.scrollTop;
	}
}

/**
 * Page width.
 * The width of the total page (usually the body element).
 * This is a tricky one, some browsers require scrollWidth, others offsetWidth, but all browsers 
 * support both properties. Therefore I see which property has the larger value. This means the 
 * page width the script below gives is never smaller than the window width.
 */
function getPageWidth(srcDocument) {
	var test1 = srcDocument.body.scrollHeight;
	var test2 = srcDocument.body.offsetHeight
	if (test1 > test2) { // all but Explorer Mac
		return srcDocument.body.scrollWidth;
	} else { // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
		return srcDocument.body.offsetWidth;
	}
}

/**
 * Page height.
 * The height of the total page (usually the body element).
 * This is a tricky one, some browsers require scrollHeight, others offsetHeight, but all browsers 
 * support both properties. Therefore I see which property has the larger value. This means the 
 * page height the script below gives is never smaller than the window height.
 */
function getPageHeight(srcDocument) {
	var test1 = srcDocument.body.scrollHeight;
	var test2 = srcDocument.body.offsetHeight
	if (test1 > test2) { // all but Explorer Mac
		return srcDocument.body.scrollHeight;
	} else { // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
		return srcDocument.body.offsetHeight;
	}
}



// ******** tree functions **************************

function getScrollTop()
	{
	if(window.pageYOffset)
		return window.pageYOffset;
	if (document.body && document.body.scrollTop)
		return document.body.scrollTop;
	return 0;
	}

function getScrollLeft()
	{
	if(window.pageXOffset)
		return window.pageXOffset;
	if (document.body && document.body.scrollLeft)
		return document.body.scrollLeft;
	return 0;
	}

function reloadTree(link, treeWindow)
	{
	if(!treeWindow)
		treeWindow = window;
	
	var y = getScrollTop();
	if(y > 0)
		link += "&offset=" + y;
	window.location.replace(link);
	}

var folderSelectorWindow = null;

function openFolderSelector(section, root, active, title, parameters)
	{
	var url = '/xRed/dialog.jsp?section=' + section + '&rootid=' + root + '&operation=3' // 3 == TreeContent.OPERATION_MOVE
		+ '&dlgtitle=' + title + '&dlgscroll=auto';
	if(active)
		url += '&active=' + active;
	if(parameters)
		url += '&' + parameters;
	
	folderSelectorWindow = openCenteredDialog(url, '', 500, 500, true, 'ie', false, window);
	return folderSelectorWindow;
	}

function closeFolderSelector()
	{
	if(folderSelectorWindow && folderSelectorWindow.close)
		folderSelectorWindow.close();
	}

var multiFolderSelectorWindow = null;

function openMultiFolderSelector(parameters)
	{
	var url = '/xRed/dialog/folderselect/dialog.jsp';
	if(parameters)
		url += '?' + parameters;
	multiFolderSelectorWindow = openCenteredDialog(url, '', 600, 700, true, 'ie', false, window);
	return multiFolderSelectorWindow;
	}

function closeMultiFolderSelector()
	{
	if(multiFolderSelectorWindow && multiFolderSelectorWindow.close)
		multiFolderSelectorWindow.close();
	multiFolderSelectorWindow = null;
	}



// ******** input-field functions **************************

var isOpera = navigator.userAgent.toLowerCase().indexOf("opera") >= 0;

function isAvailableLocalPreview() {
	if(navigator.userAgent.toLowerCase().indexOf("opera") < 0 && document.all) {
		if(navigator.appVersion.indexOf("MSIE 4.") >= 0 || navigator.appVersion.indexOf("MSIE 5.") >= 0 || navigator.appVersion.indexOf("MSIE 6.") >= 0) {
			return true;
		}
	}
	return false;
}


var fieldImagesSaved = new Array();
var fieldImagesCurrent = new Array();


function updateFieldImage(thisField, fieldImageEmpty) {
	var fieldId = thisField.id;
	var filename = trim(thisField.value);
	if(filename != fieldImagesCurrent[fieldId] && isAvailableLocalPreview()) {
		if(filename == "") {
			filename = fieldImagesSaved[fieldId];
		}
		var imgPreview = document.getElementById(fieldId + "-preview");
		if(imgPreview != null) {
			var tdPreview = document.getElementById(fieldId + "-tdpreview");
			var tdEmptytext = document.getElementById(fieldId + "-tdemptytext");
			if(tdPreview != null && tdEmptytext != null) {
				tdPreview.style.display = (filename == "" ? "none" : "");
				tdEmptytext.style.display = (filename == "" ? "" : "none");
			}
			imgPreview.style.border = (filename == "" ? "0px" : "1px solid");
			if(filename == "") {
				filename = fieldImageEmpty;
			}
			imgPreview.style.visibility = (filename == "" ? "hidden" : "visible");
			imgPreview.src = filename;
		}
	}
	fieldImagesCurrent[fieldId] = filename;
}

function deleteFieldFile(fieldId, fieldImageEmpty, confirmText) {
	if(!confirmText || confirmText == "") {
		confirmText = "Wollen Sie die Datei wirklich löschen?";
	}
	if(!confirm(confirmText)) {
		return;
	}
	var delButton = document.getElementById(fieldId + "-delbutton");
	if(delButton != null) {
		delButton.style.visibility = "hidden";
	}
	var delField = document.getElementById(fieldId + "-delupload");
	if(delField != null) {
		delField.value = "true";
	}
	if(fieldImageEmpty == null) {
		fieldImageEmpty = "";
	}
	if(fieldImageEmpty != fieldImagesCurrent[fieldId]) {
		
		// Preview-Image nur dann entfernen, wenn im Upload-Feld nichts steht:
		if(!isAvailableLocalPreview() || trim(document.getElementById(fieldId).value) == "") {
			var imgPreview = document.getElementById(fieldId + "-preview");
			if(imgPreview != null) {
				var tdPreview = document.getElementById(fieldId + "-tdpreview");
				var tdEmptytext = document.getElementById(fieldId + "-tdemptytext");
				if(tdPreview != null && tdEmptytext != null) {
					tdPreview.style.display = "none";
					tdEmptytext.style.display = "";
				}
				if(fieldImageEmpty == '') {
					imgPreview.style.visibility = "hidden";
				} else {
					imgPreview.style.border = "0px";
				}
				imgPreview.src = fieldImageEmpty;
			}
			fieldImagesCurrent[fieldId] = fieldImageEmpty;
		}
		fieldImagesSaved[fieldId] = ""; // Durch Löschbutton so tun, als sei nichts gespeichert.
	}
	var aDownload = document.getElementById(fieldId + "-download");
	if(aDownload != null) {
		aDownload.style.visibility = "hidden";
	}
}
// Upload-Field: id = fieldId
// Delete-Hidden-Field: id = fieldId + "-delupload"
// Delete-Button: id = fieldId + "-delbutton"
// Download-Link: id = fieldId + "-download"
// Preview-Image: id = fieldId + "-preview"
// Saved Image: fieldImagesSaved[fieldId]
// Current Image: fieldImagesCurrent[fieldId]

// TD Preview-Image: id = fieldId + "-tdpreview"
// TD Preview-Text: id = fieldId + "-tdemptytext"



// ******** tab-form functions **************************


function displayElement(elementId, visible) {
	var element = document.getElementById(elementId);
	if(element != null) {
		element.style.display = (visible ? "" : "none");
	}
}

/** 
 * Aufschalten eines Formular-Panels
 * Aufruf in übergeordneter Seite.
 * 
 * @param idPanel des anzuzeigenden Panels
 */
function showForm(idPanel, idFieldTabOpened) {
	
	/** Verbegen aller Panele */
	hideForm();
	/** Anzeigen des Formular-Panels */
	displayElement("form_"+idPanel, true);
	/** Aktivieren des zugehörigen Reiters */
	var tabLabel = document.getElementById("tabs_"+idPanel);
	if(tabLabel) {
		tabLabel.className = "tabselect";
	}
	
	// Merken des geöffneten Reiters in hidden-Field:
	if(!idFieldTabOpened) {
		idFieldTabOpened = "form.main.tabOpened";
	}
	var fieldTabOpened = document.getElementById(idFieldTabOpened);
	if(fieldTabOpened != null) {
		fieldTabOpened.value = idPanel;
	}
}

/**
 * Verbergen aller Formular-Panele und deselektieren aller Reiter
 * Aufruf in übergeordneter Seite.
 */
function hideForm() {
	
	var tags = document.getElementsByTagName("tr");
	for(i=0;i<tags.length;++i) {
		/** Verbergen der Formular-Panele */
		if(tags[i].className=="formarea") {
			tags[i].style.display="none";
		}
	}
	var tags = document.getElementsByTagName("td");
	for(i=0;i<tags.length;++i) {
		/** Deselektieren der Reiter */
		if(tags[i].className=="tabselect") {
			tags[i].className="tabnormal";
		}
	}
}


