function getMonthDays(year, month) {
     return 32 - new Date(year, month, 32).getDate();
}
Date.prototype.getMonthDays = function (){
	return 32 -  new Date(this.getYear(), this.getMonth(), 32).getDate();
}
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}
var visibleCalendars = [];
var monthNames = ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"]
function getCalendar(year, month, parentId, element){
	var parent = document.getElementById(parentId);
	var output = "";
	var pastYear, pastMonth, nextYear, nextMonth;
	if(month == 0){
		pastYear = year - 1;
		pastMonth = 11;
		nextYear = year;
		nextMonth = month + 1;
	}else if(month == 11){
		pastYear = year;
		pastMonth = month - 1;
		nextYear = year + 1;
		nextMonth = 0;
	}else{
		pastYear = year;
		pastMonth = month - 1;
		nextYear = year;
		nextMonth = month + 1;
	}
	output += "<div style=\"text-align:center;\"><a href=\"javascript:void(0)\" onclick=\"getCalendar(" + pastYear + ", " + pastMonth + ", '" + parentId + "', '" + element + "')\">&lt;</a> ";
	output += monthNames[month];
	output += " <a href=\"javascript:void(0)\" onclick=\"getCalendar(" + nextYear + ", " + nextMonth + ", '" + parentId + "', '" + element + "')\">&gt;</a></div>";
	output += "<table class=\"calendar\"><tr>"
	var firstDay = (new Date(year, month, 1)).getDay();
	if(firstDay == 0) firstDay = 7;
	for(var i = 1; i < firstDay; i++){
		output += "<td class=\"nosel\">&nbsp;</td>";
	}
	var monthDays = getMonthDays(year, month);
	var day;
	for(var i = 1; i <= monthDays; i++){
		day = new Date(year, month, i);
		output += "<td class=\"" + (day.getDay() == 0 ? "sunday" : "weekday") + "\">";
		output += "<a href=\"javascript:void(0)\" onclick=\"";
		output += "selectDate('" + parent.id + "', '" + element + "', '" + i + "\/" + (month + 1) + "\/" + year +"')";
		output += "\" class=\"" + (day.getDay() == 0 ? "sunday" : "weekday") + "\">" + i + "</a></td>";
		if(day.getDay() == 0 && i < monthDays){
			output += "</tr>\n<tr>";
		}else if(day.getDay() == 0){
			output += "</tr>\n";
		}
	}
	if((new Date(year, month, i-1)).getDay() != 0){
		do{
			output += "<td class=\"nosel\">&nbsp;</td>";
		}while((new Date(year, month, i++)).getDay() != 0)
	}
	output += "</table>\n\n";
	var div = document.getElementById(element);
	div.style.display = "block";
	div.style.top = parent.offsetTop + parent.offsetHeight + "px";
	div.style.left = parent.offsetLeft + "px"; // + parent.offsetHeight;
	div.style.zIndex = 9999;
	div.innerHTML = output;
	visibleCalendars.push(parentId);
}
function showCalendar(year, month, parent, element){
	var id = visibleCalendars.indexOf(parent);
	if(id == -1){
		if(isNaN(year) || year == null){
			year = document.getElementById(parent).value.split("/")[2];
			if(isNaN(year) || year == null) year = (new Date()).getFullYear();
		}
		if(isNaN(month) || month == null){
			month = document.getElementById(parent).value.split("/")[1] - 1;
			if(isNaN(month) || month == null) month = (new Date()).getMonth();
		}
		getCalendar(year, month, parent, element);
	}else{
		hideCalendar(parent, element);
	}
}
function hideCalendar(parentId, element){
	visibleCalendars.splice(visibleCalendars.indexOf(parentId), 1);
	document.getElementById(element).innerHTML = "";
	document.getElementById(element).style.display = "none";
}
function selectDate(parentId, element, date){
	document.getElementById(parentId).value = date;
	hideCalendar(parentId, element);
}