var TODAY = new Date();
var calDiv = null;
var calDivXRel, calDivYRel;
var weekendColor = "#e0e0e0";
var fontface = "Verdana";
var isNS6up = ((navigator.userAgent.toLowerCase().indexOf('mozilla')!=-1) && ((navigator.userAgent.toLowerCase().indexOf('spoofer')==-1) && (navigator.userAgent.toLowerCase().indexOf('compatible') == -1))) && (parseInt(navigator.appVersion) >= 5);
var isIE4up = (navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (parseInt(navigator.appVersion) >= 4);
var deltaX, deltaY;

if (isIE4up) {
	deltaX = 0;
	deltaY = 23;
} else {
	deltaX = 0;
	deltaY = 0;
}

Calendar.MonthNames = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];
Calendar.DaysInMonthNY = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Calendar.DaysInMonthLY = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(elem, myMonth, myYear) {
	if ((myMonth == null) && (myYear == null))	return;

	if (myMonth == null) {
		this.gMonthName = null;
		this.gMonth = null;
	} else {
		this.gMonthName = Calendar.MonthNames[myMonth];
		this.gMonth = new Number(myMonth);
	}

	this.gYear = myYear;
	this.gReturnItem = elem;
}

Calendar.getDaysOfMonth = function(myMonth, myYear) {
	if ((myYear % 4) == 0) {
		if ((myYear % 100) == 0 && (myYear % 400) != 0) {
			return Calendar.DaysInMonthNY[myMonth];
		} else {
			return Calendar.DaysInMonthLY[myMonth];
		}
	} else {
		return Calendar.DaysInMonthNY[myMonth];
	}
}

Calendar.addMonthsToDate = function(myMonth, myYear, incMonth, incYear) {
	var retValues = new Array();
	var diffMonth;
	diffMonth = parseInt(myMonth) + parseInt(incMonth);
	retValues[0] = (((diffMonth) % 12) + 12) % 12;
	retValues[1] =  parseInt(myYear) + parseInt(incYear) + Math.floor(diffMonth / 12);
	return retValues;
}

Calendar.prototype.makeHTML = function() {
	var retCode = ""
	
	// Tabellen oeffnen
	retCode += '<table width="180" border="0" cellpadding="1" cellspacing="0"><tr><td class="calHeaderYMSelected">';

	retCode += '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="calHeaderYM">';
	retCode += 'Wählen Sie ein Datum';
	retCode += '</td><td align="right" class="calHeaderYM">';
	retCode += '<a href="#" onClick="hideLayer(); window.status=\'\'; return true;" onMouseOver="window.status=\'Schliessen\'; return true;" onMouseOut="window.status=\'\'; return true;" >[x]</a>';
	retCode += '</td></tr></table>';

	// Anzeige Monat / Jahr
	retCode += '<table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td class="calHeaderYMAlternating">' + this.gMonthName + ' ' + this.gYear + '</td></tr></table>';

	// Monats- und Jahresnavigation
	var prevMMYYYY = Calendar.addMonthsToDate(this.gMonth, this.gYear, -1, 0);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];
	//
	var nextMMYYYY = Calendar.addMonthsToDate(this.gMonth, this.gYear, 1, 0);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	//
	retCode += '<table width="100%" border="1" cellspacing="0" cellpadding="0"><tr>';
	retCode += '<td align="center" class="calDimSun"><a href="#" onMouseOver="window.status=\'Ein Jahr zur&uuml;ck\'; return true;" onMouseOut="window.status=\'\'; return true;" onClick="renderCalendar(\'' + this.gReturnItem + '\', \'' + this.gMonth + '\', \'' + (parseInt(this.gYear)-1) + '\');">[&lt;Jahr]<\/a></td>';
	retCode += '<td align="center" class="calDimSun"><a href="#" onMouseOver="window.status=\'Einen Monat zur&uuml;ck\'; return true;" onMouseOut="window.status=\'\'; return true;" onClick="renderCalendar(\'' + this.gReturnItem + '\', \'' + prevMM + '\', \'' + prevYYYY + '\');">[&lt;Mon]<\/a></td>';
	retCode += '<td align="center" class="calDimSun"></td>';
	retCode += '<td align="center" class="calDimSun"><a href="#" onMouseOver="window.status=\'Einen Monat vorw&auml;rts\'; return true;" onMouseOut="window.status=\'\'; return true;" onClick="renderCalendar(\'' + this.gReturnItem + '\', \'' + nextMM + '\', \'' + nextYYYY + '\');">[Mon&gt;]<\/a></td>';
	retCode += '<td align="center" class="calDimSun"><a href="#" onMouseOver="window.status=\'Ein Jahr vorw&auml;rts\'; return true;" onMouseOut="window.status=\'\'; return true;" onClick="renderCalendar(\'' + this.gReturnItem + '\', \'' + this.gMonth + '\', \'' + (parseInt(this.gYear)+1) + '\');">[Jahr&gt;]<\/a></td>';
	retCode += '</tr></table>';

	// Abstand
	retCode += '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td><img src="images/einpix.gif" alt="" width="1" height="10"></td></tr></table>';

	// Monatsuebersicht
	retCode += this.getCalendarCode();

	// Abstand
	retCode += '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td><img src="images/einpix.gif" alt="" width="1" height="3"></td></tr></table>';

	// Tabellen schliessen
	retCode += "</td></tr></table>";

	return retCode;
}

Calendar.prototype.getCalendarCode = function() {
	var retCode = "";
	retCode += '<div align=center><table border="1" bgcolor="white">';
	retCode += this.getHeaderCode()
	retCode += this.getMonthCode();
	retCode += '</table></div>';
	return retCode;
}

Calendar.prototype.getHeaderCode = function() {
	var retCode;
	
	retCode = "<tr>";
	retCode += "<td width=\"14%\" class=\"calHWeekC\">Mo</td>";
	retCode += "<td width=\"14%\" class=\"calHWeekC\">Di</td>";
	retCode += "<td width=\"14%\" class=\"calHWeekC\">Mi</td>";
	retCode += "<td width=\"14%\" class=\"calHWeekC\">Do</td>";
	retCode += "<td width=\"14%\" class=\"calHWeekC\">Fr</td>";
	retCode += "<td width=\"14%\" class=\"calHSatC\">Sa</td>";
	retCode += "<td width=\"14%\" class=\"calHSunC\">So</td>";
	retCode += "</tr>";
	
	return retCode;
}

Calendar.prototype.getMonthCode = function() {
	var myDate = new Date();
	myDate.setDate(1);
	myDate.setMonth(this.gMonth);
	myDate.setFullYear(this.gYear);

	var myFirstDay = (myDate.getDay() + 6) % 7;
	var myDay = 1;
	var myLastDay = Calendar.getDaysOfMonth(this.gMonth, this.gYear);
	var isLastDay = false;
	var retCode = "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/
	retCode = retCode + "<tr>";
	for (i=0; i<myFirstDay; i++) {
		retCode = retCode + "<td width=\"14%\" class=\"" + this.getDayClass(i) + "\"> </td>";
	}

	// Write rest of the 1st week
	for (j=myFirstDay; j<7; j++) {
		retCode = retCode + "<td width=\"14%\" class=\"" + this.getDayClass(j) + "\">" + 
			"<A HREF='javascript:void(0);' " + 
				"onMouseOver=\"window.status='Datum " + this.getFormattedDate(myDay) + " setzen'; return true;\" " +
				"onMouseOut=\"window.status=' '; return true;\" " +
				"onClick=\"document." + this.gReturnItem + ".value='" + 
				this.getFormattedDate(myDay) + 
				"'; on_change(); hideLayer();\">" + 
				this.getDayCode(myDay) + 
			"</a>" + 
			"</td>";
		myDay++;
	}
	retCode = retCode + "</tr>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		retCode = retCode + "<tr>";

		for (j=0; j<7; j++) {
			retCode = retCode + "<td width=\"14%\" class=\"" + this.getDayClass(j) + "\">" + 
				"<a href='javascript:void(0);' " +
					"onMouseOver=\"window.status='Datum " + this.getFormattedDate(myDay) + " setzen'; return true;\" " +
					"onMouseOut=\"window.status=' '; return true;\" " +
					"onClick=\"document." + this.gReturnItem + ".value='" + 
					this.getFormattedDate(myDay) + 
					"'; on_change(); hideLayer();\">" + 
				this.getDayCode(myDay) + 
				"</a>" + 
				"</td>";
			myDay++;

			if (myDay > myLastDay) {
				isLastDay = true;
				break;
			}
		}

		if (j == 6) {
			retCode = retCode + "</tr>";
		}
		if (isLastDay) {
			break;
		}
	}
	
	// Auffuellen der letzten Woche
	for (m=1; m<(7-j); m++) {
			retCode = retCode + "<td width=\"14%\" class=\"" + this.getDayClass(j+m) + "\"> </td>";
	}
	
	return retCode;
}

Calendar.prototype.getDayCode = function(myDay) {
	if ((myDay == TODAY.getDate()) && (this.gMonth == TODAY.getMonth()) && (this.gYear == TODAY.getFullYear())) {
		return ("<span style=\"background-color:yellow\">" + myDay + "</span>");
	} else {
		return (myDay);
	}
}

Calendar.prototype.getDayClass = function(myDay) {
	var i, dayClass;
	if (myDay == 5) {
		dayClass = "calSatC";
	} else {
		if (myDay == 6) {
			dayClass = "calSunC";
		} else {
			dayClass = "calWeekC";
		}
	}
	return dayClass;
}

Calendar.prototype.getFormattedDate = function(myDay) {
	var myMonth = 1 + this.gMonth;
	myMonth = '0' + myMonth
	myMonth = myMonth.substr(myMonth.length - 2, 2)
	myDay = '0' + myDay
	myDay = myDay.substr(myDay.length - 2, 2)
	return myDay + "." + myMonth + "." + this.gYear;
}

function showLayer(){
	calDiv.style.visibility="visible";
	return true
}

function hideLayer(){
	calDiv.style.visibility="hidden";
	return true;
}

function renderCalendar(elem, myMonth, myYear) {
	var myCal = new Calendar(elem, myMonth, myYear);
	hideLayer();
	calDiv.innerHTML =  myCal.makeHTML();
	showLayer();
}

function showCalendar(elem, myMonth, myYear, xPosRel, yPosRel, posDivName) {
	showCalendarOpt(elem, myMonth, myYear, xPosRel, yPosRel, posDivName);
	if (isIE4up) {
		showCalendarOpt(elem, myMonth, myYear, xPosRel, yPosRel, posDivName);
	}
}

function showCalendarOpt(elem, myMonth, myYear, xPosRel, yPosRel, posDivName) {
	if (myMonth == "" || myMonth == null) {
		myMonth = new String(TODAY.getMonth());
	} else {
		// kein parseInt() wg. Bug in Javascript bei parseInt('08') und parseInt('09')
		myMonth = myMonth - 1;
	}
	if (myYear == "" || myYear == null) {
		myYear = new String(TODAY.getFullYear().toString());
	} else {
		myYear  = parseInt(myYear );
	}
	calDiv = document.getElementById('calDiv');
	calDivXRel = xPosRel;
	calDivYRel = yPosRel;
	moveLayer(posDivName);
	renderCalendar(elem, myMonth, myYear);
}

function moveLayer(myPosDivName){
	var posDivName = myPosDivName
	if (calDiv) {
		if (posDivName == "" || posDivName == null) {
			posDivName = 'posDiv'
		}
		var posDiv = document.getElementById(posDivName);
		if (posDiv) {
			var oleft = posDiv.offsetLeft + calDivXRel + deltaX;
			var otop = posDiv.offsetTop + calDivYRel + deltaY;
			var posLocator = document.getElementById('posOuterLocator');
			if (posLocator) {
				var parentDiv = posLocator.parentNode;
				var posInnerLocator = document.getElementById('posInnerLocator');
				oleft = oleft - parentDiv.offsetLeft - posInnerLocator.offsetLeft;
				otop = otop - parentDiv.offsetTop;
			}
			calDiv.style.left =  oleft + 'px';
			calDiv.style.top = otop + 'px';
		}
	}
}

window.onresize=function(){moveLayer();}