var inCorso = false;
var __calArgs = null;
var nomeMese = "";
dateApp = new Array();

//new dateAppuntamenti();

function settaCal(tipo_evento, dest, mese, anno)
{
	if (dest == undefined)
	{
		dest = "calenBody";
	}
	if (tipo_evento == undefined)
	{
		tipo_evento = 0;
	}
	if (document.getElementById(dest) == undefined) return;
	if (settaCal.arguments.length == 4)
	{
		faiCalendario(dest, tipo_evento, mese, anno);
	}
	else
	{
		faiCalendario(dest, tipo_evento);
	}
//	threeColumn();
}

function dateAppuntamenti(tipo)
{
	this.tipo = tipo;
	this.hasVal = _hasVal;
//	this.getLink = _getLink;
	this.add = _add;
	this.giorni = new Array();
	this.lnk = "svl/lstDoc?";
	
	function _hasVal(giorno)
	{
		for (var i = 0; i < this.giorni.length; i++)
		{
			if (this.giorni[i].getGiorno() == giorno)
			{
				return true;
			}
		}				
		return false;
	}
	
/*	
	function _getLink(giorno, template)
	{
		for (var i = 0; i < this.giorni.length; i++)
		{
			if (this.giorni[i].getGiorno() == giorno)
			{
				return "<a href='" + this.lnk + "p0="+this.tipo+"&dS=" + this.giorni[i].getData() + "&dE=" + this.giorni[i].getData() + "&t6="+template+"'>";
			}
		}				
		return "";
	}
*/	
	function _add(data, numero, giorno)
	{
	var gg;
		gg = new giornoApp(data, numero, giorno)
		this.giorni.push(gg);
	}
	
}

function giornoApp(data, numero, giorno)
{
	this.data = data;
	this.numero = numero;
	this.giorno = giorno;
	this.getData = _getData;
	this.getGiorno = _getGiorno;
	this.getNumero = _getNumero;
	
	function _getGiorno()
	{
		return this.giorno;
	}
	function _getData()
	{
		return this.data;
	}
	function _getNumero()
	{
		return this.numero;
	}
}

function faiCalendario(dest, tipo_evento, mese, anno, template)
{
/*
var giorni = new Array('Lu','Ma','Mr','Gi','Ve','Sa','Do'); 
var mesi = new Array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');
*/
var len = faiCalendario.arguments.length;
var oggi = new Date();
var oggiG = oggi.getDate();
var oggiM = oggi.getMonth() + 1;
var oggiY = oggi.getYear();
var dataRichiesta = new Date();
var start = false;
var co;
var ri;
var gset;
var gg = 0;
var stGG = "";
var spazio = "&nbsp;";
var tr_init = "<tr>";
var tr_end = "</tr>";
var table_init = "<table>";
var table_end = "</table>";
//var fr_sxanno = "";
var fr_sxmese = ""; 
//var fr_dxanno = "";
var fr_dxmese = ""; 
var td_initIntestMese = "<th class='caltdint' colspan='5'>";
var td_initIntest = "<td class='caltdint'>";
var td_initIntestSx = "<th class='caltdint'>";
var td_initIntestDx = "<th class='caltdint'>";
var td_end = "</td>";
var td_startDay = "<th>";
var td_startDayDom = "<th class='caltddaydom'>";
var th_end = "</td>";
var td_init = "<td>";
var td_oggi_init = "<td class='caltdoggi'>";
var a_end = "</a>";

var trElem;
var tdElem;
var aElem;
var gs;
var as;
var ms;
var anno_prec;
var anno_succ;
var mese_prec;
var mese_succ;
	
	for (var i = document.getElementById(dest).rows.length - 1; i >= 0; i--)
	{
		document.getElementById(dest).deleteRow(i);
	}

	if (oggiY < 1900) oggiY += 1900;
	
	if (anno < 1900) anno += 1900;
	dataRichiesta.setDate(1);	
	if (len == 4)
	{
		dataRichiesta.setYear(anno);
		dataRichiesta.setMonth(mese - 1);
	}
	if (dataRichiesta.getFullYear)
	{
		anno = dataRichiesta.getFullYear(); 
	}
	else
	{
		anno = dataRichiesta.getYear(); 
	}
	mese = dataRichiesta.getMonth();
	giorno = dataRichiesta.getDate();
	gset = dataRichiesta.getDay();

	if (anno < 1900) anno += 1900;

	as = anno+"";
	ms = (mese+1)+"";
	
	if (ms.length < 2) ms = "0"+ms;
	
	gset -= 1;
	if (gset < 0)
	{
		gset = 6;
	}
/*	
	fr_sxmese = "<a href='index.jsp?mese="+(mese)+"&amp;anno="+(anno)+"' onclick='javascript:go_mese(" + (tipo_evento) + ", \"" + (dest) + "\", " + (mese) + ", " + anno + ", "+ template + ");return false;'><img src='immagini/comuni/Previousd-1.gif' /></a>";
	fr_dxmese = "<a href='#' onclick='javascript:go_mese(" + (tipo_evento) + ", \"" + (dest) + "\", " + (mese + 2) + ", " + anno + ", " + template + ");return false;'><img src='immagini/comuni/Nextd-1.gif' /></a>";
	stringa += table_init;
	stringa += tr_init;
	stringa += td_initIntestSx + fr_sxmese + th_end;
	stringa += td_initIntestMese + mesi[mese] + spazio + anno + th_end;
	stringa += td_initIntestDx + fr_dxmese + th_end;
	stringa += tr_end;

	stringa += tr_init;
	for (var i = 0; i <6; i++)
	{
		stringa += td_startDay + giorni[i] + th_end;
	}
	stringa += td_startDayDom + giorni[6] + th_end;
	stringa += tr_end;
*/	
	for (var i = 0; i < 42; i++)
	{
		co = i % 7;
		ri = Math.floor(i / 7);
		if (gset == co && ri == 0)
		{
			start = true;
			gg = 1;
		}
		if (co == 0)
		{
			trElem = document.createElement("tr");
		}
		tdElem = document.createElement("td");
		if (start == true)
		{
			gs = gg+"";
			if (gs.length < 2) gs = "0"+gs;
			stGG = gs + "/" + ms + "/" + as;
			if (dateApp['aa'+tipo_evento].hasVal(gg))
			{
				aElem = document.createElement("a");
				tdElem.className = "conlink";
				aElem.setAttribute("href", "svl/lst_eventi.htm?reverse=1&eve_data_inizio="+stGG+"&eve_data_fine="+stGG);
				aElem.appendChild(document.createTextNode(gg));
//				stGG = dateApp['aa'+tipo_evento].getLink(gg, template) + stGG + a_end;
				tdElem.appendChild(aElem);
			}
			else
			{
				tdElem.className = "";
				tdElem.appendChild(document.createTextNode(gg));
			}
			if (anno == oggiY && mese == (oggiM -1) && gg == oggiG)
			{
				tdElem.className += " caltdoggi";
			}
			gg += 1;
			dataRichiesta.setDate(gg);	
			if (mese != dataRichiesta.getMonth())
			{
				start = false;
			}
			
		}
		else
		{
			tdElem.innerHTML = spazio;
		}
		trElem.appendChild(tdElem);
		
		if (co == 6)
		{
			document.getElementById(dest).appendChild(trElem);
		}
	}
	anno_prec = anno;
	anno_succ = anno;
	mese_prec = mese;
	mese_succ = mese + 2;
	
	if (mese_prec < 1)
	{
		mese_prec = 12;
		anno_prec = anno_prec - 1;
	}
	if (mese_succ > 12)
	{
		mese_succ = 1;
		anno_succ = anno_succ + 1;
	}

	document.getElementById("mesePrev").setAttribute("href", "index.jsp?mese="+mese_prec+"&anno="+anno_prec);
	document.getElementById("meseSucc").setAttribute("href", "index.jsp?mese="+mese_succ+"&anno="+anno_succ);

	document.getElementById("mesePrev").onclick = new Function("go_mese("+tipo_evento+", '"+dest+"', "+mese_prec+", "+anno_prec+");return false;");
	document.getElementById("meseSucc").onclick = new Function("go_mese("+tipo_evento+", '"+dest+"', "+mese_succ+", "+anno_succ+");return false;");
	
//	document.getElementById("nomMese").innerHTML = nomeMese;
//	stringa += table_end;
	
}

function go_mese(tipo_evento, dest, mese, anno, template)
{
var elem;
var fElem;
var len = go_mese.arguments.length;
var oggi = new Date();
var fD = calcolaAppPath(location);
var appResLay = document.getElementById('appResLay');
var appResLayCont = document.getElementById('appResLay');

	if (go_mese.arguments.length == 0 && __calArgs != null)
	{
		tipo_evento = __calArgs[0];
		dest = __calArgs[1];
		mese = __calArgs[2];
		anno = __calArgs[3];
		template = __calArgs[4];
	}
	
	if (mese == undefined && anno == undefined)
	{
		anno = oggi.getYear();
		mese = oggi.getMonth() + 1;
		if (anno < 1900)
		{
			anno += 1900;
		}
	}

	if (tipo_evento == undefined)
	{
		tipo_evento = 0;
	}
	if (dest == undefined)
	{
		dest = "calenBody";
	}
	if (template == undefined)
	{
		template = 0;
	}
	if (inCorso == false)
	{
		__calArgs = null;
		inCorso = true;
		if(mese < 1)
		{
			mese = 12;
			anno -= 1;
		}
		if(mese > 12)
		{
			mese = 1;
			anno += 1;
		}

/*
		if (appResLay.contentDocument)
		{
			appResLay = appResLay.contentDocument;
		}
		else if (appResLay.contentWindow)
		{
			appResLay = appResLay.contentWindow;
		}

		if (appResLay.document)
		{
			appResLay = appResLay.document;
		}
*/
		if (appResLay.contentDocument)
		{
			appResLay = appResLay.contentDocument;
		}
		else if (appResLay.contentWindow)
		{
			appResLay = appResLay.contentWindow;
		}

		if (appResLay.document)
		{
			appResLay = appResLay.document;
		}

//		fElem = appResLay.createElement("<form method='POST' action='"+fD+"svl/calDoc'>");
		fElem = appResLay.createElement("form");
		fElem.setAttribute("method", "POST");
		fElem.setAttribute("action", fD+'svl/cal_eventi.htm');
		fElem.setAttribute("id", "dumfor");
		fElem.setAttribute("name", "dumfor");
		fElem.setAttribute("style", "display: none; visibility: hidden");
			fElem.setAttribute("target", "appResLay");
/*		
		if (appResLay == document)
		{
		}
*/
//		elem = appResLay.createElement("<input type=hidden name='mese' value='"+mese+"'>");
		elem = appResLay.createElement("input");
		elem.setAttribute("type", "hidden");
		elem.setAttribute("name", "mese");
		elem.setAttribute("id", "mese");
		elem.setAttribute("value", mese);
		fElem.appendChild(elem);

//		elem = appResLay.createElement("<input type=hidden name='anno' value='"+anno+"'>");
		elem = appResLay.createElement("input");
		elem.setAttribute("type", "hidden");
		elem.setAttribute("name", "anno");
		elem.setAttribute("id", "anno");
		elem.setAttribute("value", anno);
		fElem.appendChild(elem);
//		elem = appResLay.createElement("<input type=hidden name='tipo_evento' value='"+tipo_evento+"'>");
		elem = appResLay.createElement("input");
		elem.setAttribute("type", "hidden");
		elem.setAttribute("name", "tipo");
		elem.setAttribute("id", "tipo");
		elem.setAttribute("value", tipo_evento);
		fElem.appendChild(elem);
//		elem = appResLay.createElement("<input type=hidden name='dest' value='"+dest+"'>");
		elem = appResLay.createElement("input");
		elem.setAttribute("type", "hidden");
		elem.setAttribute("name", "dest");
		elem.setAttribute("id", "dest");
		elem.setAttribute("value", dest);
		fElem.appendChild(elem);
//		elem = appResLay.createElement("<input type=hidden name='tpl' value='"+template+"'>");
		elem = appResLay.createElement("input");
		elem.setAttribute("type", "hidden");
		elem.setAttribute("name", "tpl");
		elem.setAttribute("id", "tpl");
		elem.setAttribute("value", template);
		fElem.appendChild(elem);

		elem = appResLay.createElement("input");
		elem.setAttribute("type", "submit");
		elem.setAttribute("name", "s");
		elem.setAttribute("id", "s");
		elem.setAttribute("value", "s");
		elem.setAttribute("style", "display: none; visibility: hidden");
		fElem.appendChild(elem);

		appResLay.body.appendChild(fElem);


		document.getElementById('appResLay').style.width="1px";		
		document.getElementById('appResLay').style.height="1px";		
		document.getElementById('appResLay').style.display="";		
		document.getElementById('appResLay').style.visibility="hidden";		
		
		appResLay.forms['dumfor'].submit();
	}
	else
	{
		__calArgs = new Array();
		__calArgs[0] = tipo_evento;
		__calArgs[1] = dest;
		__calArgs[2] = mese;
		__calArgs[3] = anno;
		__calArgs[4] = template;
		setTimeout(go_mese, 1000);
	}
}

function azzeraCal(tipo)
{
	dateApp['aa'+tipo] = new dateAppuntamenti(tipo);
}

function aggiungiCal(data, numero, giorno, tipo)
{
	dateApp['aa'+tipo].add(data, numero, giorno);
}
function doneLoad(tipo_evento, dest, mese, anno)
{

	settaCal(tipo_evento, dest, mese, anno);
	inCorso = false;
}
function threeColumn() 
{
	var elm1 = document.getElementById('middleLeft');
	var elm2 = document.getElementById('middleCenter');
	var elm3 = document.getElementById('middleRight');
	var elm4 = document.getElementById('middleCenRight');
	var x1 = -9999;
	var x2 = -9999;
	var x3 = -9999;
	var x4 = -9999;
	var max = -9999;
	if (elm1)
	{
		elm1.style.height = 'auto';
		if (elm1.offsetHeight)
		{
			x1 = elm1.offsetHeight;
		}
	}
	if (elm2)
	{
		elm2.style.height = 'auto';
		if (elm2.offsetHeight)
		{
			x2 = elm2.offsetHeight;
		}
	}
	if (elm3)
	{
		elm3.style.height = 'auto';
		if (elm3.offsetHeight)
		{
			x3 = elm3.offsetHeight;
		}
	}
	if (elm4)
	{
		elm4.style.height = 'auto';
		if (elm4.offsetHeight)
		{
			x4 = elm4.offsetHeight;
		}
	}
	
	if (parseInt(x1+"", 10) > max)
	{
		max = parseInt(x1+"", 10);
	}
	if (parseInt(x2+"", 10) > max)
	{
		max = parseInt(x2+"", 10);
	}
	if (parseInt(x3+"", 10) > max)
	{
		max = parseInt(x3+"", 10);
	}
	if (parseInt(x4+"", 10) > max)
	{
		max = parseInt(x4+"", 10);
	}
	
	if (elm1) elm1.style.height = max + "px";
	if (elm2) elm2.style.height = max + "px";
	if (elm3) elm3.style.height = max + "px";
	if (elm4) elm4.style.height = max + "px";
	
}
