function leapYear(year) {
	if (year % 4 == 0) // basic rule
		return true // is leap year
	return false // is not leap year
}

function getDays(month, year) {
	// create array to hold number of days in each month
	var ar = new Array(12)
	ar[0] = 31 // January
	ar[1] = (leapYear(year)) ? 29 : 28 // February
	ar[2] = 31 // March
	ar[3] = 30 // April
	ar[4] = 31 // May
	ar[5] = 30 // June
	ar[6] = 31 // July
	ar[7] = 31 // August
	ar[8] = 30 // September
	ar[9] = 31 // October
	ar[10] = 30 // November
	ar[11] = 31 // December
	// return number of days in the specified month (parameter)
	return ar[month-1]
}

function getMonthName(month) {
	// create array to hold name of each month
	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 name of specified month (parameter)
	return ar[month-1]
}

function setCal(day, month, year) {
	// standard time attributes
	var monthName = getMonthName(month)
	var date = new Date(year, month, day)
	// create instance of first day of month, and extract the day on which it occurs
	var firstDayInstance = new Date(year, month-1, 1)
	var firstDay = firstDayInstance.getDay()
	if (firstDay == 0) firstDay = 7;
	firstDayInstance = null
	// number of days in current month
	var days = getDays(month, year)
	// call function to draw calendar
	drawCal(firstDay, days, date, monthName, year)
}

function drawCal(firstDay, lastDate, date, monthName, year) {
	// create basic table structure
	var text = "" // initialize accumulative variable to empty string
	text += '<table width="100%"  border="0" cellspacing="0" cellpadding="0">' // table settings
	text += '<tr><td colspan="7"><hr color="#CCCCCC" size="1"></td><tr>'
	text += '<tr><td colspan="7" align="center" class="titolisezioni">'
	text += monthName + ' ' + year
	text += '</td></tr>'
	text += '<tr><td colspan="7"><hr color="#CCCCCC" size="1"></td></tr>'
	// variables to hold constant settings
	var openCol = '<td class="titolisezioni" width=10 height=20>'
	var closeCol = '</td>'
	// create array of abbreviated day names
	var weekDay = new Array(7)
	weekDay[0] = "Lu"
	weekDay[1] = "Ma"
	weekDay[2] = "Me"
	weekDay[3] = "Gi"
	weekDay[4] = "Ve"
	weekDay[5] = "Sa"
	weekDay[6] = "Do"
	// create first row of table to set column width and specify week day
	text += '<TR ALIGN="center" VALIGN="center">'
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += openCol + weekDay[dayNum] + closeCol
	}
	text += '</TR>'
	// declaration and initialization of two variables to help with tables
	var digit = 1
	var curCell = 1
	for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
		text += '<TR ALIGN="right" VALIGN="top">'
		for (var col = 1; col <= 7; ++col) {
			if (digit > lastDate)
				break
			if (curCell < firstDay) {
				text += '<TD class="testigiornical"></TD>';
				curCell++
			} else {
				if (digit == date.getDate())
					text += openCol + digit + closeCol
				else
					text += openCol + '<a href="#" onClick="changeDay(' + digit + ')">' + digit + '</a>'
				digit++
			}
		}
		text += '</TR>'
	}
	// close all basic table tags

	text += '</TABLE>'

	// print accumulative HTML string
	document.write(text)
}