var monthNames=new Array("Leden","Únor","Březen","Duben",
		"Květen","Červen","Červenec","Srpen",
		"Září","Říjen","Listopad","Prosinec");
var dayNames=new Array("Po","Út","St",
		"Čt","Pá","So","Ne");

function WriteCell( bgColor, day ) {
	document.write("<td class='" + bgColor + "'>" + 
		(day!=0?day:"&nbsp;") + 
		"</td>");
}

function IsToday( d, m, y ) {
var myDate= new Date();

	if ( 	(myDate.getDate()==d) && 
		(myDate.getMonth() == m) && 
		(myDate.getFullYear() == y) )	{
		return true
	}

	return false
}

function WriteMonthTable(the_month,the_year) {
var cellColor
var day_counter=1;
var first_day= new Date(the_year,the_month,1);
    first_day=first_day.getDay();
    first_day=first_day==0?6:first_day-1	
var day_in_month=31;
    if (the_month == 3 || the_month == 5 || the_month == 8 || the_month == 10) { 
	day_in_month=30;
    }
    if (the_month == 1) {
	day_in_month=28;
	if ( ((the_year % 4 == 0) && (the_year % 100 != 0)) || (the_year % 400 == 0) ) {
		day_in_month=29;
	}	
    }

    document.write("<table class='cal-table' cellspacing='1'>");

    // Zahlavi tabulky
    document.write("<tr height='35'><td colspan='7' class='mesic'><b>" + monthNames[the_month] + " " + the_year + "</b></td></tr>");

    // Zahlavi dnu
    document.write("<tr>");
    for( var i=0; i<7; i++ ) {
	document.write("<td class='den'>" 
		+ dayNames[i] + "</td>");
    }
    document.write("</tr>")

    // Prvni radek tabulky
    document.write("<tr>");
    for (i=0;i<(first_day);i++) {
    	WriteCell("prazdna",0)
    }
    for (i=first_day;i<7;i++) {
	cellColor = IsToday(day_counter,the_month,the_year) ? "red" : "white"
	WriteCell(cellColor,day_counter)
	day_counter=day_counter + 1;
    }
    document.write("</tr>");

    // Dalsi radky
    for (a=1;a<6;a++) {
	document.write("<tr>");
	for (b=0;b<7;b++) {
		cellColor = IsToday(day_counter,the_month,the_year) ? "activ" : "white"

		WriteCell(cellColor,day_counter)
		if (day_counter >= day_in_month){
			for (i=b;i<6;i++) {
				WriteCell("prazdna",0)
			}
			b=7; a=5;
		}
		day_counter=day_counter + 1;
	}
	document.write("</tr>");	
    }
    document.write("</table>");
}

// Vykresleni tabulky pro aktualni mesic
var pomDate= new Date();
WriteMonthTable( pomDate.getMonth(),pomDate.getFullYear() );
