	var http_request = false;
	
	function makePOSTRequest(url, parameters, theForm, bReload, bSubmit, bOpener) {
		window.sForm = theForm;
		window.sSubmit = bSubmit;
		window.sReload = bReload;
		window.sOpener = bOpener;

		http_request = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
				http_request.overrideMimeType('text/html');
			}
		} else if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

		if (!http_request) {
			alert('Cannot create XMLHTTP instance');
			return false;
		}

		if(sSubmit) {
			http_request.onreadystatechange = alertContents;
		}
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameters.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);
	}

	function alertContents() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				//alert(http_request.responseText);
				result = http_request.responseText;
				//document.getElementById('myspan').innerHTML = result;

				if(sSubmit && sOpener === true) {
					opener.document.forms[sForm].submit();
				} else if(sSubmit && sOpener === false) {
					document.forms[sForm].submit();
				}

				if(sReload) {
					location.reload();
				}
			} else {
				alert('There was a problem with the request.');
			}
		}
	}

	function deleteRow(theTable, thePK, theRow, theMsg, theForm, theReload, theSubmit, theOpener) {
		if(confirm("Confermi l'eliminazione " + theMsg + "?")) {
			var poststr = "table=" + encodeURI(theTable) + "&pkName=" + encodeURI(thePK) + "&pkValue=" + encodeURI(theRow);
			makePOSTRequest('mosmodule/delete-row.php', poststr, theForm, theReload, theSubmit, theOpener);
		}
	}

	function go(theParam, theValue) {
			var vParams = new Array();
			if (location.search != '') {
				vParams = location.search.substring(1).split("&");
			}

			for (j=0; j < arguments.length-1; j+=2) {
				bFound = false;
				
				for (i=0; i < vParams.length; i++) {
					if (vParams[i].substring(0,vParams[i].indexOf("=")) == arguments[j]) {
						var modifier = "";
						if ((vParams[i] == arguments[j] + "=" + arguments[j+1]) && (arguments[j]=='sort') && (arguments[j+1]!='')) modifier = " DESC";
						vParams[i] = arguments[j] + "=" + escape(arguments[j+1]) + modifier;
						bFound = true;
					}
				}
				if (!bFound) vParams[vParams.length] = arguments[j] + "=" + escape(arguments[j+1]);
			}
		
			theParams = vParams.join("&");
			if (theParams!='') theParams = "?" + theParams;

			theURL = "http://" + location.host + location.pathname + theParams;
			
			//alert(theURL);

			if (((arguments.length % 2)==1)) {
				if (arguments[arguments.length-1]=='new') {
					window.open(theURL,'_blank','status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=500,height=400');
				} else {
					theURL = arguments[arguments.length-1] + theParams;
					location.href = theURL;
				}
			} else {
				location.href = theURL;
			}
		}

	function searchPage(theTot) {
		el = document.getElementById("pagetosearch");
		theReqPage = el.value;

		if(theReqPage == "") {
			alert("Inserire un numero di pagina.");
			el.focus();
			return;
		} else if(theReqPage == 0 || theReqPage < 0) {
			if(theReqPage == 1) {
				theMsg = "pari a";
			} else {
				theMsg = "minore di";
			}
			alert("Il numero non puņ essere " + theMsg + " 0.");
			el.value = 1;
			el.focus();
			return;
		} else if(isNaN(theReqPage)) {
			if(theReqPage.toString().indexOf(",") != -1) {
				alert("Nel campo non sono ammesse cifre decimali.");
				el.value = 1;
				el.focus();
				return;
			}
			alert("Inserire nel campo un valore numerico.");
			el.value = 1;
			el.focus();
			return;
		} else if (theReqPage.toString().indexOf(".") != -1) {
			alert("Nel campo non sono ammesse cifre decimali.");
			el.value = 1;
			el.focus();
			return;
		} else if(theReqPage > theTot) {
			theReqPage = theTot;
		}

		go('page',theReqPage);
	}

	function searchPageDD(theTot, thePos) {
		el = document.getElementById("pagetosearch"+thePos);
		theReqPage = el.value;

		if(theReqPage == "") {
			alert("Inserire un numero di pagina.");
			el.focus();
			return;
		} else if(theReqPage == 0 || theReqPage < 0) {
			if(theReqPage == 1) {
				theMsg = "pari a";
			} else {
				theMsg = "minore di";
			}
			alert("Il numero non puņ essere " + theMsg + " 0.");
			el.value = 1;
			el.focus();
			return;
		} else if(isNaN(theReqPage)) {
			if(theReqPage.toString().indexOf(",") != -1) {
				alert("Nel campo non sono ammesse cifre decimali.");
				el.value = 1;
				el.focus();
				return;
			}
			alert("Inserire nel campo un valore numerico.");
			el.value = 1;
			el.focus();
			return;
		} else if (theReqPage.toString().indexOf(".") != -1) {
			alert("Nel campo non sono ammesse cifre decimali.");
			el.value = 1;
			el.focus();
			return;
		} else if(theReqPage > theTot) {
			theReqPage = theTot;
		}
		go('pagina',theReqPage);
	}

	function cloneItem(theTable, thePK, theRow, theUserID, theForm, theReload, theSubmit, theOpener) {
		if(confirm("Vuoi duplicare questo elemento?")) {
			var poststr = "table=" + encodeURI(theTable) + "&pkName=" + encodeURI(thePK) + "&pkValue=" + encodeURI(theRow) + "&userID=" + theUserID;
			makePOSTRequest('mosmodule/clone.php', poststr, theForm, theReload, theSubmit, theOpener);
		}
	}

	function publish(theTable, thePK, theRow, thePField, theForm, theReload, theSubmit, theOpener) {
		if(confirm("Vuoi pubblicare questo elemento?")) {
			var poststr = "table=" + encodeURI(theTable) + "&pkName=" + encodeURI(thePK) + "&pkValue=" + encodeURI(theRow) + "&pField=" + thePField + "&task=publish";
			makePOSTRequest('mosmodule/update.php', poststr, theForm, theReload, theSubmit, theOpener);
		}
	}

	function unpublish(theTable, thePK, theRow, thePField, theForm, theReload, theSubmit, theOpener) {
		if(confirm("Vuoi sospendere la pubblicazione di questo elemento?")) {
			var poststr = "table=" + encodeURI(theTable) + "&pkName=" + encodeURI(thePK) + "&pkValue=" + encodeURI(theRow) + "&pField=" + thePField + "&task=unpublish";
			makePOSTRequest('mosmodule/update.php', poststr, theForm, theReload, theSubmit, theOpener);
		}
	}

	function addToLoc(theTable, thePK, theRow, thePField, theForm, theReload, theSubmit, theOpener) {
		if(confirm("Vuoi aggiungere questo elemento alla locandina annuale?")) {
			var poststr = "table=" + encodeURI(theTable) + "&pkName=" + encodeURI(thePK) + "&pkValue=" + encodeURI(theRow) + "&pField=" + thePField + "&task=publish";
			makePOSTRequest('mosmodule/update.php', poststr, theForm, theReload, theSubmit, theOpener);
		}
	}

	function removeFromLoc(theTable, thePK, theRow, thePField, theForm, theReload, theSubmit, theOpener) {
		if(confirm("Vuoi rimuovere questo elemento dalla locandina annuale?")) {
			var poststr = "table=" + encodeURI(theTable) + "&pkName=" + encodeURI(thePK) + "&pkValue=" + encodeURI(theRow) + "&pField=" + thePField + "&task=unpublish";
			makePOSTRequest('mosmodule/update.php', poststr, theForm, theReload, theSubmit, theOpener);
		}
	}

	function showPreview(theTable, thePK, theRow) {
		window.open('mosmodule/scheda.php?table=' + theTable + '&pk=' + thePK + '&rowid=' + theRow + '&preview=y','preview','width=650,height=630,scrollbars=yes');
	}


	function showDate(theRow) {
		window.open('mosmodule/date.php?popup=true&rowid=' + theRow,'date','width=200,height=400');
	}

	function ricercaEventi(theLang, bRemote) {
		lDa = document.getElementById("ada").value + "-" + document.getElementById("mda").value + "-" + dueCifre(document.getElementById("gda").value);
		lA = document.getElementById("aa").value + "-" + document.getElementById("ma").value + "-" + dueCifre(document.getElementById("ga").value);
		//eval(go('testo', document.getElementById("testoRicerca").value, 'tipologia', document.getElementById("tipologiaRicerca").value, 'comune', document.getElementById("comuneRicerca").value, 'da', lDa, 'a', lA));

		if(bRemote) {
			basePath = "/";
		} else {
			basePath = "/apt/";
		}

		basePath += theLang + "/";

		switch(theLang) {
			case "it":
				basePath += "trova/eventi-della-provincia-di-firenze.html";
				break;

			case "en":
				basePath += "find/events.html";
				break;
		}


		location.href = basePath + "?testo=" + document.getElementById("testoRicerca").value + "&tipologia=" + document.getElementById("tipologiaRicerca").value + "&comune=" + document.getElementById("comuneRicerca").value + "&da=" + lDa + "&a=" + lA;
	}

	function ricercaMusei() {
		eval(go('testo', document.getElementById("testoRicerca").value, 'tipologia', document.getElementById("tipologiaRicerca").value, 'comune', document.getElementById("comuneRicerca").value));
	}


	function dueCifre(theNum) {
		if(theNum.length == 1) {
			return "0" + theNum;
		} else {
			return theNum;
		}
	}


	function resettaForm(theForm) {
		f = document.forms[theForm];
		els = f.elements;

		for(i=0;i<els.length;i++) {
			els[i].value = "";
		}
	}

	function showLite() {
		document.getElementById("dovedormirelite").style.display = "block";
		document.getElementById("tdLite").className = "searchTabActive";
		document.getElementById("tdFull").className = "searchTab";
		document.getElementById("dovedormirefull").style.display = "none";
	}

	function showFull() {
		document.getElementById("dovedormirefull").style.display = "block";
		document.getElementById("dovedormirelite").style.display = "none";
		document.getElementById("tdFull").className = "searchTabActive";
		document.getElementById("tdLite").className = "searchTab";
	}