function az(MM, YYYY, action, hSelInit, hSelOff) {

	// action = departure / return;
	// definisco la data:
	var d = new Date();

	if( !MM && !YYYY ){
		var MM = document.getElementById(action+'Month').value-1;

		if( MM.toString().length == 6 ){
			if(MM.toString().substr(4,1) == '0')
				MM = parseInt(MM.toString().substr(5,1));
			else
				MM = parseInt(MM.toString().substr(4,2));
		}

		var YYYY = d.getFullYear();
		if( MM < d.getMonth() )
			YYYY++;
	}

	d.setDate(1);

	d.setMonth(MM);
	d.setFullYear(YYYY);

	var totalMonthDays = getMonthDays(MM, YYYY);

	var months = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
	
	// fill the calendar with the first days of the week in the first line of the month to display:
	var calendar = new Array();
	
	// Sunday hack
	var j=(d.getDay()==0)?7:d.getDay();

	for (var i = 1; i < j; i++) {
		calendar.push("");
	}
	
	// fill the calendar with the days of the month to display:
	for (var i = 1; i <= totalMonthDays; i++) {
		calendar.push(i);
	}
	
	//
	var month = new Array();
	for (var i = 0; i < 7; i++) {
		month.push(new Array());
		for (var j = 0; j < 7; j++) {
			month[i].push(calendar[(j + i * 7)]);
		}
	}
	
	// increase and decrease parameters value:
	if (MM == 11) {
		nextMonth = 0;
		nextYear = YYYY + 1;
		prevMonth = MM - 1;
		prevYear = YYYY;
	} else if (MM == 0) {
		prevMonth = 11;
		prevYear = YYYY - 1;
		nextMonth = MM + 1;
		nextYear = YYYY;
	} else {
		prevMonth = MM - 1;
		nextMonth = MM + 1;
		prevYear = YYYY;
		nextYear = YYYY;
	}
	
	if( !hSelInit && !hSelOff ){
		hSelInit = 0; hSelOff = 7;
	}

	
	
	var table = "<table class=\"calendar calend-" + action + "\"><tr class=\"calCenter\"><td colspan=\"2\">";
	table += "<a href=\"#\" onclick=\"az(" + prevMonth + "," + prevYear + ",'" + action + "', '" + hSelInit +"', '" + hSelOff + "'); return false;\"><img src=\"http://www.opodo.it/images/Global/arrow_onwhiteleft.gif\"></a></td>\n";
	table += "<td colspan=\"3\"><div id=\"cMonth\">" + months[MM] + "</div><div id=\"cYear\">" + YYYY + "</div></td><td colspan=\"2\">";
	table += "<a href=\"#\" onclick=\"az(" + nextMonth + "," + nextYear + ",'" + action + "', '" + hSelInit +"', '" + hSelOff + "'); return false;\"><img src=\"http://www.opodo.it/images/Global/arrow_onwhite.gif\"></a></td></tr>\n";
	table += "<tr><td>L</td><td>M</td><td>M</td><td>G</td><td>V</td><td>S</td><td>D</td></tr><tr>\n";
	for (var i = 0; i < month.length; i++) {
		table += "<tr>";
		for (var k = 0; k < month[i].length; k++) {
			var link = typeof(month[i][k]) == "undefined" ? "" : month[i][k];
			table += "<td>";
			if (link != "") { table += "<a href=\"#\" onclick=\"passValue(" + link + "," + (MM + 1) + "," + YYYY + ",'" + action + "'); calendarClose(" + hSelInit + ", " + hSelOff + "); return false;\">" + link + "</a>"; }
			table += "</td>\n";
		}
		table += "</tr>\n";
		if (link == "") break;
	}
	table += "<tr><td colspan=\"7\" class=\"calRight\"><a href=\"#\" onclick=\"calendarClose(" + hSelInit + ", " + hSelOff + "); return false;\">Chiudi</a></td></tr></table>\n";

	var sel = document.getElementsByTagName("select");

	for(i=hSelInit;i<hSelOff;i++){
		sel[i].style.visibility = 'hidden';
	}
	
	document.getElementById('calendarBox').style.display = "block";
	document.getElementById('calendarBox').innerHTML = table;
}

function calendarClose(hSelInit, hSelOff) {
	document.getElementById('calendarBox').style.display = "none";
	var sel = document.getElementsByTagName("select");

	if( !hSelInit && !hSelOff ){
		hSelInit = 0; hSelOff = 7;
	}

	for(i=hSelInit;i<hSelOff;i++){
		sel[i].style.visibility = 'visible';
	}
}

function getMonthDays(MM, YYYY) {
	var monthDays = new Array();
	monthDays.push(31); // january
	YYYY % 4 == 0 ? monthDays.push(29) : monthDays.push(28); // february
	monthDays.push(31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // other months
	return monthDays[MM];
}

function passValue(d, m, y, action) {
	//if (action == "departure") {
		var day = document.getElementById(action+'Day');
		var month = document.getElementById(action+'Month');
	//} else if (action == "return") {
	//	var day = document.getElementById('returnDay');
	//	var month = document.getElementById('returnMonth');
	//}
	var mInt = m;
	if(action.indexOf('Date') == -1){
		d = (d < 10) ?  "0" + d : d;
		m = (m < 10) ?  "0" + m : m;
	}

	for (var i = 0; i < month.options.length; i++) {
		if (month.options[i].value == "" + y + m || month.options[i].value == "" + mInt) { month.options[i].selected = true; }
	}
	
	if(action.indexOf('Date') == -1){
		_appendDayOptions(action+'Day', month.options[month.selectedIndex].value);
	}
	
	for (var i = 0; i < day.options.length; i++) {
		if (day.options[i].value == d) { day.options[i].selected = true; }
	}


}
