	
// dynamic field updates

function dateFromString(datestring, day_offset) {
	// per regex überprüfen, format (d)d.(m)m.yyyy
	var rgx = datestring.search(/^([1-9]|[012]\d|3[01])\.(0[1-9]|[1-9]|1[0-2])\.([1-2]\d{3})$/);
	if(rgx == -1) {
		return false;
	}
	
	var date_ar = datestring.split(".");
	if(date_ar.length == 3) {
		var day_val = parseInt(date_ar[0],10);

		if(day_offset && day_offset != 0) {
			day_val = day_val+parseInt(day_offset,10);
		}

		var d = new Date(date_ar[2], --date_ar[1], day_val);

		return d;
	}
	return false;
}

function ddUpdate(field_from, field_to) {
	var date_ar = field_from.value.split(".");
	var d = dateFromString(field_from.value,1);
	if(d) {	
		field_to.value = d.getDate()+'.'+(d.getMonth()+1)+'.'+d.getFullYear();
		return true;
	}
	return false;
}

/**********************************************************************
*          Calendar JavaScript [DOM] v3.02 by Michael Loesler          *
************************************************************************
* Copyright (C) 2005-07 by Michael Loesler, http//derletztekick.com    *
*                                                                      *
*                                                                      *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or    *
* (at your option) any later version.                                  *
*                                                                      *
* This program is distributed in the hope that it will be useful,      *
* but WITHOUT ANY WARRANTY; without even the implied warranty of       *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
* GNU General Public License for more details.                         *
*                                                                      *
* You should have received a copy of the GNU General Public License    *
* along with this program; if not, see <http://www.gnu.org/licenses/>  *
* or write to the                                                      *
* Free Software Foundation, Inc.,                                      *
* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.            *
*                                                                      *
 **********************************************************************/
 
 var activeElement;
 var updateElement;

function findeL(obj){
	var lPos = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			lPos += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
	lPos += obj.x;
	return lPos;
}

function findeO(obj){
	var oPos = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			oPos += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	oPos += obj.y;
	return oPos;
} 


setInputValue = function(day, month, year) {
	var element;
	if(day && month && year) {
		element=document.getElementById("calendar");
		//activeElement.value = month+'/'+day+'/'+year;
		activeElement.value = day+'.'+month+'.'+year;
		element.style.visibility = 'hidden';
	}
	if(updateElement) {
		ddUpdate(activeElement, document.getElementById(updateElement));
	}
	return true;
}


	function CalendarJS() {
		this.now = new Date();
		this.dayname = ["Mo","Di","Mi","Do","Fr","Sa","So"];
		this.monthname = ["Januar","Februar","M&auml;rz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"];	
		this.tooltip = ["vorheriger Monat","naechster Monat"];
		this.monthCell = document.createElement("th");
		this.monthCellContent = document.createElement("span");
		this.tableHead = null;
		this.parEl = null;
	
		
		this.changeDate = function(vdate) {
			this.date = vdate.getDate();
			this.month = this.mm = vdate.getMonth();
			this.year = this.yy = vdate.getFullYear();
			this.show();
		}
	
		
		this.init = function( id ) {
			this.date = this.now.getDate();
			this.month = this.mm = this.now.getMonth();
			this.year = this.yy = this.now.getFullYear();
			this.monthCell.colSpan = 5;
			this.monthCellContent.innerHTML = this.monthname[this.mm]+" "+this.yy;
			this.monthCell.appendChild(this.monthCellContent);
			//this.monthCell.appendChild(document.createTextNode( this.monthname[this.mm]+" "+this.yy ));
			this.tableHead = this.createTableHead();
			this.parEl = document.getElementById( id );
			this.show();
			
			return this;
		}
		
		this.removeElements = function( Obj ) {
			for (var i=0; i<Obj.childNodes.length; i++)
				Obj.removeChild(Obj.childNodes[i]);
			return Obj;
		}
			
		this.show = function() {
			this.parEl = this.removeElements( this.parEl );
			this.monthCell.firstChild.innerHTML = this.monthname[this.mm]+" "+this.yy;
			//this.monthCell.firstChild.replaceData(0, this.monthCell.firstChild.nodeValue.length, this.monthname[this.mm]+" "+this.yy);
			var table = document.createElement("table");
			table.appendChild( this.createTableBody() );
			table.appendChild( this.tableHead );
			this.parEl.appendChild( table );
		}
				
		this.createTableHead = function() {
			var thead = document.createElement("thead");
			var tr = document.createElement("tr");
			var th = document.createElement("th");
			th.appendChild(document.createTextNode( "\u00AB" ));
			th.Instanz = this;
			th.onclick = function() { this.Instanz.switchMonth("prev"); };
			th.title = this.tooltip[0];
			try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
			tr.appendChild( th );
			tr.appendChild( this.monthCell );			
			th = document.createElement("th");
			th.appendChild(document.createTextNode( "\u00BB" ));
			th.Instanz = this;
			th.onclick = function() { this.Instanz.switchMonth("next"); };
			th.title = this.tooltip[1];
			try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
			tr.appendChild( th );
			thead.appendChild( tr );
			tr = document.createElement('tr');
			for (var i=0; i<this.dayname.length; i++)
				tr.appendChild( this.getCell("th", this.dayname[i], "weekday" ) );
			thead.appendChild( tr );
			return thead;
		}
		
		this.createTableBody = function() {
			var dayspermonth = [31,28,31,30,31,30,31,31,30,31,30,31];
			var sevendaysaweek = 0;
			var begin = new Date(this.yy, this.mm, 1);
			var firstday = begin.getDay()-1;
			if (firstday < 0)
				firstday = 6;
			if ((this.yy%4==0) && ((this.yy%100!=0) || (this.yy%400==0)))
				dayspermonth[1] = 29;
			var tbody = document.createElement("tbody");
			var tr = document.createElement('tr');
			
			for (var i=0; i<firstday; i++, sevendaysaweek++)
				tr.appendChild( this.getCell( "td", " ", null ) );

			for (var i=1; i<=dayspermonth[this.mm]; i++, sevendaysaweek++){
				if (this.dayname.length == sevendaysaweek){
					tbody.appendChild( tr );
					tr = document.createElement('tr');
					sevendaysaweek = 0;
				}
				if (i==this.date && this.mm==this.month && this.yy==this.year && (sevendaysaweek == 5 || sevendaysaweek == 6))
					tr.appendChild( this.getCell( "td", i, "today weekend", i, this.mm+1, this.yy ) );
				else if (i==this.date && this.mm==this.month && this.yy==this.year)
					tr.appendChild( this.getCell( "td", i, "today", i, this.mm+1, this.yy ) );
				else if (sevendaysaweek == 5)
					tr.appendChild( this.getCell( "td", i, "saturday weekend", i, this.mm+1, this.yy ) );
				else if (sevendaysaweek == 6)
					tr.appendChild( this.getCell( "td", i, "sunday weekend", i, this.mm+1, this.yy ) );
				else
					tr.appendChild( this.getCell( "td", i, null, i, this.mm+1, this.yy ) );
			}
	
			for (var i=sevendaysaweek; i<this.dayname.length; i++)
				tr.appendChild( this.getCell( "td", " ", null  ) );
	
			tbody.appendChild( tr );
			return tbody;
			
		}
		
		this.getCell = function(tag, str, cssClass, day, month, year) {
			var El = document.createElement( tag );
			El.appendChild(document.createTextNode( str ));
			if (cssClass != null)
				El.className = cssClass;
			El.onclick = function() { setInputValue(day, month, year); }
			return El;
		}
		
		
		
		this.switchMonth = function( s ){
			switch (s) {
				case "prev": 
					this.yy = (this.mm == 0)?this.yy-1:this.yy;
					this.mm = (this.mm == 0)?11:this.mm-1;
				break;
				
				case "next":
					this.yy = (this.mm == 11)?this.yy+1:this.yy;
					this.mm = (this.mm == 11)?0:this.mm+1;
				break;
			}
			this.show();
		}
	}
	
	var DOMContentLoaded = false;
	function addContentLoadListener (func) {
		if (document.addEventListener) {
			var DOMContentLoadFunction = function () {
				window.DOMContentLoaded = true;
				func();
			};
			document.addEventListener("DOMContentLoaded", DOMContentLoadFunction, false);
		}
		var oldfunc = (window.onload || new Function());
		window.onload = function () {
			if (!window.DOMContentLoaded) {
				oldfunc();
				func();
			}
		};
	}
		
	initCalendarScript = function() {
		var element;
		if(document.getElementById("calendar")) {
			document.getElementById("calendar").style.left = '-5000px';
			document.getElementById("calendar").style.top = '-5000px';
			calJS = new CalendarJS().init("calendar");

			var allElems = document.getElementsByTagName('*');
			for (var i = 0; i < allElems.length; i++) {
				var thisElem = allElems[i];
				if (thisElem.className && thisElem.className == 'calendar_link') {
					thisElem.style.visibility = 'visible';
				}
			}

		}	
	}
	
	addContentLoadListener( function() { 
		initCalendarScript();
	} );
	
	function calendarShow(inputId, linkId, fUpdateElement) {
			var pos,element;
			
			if(calJS && document.getElementById(inputId)) {
				// kalender existiert, wert aus inputfeld setzen	
				
				var vsource = document.getElementById(inputId);

				calJS.changeDate(dateFromString(vsource.value,0));
			}	
			activeElement = document.getElementById(inputId);
 			element=document.getElementById("calendar");
 			updateElement = fUpdateElement;
 			if(element.style.visibility == 'visible') {
	 			element.style.visibility = 'hidden';
	 			element.style.left			= '-5000px';
	 			element.style.right			= '-5000px';
 			} else {
	 			element.style.left = 0;
	 			element.style.top  = 0;
	 			if((navigator.appName=="Microsoft Internet Explorer") && (ms_page===false) && (navigator.appVersion.indexOf("MSIE 8")<0)) {
		 			var left = findeL(document.getElementById(linkId))-findeL(document.getElementById('suche_mit_deinen_reisedaten_container'));
	 				var postop = (findeO(document.getElementById(linkId))+16-findeO(document.getElementById('suche_mit_deinen_reisedaten_container')));
	 			} else {
		 			var left = findeL(document.getElementById(linkId));
	 				var postop = findeO(document.getElementById(linkId))+16;
 				};
 				element.style.left = left+'px';
 				element.style.top = postop+'px';
 			
 				element.style.visibility = 'visible';
			}
	}
	
	
