// Methode ltrim (left trim) zum String-Objekt hinzufügen
// Diese Methode löscht, ausgehend vom Anfang der Zeichenkette,
// alle Zeichen, die bei deren Vorkommen entfernt werden sollen.
// Der Parameter clist ist optional und gibt eine Liste von Zeichen vor,
// die von der Methode herausgelöscht werden sollen.
// Wird dieser Parameter nicht übergeben, so werden alle Whitespaces
// gelöscht, die am Anfang des Strings stehen.
String.prototype.ltrim = function (clist)
{
	// Wurde eine Zeichenkette mit den zu entfernenden
	// Zeichen übergeben?
	if (clist)
	{
		// In diesem Fall sollen nicht Whitespaces, sondern
		// alle Zeichen aus dieser Liste gelöscht werden,
		// die am Anfang des Strings stehen.
		return this.replace (new RegExp ('^[' + clist + ']+'), '');
	}
	// Führende Whitespaces aus dem String entfernen
	// und das resultierende String zurückgeben.
	return this.replace (/^\s+/, '');
}

// Die Methode rtrim (right trim) erweitert ebenfalls das String-Objekt.
// Im Gegensatz zu ltrim wird hier aber vom Ende des Strings ausgegangen.
// Es werden also alle Whitespaces bzw. die Zeichen aus der übergebenen
// Zeichenliste gelöscht, die am Ende des Strings stehen.
String.prototype.rtrim = function (clist)
{
	// Zeichenkette mit den zu entfernenden Zeichen angegeben?
	if (clist)
	{
		// Zeichen aus der Liste, die am Ende des String stehen
		// löschen.
		return this.replace (new RegExp ('[' + clist + ']+$'), '');
	}
	// Whitespaces am Ende des Strings ertfernen und dann das Ergebnis
	// dieser Operation zurückgeben.
	return this.replace (/\s+$/, '');
}


// Implementierung einer JavaScript trim Function als Erweiterung
// des vordefinierten JavaScript String-Objekts.
// Die Methode bedient sich den zuvor definierten Methoden ltrim
// und rtrim und kombiniert diese miteinander.
// Mit dem Parameter clist kann man auch hier eine Liste von Zeichen
// angeben, die vom Anfang, wie auch vom Ende der Zeichenkette entfernt
// werden soll.
String.prototype.trim = function (clist)
{
	// Wird der Parameter clist angegeben, so werden statt der Whitespaces
	// die in dieser Variablen angegebenen Zeichen "getrimmt".
	if (clist)
	{
		// Führende und abschließende Zeichen aus der Liste entfernen.
		return this.ltrim (clist).rtrim (clist);
	}
	// Whitespaces vom Anfang und am Ende entfernen
	return this.ltrim ().rtrim ();
}

function SetLastModifiedDate( elementID )
{
	var element = document.getElementById( elementID )
	if( element != null )
	{
		// get document's last modified date
		var dateLastMod = new Date(document.lastModified);
		// enhance Year to 4 Digits
		var nYear = dateLastMod.getYear();
		if( nYear < 10 )
		{
			nYear = nYear + 2000;
		}
		else if( nYear < 2000 )
		{
			nYear = nYear + 1900;
		}
		var strDay = dateLastMod.getDate();
		if( dateLastMod.getDate() < 10 )
		{
			strDay = '0' + strDay;
		}
		var strMonth = dateLastMod.getMonth() + 1;
		if( dateLastMod.getMonth() < 9 )
		{
			strMonth = '0' + strMonth;
		}
		var strDate = 'Letzte &Auml;nderung: ' + strDay + '.' + strMonth + '.' + nYear;
		
		element.innerHTML = strDate;
	}
}

function LayoutContent()
{
	// change in case of new general layout 
	var iHeaderHight = 65;
	var iFooterHight = 82;
	var iH1Hight = 30;

	// get Dimensions of Window - different way for firefox and IE ... !!
	var iHeight = -1;
	var iWidth = -1;
	if( window.innerHeight )
	{
		// FF knows document.body.offsetHeight -> but correctly the result is height of real body and not available window...
		iHeight = window.innerHeight;
		iWidth = window.innerWidth;
	}
	else if( document.body && document.body.offsetHeight )
	{
		// IE don't know window.innerHeight
		iHeight = document.body.offsetHeight;
		iWidth = document.body.offsetWidth;
	}
	if( iHeight > 0 && iWidth > 0 )
	{
		var htmlContent = document.getElementById( 'contentCol' );
		var htmlContentBody = document.getElementById( 'contentBody' );
		if( htmlContent != null && htmlContentBody != null )
		{
			var iContentOffset = iHeaderHight; //htmlContent.offsetTop;
			var iContentHeight = htmlContent.offsetHeight;
			var iNewContentHeight = iHeight - iContentOffset - iFooterHight;

			htmlContent.style.height = iNewContentHeight;
			htmlContentBody.style.maxHeight = iNewContentHeight - iH1Hight;
			htmlContentBody.style.height = iNewContentHeight - iH1Hight;
			document.getElementById( 'navigationCol' ).style.height = iNewContentHeight;
		}
		var htmlHeader = document.getElementById( 'headerDiv' );
		var htmlBckGnd1 = document.getElementById( 'bckgnd1' );
		var htmlBckGnd2 = document.getElementById( 'bckgnd2' );
		if( htmlHeader != null && htmlBckGnd1 != null && htmlBckGnd2 !=null )
		{
			htmlBckGnd1.style.height = iHeight;
			htmlBckGnd1.style.width = (iWidth - 970) / 2;
			htmlBckGnd2.style.height = iHeight;
			htmlBckGnd2.style.width = (iWidth - 970) / 2;
			htmlBckGnd2.style.left = iWidth - (iWidth - 970) / 2;
		}
	}
}

//-------------------------------------------------------------------
// Calculate Absolute Position of given Element
//-------------------------------------------------------------------
function getPosition( element )
{
	var elem=element,tagname="",x=0,y=0;
  
	// solange elem ein Objekt ist und die Eigenschaft offsetTop enthaelt
	// wird diese Schleife fuer das Element und all seine Offset-Eltern ausgefuehrt
	while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined"))
	{
		y+=elem.offsetTop;     /* Offset des jeweiligen Elements addieren */
		x+=elem.offsetLeft;    /* Offset des jeweiligen Elements addieren */
		tagname=elem.tagName.toUpperCase(); /* tag-Name ermitteln, Grossbuchstaben */

		/* wenn beim Body-tag angekommen elem fuer Abbruch auf 0 setzen */
		if (tagname=="BODY")
			elem=0;

		// wenn elem ein Objekt ist und offsetParent enthaelt Offset-Elternelement ermitteln
		if (typeof(elem)=="object")
			if (typeof(elem.offsetParent)=="object")
				elem=elem.offsetParent;
	}

	// Objekt mit x und y zurueckgeben
	position=new Object();
	position.x=x;
	position.y=y;
	
	return position;
}


function ShowNote( event, strTitle, strText, iLeft )
{
	if( event == null )
	{
		event = window.event;
	}
	// gets note1 element
	var note = document.getElementById( 'note' );
	var noteh4 = document.getElementById( 'noteh4' );
	var notetext = document.getElementById( 'notetext' );
	if( note && noteh4 && notetext )
	{
		// assigns X,Y mouse coordinates to 'note1' element
		noteh4.innerHTML = unescape( strTitle );
		notetext.innerHTML = unescape( strText );

		note.style.top = event.clientY + 5;
		if( iLeft )
		{
			note.style.left = event.clientX - 5 - note.clientWidth;
		}
		else
		{
			note.style.left = event.clientX + 5;
		}
	
		// makes note1 element visible
		note.style.visibility = 'visible';
	}
}
	
function HideNote()
{
	// gets note1 element
	var note = document.getElementById( 'note' );
	if( note )
	{
		// hides note
		note.style.visibility='hidden';
	}
}

function FormatDate( strIn )
{
	// Sample Input: '1995-08-13T00:00:00'
	// first parse year, month and day
	var strYear = strIn.substr(0, 4);
	var strMonth = strIn.substr(5, 2);
	var strDay = strIn.substr(8, 2);
	// compose in German Format
	var strOut = strDay + '.' + strMonth + '.' + strYear;
	
	return strOut;
}

function CheckDateInput( strDateIn )
{
	// we only accept '.' as Delimiter and expect sequence D.M.Y
	var strSplit = strDateIn.split(".");

	// First there must be three parts
	if( strSplit.length != 3 )
	{
		return false;
	}
	
	// Caution: remove leading '0' otherwise parseInt treats values as octal and return '0' in case of '08' and '09' !!!!
	var iD = parseInt( strSplit[0].replace (/^0+/,"") );
	var iM = parseInt( strSplit[1].replace (/^0+/,"") );
	var iY = parseInt( strSplit[2].replace (/^0+/,"") );
	if( iD == NaN || iM == NaN || iY == NaN )
	{
		return false;
	}
	// values < 100 are 21'st century!
	if( iY < 100 )
	{
		iY += 2000;
	}
	iM--;
	// initializte Date-Object
	var date = new Date( iY, iM, iD );
	
	// and compare all parts
	var iD2 = date.getDate();
	var iM2 = date.getMonth();
	var iY2 = date.getFullYear();
	if( iD != iD2 || iM != iM2 || iY != iY2 )
	{
		// alert( 'In: ' + iD + '.' + iM + '.' + iY + '\nOut: ' + iD2 + '.' + iM2 + '.' + iY2 );
		return false;
	}

	return true;
}

function FormatDateInput( strDateIn )
{
	var strDate = '';
	if( strDateIn != '' )
	{
		// we only accept '.' as Delimiter and expect sequence D.M.Y
		var strSplit = strDateIn.split(".");
		
		var iD = parseInt( strSplit[0].replace (/^0+/,"") );
		var iM = parseInt( strSplit[1].replace (/^0+/,"") );
		var iY = parseInt( strSplit[2].replace (/^0+/,"") );

		// values < 100 are 21'st century!
		if( iY < 100 )
		{
			iY += 2000;
		}

		if( iD < 10 )
		{
			strDate = '0';
		}
		strDate += iD + '.';
		if( iM < 10 )
		{
			strDate += '0';
		}
		strDate += iM + '.' + iY;
	}

	return strDate;
}


function FormatCurrency( strIn )
{
	return strIn.toFixed(2).replace(".", ",");
}

function CheckCurrencyInput( strIn )
{
	// Remove Whitespaces
	var strCheck = strIn.trim();
	
	// Remove Separator
	strCheck = strCheck.replace( ".", "" );
	strCheck = strCheck.replace( ",", "." );
	
	if( strCheck != '' )
	{
		// TODO: remove leading sign...
		if( isNaN( strCheck ) )
		{
			// check for valid characters
			if ( !strCheck.match (/^[0-9]*[.][0-9]+$/) && !strCheck.match (/^[0-9]+[.][0-9]*$/) )
			{
				return false;
				alert( strCheck );
			}
		}
	}
	
	return true;
}

function FormatCurrencyInput( strIn )
{
	// Remove Whitespaces
	var strOut = strIn.trim();
	
	var idx = strOut.indexOf( ',' );
	if( idx	== -1 )
	{
		strOut += ',00';
	}
	else
	{
		var nk = strOut.length - idx;
		while( nk < 3 )
		{
			strOut += '0';
			nk++;
		}
		if( nk > 3 )
		{
			strOut = strOut.substr( 0, idx + 3 );
		}
	}

	return strOut;
}

function DeleteEntry( strName, strURL )
{
	var bOK = false;
	if( strName == '' )
	{
		bOK = window.confirm( "Eintrag löschen?" );
	}
	else
	{
		bOK = window.confirm( "Eintrag <" + strName + "> löschen?" );
	}
	if( bOK ==true )
	{
		window.location = strURL;
	}
}

function DefInitForm( id )
{
	var oFocus = document.getElementById( id );
	if( oFocus != null )
	{
		oFocus.focus();
	}
}

function ValidateInput( aMand, aDate, aInt, aCur )
{
	var item;
	var focusItem = null;
	var errors = 0;
	var i;
	
	// check mandatory fields
	for( i = 0; i < aMand.length; i++ )
	{
		item = document.getElementById( aMand[i] );
		if( item != null )
		{
			if( item.value == '' )
			{
				focusItem = item;
				errors++;
				item.className = item.className.replace( /Mand/, 'MError' );
				item.className = item.className.replace( /OK/, 'Error' );
			}
			else
			{
				item.className = item.className.replace( /MError/, 'Mand' );
				item.className = item.className.replace( /Error/, 'OK' );
			}
		}
	}

	// Date-values must be valid, if specified
	for( i = 0; i < aDate.length; i++ )
	{
		item = document.getElementById( aDate[i] );
		if( item != null && item.value != '' )
		{
			if( CheckDateInput( item.value ) == false )
			{
				focusItem = item;
				errors++;
				item.className = item.className.replace( /Mand/, 'MError' );
				item.className = item.className.replace( /OK/, 'Error' );
			}
			else
			{
				item.value = FormatDateInput( item.value );
				item.className = item.className.replace( /MError/, 'Mand' );
				item.className = item.className.replace( /Error/, 'OK' );
			}
		}
	}

	// Check Numeric Values
	for( i = 0; i < aInt.length; i++ )
	{
		item = document.getElementById( aInt[i] );
		if( item != null )
		{
			if( item.value == '' )
			{
				item.value = '0';
				item.className = item.className.replace( /MError/, 'Mand' );
				item.className = item.className.replace( /Error/, 'OK' );
			}
			else if( isNaN( item.value ) )
			{
				focusItem = item;
				errors++;
				item.className = item.className.replace( /Mand/, 'MError' );
				item.className = item.className.replace( /OK/, 'Error' );
			}
			else
			{
				item.className = item.className.replace( /MError/, 'Mand' );
				item.className = item.className.replace( /Error/, 'OK' );
			}
		}
	}

	// Check Currency Values
	for( i = 0; i < aCur.length; i++ )
	{
		item = document.getElementById( aCur[i] );
		if( item != null )
		{
			if( item.value == '' )
			{
				item.value = '0,00';
				item.className = item.className.replace( /MError/, 'Mand' );
				item.className = item.className.replace( /Error/, 'OK' );
			}
			else if( CheckCurrencyInput( item.value ) == false )
			{
				focusItem = item;
				errors++;
				item.className = item.className.replace( /Mand/, 'MError' );
				item.className = item.className.replace( /OK/, 'Error' );
			}
			else
			{
				item.value = FormatCurrencyInput( item.value );
				item.className = item.className.replace( /MError/, 'Mand' );
				item.className = item.className.replace( /Error/, 'OK' );
			}
		}
	}
	
	// if errors found, dispplay message and set focus to last input
	if( errors > 0 )
	{
		var strMsg;
		if( errors == 1 )
		{
			strMsg = unescape("Das rot gekennzeichnete Feld wurde nicht korrekt ausgef%FCllt\n\n");
		}
		else
		{
			strMsg = unescape("Die rot gekennzeichneten Felder wurden nicht korrekt ausgef%FCllt\n\n");
		}
		strMsg += unescape("Vervollst%E4ndigen Sie bitte Ihre Angaben.");
		alert( strMsg );
		if( focusItem != null )
		{
			focusItem.focus();
		}
		return false;
	}
	
	return true;
}
