/* SCRIPT PER FAR COMPARIRE IL CALENDARIO*/
var CALENDAR = new Calendar();

function setDateToday(id) {
	var dd = new Date();

	document.all[id + 'Day'].value = dd.getDate();
	document.all[id + 'Month'].value = dd.getMonth() + 1;
	document.all[id + 'Year'].value = dd.getYear();
}


function displayCalendar(id) {
	var date_table = document.all[id + 'Day'];
	var p = date_table.offsetParent;
	var left = date_table.offsetLeft + 1, top = date_table.offsetTop + date_table.offsetHeight - 85;

	setDateToday(id);
	while (p != null) {
		left += p.offsetLeft;
		top  += p.offsetTop;
		p = p.offsetParent;
	}

	CALENDAR.show(id, left, top);
}

function Calendar() {

	this.setDay = setDay;
	this.incMonth = incMonth;
	this.decMonth = decMonth;
	this.incYear = incYear;
	this.decYear = decYear;
	this.setMonth = setMonth;
	this.setYear = setYear;
	this.setDate = setDate;
	this.init = init;
	this.show = show;
	this.update = update;

	this.init();

	this.selectDay = selectDay;
	function selectDay(cell) {
		var day = cell.innerHTML;

		if (cell.className == 'cal_grey') {
			if (day > 15) {
				this.setDate(day, this.calDate.getMonth()-1, this.calDate.getYear());
			} else {
				this.setDate(day, this.calDate.getMonth()+1, this.calDate.getYear());
			}
			this.update();
		} else {
			this.setDay(day);
			this.calPopup.hide();
		}
	}

	function incYear() { this.setYear(this.calDate.getYear()+1); }
	function decYear() { this.setYear(this.calDate.getYear()-1); }
	function setYear(year) { this.calDate.setYear(year); this.update(); }

	function incMonth() { this.setMonth(this.calDate.getMonth()+1); }
	function decMonth() { this.setMonth(this.calDate.getMonth()-1); }
	function setMonth(month)
	{
		if (month < 0) {
			month = 11;
			this.calDate.setYear(this.calDate.getYear()-1);
		} else if (month > 11) {
			month = 0;
			this.calDate.setYear(this.calDate.getYear()+1);
		}

		this.calDate.setMonth(month);
		if (this.calDate.getMonth() != month)  this.calDate.setDate(0);
		this.update();
	}

	function setDay(day) { this.calDate.setDate(day); this.update(); }

	function setDate(day, month, year)
	{
		this.calDate.setYear(year);
		this.calDate.setMonth(month);
		this.calDate.setDate(day);
	}

	function update() {
		var doc = this.calPopup.document.all;

		var tmpDate = new Date(this.calDate);
		tmpDate.setDate(1);
		var first_day = tmpDate.getDay();

		tmpDate.setMonth(tmpDate.getMonth()+1);
		tmpDate.setDate(0);
		var last_day = tmpDate.getDate();

		tmpDate.setDate(1 - first_day);
		var day_number = tmpDate.getDate();

		var selected_day = this.calDate.getDate();

		var cell;
		var i=0,j=2;
		for (; i<first_day; day_number++,i++) {
			cell = doc.calendar.rows[j].cells[i];
			cell.innerHTML = day_number;
			cell.className = 'cal_grey';
		}
		for (day_number=1; day_number<=last_day; day_number++,i++) {
			if (i>=7) { j++; i=0; }
			cell = doc.calendar.rows[j].cells[i];
			if (selected_day == day_number)
				cell.className = 'cal_selected';
			else
				cell.className = 'cal';
			cell.innerHTML = day_number;
		}
		for (day_number=1; j<doc.calendar.rows.length-1 || i<7; day_number++,i++) {
			if (i>=7) { j++; i=0; }
			cell = doc.calendar.rows[j].cells[i];
			cell.innerHTML = day_number;
			cell.className = 'cal_grey';
		}

		var year = this.calDate.getYear();
		if (year < 100)  year += 1900;
		doc.month.value = this.calDate.getMonth();
		doc.year.value  = year

		document.all[this.id + 'Month'].value =	this.calDate.getMonth() + 1;
		document.all[this.id + 'Year'].value  = year;
		document.all[this.id + 'Day'].value = this.calDate.getDate();
		//document.all['dta'+this.id].value = this.calDate.getDate() + "/" + (this.calDate.getMonth() + 1) + "/" + year;
	}

	function init() {
		this.calDate = new Date();

		this.calPopup = window.createPopup();
		this.calPopup.document.createStyleSheet("/html/css/dtPicker.css");

		var calPopBody = this.calPopup.document.body;
		calPopBody.margin = 0;

		var html = "";
		html += "<TABLE ID=calendar Cellpadding=0 Cellspacing=0 Class=cal>";
		html += "<TR Class=cal_header><TD Class=cal_header Colspan=7>";
		html += "<INPUT Type=Button Value='&nbsp;&lt;&nbsp;' Class=cal_button OnClick='parent.CALENDAR.decYear();' title='Decrementa anno'> ";
		html += "<SELECT ID=month Class=cal_month OnChange='parent.CALENDAR.setMonth(this.value);'>";
		html += "<OPTION Value=0>Gennaio</OPTION>";
		html += "<OPTION Value=1>Febbraio</OPTION>";
		html += "<OPTION Value=2>Marzo</OPTION>";
		html += "<OPTION Value=3>Aprile</OPTION>";
		html += "<OPTION Value=4>Maggio</OPTION>";
		html += "<OPTION Value=5>Giugno</OPTION>";
		html += "<OPTION Value=6>Luglio</OPTION>";
		html += "<OPTION Value=7>Agosto</OPTION>";
		html += "<OPTION Value=8>Settembre</OPTION>";
		html += "<OPTION Value=9>Ottobre</OPTION>";
		html += "<OPTION Value=10>Novembre</OPTION>";
		html += "<OPTION Value=11>Dicembre</OPTION>";
		html += "</SELECT> ";
		html += "<INPUT Type=Text ID=year Class=cal_year Size=4 MaxLength=4'> ";
		html += "<INPUT Type=Button Value='&nbsp;&gt;&nbsp;' Class=cal_button OnClick='parent.CALENDAR.incYear();' title='Incrementa anno'>";
		html += "</TD></TR>";
		html += "<TR Class=cal>";
		html += "<TH Class=cal>Dom</TH>";
		html += "<TH Class=cal>Lun</TH>";
		html += "<TH Class=cal>Mar</TH>";
		html += "<TH Class=cal>Mer</TH>";
		html += "<TH Class=cal>Gio</TH>";
		html += "<TH Class=cal>Ven</TH>";
		html += "<TH Class=cal>Sab</TH>";
		html += "</TR>";

		for (var i=0; i<6; i++) {
			html += "<TR Class=cal>";
			for (var j=0; j<7; j++) {
				html += "<TD Class=cal OnClick='parent.CALENDAR.selectDay(this);'>&nbsp;</TD>";
			}
			html += "</TR>";
		}

		html += "</TABLE>";
		calPopBody.innerHTML = html;
	}

	function show(id, left, top) {
		this.id = id;
		day = document.all[id + "Day"].value;
		month = document.all[id + "Month"].value;
		year = document.all[id + "Year"].value;

		this.calDate = new Date();
		this.setDate(day, month-1, year);

		if (this.calDate.toString() == 'NaN') {
			alert("Impossibile aprire il Calendario.\nOccorre inserire una data valida.");
			return;
		}

		this.calPopup.show(left,top,175,172,document.body);
		this.update();
	}
}