var calendarInput, calendarTable, calendarCell, calendarTimer, calendarFirstDate, calendarLastDate, calendarAddition;

function activateCalendar(object, addition, firstDate, lastDate)
{
	calendarAddition = addition;
	calendarFirstDate = getDate(firstDate);
	calendarLastDate = getDate(lastDate);
	if (document.getElementById('calendarTable')) document.body.removeChild(document.getElementById('calendarTable'));
	calendarInput = object;
	calendarInput.onblur = new Function("hideCalendar(1);");
	calendarTable = document.body.appendChild(document.createElement('table'))
	calendarTable.id = 'calendarTable';
	calendarTable.style.left = absLeft(object) + 'px';
	calendarTable.style.top = (absTop(object) + object.offsetHeight) + 'px';
	
	/*
	if (window.event) calendarTable.style.left = window.event.clientX + 20;
	if (window.event) calendarTable.style.top = window.event.clientY + document.body.scrollTop;
	if (window.event) if (document.body.offsetWidth < window.event.clientX + 180) calendarTable.style.left = window.event.clientX - 130;
	if (window.event) if (document.body.offsetHeight < window.event.clientX + 150) calendarTable.style.top = window.event.clientY + document.body.scrollTop - 120;
	*/
	showCalendar();
};

function absLeft(el) 
{
  return (el.offsetParent)? 
	el.offsetLeft+absLeft(el.offsetParent) : el.offsetLeft;
}

function absTop(el) 
{
	return (el.offsetParent)? 
	el.offsetTop+absTop(el.offsetParent) : el.offsetTop;
}

function showCalendar(selectedYear, selectedMonth, surrounding)
{
	hideCalendar(60);
	calendarBody = calendarTable.appendChild(document.createElement('tbody'));
	currentDate = new Date();
	if (isNaN(selectedYear)) selectedYear = currentDate.getYear() % 1900 + 1900;
	if (isNaN(selectedMonth)) selectedMonth = currentDate.getMonth();
	if (isNaN(surrounding)) surrounding = 0;
	startOfMonth = new Date(selectedYear, selectedMonth, 1);
	startOfSelection =  - (startOfMonth.getDay() + 5) % 7 - surrounding * 7;
	selectedYear = startOfMonth.getYear() % 1900 + 1900;
	selectedMonth = startOfMonth.getMonth();
	weekday = new Array('Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag');
	calendarBody.appendChild(document.createElement('tr'));
	calendarCell = calendarBody.lastChild.appendChild(document.createElement('td'));
	calendarCell.title = 'erweitern';
	calendarCell.onclick = new Function("hideCalendar(); showCalendar(" + selectedYear + ", " + selectedMonth + ", " + (surrounding + 2) + ");");
	calendarCell.appendChild(document.createTextNode('\u2026'));
	calendarCell = calendarBody.lastChild.appendChild(document.createElement('td'));
	calendarCell.title = 'r\u00fcckw\u00e4rts';
	if (currentDate.getYear() % 1900 + 1900 < selectedYear || currentDate.getYear() % 1900 + 1900 == selectedYear && currentDate.getMonth() < selectedMonth) calendarCell.className = 'current';
	calendarCell.onclick = new Function("hideCalendar(); showCalendar(" + selectedYear + ", " + (selectedMonth - 1) + ", " + surrounding + ");");
	calendarCell.appendChild(document.createTextNode('<'));
	calendarCell = calendarBody.lastChild.appendChild(document.createElement('th'));
	calendarCell.title = 'Jahr wechseln';
	if (currentDate.getYear() % 1900 + 1900 == selectedYear && currentDate.getMonth() == selectedMonth) calendarCell.className = 'current';
	calendarCell.onclick = new Function("hideCalendar(); showCalendar(prompt(\"Jahr\", currentDate.getYear() % 1900 + 1900), currentDate.getMonth(), " + surrounding + ");");
	calendarCell.colSpan = 3;
	calendarCell.appendChild(document.createTextNode(String(selectedYear) + '-' + ((selectedMonth < 9) ? '0' : '') + String(selectedMonth + 1)));
	calendarCell = calendarBody.lastChild.appendChild(document.createElement('td'));
	calendarCell.title = 'vorw\u00e4rts';
	if (currentDate.getYear() % 1900 + 1900 > selectedYear || currentDate.getYear() % 1900 + 1900 == selectedYear && currentDate.getMonth() > selectedMonth) calendarCell.className = 'current';
	calendarCell.onclick = new Function("hideCalendar(); showCalendar(" + selectedYear + ", " + (selectedMonth + 1) + ", " + surrounding + ");");
	calendarCell.appendChild(document.createTextNode('>'));
	calendarCell = calendarBody.lastChild.appendChild(document.createElement('td'));
	calendarCell.title = 'schlie\u00dfen';
	calendarCell.onclick = new Function("hideCalendar();");
	calendarCell.appendChild(document.createTextNode('\u00d7'));
	for (selectedDay = startOfSelection; selectedDay < startOfSelection + surrounding * 14 + 42; selectedDay++) {
		if ((selectedDay - startOfSelection) % 7 == 0) calendarBody.appendChild(document.createElement('tr'));
		selectedDate = new Date(selectedYear, selectedMonth, selectedDay);
		calendarCell = calendarBody.lastChild.appendChild(document.createElement('td'));
		if (selectedDate.getDay() > 5 || selectedDate.getDay() < 1) calendarCell.className = 'weekend';
		if (selectedDate.getMonth() != selectedMonth) calendarCell.className = 'surrounding';
		if (selectedDate.getYear() == currentDate.getYear() && selectedDate.getMonth() == currentDate.getMonth() && selectedDate.getDate() == currentDate.getDate()) calendarCell.className = 'current';
		calendarCell.className += (selectedDate >= calendarFirstDate && selectedDate <= calendarLastDate) ? ' valid' : ' invalid';
 		calendarCell.title = weekday[selectedDate.getDay()] + ', ' + getDateISO(selectedDate);
		calendarCell.onmouseover = new Function("this.id = 'hover';");
		calendarCell.onmouseout = new Function("this.id = '';");
		calendarCell.onclick = new Function("submitDate('" + getDateISO(selectedDate) + "'); hideCalendar();");
		calendarCell.appendChild(document.createTextNode(selectedDate.getDate()));
	};
};

function hideCalendar(delay)
{
	if (calendarTimer) window.clearTimeout(calendarTimer);
	if (isNaN(delay)) calendarTable.removeChild(calendarBody);
	else calendarTimer = window.setTimeout("calendarTable.removeChild(calendarBody);", delay *1000);
};

function submitDate(date)
{
	calendarInput.value = date + (calendarAddition ? calendarAddition : '');
};

function getDate (date) {
	if (date) return new Date(date.substr(0, 4), date.substr(5, 2) - 1, date.substr(8, 2));
}

function getDateISO (date) {
	if (date) return String(date.getYear() % 1900 + 1900) + '-' + ((date.getMonth() < 9) ? '0' : '') + String(date.getMonth() + 1) + '-' + ((date.getDate() <= 9) ? '0' : '') + String(date.getDate());
}