

function selectDay(dateString)
{
	var cbsf = document.getElementById('cb-search-form');
	cbsf.date_from.value = dateString;
	cbsf.date_to.value = dateString;
	cbsf.submit();
}

function selectMonth(dateString)
{
	tmpstr = new String(dateString);
	var splits = tmpstr.split(",");
	var cbsf = document.getElementById('cb-search-form');
	cbsf.date_from.value = splits[0];
	cbsf.date_to.value = splits[1];
	cbsf.submit();
}

/* MarcGrabanski.com v2.4 */
/* Pop-Up Calendar Built from Scratch by Marc Grabanski */
/* Enhanced by Keith Wood (kbwood@iprimus.com.au). */
/* Under the Creative Commons Licence http://creativecommons.org/licenses/by/3.0/
	Share or Remix it but please Attribute the authors. 
*/	
/*	
	edited by Alexander Nolis for Kulturserver.de
	$Date: 2007-08-02 17:00:00 +0100 (Thu, 08 Aug 2007) $
	$Rev: 0001 $
	
*/
var popUpCal = {
	initDay: 0,
	initDayTo: 0,	
	initMonth: 0, // 0-11
	initYear: 0, // 4-digit year
	selectedDay: 0,
	selectedMonth: 0, // 0-11
	selectedYear: 0, // 4-digit year
	clearText: 'Clear', // Display text for clear link
	closeText: 'Close', // Display text for close link
	prevText: '<img src="http://dev.heimat.de/module/pics/cal_arrow_left.gif" alt="" />', // Display text for previous month link
	nextText: '<img src="http://dev.heimat.de/module/pics/cal_arrow_right.gif" alt="" />', // Display text for next month link
	currentText: 'Heute', // Display text for current month link
	appendText: '', // Display text following the input box, e.g. showing the format
	buttonText: '...', // Text for trigger button
	buttonImage: '', // URL for trigger button image
	buttonImageOnly: false, // True if the image appears alone, false if it appears on a button
	language: 1,
	dayNamesMl: {
				 "1":['So','Mo','Di','Mi','Do','Fr','Sa'], //de
				 "2":['Su','Mo','Tu','We','Th','Fr','Sa'], //en
				 "3":['Di','Lu','Ma','Me','Je','Ve','Sa'], //fr
				 "4":['Ma','Di','Wo','Do','Vr','Za','Zo']  //nl
				},
	dayNames: ['So','Mo','Di','Mi','Do','Fr','Sa'], // Names of days starting at Sunday
	monthNamesMl: {
	               "1":['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],   //de
				   "2":['January','February','March','April','May','June','July','August','September','October','November','December'], //en
				   "3":['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'], //fr
				   "4":['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'] //nl
					},
	monthNames: ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'], // Names of months
	dateFormat: 'DMY/', // First three are day, month, year in the required order, fourth is the separator, e.g. US would be 'MDY/'
	yearRange: '-10:+10', // Range of years to display in drop-down, either relative to current year (-nn:+nn) or absolute (nnnn:nnnn)
	changeMonth: false, // True if month can be selected directly, false if only prev/next
	changeYear: false, // True if year can be selected directly, false if only prev/next
	firstDay: 1, // The first day of the week, Sun = 0, Mon = 1, ...
	changeFirstDay: false, // True to click on day name to change, false to remain as set
	showOtherMonths: false, // True to show dates in other months, false to leave blank
	minDate: null, // The earliest selectable date, or null for no limit
	maxDate: null, // The latest selectable date, or null for no limit
	speed: 'medium', // Speed of display/closure
	autoPopUp: 'focus', // 'focus' for popup on focus, 'button' for trigger button, or 'both' for either
	closeAtTop: true, // True to have the clear/close at the top, false to have them at the bottom
	hideIfNoPrevNext: true, // True to hide next/previous month links if not applicable, false to just disable them
	customDate: null, // Function that takes a date and returns an array with [0] = true if selectable, false if not,
		// [1] = custom CSS class name(s) or '', e.g. popUpCal.noWeekends
	fieldSettings: null, // Function that takes an input field and returns a set of custom settings for the calendar
	inline: true, // this sets the calendar to always display, use this with customEvent
	customEvent: false, // define an action for the inline calendar

	/* Initialisation. */
	init: function() {
		this.popUpShowing = false;
		this.selectingMonthYear = false;
		this.standalone = false;
		this.lastInput = null;
		this.disabledInputs = [];
		this.calendarDiv = $('<div id="calendar_div"></div>');		
		$('body').append(this.calendarDiv);
		$(document).mousedown(popUpCal.checkExternalClick);
	},

	/* Handle keystrokes. */
	doKeyDown: function(e) {
		if (popUpCal.popUpShowing) {
			switch (e.keyCode) {
				case 9:  popUpCal.hideCalendar(''); break; // hide on tab out
				case 13: popUpCal.selectDate(); break; // select the value on enter
				case 27: popUpCal.hideCalendar(popUpCal.speed); break; // hide on escape
				case 33: popUpCal.adjustDate(-1, (e.ctrlKey ? 'Y' : 'M')); break; // previous month/year on page up/+ ctrl
				case 34: popUpCal.adjustDate(+1, (e.ctrlKey ? 'Y' : 'M')); break; // next month/year on page down/+ ctrl
				case 35: if (e.ctrlKey) popUpCal.clearDate(); break; // clear on ctrl+end
				case 36: if (e.ctrlKey) popUpCal.gotoToday(); break; // current on ctrl+home
				case 37: if (e.ctrlKey) popUpCal.adjustDate(-1, 'D'); break; // -1 day on ctrl+left
				case 38: if (e.ctrlKey) popUpCal.adjustDate(-7, 'D'); break; // -1 week on ctrl+up
				case 39: if (e.ctrlKey) popUpCal.adjustDate(+1, 'D'); break; // +1 day on ctrl+right
				case 40: if (e.ctrlKey) popUpCal.adjustDate(+7, 'D'); break; // +1 week on ctrl+down
			}
		}
		else if (e.keyCode == 36 && e.ctrlKey) { // display the calendar on ctrl+home
			popUpCal.showFor(this);
		}
	},

	/* Filter entered characters. */
	doKeyPress: function(e) {
		var chr = String.fromCharCode(e.charCode == undefined ? e.keyCode : e.charCode);
		return (chr < ' ' || chr == popUpCal.dateFormat.charAt(3) || (chr >= '0' && chr <= '9')); // only allow numbers and separator
	},

	/* Attach an inline calendar to a div. */
	displayInline: function(target) {
		$(target).append('');
		popUpCal.language = this.language ? this.language : 1;
		popUpCal.dayNames = this.dayNamesMl[this.language];
		popUpCal.monthNames = this.monthNamesMl[this.language];
		popUpCal.currentDay = new Date().getDate();
		popUpCal.currentMonth = new Date().getMonth();
		popUpCal.currentYear = new Date().getFullYear();
		popUpCal.selectedDay = this.initDay ? this.initDay : new Date().getDate();
		popUpCal.selectedMonth = this.initMonth ? this.initMonth-1 : new Date().getMonth();
		popUpCal.selectedYear = this.initYear ? this.initYear : new Date().getFullYear();
		popUpCal.adjustDate();
		popUpCal.showCalendar(); 
	},
	
	/* Construct and display the calendar. */
	showCalendar: function() {
		this.popUpShowing = true;

		var today = new Date();
		today = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // clear time
		// build the calendar HTML

		var dateFrom = '1.' + (this.selectedMonth+1) + '.' + this.selectedYear;
		var dateTo = this.getDaysInMonth(this.selectedYear,this.selectedMonth) + '.' + (this.selectedMonth+1) + '.' + this.selectedYear;

		var html = '<div id="monthName">' +
			'<div id="leftArrow"><a href="javascript://" onclick="popUpCal.adjustDate(-1, \'M\');return false;"><div id="leftArrowLink">' + this.prevText + '</div></a></div>' +
			'<div id="rightArrow"><a href="javascript://" onclick="popUpCal.adjustDate(+1, \'M\');return false;"><div id="rightArrowLink">' + this.nextText + '</div></a></div>' +
			'<a href="javascript://" onclick="javascript:selectMonth(\''+ dateFrom + ',' + dateTo + '\');return false;">' + this.monthNames[this.selectedMonth] + ' <span id="cb-calendarYear">' + this.selectedYear + '</span></a></div>';

		html += '<ul>';
		for (var dow = 0; dow < 7; dow++) {
			html += '<li><a href="javascript://" class="inactive">' + this.dayNames[(dow + this.firstDay) % 7] + '</a></li>';
		}
		html += '</ul>';
		var daysInMonth = this.getDaysInMonth(this.selectedYear, this.selectedMonth);
		var leadDays = (this.getFirstDayOfMonth(this.selectedYear, this.selectedMonth) - this.firstDay + 7) % 7;
		var currentDate = new Date(this.currentYear, this.currentMonth, this.currentDay);
		var initDate = new Date(this.initYear, this.initMonth-1, this.initDay);
		var initDateTo = new Date(this.initYear, this.initMonth-1, this.initDayTo);
		var printDate = new Date(this.selectedYear, this.selectedMonth, 1 - leadDays);
		var numRows = Math.ceil((leadDays + daysInMonth) / 7); // calculate the number of rows to generate
		for (var row = 0; row < numRows; row++) { // create calendar rows
			html += '<ul>';
			for (var dow = 0; dow < 7; dow++) { // create calendar days
				var customSettings = (this.customDate ? this.customDate(printDate) : [true, '']);
				var otherMonth = (printDate.getMonth() != this.selectedMonth);
				var unselectable = otherMonth || !customSettings[0] ||
					(this.minDate && printDate < this.minDate) ||
					(this.maxDate && printDate > this.maxDate);
				html += '<li>' + // actions
					(unselectable ? (this.showOtherMonths ? this.printDate.getDate() : '') : '<a href="javascript://" onclick="javascript:selectDay(\'' + printDate.getDate() + '.' + (this.selectedMonth+1) + '.' +  this.selectedYear + '\'); return false;"' + 
					' class="' + (printDate.getTime() == currentDate.getTime() ? 'today' : '') + (printDate.getTime() == initDate.getTime() || (this.initDayTo != 0 && printDate.getTime() >= initDate.getTime() && printDate.getTime() <= initDateTo.getTime()) ? ' chosen' : '') + '">' + printDate.getDate() + '</a></li>');
				printDate.setDate(printDate.getDate() + 1);
			}
			html += '</ul>';
		}
/*
		html += (this.closeAtTop || this.inline ? '' : controls) +
			'<!--[if lte IE 6.5]><iframe src="javascript:false;" id="calendar_cover"></iframe><![endif]-->';
*/
		html += '<br clear=all />';
		// add calendar to element to calendar div
		if (!this.inline) {
			this.calendarDiv.empty().append(html).show(this.speed, this.coverSelects);
			if (this.speed == '') this.coverSelects();
			this.input[0].focus();
		}
		else {
			$('#cb-calendar').empty().append(html);
		}
	}, // end showCalendar

	/* Fix IE < 7 select problems. */
	coverSelects: function() {
		if ($.browser.msie) {
			$('#calendar_cover').css({width: popUpCal.calendarDiv[0].offsetWidth + 4,
				height: popUpCal.calendarDiv[0].offsetHeight + 4});
		}
	},
	
	/* Hide the calendar from view. */
	hideCalendar: function(speed) {
		if (this.popUpShowing) {
			this.calendarDiv.hide(speed, this.tidyStandalone);
			if (speed == '') this.tidyStandalone();
			this.popUpShowing = false;
			this.lastInput = null;
			this.prompt = null;
			if (this.standalone && $.blockUI) $.unblockUI();
			this.standalone = false;
		}
	},

	/* Tidy up after a dialog display. */
	tidyStandalone: function() {
		popUpCal.calendarDiv.removeClass('calendar_dialog');
		$('calendar_prompt').remove();
	},

	/* Action for current link. */
	gotoToday: function() {
		var date = new Date();
		popUpCal.selectedDay = date.getDate();
		popUpCal.selectedMonth = date.getMonth();
		popUpCal.selectedYear = date.getFullYear();
		popUpCal.adjustDate();
	},

	/* Action for selecting a new month/year. */
	selectMonthYear: function(select, period) {
		popUpCal.selectingMonthYear = false;
		popUpCal[period == 'M' ? 'selectedMonth' : 'selectedYear'] =
			select.options[select.selectedIndex].value - 0;
		popUpCal.adjustDate();
	},

	/* Restore input focus after not changing month/year. */
	clickMonthYear: function() {
		if (popUpCal.selectingMonthYear && !$.browser.msie) {
			popUpCal.input[0].focus();
		}
		popUpCal.selectingMonthYear = !popUpCal.selectingMonthYear;
	},

	/* Action for changing the first week day. */
	changeWeekDay: function(a) {
		for (var i = 0; i < 7; i++) {
			if (popUpCal.dayNames[i] == a.firstChild.nodeValue) {
				popUpCal.firstDay = i;
				break;
			}
		}
		popUpCal.showCalendar();
	},

	/* Action for selecting a day. */
	selectDay: function(td) {
		popUpCal.selectedDay = $("a", td).html();
		popUpCal.selectDate();
	},

	/* Update the input field with the selected date. */
	selectDate: function(value) {
		this.currentDay = this.selectedDay;
		this.currentMonth = this.selectedMonth;
		this.currentYear = this.selectedYear;
		this.date = (value != null ? value : this.formatDate(this.selectedDay, this.selectedMonth, this.selectedYear));
		if (this.customEvent) {
			this.customEvent();  // trigger it
		} 
		else {
			// set the input field with the date and fire the change event
			this.input.val(this.date);
			this.input.trigger('change');
		}
		if (!this.inline) {
			this.hideCalendar(this.speed);
		}
	},

	/* Erase the input field and hide the calendar. */
	clearDate: function() {
		this.selectDate('');
	},

	/* Close calendar if clicked elsewhere. */
	checkExternalClick: function(event) {
		if (popUpCal.popUpShowing && !(popUpCal.standalone && $.blockUI)) {
			var node = event.target;
			var cal = popUpCal.calendarDiv[0];
			while (node && node != cal && node.className != 'calendar_trigger') {
				node = node.parentNode;
			}
			if (!node) {
				popUpCal.hideCalendar('');
			}
		}
	},

	/* Set as customDate function to prevent selection of weekends. */
	noWeekends: function(date) {
		var day = date.getDay();
		return [(day > 0 && day < 6), ''];
	},

	/* Format and display the given date. */
	formatDate: function(day, month, year) {
		month++; // adjust javascript month
		var dateString = '';
		for (var i = 0; i < 3; i++) {
			dateString += this.dateFormat.charAt(3) +
				(this.dateFormat.charAt(i) == 'D' ? (day < 10 ? '0' : '') + day :
				(this.dateFormat.charAt(i) == 'M' ? (month < 10 ? '0' : '') + month :
				(this.dateFormat.charAt(i) == 'Y' ? year : '?')));
		}
		return dateString.substring(this.dateFormat.charAt(3) ? 1 : 0);
	},


	/* Ensure numbers are not treated as octal. */
	trimNumber: function(value) {
		if (value == '')
			return '';
		while (value.charAt(0) == '0') {
			value = value.substring(1);
		}
		return value;
	},


	/* Adjust one of the date sub-fields. */
	adjustDate: function(offset, period, dontShow) {
		var date = new Date(this.selectedYear + (period == 'Y' ? offset : 0),
			this.selectedMonth + (period == 'M' ? offset : 0),
			this.selectedDay + (period == 'D' ? offset : 0));
		// ensure it is within the bounds set
		date = (this.minDate && date < this.minDate ? this.minDate : date);
		date = (this.maxDate && date > this.maxDate ? this.maxDate : date);
		this.selectedDay = date.getDate();
		this.selectedMonth = date.getMonth();
		this.selectedYear = date.getFullYear();
		if (!dontShow) {
			this.showCalendar();
		}
	},

	/* Find the number of days in a given month. */
	getDaysInMonth: function(year, month) {
		return 32 - new Date(year, month, 32).getDate();
	},

	/* Find the day of the week of the first of a month. */
	getFirstDayOfMonth: function(year, month) {
		return new Date(year, month, 1).getDay();
	},

	/* Determines if we should allow a "next/prev" month display change. */
	canAdjustMonth: function(offset) {
		var date = new Date(this.selectedYear, this.selectedMonth + offset, 1);
		if (offset < 0) {
			date.setDate(this.getDaysInMonth(date.getFullYear(), date.getMonth()));
		}
		return this.isInRange(date);
	},

	/* Is the given date in the accepted range? */
	isInRange: function(date) {
		return ((!this.minDate || date >= this.minDate) && (!this.maxDate || date <= this.maxDate));
	},

	/* Find an object's position on the screen. */
	findPos: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent) {
				var origcurleft = curleft;
				curleft += obj.offsetLeft;
				if (curleft < 0) {
					curleft = origcurleft;
				}
				curtop += obj.offsetTop;
			}
		}
		return [curleft,curtop];
	}
};

/* Attach the calendar to a jQuery selection. */
$.fn.calendar = function(settings) {
	// customise the calendar object
	$.extend(popUpCal, settings || {});
	// attach the calendar to each nominated input element
	return this.each(function() {
		if (this.nodeName.toLowerCase() == 'input') {
			popUpCal.connectCalendar(this);
		} 
		else if (this.nodeName.toLowerCase() == 'div') {
			popUpCal.displayInline(this);
		}
	});
};

/* Initialise the calendar. */
$(document).ready(function() {
   popUpCal.init();
});
