var calendarTargetfield;
var calendarTargetdiv = 'fgabi2005calendar';
var calendarYear;
var calendarMonth;

function initcalendar(evt, targetfield) {
	calendarTargetfield = targetfield;
	document.getElementById(calendarTargetdiv).style.position = 'absolute';
	document.getElementById(calendarTargetdiv).style.background = 'white';
	document.getElementById(calendarTargetdiv).style.border = 'black 1px solid';
	try {
		document.getElementById(calendarTargetdiv).style.left = evt.pageX;
		document.getElementById(calendarTargetdiv).style.top = evt.pageY;
	} catch(err) {
		document.getElementById(calendarTargetdiv).style.left = evt.x;
		document.getElementById(calendarTargetdiv).style.top = evt.y;	
	}
	//document.onclick = function(){document.getElementById(calendarTargetdiv).style.visibility = 'hidden';}
	showcalendar();
}

function showcalendar(year, month) {
	document.getElementById(calendarTargetdiv).innerHTML = '';
	
	var strTemp = '';
	var objDate = new Date();
	var months = new Array("Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
	
	if((year != '') && (parseInt(year) < 2038) && (parseInt(year) != 'NaN' ))
		objDate.setYear(year);
	
	if((month != '') && (parseInt(month) <= 12) && (parseInt(month) >= 1))
		objDate.setMonth(month);
	
	calendarYear = objDate.getFullYear();
	calendarMonth = objDate.getMonth();
	
	// -----------------------------------------------
	strTemp = '<table width=\"280px\">' +
		'	<tr>' +
		'		<td width=\"1%\">' +
		'			<select id=\"month\" onchange=\"showcalendar(document.getElementById(\'year\').value, document.getElementById(\'month\').value);\">';
	for(i = 0; i < 12; i++) {
		if(i == objDate.getMonth()) strTemp += '<option value=\"' + i + '"\" selected>' + months[i];
		else strTemp += '<option value=\"' + i + '"\">' + months[i];
	}
	strTemp += '</select>' +
		'		</td>';
	// -----------------------------------------------
	
	// -----------------------------------------------
	strTemp += '		<td width=\"1%\">' +
		'			<select id=\"year\" onchange=\"showcalendar(document.getElementById(\'year\').value, document.getElementById(\'month\').value);\">';
		//strTemp += '<option value=\"' + objDate.getFullYear() + '\">' + objDate.getFullYear();
	for(i = objDate.getFullYear() - 5; i <= objDate.getFullYear() + 5; i++) {
		if(i == objDate.getFullYear()) strTemp += '<option value=\"' + i + '"\" selected>' + i;
		else strTemp += '<option value=\"' + i + '"\">' + i;
	}
	strTemp += '</select>' +
		'		</td>';
	// -----------------------------------------------
	
	//strTemp += '<td><a href=\"javascript:showcalendar(\'\', document.getElementById(\'year\').value, document.getElementById(\'month\').value);\">Go</a></td>';
	
	strTemp += '<td align=\"right\"><a href=\"javascript:hidecalendar();\">X</a></td>';
	
	strTemp += '</tr></table>';
	
	strTemp += '<table width=\"280px\" border=1>\n' +
		'<tr>' +
		'	<th width=\"14%\">Mo</th>' +
		'	<th width=\"14%\">Di</th>' +
		'	<th width=\"14%\">Mi</th>' +
		'	<th width=\"14%\">Do</th>' +
		'	<th width=\"14%\">Fr</th>' +
		'	<th width=\"15%\">Sa</th>' +
		'	<th width=\"15%\">So</th>' +
		'</tr>' +
		'<tr>';
		
	objDate.setDate(1);
	
	switch(objDate.getDay()) {
		case 0:
			strTemp += '<td></td>';
		case 6:
			strTemp += '<td></td>';
		case 5:
			strTemp += '<td></td>';
		case 4:
			strTemp += '<td></td>';
		case 3:
			strTemp += '<td></td>';
		case 2:
			strTemp += '<td></td>';
		case 1:
			strTemp += '<td><a href=\"javascript:selectDate(1);\">1</a></td>';
	}
	
	for(i = 2; true; i++) {
		objDate.setDate(i);
		if(objDate.getDate() != i) break;
		if(objDate.getDay() == 1) strTemp += '</tr><tr>';
		strTemp += '<td><a href=\"javascript:selectDate(' + i + ');\">' + i + '</a></td>';
	}
	
	document.getElementById(calendarTargetdiv).innerHTML = strTemp;
	document.getElementById(calendarTargetdiv).style.position = 'absolute';
	document.getElementById(calendarTargetdiv).style.visibility = 'visible';
	
}

function selectDate(day) {
	//document.getElementById(calendarTargetfield).value = day + '.' + (calendarMonth + 1) + '.' + calendarYear;
	document.getElementsByName(calendarTargetfield)[0].value = day + '.' + (calendarMonth + 1) + '.' + calendarYear;
	document.getElementById(calendarTargetdiv).style.visibility = 'hidden';
}

function hidecalendar() {
	document.getElementById(calendarTargetdiv).style.visibility = 'hidden';
}