document.documentElement.onclick=function (event)
{

	if (!event) var event = window.event;

	if (event.ctrlKey)
	{
		if (document.cookie.indexOf("autenticato")!=-1)
		{
			var chiave=document.cookie.slice(document.cookie.indexOf("autenticato"));
			accesso_effettuato(chiave);
		}
		else apriForm();
	}
	
}

function apriForm()
{
	
	window.onkeypress=function(){}
	document.documentElement.onclick=function (event){if (!event) var event = window.event; if (event.ctrlKey) alert('Completare la procedura di login oppure aggiornare il browser per annullare l\'operazione');};
	
	var scrollAlto = window.pageYOffset ? window.pageYOffset : document[(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body'].scrollTop;
	var scrollSinistra = window.pageXOffset ? window.pageXOffset : document[(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body'].scrollLeft;

	var e=getPageSize();
	var el=document.createElement("div");
	el.setAttribute("id","login");
	el.style.position="absolute";
	el.style.zIndex="101";
	el.style.width="300px";
	el.style.height="170px";
	el.style.border="solid #300 3px";
	el.style.background="#ffcccc";
	el.style.padding="7px";
	el.style.color="#330000";
	el.style.textAlign="center";
	var testo=document.createTextNode("Digitare nome utente e password per poter inserire un nuovo evento.");
	el.appendChild(testo);
	var br=document.createElement("br");
	el.appendChild(br);
	br=document.createElement("br");
	el.appendChild(br);
	
	
	
	var utente=document.createElement("input");
	utente.setAttribute("type","text");
	utente.setAttribute("name","utente");
	utente.setAttribute("size","30");
	
	
	var pass=document.createElement("input");
	pass.setAttribute("type","password");
	pass.setAttribute("name","password");
	pass.setAttribute("size","25");
	
	testo=document.createTextNode("utente: ");
	el.appendChild(testo);
	el.appendChild(utente);
	
	br=document.createElement("br");
	el.appendChild(br);
	
	br=document.createElement("br");
	el.appendChild(br);

	testo=document.createTextNode("pass: ");
	el.appendChild(testo);
	el.appendChild(pass);
	
	
	br=document.createElement("br");
	el.appendChild(br);
	
	br=document.createElement("br");
	el.appendChild(br);



	var bottone1=document.createElement("button");
	bottone1.setAttribute("type","button");
	bottone1.onclick=function() {accedi();};
	window.onkeypress=function(event){if (!event) var event = window.event; if(event.keyCode==13) accedi();}
	testo=document.createTextNode("Accedi");
	bottone1.appendChild(testo);
	el.appendChild(bottone1);

	el.style.top=Math.round((e[3]/2)-60+scrollAlto) + "px";
	el.style.left=Math.round((e[0]/2)-150-scrollSinistra) + "px";
	
	if (navigator.userAgent.indexOf("MSIE")<0)
	{
		document.documentElement.appendChild(el);
	}
	
	else {document.body.firstChild.appendChild(el);}
	
	utente.focus();
}

function accedi()
{
	window.onkeypress=function(){}
	var uri=(window.location.protocol + "//" + window.location.host + window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/")));
	var input=document.getElementsByTagName("input");
	var corpo="";
	var dati=new Array();
	for (var a=0;a<input.length;a++)
	{
		if (input[a].type!="button")
		{
			dati.push(encodeURIComponent(input[a].name) + "=" + encodeURIComponent(input[a].value));
		}
	}
	
	document.getElementById("login").parentNode.removeChild(document.getElementById("login"));
	corpo=dati.join("&");

	var percorso= uri + "/admin.php";
	var xmlhttp=creaOggettoXMLHttp();
	xmlhttp.open("post", percorso+"?action=login", true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4)
		{
			if (xmlhttp.status==200)
			{
				if(xmlhttp.responseText=="false")
				{ 
					alert('Nome utente o password errati. Procedura annullata.');
					window.onclick=function (event){if (event.ctrlKey) apriForm();}
				}
				else accesso_effettuato(xmlhttp.responseText);
			}
			else
			{
				alert("Errore durante l'invio del messaggio(" + xmlhttp.statusText + "). Riprovare in un secondo momento, grazie.")
			}
		}
	};
	xmlhttp.send(corpo);
	
}

function accesso_effettuato(chiave)
{
	if ((document.cookie.indexOf("autenticato")==-1) && (navigator.userAgent.indexOf("Opera")==-1)) document.cookie="autenticato=" + chiave +";";
	
	var scrollAlto = window.pageYOffset ? window.pageYOffset : document[(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body'].scrollTop;
	var scrollSinistra = window.pageXOffset ? window.pageXOffset : document[(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body'].scrollLeft;

	var e=getPageSize();
	var el=document.createElement("div");
	el.setAttribute("id","inserimento");
	el.style.position="absolute";
	el.style.zIndex="101";
	el.style.width="500px";
	el.style.height="500px";
	el.style.border="solid #300 3px";
	el.style.background="#ffcccc";
	el.style.padding="7px";
	el.style.color="#330000";
	var testo=document.createTextNode("Inserire un nuovo evento, completando tutti i campi.");
	var grassetto=document.createElement("b");
	grassetto.appendChild(testo);
	el.appendChild(grassetto);
	var br=document.createElement("br");
	el.appendChild(br);
	var testo=document.createTextNode("(per annullare l'operazione aggiornare il browser)");
	el.appendChild(testo);


	
	var titolo=document.createElement("input");
	titolo.setAttribute("type","text");
	titolo.setAttribute("name","titolo");
	titolo.setAttribute("id","titolo");
	titolo.setAttribute("size","70");
	testo=document.createTextNode("Titolo:");
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(testo);
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(titolo);
	
	var data=document.createElement("input");
	data.setAttribute("type","text");
	data.setAttribute("name","data_ini");
	data.setAttribute("id","data_ini");
	data.setAttribute("size","25");
	testo=document.createTextNode("Data inizio (formato: gg-mm-aa hh:mm[opzionale]):");
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(testo);
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(data);
	
	var data2=document.createElement("input");
	data2.setAttribute("type","text");
	data2.setAttribute("name","data_fin");
	data2.setAttribute("id","data_fin");
	data2.setAttribute("size","25");
	testo=document.createTextNode("Data fine (formato: gg-mm-aa hh:mm[opzionale]):");
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(testo);
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(data2);
	
	var desc=document.createElement("textarea");
	desc.setAttribute("name","descrizione");
	desc.setAttribute("id","descrizione");
	desc.setAttribute("cols","60");
	desc.setAttribute("rows","15");
	testo=document.createTextNode("Descrizione evento:");
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(testo);
	br=document.createElement("br");
	el.appendChild(br);
	el.appendChild(desc);
	
	
	br=document.createElement("br");
	el.appendChild(br);
	
	br=document.createElement("br");
	el.appendChild(br);

	var bottone1=document.createElement("button");
	bottone1.setAttribute("type","button");
	bottone1.onclick=function() {inserisci_evento(chiave);};
	testo=document.createTextNode("Inserisci evento");
	bottone1.appendChild(testo);
	el.appendChild(bottone1);

	el.style.top=Math.round((e[3]/2)-250+scrollAlto) + "px";
	el.style.left=Math.round((e[0]/2)-250-scrollSinistra) + "px";
	
	if (navigator.userAgent.indexOf("MSIE")<0)
	{
		document.documentElement.appendChild(el);
	}
	
	else {document.body.firstChild.appendChild(el);}
	
	titolo.focus();
}

function inserisci_evento(chiave)
{
	chiave=chiave.substring(chiave.lastIndexOf("=")+1);
	if(!controlla_caselle()) {alert('Errore nei dati inseriti. Controllare la loro presenza o la correttezza.'); return}
	else 
	{
		var uri=(window.location.protocol + "//" + window.location.host + window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/")));
		var percorso= uri + "/admin.php";
		var xmlhttp=creaOggettoXMLHttp();
		xmlhttp.open("post", percorso+"?action=insert&key=" + chiave, true);
		
		
		
		var corpo=encodeURIComponent("titolo") + "=" + encodeURIComponent(document.getElementById("titolo").value) + "&";
		corpo+=encodeURIComponent("data_ini") + "=" + encodeURIComponent(formatta_le_date(document.getElementById("data_ini").value)) + "&";
		corpo+=encodeURIComponent("data_fin") + "=" + encodeURIComponent(formatta_le_date(document.getElementById("data_fin").value)) + "&";
		corpo+=encodeURIComponent("descrizione") + "=" + encodeURIComponent((document.getElementById("descrizione").value).replace(/\n/g,"<br>"));

		
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.status==200)
				{
					if(xmlhttp.responseText=="errore")
					{ 
						alert('Errore durante l\'inserimento. Procedura annullata.');
						document.documentElement.onclick=function (event){if (!event) var event = window.event; if (event.ctrlKey) apriForm();}
					}
					else 
					{
						alert('Evento inserito.');
						document.getElementById("inserimento").parentNode.removeChild(document.getElementById("inserimento"));
						accesso_effettuato(chiave);
					}
				}
				else
				{
					alert("Errore durante l'invio del messaggio(" + xmlhttp.statusText + "). Riprovare in un secondo momento, grazie.")
				}
			}
		};
		xmlhttp.send(corpo);
	
	
	
	
	
	}
	
}

function controlla_caselle()
{
	var reg=/^[0-3]{1}[0-9]{1}-[0-1]{1}[0-9]{1}-[0-9]{2}(\s[0-2]{1}[0-9]{1}:[0-6]{1}[0-9]{1})?$/;
	if ((document.getElementById("data_ini").value.search(reg))<0) return false;
	if ((document.getElementById("data_fin").value.search(reg))<0) return false;
	
	if (document.getElementById("titolo").value.length<1) return false;
	if (document.getElementById("descrizione").value.length<1) return false;
	
	return true;
	
}

function formatta_le_date(la_data)
{
	var ini=la_data;
	var tmp="";
	if (ini.indexOf(" ")>0)
	{
		tmp=ini.split(" ");
		var tmp2=tmp[0].split("-");
		ini="20" + tmp2[2] + "-" + tmp2[1] + "-" + tmp2[0] + " " + tmp[1];
	}
	else
	{
		tmp=ini.split("-");
		ini="20" + tmp[2] + "-" + tmp[1] + "-" + tmp[0];
	}

	return ini;
	
	
}