function RemoveFromArray( ar, o ) {
	var ar2 = new Array;
	for( var i = 0; i < ar.length; i++ ) {
		if( ar[i] != o ) {
			ar2.push( ar[i] );
		}
	}
	return ar2;
}

function AddToArray( ar, o ) {
	var bIsInArray = false;
	for( var i = 0; i < ar.length; i++ ) {
		if( ar[i] == o ) {
			bIsInArray = true;
		}
	}

	if( !bIsInArray ) {
		ar.push( o );
	}
	return ar;
}

function InsertInArrayAfter( ar, oNew, oPos ) {
	var nIndex = 0;
	for( var i = 0; i < ar.length; i++ ) {
		if( ar[i] == oPos ) {
			nIndex = i + 1;
		}
	}

	ar.splice( nIndex, 0, oNew );
	return ar;
}

function IsInArray( o, ar ) {
	for( var i = 0; i < ar.length; i++ ) {
		if( o == ar[i] ) {
			return true;
		}
	}
	return false;
}

function SwapClass( strId, strClass1, strClass2 ) {
	var cField = window.document.getElementById( strId );
	if( cField.className == strClass1 ) {
		cField.className = strClass2;
	} else if( cField.className == strClass2 ) {
		cField.className = strClass1;
	}
}

function GetParentOfNodeByTagName( oNode, strTagName ) {
	strTagName = strTagName.toLowerCase();

	while( oNode != null ) {
		var strCurrentTagName = oNode.tagName.toLowerCase();

		if( strCurrentTagName == strTagName ) {
			return oNode;
		}

		oNode = oNode.parentNode;
	}

	return null;
}

function RegisterEventHandler( o, strEvent, fHandler ) {
	if( o.addEventListener ) {
		o.addEventListener( strEvent, fHandler, false );
	} else if( o.attachEvent ) {
		o.attachEvent( "on" + strEvent, fHandler );
	}
}

function SwapSelection( strField ) {
	var cList = window.document.getElementById( strField );
	var arList = cList.value.length != 0 ? cList.value.split( "," ) : new Array();

	var cLeft = window.document.getElementById( "_" + strField + "_left" );
	var cRight = window.document.getElementById( "_" + strField + "_right" );

	for( var i = 0; i < cLeft.options.length; i++ ) {
		if( cLeft.options[i].selected ) {
			arList = AddToArray( arList, cLeft.options[i].value );
			cRight.options[cRight.options.length] = new Option( cLeft.options[i].text, cLeft.options[i].value, false, false );
			cLeft.options[i] = null;
			i--;
		}
	}

	for( var i = 0; i < cRight.options.length; i++ ) {
		if( cRight.options[i].selected ) {
			arList = RemoveFromArray( arList, cRight.options[i].value );
			cLeft.options[cLeft.options.length] = new Option( cRight.options[i].text, cRight.options[i].value, false, false );
			cRight.options[i] = null;
			i--;
		}
	}

	cList.value = arList;
}

function _SortSelectionSwapOptions( cSort, i, j ) {
	var t = new Option( cSort.options[i].text, cSort.options[i].value, cSort.options[i].defaultSelected, cSort.options[i].selected );
	cSort.options[i] = new Option( cSort.options[j].text, cSort.options[j].value, cSort.options[j].defaultSelected, cSort.options[j].selected );
	cSort.options[j] = t;
}

function _SortSelectionMoveToBottom( cSort, i ) {
	var l = cSort.options.length;
	cSort.options.length++;
	cSort.options[l] = new Option( cSort.options[i].text, cSort.options[i].value, cSort.options[i].defaultSelected, cSort.options[i].selected );
	cSort.options[i] = null;
}

function _SortSelectionSave( cSort, cHelper ) {
	var ar = new Array();

	for( var i = 0; i < cSort.options.length; i++ ) {
		ar.push( cSort.options[i].value );
	}

	cHelper.value = ar.toString();
}

function SortSelectionAllUp( strField ) {
	var cSort = window.document.getElementById( "_" + strField );

	for( var i = 0, j = 0; i < cSort.options.length; i++, j++ ) {
		if( j < cSort.options.length - 1 && !cSort.options[j].selected ) {
			_SortSelectionMoveToBottom( cSort, j );
			j--;
		}
	}

	_SortSelectionSave( cSort, window.document.getElementById( strField ) );
}

function SortSelectionAllDown( strField ) {
	var cSort = window.document.getElementById( "_" + strField );

	for( var i = 0, j = 0; i < cSort.options.length; i++, j++ ) {
		if( j < cSort.options.length - 1 && cSort.options[j].selected ) {
			_SortSelectionMoveToBottom( cSort, j );
			j--;
		}
	}

	_SortSelectionSave( cSort, window.document.getElementById( strField ) );
}

function SortSelectionOneUp( strField ) {
	var cSort = window.document.getElementById( "_" + strField );

	for( var i = 0; i < cSort.options.length; i++ ) {
		if( i > 0 && cSort.options[i].selected && !cSort.options[i-1].selected ) {
			_SortSelectionSwapOptions( cSort, i, i-1 );
		}
	}

	_SortSelectionSave( cSort, window.document.getElementById( strField ) );
}

function SortSelectionOneDown( strField ) {
	var cSort = window.document.getElementById( "_" + strField );

	for( var i = cSort.options.length-1; i > 0; i-- ) {
		if( i < cSort.options.length && cSort.options[i-1].selected && !cSort.options[i].selected ) {
			_SortSelectionSwapOptions( cSort, i, i-1 );
		}
	}

	_SortSelectionSave( cSort, window.document.getElementById( strField ) );
}

function SubSelectionAdd( strSelection, strValue, strTitle ) {
	var o = window.document.createElement( "option" );
	o.setAttribute( "value", strValue );
	o.appendChild( window.document.createTextNode( strTitle ) );

	var cSub = window.document.getElementById( "_" + strSelection );
	cSub.appendChild( o );
	cSub.selectedIndex = cSub.length - 1;

	var cHidden = window.document.getElementById( strSelection );
	if( cHidden.value == "" ) {
		cHidden.value = strValue;
	} else {
		cHidden.value = cHidden.value + "," + strValue;
	}

	var cHiddenNewFieldCounter = window.document.getElementById( strSelection + "_counter" );
	cHiddenNewFieldCounter.value = parseInt( cHiddenNewFieldCounter.value ) + 1;

	var cHiddenSubPageIndicator = window.document.getElementById( "subpage" );
	cHiddenSubPageIndicator.value = strSelection;
}

function SubSelectionUpdate( strSelection ) {
	var cHiddenSubPageIndicator = window.document.getElementById( "subpage" );
	cHiddenSubPageIndicator.value = strSelection;
}

function SubSelectionDel( strSelection ) {
	var cList = window.document.getElementById( strSelection );
	var arList = cList.value.length != 0 ? cList.value.split( "," ) : new Array();
	var cSub = window.document.getElementById( "_" + strSelection );
	arList = RemoveFromArray( arList, cSub.value );
	cList.value = arList.toString();

	if( cSub.value.substring( 0, 1 ) == "u" ) {
		var strToken = "d" + cSub.value.substring( 1 );
		var cDeleted = window.document.getElementById( strSelection + "_deleted" );
		cDeleted.value = cDeleted.value.length != 0 ? ( cDeleted.value + "," + strToken ) : strToken;
	}

	for( var i = 0; i < cSub.options.length; i++ ) {
		if( cSub.options[i].value == cSub.value ) {
			cSub.options[i] = null;
			break;
		}
	}
}

/* BEGIN: Calender Control */

if( !Date.prototype.getFullYear ) {
	Date.prototype.getFullYear = new Function( "return this.getYear() > 999 ? this.getYear() : 1900 + this.getYear()" );
}

var arMonths = new Array( "Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" );
var arDaysOfWeek = new Array( "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" );

var mapCalenders = new Array();

function Calender( strCalenderId ) {
	this.m_strCalenderId = strCalenderId;
	this.m_strSelectionId = strCalenderId + "_selection";
	this.m_strFieldId = strCalenderId + "_field";
	this.m_bIsOpen = false;
}

Calender.prototype.show = function() {
	if( !this.m_bIsOpen ) {
		var cDate = document.getElementById( this.m_strFieldId );
		var dDate = null;
		if( cDate.value != "" ) {
			var arDate = cDate.value.split( "-" );
			dDate = new Date( arDate[0], arDate[1]-1, arDate[2] );
		} else {
			dDate = new Date();
		}

		Calender_Draw( this.m_strCalenderId, dDate );

		var cSelection = document.getElementById( this.m_strSelectionId );
		cSelection.style.width = document.getElementById( this.m_strCalenderId ).offsetWidth + "px";
		var cSelectionContent = cSelection.firstChild;
		while( cSelectionContent && cSelectionContent.tagName.toLowerCase() != "table" ) {
			cSelectionContent = cSelectionContent.nextSibling;
		}
		if( cSelectionContent ) {
			cSelectionContent.style.width = document.getElementById( this.m_strCalenderId ).offsetWidth + "px";
		}
		cSelection.style.visibility = "visible";

		window.setTimeout( "mapCalenders[\"" + this.m_strCalenderId + "\"].m_bIsOpen = true;", 500 );
	}
}

Calender.prototype.hide = function() {
	if( this.m_bIsOpen ) {
		var cSelection = document.getElementById( this.m_strSelectionId );
		cSelection.style.visibility = "hidden";
		cSelection.style.height = "0px";
		this.m_bIsOpen = false;
	}
}

Calender.prototype.isOpen = function() {
	return this.m_bIsOpen;
}

function Calender_Toggle( strCalenderId ) {
	if( mapCalenders[strCalenderId].isOpen() ) {
		mapCalenders[strCalenderId].hide();
	} else {
		mapCalenders[strCalenderId].show();
	}
}

function Calender_SelectDate( strCalenderId, dateValue ) {
	var cSelection = document.getElementById( strCalenderId + "_selection" );
	cSelection.style.visibility = "hidden";
	cSelection.style.height = "0px";
	var cDate = document.getElementById( strCalenderId + "_field" );
	cDate.value = dateValue;
}

function Calender_Draw( strCalenderId, d ) {
	var now = new Date();
	var nMonthNow = now.getMonth() + 1;

	var nMonth = d.getMonth() + 1;
	var nYear = d.getFullYear();
	var nStartDay = ( ( d.getDay() + 7 ) - ( d.getDate() % 7 ) ) % 7;
	var nStopDay = 31;
	if( nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11 ) {
		nStopDay--;
	} else if( nMonth == 2 ) {
		nStopDay = nStopDay - 3;
		if( nYear % 4 == 0 ) nStopDay++;
		if( nYear % 100 == 0 ) nStopDay--;
		if( nYear % 400 == 0 ) nStopDay++;
	}

	var htmlSelection = "<table>";
	htmlSelection += "<tr><th colspan=\"7\">";
	htmlSelection += "<a href=\"#\" onclick=\"Calender_Draw('" + strCalenderId + "',new Date(" + ( nMonth == 1 ? ( nYear - 1 ) : nYear ) + "," + ( nMonth == 1 ? 11 : ( nMonth - 2 ) ) + "," + d.getDate() + "));\">&lt;&lt;</a>";
	htmlSelection += " " + arMonths[nMonth-1] + " " + nYear + " ";
	htmlSelection += "<a href=\"#\" onclick=\"Calender_Draw('" + strCalenderId + "',new Date(" + ( nMonth == 12 ? ( nYear + 1 ) : nYear ) + "," + ( nMonth == 12 ? 0 : nMonth ) + "," + d.getDate() + "));\">&gt;&gt;</a>";
	htmlSelection += "</th></tr>";

	var nDayOfMonth = 1;
	htmlSelection += "<tr>";
	for( var i = 0; i <= 6; i++ ) {
		htmlSelection += "<th>" + arDaysOfWeek[i] + "</th>";
	}
	htmlSelection += "</tr>";
	for( var i = 0; i <= 5 && nDayOfMonth <= nStopDay; i++ ) {
		htmlSelection += "<tr>";
		for( var j = 0; j <= 5; j++ ) {
			if( i == 0 && j < nStartDay ) {
				htmlSelection += "<td></td>";
			} else {
				if( nDayOfMonth > nStopDay ) {
					htmlSelection += "<td></td>";
				} else {
					var strDate = nYear + "-" + ( nMonth < 10 ? "0" : "" ) + nMonth + "-" + ( nDayOfMonth < 10 ? "0" : "" ) + nDayOfMonth;
					if( nDayOfMonth == now.getDate() && nMonth == nMonthNow ) {
						htmlSelection += "<td class=\"today\">";
					} else if( nDayOfMonth == d.getDate() ) {
						htmlSelection += "<td class=\"selected\">";
					} else {
						htmlSelection += "<td>";
					}
					htmlSelection += "<a href=\"#\" onclick=\"Calender_SelectDate('" + strCalenderId + "','" + strDate + "');\">" + nDayOfMonth + "</a></td>";
					nDayOfMonth++;
				}
			}
		}
		if( nDayOfMonth > nStopDay ) {
			htmlSelection += "<td></td>";
		} else {
			var strDate =  nYear + "-" + ( nMonth < 10 ? "0" : "" ) + nMonth + "-" + ( nDayOfMonth < 10 ? "0" : "" ) + nDayOfMonth;
			if( nDayOfMonth == now.getDate() && nMonth == nMonthNow ) {
				htmlSelection += "<td class=\"today\">";
			} else if( nDayOfMonth == d.getDate() ) {
				htmlSelection += "<td class=\"selected\">";
			} else {
				htmlSelection += "<td>";
			}
			htmlSelection += "<a href=\"#\" onclick=\"Calender_SelectDate('" + strCalenderId + "','" + strDate + "');\">" + nDayOfMonth + "</a></td>";
			nDayOfMonth++;
		}
		htmlSelection += "</tr>";
	}
	htmlSelection += "</table>";

	var cSelection = document.getElementById( strCalenderId + "_selection" );
	cSelection.innerHTML = htmlSelection;
}

function Calender_HideAll() {
	for( var c in mapCalenders ) {
		mapCalenders[c].hide();
	}
}

RegisterEventHandler( document, "click", Calender_HideAll );

/* END: Calender Control */

/* BEGIN: Selection Control */

function toggleSelection( strSelectionId, strSelectionSelectionId, strFieldId ) {
	var cSelection = document.getElementById( strSelectionSelectionId );
	if( cSelection.style.visibility == "visible" ) {
		cSelection.style.visibility = "hidden";
		cSelection.style.height = "0px";
	} else {
		cSelection.style.width = document.getElementById( strSelectionId ).offsetWidth + "px";
		cSelection.style.visibility = "visible";
	}
}

/* END: Selection Control */

/* BEGIN: Completion Control */

var arCompletion_Field = new Array;

function Completion_KeyUp( strFieldname, nMinChars, nKeyTimeOut ) {
	if( arCompletion_Field[strFieldname] ) {
		window.clearTimeout( arCompletion_Field[strFieldname] );
	}

	oCompletionName = window.document.getElementById( strFieldname + "_name" );
	if( oCompletionName.value.length > nMinChars ) {
		arCompletion_Field[strFieldname] = window.setTimeout( "Completion_RequestData( '" + strFieldname + "' );", nKeyTimeOut );
	}
}

function Completion_RequestData( strFieldname ) {
	oCompletionName = window.document.getElementById( strFieldname + "_name" );
	DoXmlHttpRequest( strScriptName, "completion", strFieldname, oCompletionName, "_ajax_completion=" + EncodeURL( strFieldname ), Completion_ShowResultset );
}

function Completion_ShowResultset( strFieldname, strContent ) {
	oCompletionName = window.document.getElementById( strFieldname + "_name" );
	oCompletionLayer = window.document.getElementById( strFieldname + "_layer" );
	oCompletionLayer.style.width = oCompletionName.offsetWidth + "px";
	oCompletionLayer.style.display = "block";
	oCompletionLayer.innerHTML = strContent;
}

function Completion_HideResultset( strFieldname ) {
	oCompletionLayer = window.document.getElementById( strFieldname + "_layer" );
	oCompletionLayer.style.display = "none";
}

function Completion_SelectValue( strFieldname, strValue, strName ) {
	oCompletionValue = window.document.getElementById( strFieldname );
	oCompletionValue.value = strValue;
	oCompletionName = window.document.getElementById( strFieldname + "_name" );
	oCompletionName.value = strName;
}

/* END: Completion Control */

/* BEGIN: Tree Control */

function ToggleTreeControl( cToggleLink, strFieldname, strKey ) {
	var cField = window.document.getElementById( "tcl_" + strFieldname + "_" + strKey );
	if( cField.className == "visible" ) {
		cField.className = "invisible";
		cToggleLink.getElementsByTagName("img")[0].src = "/elements/tree_open.png";
	} else if( cField.className == "invisible" ) {
		cField.className = "visible";
		cToggleLink.getElementsByTagName("img")[0].src = "/elements/tree_close.png";
	}
}

/* END: Tree Control */

/* BEGIN: Graphical Checkbox */

function ToggleGraphicalCheckbox( oImage, strFieldname, strValue ) {
	var oForm = GetParentOfNodeByTagName( oImage, "form" );
	var cField = oForm.elements[strFieldname];
	if( cField.value == strValue ) {
		cField.value = "";
		oImage.src = "/elements/checkbox/round/unchecked.png";
	} else {
		cField.value = strValue;
		oImage.src = "/elements/checkbox/round/checked.png";
	}
}

/* END: Graphical Checkbox */

function SubmitForm( strFieldname, strValue ) {
	var cButtonGeneric = document.getElementById( "button_generic" );
	cButtonGeneric.name = strFieldname;
	if( arguments.length > 1 ) {
		cButtonGeneric.value = strValue;
	}
	var oForm = GetParentOfNodeByTagName( cButtonGeneric, "form" );
	oForm.submit();
}

function GetFormFieldValues( oForm ) {
	var arFields = new Array();

	for( var i = 0; i < oForm.elements.length; i++ ) {
		var oField = oForm.elements[i];
		var bTransport = true;
		switch( oField.type ) {
			case "checkbox":
			case "radio":
				bTransport = oField.checked;
				break;
			default:
				break;
		}
		if( bTransport ) {
			arFields.push( oField.name + "=" + EncodeURL( oField.value ) );
		}
	}

	return arFields;
}

function EncodeURL( strValue ) {
	if( typeof encodeURIComponent != 'undefined' ) {
		return encodeURIComponent( strValue );
	} else {
		return escape( strValue );
	}
}

var strLocalUrlPrefix = window.location.protocol + "://" + window.location.host;

function GetXmlHttpRequest() {
	if( typeof XMLHttpRequest != 'undefined' ) {
		return new XMLHttpRequest();
	}

	try {
		var oXmlHttpRequest = new ActiveXObject( "Msxml2.XMLHTTP" );
		return oXmlHttpRequest;
	} catch(e) {}

	try {
		var oXmlHttpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
		return oXmlHttpRequest;
	} catch(e) {}

	return null;
}

function DoXmlHttpRequestCallBack( oDestNode, strContent ) {
	oDestNode.innerHTML = strContent;
	var arScript = oDestNode.getElementsByTagName( "script" );
	for( var i = 0; i < arScript.length; i++ ) {
		eval( arScript[i].text );
	}
}

function DoXmlHttpRequest( strUrl, strMode, oDestNode, oNode, strAdditionalParameters, fCallBack ) {
	if( strAdditionalParameters == null ) {
		strAdditionalParameters = "";
	}

	if( fCallBack == null ) {
		fCallBack = DoXmlHttpRequestCallBack;
	}

	var oXmlHttpRequest = GetXmlHttpRequest();

	if( oXmlHttpRequest == null ) {
		return false;
	}

	oXmlHttpRequest.onreadystatechange = function() {
		if( oXmlHttpRequest.readyState == 4 ) {
			if( oXmlHttpRequest.status == 200 ) {
				fCallBack( oDestNode, oXmlHttpRequest.responseText );
			} else {
				alert( "Status: " + oXmlHttpRequest.status + "\nContent: " + oXmlHttpRequest.responseText );
			}
		}
	}

	var strParameters = "";
	if( oNode != null ) {
		var oForm = GetParentOfNodeByTagName( oNode, "form" );
		var arFields = GetFormFieldValues( oForm );
		arFields.push( "_ajax_mode=" + strMode );

		strParameters = arFields.join( "&" );
	}

	if( strUrl.indexOf( "/" ) != 0 && strUrl.indexOf( strLocalUrlPrefix ) != 0 ) {
		if( strParameters == "" ) {
			strParameters = "_ajax_proxy_url=" + encodeURIComponent(strUrl);
		} else if ( strAdditionalParameters != "" ) {
			strParameters += "&_ajax_proxy_url=" + encodeURIComponent(strUrl);
		}
		strUrl = "/ajax_proxy.php";
	}

	if( strAdditionalParameters != "" ) {
		if( strParameters == "" ) {
			strParameters = strAdditionalParameters;
		} else if ( strAdditionalParameters != "" ) {
			strParameters += "&" + strAdditionalParameters;
		}
	}

	oXmlHttpRequest.open( "post", strUrl, true );
	oXmlHttpRequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );

	oXmlHttpRequest.send( strParameters );
}

function DoSessionRefresh() {
	var oXmlHttpRequest = GetXmlHttpRequest();

	if( oXmlHttpRequest == null ) {
		return false;
	}

	oXmlHttpRequest.onreadystatechange = function() {
		if( oXmlHttpRequest.readyState == 4 ) {
			if( oXmlHttpRequest.status == 200 ) {
				// diagnostic message
			}
		}
	}

	oXmlHttpRequest.open( "get", "/alive.php", true );

	oXmlHttpRequest.send( "" );
}

function StartSessionRefresh( nSeconds ) {
	window.setInterval( "DoSessionRefresh();", nSeconds * 1000 );
}