function daysInMonth(year, month) { 
	result = 31;
	if ((month==4)||(month==6)||(month==9)||(month==11)) result = 30;
	if (month==2) {
		result = 28;
		if (year%4==0) result = 29;
	}
	return result;		
}
		
function formatDayMonth(nvalue) {
	if(nvalue < 10)
	nvalue = "0" + nvalue;
	return nvalue;
}

function datesEqual(date1, date2) {
	if((date1.getYear()==date2.getYear())&&(date1.getMonth()==date2.getMonth())&&(date1.getDate()==date2.getDate()))
		return true;
	else
		return false;
}

var selectedDate, strAction, initPeriod;

function updateCalendar() {
	var year = document.getElementById('calyear').value;
	var month = document.getElementById('calmonth').value;
	var startdate = new Date(year, month-1, 1)
	var dayOfWeek = startdate.getDay();
	var dayCount = daysInMonth(year, month);
	for (var i=1; i<=dayCount; i++)	{
		var strHTML = "";
		strHTML = strHTML + "<a href='" + strAction 
				  + "?date=" + formatDayMonth(i) + "." + formatDayMonth(month) + "." + year + "'>";
		if((dayOfWeek==0)||(dayOfWeek==6))
			strHTML = strHTML + "<font color=#A0A4A0>";
		if((datesEqual(selectedDate, new Date(year, month-1, i))==true)&&(initPeriod!="month"))
			strHTML = strHTML + "<font color=#9c0000><b>";
		strHTML = strHTML + i + "</a>";
		document.getElementById("calday"+i).innerHTML = strHTML;
		dayOfWeek++;
		if (dayOfWeek > 6) dayOfWeek = 0;
	}
	for (; i<=31; i++)	{
		document.getElementById("calday"+i).innerHTML = "  ";
	}
	dayOfWeek = startdate.getDay();	
	for (j=1; j<=31; j++) {
		strHTML="";
		if((dayOfWeek==0)||(dayOfWeek==6))
			strHTML = strHTML + "<font color=#A0A4A0>";
		if((datesEqual(selectedDate, new Date(year, month-1, j))==true)&&(initPeriod!="month"))
			strHTML = strHTML + "<font color=#9c0000>";
		switch (dayOfWeek) {
			case 0: strHTML += "Su "; break;
			case 1: strHTML += "Mo "; break;
			case 2: strHTML += "Tu "; break;
			case 3: strHTML += "We "; break;
			case 4: strHTML += "Th "; break;
			case 5: strHTML += "Fr "; break;
			case 6: strHTML += "Sa "; break;
		}
		document.getElementById("caldayname" + j).innerHTML = strHTML;
		dayOfWeek++;
		if (dayOfWeek > 6) dayOfWeek = 0;
	}
	return true;
}

function initCalendar(year, month, day, action, period) {
	selectedDate = new Date(year, month-1, day);
	strAction = action;
	initPeriod = period;
	updateCalendar();
}
