//Inizio RoolOver
//G sta per globale
//P sta per private Qundi evitare la chiamata diretta!!
var ContatoreImg = 0;
var NormImg = new Array();
var SopraImg = new Array();
var PremiImg = new Array();
var IsPageLoad = false;
var NuovaPagina = null;
var GXPosition = -1;
var GYPosition = -1;
var IsPageLoad = false;
var that = this;
var IsExplorer = (navigator.appName.toLocaleLowerCase().indexOf("explorer") > -1) ? true : false;

addEvent(window, "load", function() { IsPageLoad = true; StartMouseListener() })

function StartMouseListener() { GXPosition = -1; GYPosition = -1; addEvent(document, "mousemove", PSetPosition); }
function StopMouseListener() { removeEvent(document, "mousemove", PSetPosition); }


function PostBack(F, eventTarget, eventArgument)
{
	F.elements['__EVENTTARGET'].value = eventTarget;
	F.elements['__EVENTARGUMENT'].value = eventArgument;
	F.submit();
}



//Setta le coordinate assolute del mouse cross-browser
function PSetPosition(e)
{
	if (IsExplorer == true) { GXPosition = window.event.clientX; GYPosition = window.event.clientY }
	else { GXPosition = e.pageX; GYPosition = e.pageY }
	GXPosition += 10;
}
function GetObj(MyID) { return document.getElementById(MyID) }

//Attiva il Roolover per tutte le immagini che hanno l'id che inizia per ROOL
addEvent(window, "load", function() { RollOver() });

function RollOver()
{
	for (i = 0; i < document.images.length; i++)
	{
		if (document.images[i].src.indexOf("_norm.") > -1)
		{
			Caricamento(document.images[i])
			addEvent(document.images[i], "mouseout", function() { ChangeImg(this, NormImg[parseInt(this.getAttribute("MyID"))]) })
			addEvent(document.images[i], "mouseover", function() { ChangeImg(this, SopraImg[parseInt(this.getAttribute("MyID"))]) })
			addEvent(document.images[i], "mousedown", function() { ChangeImg(this, PremiImg[parseInt(this.getAttribute("MyID"))]) })
		}
	}
}

function ChangeImg(imgObj, NewImageObj) { imgObj.src = NewImageObj.src }
function Caricamento(img)
{
	img.setAttribute("MyID", ContatoreImg)
	NormImg[ContatoreImg] = new Image(); NormImg[ContatoreImg].src = img.src; NormImg[ContatoreImg]
	SopraImg[ContatoreImg] = new Image(); SopraImg[ContatoreImg].src = img.src.replace("_norm.", "_over.")
	PremiImg[ContatoreImg] = new Image(); PremiImg[ContatoreImg].src = img.src.replace("_norm.", "_down.")
	ContatoreImg++;
}

function EndWith(StrToControl, ExpressionToFind)
{
	var LunghExpression = ExpressionToFind.length
	var LunghStr = StrToControl.length
	var DoveDeveIniziareExpressionToFind = LunghStr - LunghExpression
	if (StrToControl.indexOf(ExpressionToFind) == DoveDeveIniziareExpressionToFind) { return true }
	else { return false }
}
//Fine RoolHover




//Inizio Cookie
//var MyStr=NomeCookie+"="+escape(DDLResidenzaObj.value)+"&"+strAnno+"="+escape(DDLNascitaObj.value)+";path=/"
function WriteCookie(MyCookieName, MyValue, ExpireDate) { var MyStr = MyCookieName + "=" + MyValue + ";path=/;expires=" + ExpireDate; document.cookie = MyStr }
function ReadCookie(CookieName)
{
	var MyCookieArray = new Array()
	var CookieValues = document.cookie
	MyCookieArray = CookieValues.split("; ")
	for (i = 0; i < MyCookieArray.length; i++)
	{
		var TempArray = MyCookieArray[i].split("=")
		if (TempArray[0] == CookieName)
		{ return unescape(TempArray[1]) }
	}
	return ""
}
//TotGiorni(365)*hh(24)*(3600)(sec/ora)*ms1000=
function WriteCookieXTotGiorni(NomeCookie, valoreCookie, TotGiorni) { var ExpireDate = new Date(); ExpireDate.setTime(ExpireDate.getTime() + (TotGiorni * 24 * 3600 * 1000)); WriteCookie(NomeCookie, valoreCookie, ExpireDate.toGMTString()) }
//Fine Cookie

//QueryString
function ValoriQueryString(NomeQuery, UrlAssolutaPagina)
{
	NomeQuery = NomeQuery.toLowerCase()//window.opener.location.href
	var MyQueryString = new String()
	MyQueryString = UrlAssolutaPagina.toLowerCase()
	if (MyQueryString.indexOf("?") > -1)
	{
		var a = new Array()
		a = MyQueryString.substring(1, MyQueryString.length)//Elimino il punto interrogativo
		a = a.split("&");
		TotQuery = a.length
		for (i = 0; i < TotQuery; i++)
		{
			var b = new Array()
			b = a[i].split("=")
			if (b.length == 2)//Vedo se c'una chiave ed un valore
			{
				if (b[0] == NomeQuery) return b[1]
			}
		}
	}
	else
	{ return null }
	return null //Non ho trovato la chiave che cercavo!!
}
//Fine Cookie


function PopUp(Destinazione, NomeFinestra, Width, Height, Ridimensionabile, scrollbars) { var left = ((window.screen.width - Width) / 2); var top = ((window.screen.height - Height) / 2); NuovaPagina = window.open(Destinazione, NomeFinestra, "toolbar=no,location=no,menuBar=no,status=yes,width=" + Width + "px,height=" + Height + "px,scrollbar=" + scrollbars + ",resizable=" + Ridimensionabile); NuovaPagina.moveTo(left, top); NuovaPagina.focus() }
function AggiornaOpener() { try { window.parent.opener.location.href = window.parent.opener.location.href } catch (e) { alert("Info: Nessuna finestra sottostante aperta.\nAggiornamento finestra sottostante non riuscito\nLa finestra comunque verra' chiusa") } }

function AggiornaFratelloDelFramePadre(IDFrameSx) { window.parent.document.getElementsByName(IDFrameSx)[0].src = window.parent.document.getElementsByName(IDFrameSx)[0].src }
function PopUp_IMG(UrlRelativaAssolutaFile)
{
	window.open("/popup_img.aspx?popupimg=" + UrlRelativaAssolutaFile, "Immagini", "toolbar=no,location=no,menuBar=no,status=yes,width=500px,height=400px,scrollbars=yes,resizable=no")
}

//Inizio Eventi
function addEvent(obj, type, fn)
{
	if (obj.addEventListener) obj.addEventListener(type, fn, false)
	else if (obj.attachEvent)
	{
		obj["e" + type + fn] = fn; obj[type + fn] = function() { obj["e" + type + fn](window.event) }
		obj.attachEvent("on" + type, obj[type + fn])
	}
}
function removeEvent(obj, type, fn)
{
	if (obj.removeEventListener)
	{
		obj.removeEventListener(type, fn, false)//NS
	} else if (obj.detachEvent) { obj.detachEvent("on" + type, obj[type + fn]); obj[type + fn] = null; obj["e" + type + fn] = null } //IE
}
//Fine Eventi

function CreaNodo(TagName, MyInnerHTML)
{
	var MyNode = document.createElement(TagName)
	if ((MyInnerHTML != undefined) && (MyInnerHTML != null)) {MyNode.innerHTML = MyInnerHTML; }
	return MyNode;
}
function RemoveChildNodes(CurrentElementObj)
{
	while (CurrentElementObj.hasChildNodes())
	{
		CurrentElementObj.removeChild(CurrentElementObj.lastChild); //alert("Nodi Dopo "+CurrentElementObj.childNodes.length)
	}
}

var ObjToolTip;
var ObjToolTipTD1;
var ObjToolTipTD2;
var IsToolTipCreato = false;
function CreaToolTip()
{
	IsPageLoad = true;
	var NodeTable = document.createElement("table");
	NodeTable.style.position = "absolute";
	NodeTable.style.border = "1px solid #000";  //NodeTable.setAttribute("id","ToolTip");NodeTable.setAttribute("class","TTBody")//NodeTable.setAttribute("className","TTBody")
	NodeTable.setAttribute("cellPadding", "3");
	NodeTable.setAttribute("cellSpacing", "0");
	NodeTable.setAttribute("width", "200");

	ObjToolTip = document.body.appendChild(NodeTable);
	ObjToolTip.style.display = "none";

	var TBody = document.createElement("tbody");
	TBody.style.marginBottom = "12px";
	TBody.style.background = "url('')";
	TBody.style.backColor = "#000000";
	NodeTable.appendChild(TBody);

	var NodeTR1 = CreaNodo("tr");
	NodeTR1.style.backgroundColor = "#0066cc";
	NodeTR1.style.fontWeight = "bold";
	NodeTR1.style.color = "#ffffff"; //NodeTR1.setAttribute("class","TTRowTitle");NodeTR1.setAttribute("className","TTRowTitle")
	TBody.appendChild(NodeTR1);

	var NodeTD1 = CreaNodo("td", ""); //NodeTD1.setAttribute("id","ToolTipTD1");NodeTD1.setAttribute("class","TTTDTitle");NodeTD1.setAttribute("className","TTTDTitle")
	NodeTD1.setAttribute("height", "5");
	ObjToolTipTD1 = NodeTR1.appendChild(NodeTD1);

	var NodeTR2 = document.createElement("tr");
	NodeTR2.style.backgroundColor = "#ffffff"; //NodeTR2.setAttribute("class","TTRowText");NodeTR2.setAttribute("className","TTRowText")
	TBody.appendChild(NodeTR2);

	var NodeTD2 = document.createElement("td"); //NodeTD2.setAttribute("id","ToolTipTD1");NodeTD2.setAttribute("class","TTTDText");NodeTD2.setAttribute("className","TTTDText")
	NodeTD2.innerHTML = "";
	ObjToolTipTD2 = NodeTR2.appendChild(NodeTD2);
	IsToolTipCreato = true;
}
var TimerID = null;
function TTOver(Title, Message, Width)
{
	if (IsToolTipCreato == false) CreaToolTip();
	if (Width != null) {ObjToolTip.setAttribute("width", Width); }
	ObjToolTipTD1.innerHTML = Title;
	ObjToolTipTD2.innerHTML = Message;
	ObjToolTipTD1.setAttribute("width", Width);
	ObjToolTipTD2.setAttribute("width", Width);
	addEvent(document, "mouseout", TTOut);
	TimerID = window.setTimeout("ReDrawPosition();ObjToolTip.style.display='block'", 500);
}
function TTOut()
{
	if (IsToolTipCreato)
	{
		clearTimeout(TimerID);
		TimerID = null;
		window.setTimeout("if(TimerID==null){ObjToolTip.style.display='none'}", 500);
		document.getElementById("LabMes");
	}
}
function ReDrawPosition()
{
	if (IsExplorer == true) { ObjToolTip.style.posLeft = GXPosition;ObjToolTip.style.posTop = GYPosition; }
	else { ObjToolTip.style.left = GXPosition + "px";ObjToolTip.style.top = GYPosition + "px"; }
}
//Aggiunge tanti zeri davanti quanti richiesti da "PostiInteriDavanti"
function FormatInt(Num, PostiInteriDavanti)
{
	Num = parseInt(Num);
	var Risultato = Num + "";
	for (i = 0; i < PostiInteriDavanti; i++)
	{
		if (Pow(10, i) > Num)
		{
			Risultato = "0" + Risultato;
		}
	}
	return Risultato;
}
//Elevamento a potenza Implementare Esponente negativo
function Pow(Base, Esponente)
{
	var Risultato = 1;
	var str = "";
	if (Esponente > 0)
	{
		for (i = 1; i <= Esponente; i++)
		{
			Risultato = Risultato * Base;
		}
	}
	return Risultato;
}
//Classe calendario
var _Calendario = function(Titolo, IDObjToWrite)
{
	var Titolo = Titolo
	var DataAttuale = new Date();
	var TextBox = document.getElementById(IDObjToWrite);
	if (TextBox.value != '')
	{
		try
		{
			var Vet = TextBox.value.split("/"); Mese = parseInt(Vet[1]) - 1
			DataAttuale.setUTCFullYear(Vet[2], Mese, Vet[0])
		} catch (e) { DataAttuale = new Date() }
	}
	var tabella;
	function IncrementaMese()
	{
		var MeseProx = DataAttuale.getUTCMonth() + 1
		if (MeseProx > 12) { MeseProx = 0; DataAttuale.setUTCFullYear(DataAttuale.getUTCFullYear() + 1, MeseProx, 1) }
		DataAttuale.setMonth(MeseProx, 1)
	}
	function DecrementaMese()
	{
		var MesePrec = DataAttuale.getUTCMonth() - 1
		if (MesePrec < 0) { MesePrec = 11; DataAttuale.setUTCFullYear(DataAttuale.getUTCFullYear() - 1, MesePrec, 1) }
		DataAttuale.setMonth(MesePrec, 1)
	}
	//Se eventuale Calendario già creato lo inserisco nelle'elemento tabella
	if (document.getElementById("TabellaDelMioJavascriptCalendar1"))
	{
		tabella = document.getElementById("TabellaDelMioJavascriptCalendar1")
	} //Altrimenti creo un nuovo Elemento tabella
	else
	{
		/*Inizio Eseguito solo la prima volta*/
		tabella = CreaNodo("table")
		tabella.id = "TabellaDelMioJavascriptCalendar1"
		//tabella.setAttribute("onmousedown","alert('ciao')")//
		addEvent(tabella, "mousedown", function() { DragStart(document.getElementById('TabellaDelMioJavascriptCalendar1'), '') })
		tabella.setAttribute("Title", "Clicca col pulsante sinistro e trascinami..")
		tabella.style.border = "1px solid #000"
		tabella.style.visibility = "hidden"
		tabella.style.position = "absolute"
		tabella.setAttribute("cellPadding", "3")
		tabella.setAttribute("cellSpacing", "0")
		tabella.setAttribute("width", "240")
		tabella.style.backgroundColor = "#fff"
		document.body.appendChild(tabella)
		/*Fine Eseguito solo la prima volta*/
	}
	PosizionaCalendario()
	Rendering()
	//Inizio Output
	function Rendering()
	{
		Clear()
		var TBody = CreaNodo("tbody"); tabella.appendChild(TBody)
		var Row, Col, Input
		Row = CreaNodo("tr"); TBody.appendChild(Row)
		Col = CreaNodo("td"); Col.setAttribute("colSpan", "6"); Col.innerHTML = '<b>' + Titolo + '</b>'; Row.appendChild(Col)
		Col = CreaNodo("td"); Col.setAttribute("width", "16"); Row.appendChild(Col)
		var Img = CreaNodo("img"); Img.setAttribute("src", "/Falcon/img_sys/delete_16.png"); addEvent(Col, "click", Clear); Img.style.cursor = "pointer"; Col.appendChild(Img)


		Row = CreaNodo("tr"); TBody.appendChild(Row)
		Col = CreaNodo("td"); Col.setAttribute("colSpan", "2"); Row.appendChild(Col)
		Input = CreaNodo("button"); Input.innerHTML = "indietro"; Input.setAttribute("accessKey", "z"); Input.setAttribute("title", "Accesso veloce: ALT+z o ALT+SHIFT+z"); Input.style.cursor = "pointer"; addEvent(Input, "click", function() { DecrementaMese(); Rendering() }); addEvent(Input, "Focus", function() { DecrementaMese(); Rendering() }); Col.appendChild(Input)
		Col = CreaNodo("td", GetFullMonth(DataAttuale) + " " + DataAttuale.getUTCFullYear()); Col.setAttribute("colSpan", "3"); Row.appendChild(Col);
		Col = CreaNodo("td"); Col.setAttribute("colSpan", "2"); Col.setAttribute("align", "right"); Row.appendChild(Col);
		Input = CreaNodo("button"); Input.innerHTML = "avanti"; Input.setAttribute("accessKey", "c"); Input.setAttribute("title", "Accesso veloce: ALT+c o ALT+SHIFT+c"); Input.style.cursor = "pointer"; addEvent(Input, "click", function() { IncrementaMese(); Rendering() }); addEvent(Input, "Focus", function() { IncrementaMese(); Rendering() }); Col.appendChild(Input)

		Row = CreaNodo("tr"); TBody.appendChild(Row)

		Col = CreaNodo("td", "Lun"); Col.setAttribute("align", "center"); Row.appendChild(Col)
		Col = CreaNodo("td", "Mar"); Col.setAttribute("align", "center"); Row.appendChild(Col)
		Col = CreaNodo("td", "Mer"); Col.setAttribute("align", "center"); Row.appendChild(Col)
		Col = CreaNodo("td", "Gio"); Col.setAttribute("align", "center"); Row.appendChild(Col)
		Col = CreaNodo("td", "Ven"); Col.setAttribute("align", "center"); Row.appendChild(Col)
		Col = CreaNodo("td", "Sab"); Col.setAttribute("align", "center"); Row.appendChild(Col)
		Col = CreaNodo("td", "Dom"); Col.setAttribute("align", "center"); Row.appendChild(Col)

		var Rows = new Array()
		var ActualRow = 0
		Rows[ActualRow] = CreaNodo("tr"); TBody.appendChild(Rows[ActualRow])
		var TotDays = DaysInMonth(DataAttuale.getUTCFullYear(), DataAttuale.getUTCMonth())
		var Oggi = new Date()
		for (i = 1; i <= TotDays; i++)
		{
			DataAttuale.setDate(i)
			if (i == 1)
			{
				var Conta = 1
				var TempDay = DataAttuale.getDay(); if (TempDay == 0) TempDay = 7
				while (Conta < TempDay)
				{
					Rows[ActualRow].appendChild(CreaNodo("td"))
					Conta++//Post incremento
				}
			}
			if ((DataAttuale.getDay() % 7) == 1)//Lunedì
			{
				ActualRow = ActualRow + 1
				Rows[ActualRow] = CreaNodo("tr")
				TBody.appendChild(Rows[ActualRow])
			}
			//Controlla se siamo nel giorno per colorarlo diversamente
			if ((DataAttuale.getUTCFullYear() == Oggi.getUTCFullYear()) && (DataAttuale.getMonth() == Oggi.getMonth()) && (DataAttuale.getDate() == Oggi.getDate()))
			{
				ColTD = CreaNodo("td", "<a href='javascript:void(0);' title='La data di oggi' style='background-color:#00C;color:#FFF;font-size:12px;font-weight:bold;display:block;text-decoration:none;width:100%'>" + i + "</div>")
			}
			else
			{
				/*Domenica*/if ((DataAttuale.getDay() % 7) == 0) ColTD = CreaNodo("td", "<span style='color:#F00'>" + i + "</span>")
				/*Giorno Normale*/else ColTD = CreaNodo("td", i)
			}
			ColTD.setAttribute("giorno", i)
			ColTD.setAttribute("align", "center")
			ColTD.style.cursor = 'pointer';
			addEvent(ColTD, "click", function() { ScriviData(this.getAttribute('giorno')) })
			Rows[ActualRow].appendChild(ColTD)
		}
	}
	function Clear()
	{
		RemoveChildNodes(tabella)//Elimina tutti i Nodi figli
	}
	function PosizionaCalendario()
	{
		if (IsExplorer) { tabella.style.posLeft = GXPosition; tabella.style.posTop = GYPosition }
		else { tabella.style.left = GXPosition + "px"; tabella.style.top = GYPosition + "px" }
		tabella.style.visibility = "visible"
	}
	function ScriviData(GG)
	{
		TextBox.value = FormatInt(GG, 2) + "/" + FormatInt(parseInt(DataAttuale.getUTCMonth()) + 1, 2) + "/" + FormatInt(DataAttuale.getUTCFullYear(), 4); RemoveChildNodes(tabella)
	}
	function CambiaData()
	{

	}
	function GetFullMonth(data)
	{
		switch (data.getMonth())
		{
			case 0: return "Gennaio"; break;
			case 1: return "Febbraio"; break;
			case 2: return "Marzo"; break;
			case 3: return "Aprile"; break;
			case 4: return "Maggio"; break;
			case 5: return "Giugno"; break;
			case 6: return "Luglio"; break;
			case 7: return "Agosto"; break;
			case 8: return "Settembre"; break;
			case 9: return "Ottobre"; break;
			case 10: return "Novembre"; break;
			case 11: return "Dicembre"; break;
		}
	}
	function DaysInMonth(AAAA, MM)
	{
		switch (MM)
		{
			case 0: return 31; break;
			case 1:
				if (AAAA % 4 == 0) { return 29 }
				else { return 28 }
				break;
			case 2: return 31; break;
			case 3: return 30; break;
			case 4: return 31; break;
			case 5: return 30; break;
			case 6: return 31; break;
			case 7: return 31; break;
			case 8: return 30; break;
			case 9: return 31; break;
			case 10: return 30; break;
			case 11: return 30; break;
		}
	}
}