//controllo il tipo di browser
var ns4;
var ie4;
var ie5;
var ns6;
var w3c;

var HTMLCode = "";
// ID da assegnare al box delle boxCaledarID nel codice HTML
var dn_boxCaledarID='boxCaledarID';			


var DaysList = new Array("GGSettimana","L","M","M","G","V","S","D");
var MonthsList = new Array("MesiAnno","Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
var MonthLength = new Array("NumGGMesi",31,29,31,30,31,30,31,31,30,31,30,31);

var QueryDate = 0;      // giorno richiesto(date)
var QueryMonth = 0;     // mese richiesto
var QueryYear = 0;      // anno richiesto
var QueryDay = 0;       // giorno della settimana riferito al giorno richiesto
var FirstDay = 0;       // giorno della settimanan del 1° giorno del mese
var WeekRef = 0;        // riferimento settimane
var WeekOne = 0;        // numerazione settimane

var Today = new Date();
var TodaysYear = Today.getYear();
var TodaysMonth = Today.getMonth() + 1;
var TodaysDate = Today.getDate();
var TodaysDay = Today.getDay() + 1;
if (TodaysYear < 2000) { TodaysYear += 1900; }
/*
	funzione onresize riportata dal file index.htm	
*/
	if(ns4)window.onresize=function(){
		setTimeout('history.go(0)',400);
	}

// Verifica la correttezza della data corrente e visualizza il calendario del giorno e mese corrente
function CheckData()
{
//	alert("CheckData dopo: QueryDate, QueryMonth, QueryYear"+QueryDate+' ,'+QueryMonth+' ,'+QueryYear);
	MonthLength[2] = CheckLeap(QueryYear);
//	alert('QueryDate, QueryMonth, QueryYear, MonthLength[2] = ' + QueryDate + ', ' +QueryMonth+ ', ' + QueryYear+ ', ' + MonthLength[2]);
	if ((QueryYear * 10000 + QueryMonth * 100 + QueryDate) < 15821220)
		{
			alert("Hai scelto una data anteriore a 20 DIc. 1582!\n\nSelezionare un'altra data.");
			Init(TodaysDate,TodaysMonth,TodaysYear);
			CheckData();
		}
	else 
		{ 
			DisplayCalendar();
		}
}

// Verifica l'anno per determinare se sia bisestile.
// in caso affermativo ritorna 29 come giorni del mese di febbraio
function CheckLeap(yy)
{
if ((yy % 100 != 0 && yy % 4 == 0) || (yy % 400 == 0)) { return 29; } // anno bisestile
else { return 28; }
}

// Ritorna il numero di settimane corrispondente alla data richiesta
function DefWeekNum(dd)
{
	numd = 0;
	numw = 0;
	for (n=1; n<QueryMonth; n++)
	{
		numd += MonthLength[n];
	}
	numd = numd + dd - (9 - DefDateDay(QueryYear,1,1));
	numw = Math.floor(numd / 7) + 1;
	
	if (DefDateDay(QueryYear,1,1) == 1) { numw++; }
	return numw;
}

// Ritorna il numero del giorno della settimana corrispondente alla data specificata
function DefDateDay(yy,mm,dd)
{
//alert('Date2Days(yy,mm,dd)'+Date2Days(yy,mm,dd));
	return Math.floor((Date2Days(yy,mm,dd)-2) % 7) + 1;
}

// trasforma la data yy, mm, dd in numeri di giorni
function Date2Days(yy,mm,dd)
{
	if (mm > 2)
	{
		var bis = Math.floor(yy/4) - Math.floor(yy/100) + Math.floor(yy/400);
		var zy = Math.floor(yy * 365 + bis);
		var zm = (mm-1) * 31 - Math.floor(mm * 0.4 + 2.3);
		return (zy + zm + dd);
	}
	else
	{
		var bis = Math.floor((yy-1)/4) - Math.floor((yy-1)/100) + Math.floor((yy-1)/400);
		var zy = Math.floor(yy * 365 + bis);
		return (zy + (mm-1) * 31 + dd);
	}
}

// Produce il codice HTML del calendario
function DisplayCalendar()
{
	HTMLCode = "";
//	alert(' DisplayCalendar: QueryDate, QueryMonth, QueryYear = ' +QueryDate + ' ' +QueryMonth+ ' ' +QueryYear);
	HTMLCode += "<div id=\"Calendar\" >";
	HTMLCode += "<form name=\"Cal\" action=\"\">";
	HTMLCode += "<table align=\"center\">";

	QueryDay = DefDateDay(QueryYear,QueryMonth,QueryDate);  	//numero posizione giorno della settimana
	WeekRef = DefWeekNum(QueryDate);													//numero settimana nell'anno
	WeekOne = DefWeekNum(1);																	//numero settimana nell'anno riferita alla prima settimana del mese
	//alert('QueryDay,WeekRef,WeekOne ' + QueryDay+' '+WeekRef+' '+WeekOne);

	HTMLCode += "<tr align=center>";
	HTMLCode += "<td colspan=8 class=\"monthYear\">";
	HTMLCode += "<a href=\"javascript:Back("+QueryDate +","+QueryMonth+","+QueryYear+"); CheckData()\" title=\"Mese precedente\"><<</a>";
	HTMLCode += "<a href=\"javascript:Init("+TodaysDate +","+TodaysMonth+","+TodaysYear+"); CheckData()\" title=\"Data corrente\">"+ MonthsList[QueryMonth] + "&nbsp;" + QueryYear + "</a>";
	HTMLCode += "<a href=\"javascript:Next("+QueryDate +","+QueryMonth+","+QueryYear+"); CheckData()\" title=\"Mese successivo\">>></a>";
	HTMLCode += "</tr><tr align=center>";
	
	for (s=1; s<8; s++)
	{
		if (QueryDay == s && QueryDate==TodaysDate) { 
			HTMLCode += "<td class=\"day\">" + DaysList[s] + "</td>"; 
		} else { 
			HTMLCode += "<td>" + DaysList[s] + "</td>"; 
		}
	}
	
	HTMLCode += "<td class=\"week\">Set.</td></tr>";
	a = 0;
	
	for (i=(1-DefDateDay(QueryYear,QueryMonth,1)); i<MonthLength[QueryMonth]; i++) 
	{
			HTMLCode += "<tr align=center>";
			for (j=1; j<8; j++)
			{
				if ((i+j) <= 0) {
					HTMLCode += "<td>&nbsp;</td>"; 
				} else if ((i+j) == QueryDate && (i+j) == TodaysDate) {
					HTMLCode += "<td class=\"currDay\">" + (i+j) + "</td>"; 
				} else if ((i+j) > MonthLength[QueryMonth]) { 
					HTMLCode += "<td>&nbsp;</td>"; 
				} else { HTMLCode += "<td>" + (i+j) + "</td>"; }
			}
//			alert(' WeekOne, a, WeekRef=' + WeekOne +' '+a+' ' +WeekRef);
			if ((WeekOne+a) == WeekRef && QueryDate==TodaysDate) { 
				HTMLCode += "<td class=\"currweek\">" + WeekRef + "</td>"; 
			} else {
				HTMLCode += "<td class=\"week\">" + (WeekOne+a) + "</td>"; 
			}
			HTMLCode += "</tr>";
			a++;
			i = i + 6;
	}
	//
	HTMLCode += "</table>";
	HTMLCode += "</form>";
	HTMLCode += "</div>";
	HTMLCode += "</div>";
	//
	// in funzione del brawser rilevato, attiva la scrittura del calendario nel document
	if(ie4)document.all[dn_boxCaledarID].innerHTML = HTMLCode; // per internet explorer
	if(w3c)document.getElementById(dn_boxCaledarID).innerHTML= HTMLCode; // per firefox e compatibili
	if(ns4){	// per netscape
		var tns4=findid(dn_boxCaledarID,document);
		tns4.document.open();
		tns4.document.write('<center>' + HTMLCode + '</center>');
		tns4.document.close();
	}
}

function Back(Day,Month,Year) {
	QueryDate = 1; // pongo il giorno come inizio mese
	QueryMonth = Month-1; // pongo il mese a quello precedente
	if (QueryMonth == 0) { // se il mese è uguale a 0, lo pongo uguale a dicembre e decremento l'anno
		QueryMonth = 12; 
		QueryYear = Year-1; 
	}
//	alert(' Back: QueryDate, QueryMonth, QueryYear = ' +QueryDate + ' ' +QueryMonth+ ' ' +QueryYear);
}

function Next(Day,Month,Year) {
		QueryDate = 1; // pongo il giorno come inizio mese
		QueryMonth = Month+1; // incremento il numero del mese
	if (QueryMonth == 13) { // se il mese supera dicembre, incremento il numero dell'anno e pongo il mese a gennaio
		QueryMonth = 1; 
		QueryYear=Year+1; 
	}
	//alert(' Next: QueryDate, QueryMonth, QueryYear = ' +QueryDate + ' ' +QueryMonth+ ' ' +QueryYear);
}
function Init(Day,Month,Year) {
		QueryDate = Day;
		QueryMonth = Month;
		QueryYear = Year;
		//alert(' Init: QueryDate, QueryMonth, QueryYear = ' +QueryDate + ' ' +QueryMonth+ ' ' +QueryYear);
}

function CheckBrowser() {
	ns4 = (navigator.appName.indexOf("Netscape")>=0 && document.layers)? true : false;
	ie4 = (document.all && !document.getElementById)? true : false;
	ie5 = (document.all && document.getElementById)? true : false;
	ns6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
	w3c = (document.getElementById)? true : false;
}
// inizializzazione funzioni javascripts
function 	CreateCalend() {
	CheckBrowser();
	Init(TodaysDate,TodaysMonth,TodaysYear);
	CheckData();
// add more functions as needed
//	BuildList();

}

