setCal();
function leapYear(year) {
	if (year % 4 == 0) {
		return true; // è bisestile
	}
	return false; // non è bisestile
}

function getDays(month, year) {
  // array giorni del mese
	var ar = new Array(12);
	ar[0] = 31;						// Gennaio
	ar[1] = (leapYear(year)) ? 29 : 28;	// Febbraio
	ar[2] = 31;						// Marzo
	ar[3] = 30;						// Aprile
	ar[4] = 31;						// Maggio
	ar[5] = 30;						// Giugno
	ar[6] = 31;						// Luglio
	ar[7] = 31;						// Agosto
	ar[8] = 30;						// Settembre
	ar[9] = 31;						// Ottobre
	ar[10] = 30;						// Novembre
	ar[11] = 31;						// Dicembre
	return ar[month];
}

function getMonthName(month) {
	// array dei mesi
	var ar = new Array(12);
	ar[0] = "Gennaio";
	ar[1] = "Febbraio";
	ar[2] = "Marzo";
	ar[3] = "Aprile";
	ar[4] = "Maggio";
	ar[5] = "Giugno";
	ar[6] = "Luglio";
	ar[7] = "Agosto";
	ar[8] = "Settembre";
	ar[9] = "Ottobre";
	ar[10] = "Novembre";
	ar[11] = "Dicembre";
	return ar[month];
}

function setCal() {
	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth();
	var monthName = getMonthName(month);
	var date = now.getDate();
	now = null;

	var firstDayInstance = new Date(year, month, 1);
	var firstDay = firstDayInstance.getDay();
	firstDayInstance = null;

	// numero giorni nel mese corrente
	var days = getDays(month, year);

	// creo il calendario
	strutturaCal(firstDay + 1, days, date, monthName, year);
}

function strutturaCal(firstDay, lastDate, date, monthName, year) {
	var text = "";
	text += '<TABLE cellspacing=1 class=Tab_sfondo width=100%>';
	text += '<TH COLSPAN=7 height=18 class=Tab_intestazione>';
	text += monthName + ' ' + year;
	text += '</TH>';

	var openCol = '<td class=giorniParola>';
	var closeCol = '</td>';

	// array nomi giorni della settimana
	var weekDay = new Array(7);
	weekDay[0] = "Do";
	weekDay[1] = "Lu";
	weekDay[2] = "Ma";
	weekDay[3] = "Me";
	weekDay[4] = "Gi";
	weekDay[5] = "Ve";
	weekDay[6] = "Sa";
	
	// Riga dei giorni in parola
	text += '<TR ALIGN="center" VALIGN="center">';
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += openCol + weekDay[dayNum] + closeCol; 
	}
	text += '</TR>';
	
	var dayOfMonth = 1;
	var curCell = 1;
	
	// Riga dei giorni in numero
	for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
		text += '<TR ALIGN="right" VALIGN="bottom" height="14">';
		for (var col = 1; col <= 7; ++col) {
			if ((curCell < firstDay) || (dayOfMonth > lastDate)) {
				text += '<td height="14" width="18" class=giorniNumero> </td>';
				curCell++
			} else {
				if (dayOfMonth == date) {
					text += '<td height="14" width="18" class=giornoToday><marquee direction="left" scrollAmount=1 style="cursor:hand">' + dayOfMonth + '</marquee></td>';
				} else {
					text += '<td height="14" width="18" class=giorniNumero>' + dayOfMonth + '</td>';
				}
				dayOfMonth++;
			}
		}
		text += '</TR>';
	}
	text += '</TABLE>';

	document.write(text); 
}