var browserType;

if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {
   browserType= "gecko"
}

function Trova_Oggetto(nome)
{
 	if (browserType == "gecko" ) Layer = eval('document.getElementById(nome)');
	else if (browserType == "ie") Layer = eval('document.getElementById(nome)');
	else Layer = eval('document.layers[nome]');
	
	return Layer;
}

function hide_Calendar()
{
	document.poppedLayer = Trova_Oggetto("Calendario");  
	document.poppedLayer.style.visibility = "hidden";
}

function show_Calendar() 
{
	document.poppedLayer = Trova_Oggetto("Calendario");  
	document.poppedLayer.style.visibility = "visible";
}

function makeArray() 
{
	for (i = 0; i<makeArray.arguments.length; i++)
	this[i + 1] = makeArray.arguments[i];
}

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}


var Calendario_Anno, Calendario_Mese, path_img;

function SetPathImg(path)
{
	path_img = path;		
}


function GetTime()
{
	var d = new Date ();
	return d.getTime();
}


function GetPrenota(giorno)
{
	try
	{
		xml_dat = new XMLHttpRequest();
		xml_dat.open('GET', 'http://www.imbarcazioniamotore.it/calendario/check_data.php?data=' + giorno + '&time=' + GetTime(), false);
		xml_dat.send(null);
		return xml_dat.responseText;
	}
	catch(e) 
	{ 
		return null;
	} 
}


function PostDataPrenotazioni(anno, mese)
{
	try
	{
		xml_dat = new XMLHttpRequest();
		xml_dat.open('GET', 'http://www.imbarcazioniamotore.it/calendario/check_mese.php?anno=' + anno + '&mese=' + mese + '&time=' + GetTime(), false);
		xml_dat.send(null);
		return xml_dat.responseText;
	}
	catch(e) 
	{ 
		return null;
	} 
}



function ShowData_Calendar(cmd)
{
	var months = new makeArray('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembere','Dicembre');
	
	var curdate = new Date();
	curdate.setMonth(curdate.getMonth() + cmd);
	
	mese = curdate.getMonth() + 1; 
	anno = curdate.getFullYear(); 
	
	document.Intestazione = Trova_Oggetto("Calendario_Titolo");  
	
	document.Intestazione.innerHTML = "<a href=\"javascript:ShowData_Calendar(" + (cmd - 1) + ");\" title=\"Vai al mese precedente\"><img src=\"" + path_img + "freccia_sx.png\" alt=\"mese precedente\" width=\"9\" height=\"9\" border=\"0\" onmouseover=\"this.src = '" + path_img + "freccia_sx_over.png';\" onmouseout=\"this.src = '" + path_img + "freccia_sx.png';\" /></a> ";
	document.Intestazione.innerHTML += months[mese] + " " + anno;
	document.Intestazione.innerHTML += " <a href=\"javascript:ShowData_Calendar(" + (cmd + 1) + ");\" title=\"Vai al mese successivo\"><img src=\"" + path_img + "freccia_dx.png\" alt=\"mese successivo\" width=\"9\" height=\"9\" border=\"0\" onmouseover=\"this.src = '" + path_img + "freccia_dx_over.png';\" onmouseout=\"this.src = '" + path_img + "freccia_dx.png';\" /></a>";

	curdate.setDate(1);
	var n_gg = curdate.getDay();
	if (n_gg == 0) {n_gg = 7;}
	var counter = 0;
	var max_gg = daysInMonth(mese-1, anno) + n_gg;

	giorni_prenotati = PostDataPrenotazioni(anno, mese).split(",");

	// Azzera le classi
	for (counter = 1; counter < 43; counter++)
	{
		document.Intestazione = Trova_Oggetto("Calendario_" + counter.toString());  
		document.Intestazione.className = "Calendario_Giorni";
		
		if (counter == 7) {document.Intestazione.className = "Calendario_GiorniDom";}
		if (counter == 14) {document.Intestazione.className = "Calendario_GiorniDom";}
		if (counter == 21) {document.Intestazione.className = "Calendario_GiorniDom";}
		if (counter == 28) {document.Intestazione.className = "Calendario_GiorniDom";}
		if (counter == 35) {document.Intestazione.className = "Calendario_GiorniDom";}
		if (counter == 42) {document.Intestazione.className = "Calendario_GiorniDom";}
		
		document.Intestazione.title = "Giorno libero e prenotabile";
	}


	for (counter = 1; counter < 43; counter++)
	{
		document.Intestazione = Trova_Oggetto("Calendario_" + counter.toString());  
		document.Intestazione.innerHTML = "&nbsp;";
	}	
	
	for (counter = n_gg; counter < max_gg; counter++)
	{
		document.Intestazione = Trova_Oggetto("Calendario_" + counter.toString());  
		document.Intestazione.innerHTML = (counter - n_gg + 1).toString();
	}
	

	for (i = 0; i < 32; i++)
	{
		if (giorni_prenotati[i] > 0 )
		{
			document.Intestazione = Trova_Oggetto("Calendario_" + (Number(giorni_prenotati[i]) + n_gg - 1).toString());  
			document.Intestazione.className = "Calendario_GiorniCopiaOVER";
			document.Intestazione.title = "Giorno prenotato!";
		}
	}
	
	Calendario_Anno = anno;
	Calendario_Mese = mese;
	SetInputVar(null);
}
