filtro_spazi = /^\s*$/;
filtro_alfanum = /^[A-z0-9_\-]+$/;
filtro_digit = /^\d+$/;
filtro_mail = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]{2,}\.)+[a-zA-Z0-9]{2,}$/;
filtro_float = /^\d+[\,\.]\d\d?$|^\d+$/;
filtro_data = /^[0-3][0-9][\/\-][01][0-9][\/\-][123][0-9]{3}$/;
filtro_flv = /\.flv$/i;
filtro_jpg = /\.jpg$/i;
filtri = new Array(filtro_alfanum	// 0
						, filtro_digit		// 1
						, filtro_mail		// 2
						, filtro_float		// 3
						, filtro_data		// 4
						, filtro_flv		// 5
						, filtro_jpg);		// 6

function strToHTML(str) {
	return str.replace(/\&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\'/g,'&#39;').replace(/\"/g,'&quot;').replace(/  /g,' &nbsp;');
}

function safeSubmit(theForm) {
	for(i = 0; i < document.links.length; i++) {
		document.links[i].href = "#";
	}
	for(i = 0; i < theForm.elements.length; i++) {
		if(theForm.elements[i].type == "button" || theForm.elements[i].type == "image" || theForm.elements[i].type == "submit") {
			theForm.elements[i].disabled = true;
		}
	}
	theForm.submit();
}

function valSelect(objSelect) {
	if(objSelect) {
		if(objSelect.selectedIndex >= 0)
			return objSelect.options[objSelect.selectedIndex].value;
		else
			return "";
	} else return "";
}

function valRadio(objRadio) {
	if(objRadio) {
		if(objRadio.length) {
			for(i = 0; i < objRadio.length; i++) {
				if(objRadio[i].checked)
					return objRadio[i].value;
			}
		} else {
			if(objRadio.checked)
				return objRadio.value;
		}
	}
	return '';
}

function checkInput(oggetto, descrizione, indice_filtri, obbligatorio) {
	if(oggetto) {
		if(oggetto.type && /^select.*/i.test(oggetto.type)) {
			if(obbligatorio && filtro_spazi.test(valSelect(oggetto))) {
				alert("PREGO SELEZIONARE " + descrizione);
				oggetto.focus();
				return false;
			}
		} else {
			if(filtro_spazi.test(oggetto.value) && obbligatorio) {
				alert("PREGO COMPILARE IL CAMPO " + descrizione);
				oggetto.focus();
				return false;
			} else if(indice_filtri >= 0 && oggetto.value != "" && !filtri[indice_filtri].test(oggetto.value)) {
				alert("ATTENZIONE, " + descrizione + " NON VALIDO");
				oggetto.focus();
				return false;
			}
		}
		return true;
	} else
		return true;
}

function premutoInvio(e) {
	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else
		keycode = e.which;
	if (keycode == 13)
		return true;
	else
		return false;
}

function full_screen() {
	if (typeof(screen.availWidth) != "undefined") {
		if(!navigator.userAgent || navigator.userAgent.toLowerCase().indexOf("msie") >= 0) {
			window.resizeTo(screen.availWidth + 8, screen.availHeight + 8);
			window.moveTo(-4, -4);
		} else {
			window.resizeTo(screen.availWidth, screen.availHeight);
			window.moveTo(0, 0);
		}
	}
}

function centerInScreen(w,h) {
	var wScreen, hScreen;
	if (typeof(screen.availWidth) != "undefined") {
		wScreen = screen.availWidth;
		hScreen = screen.availHeight;
	} else {
		wScreen = 1024;
		hScreen = 768;
	}
	resizeTo(w,h);
	moveTo((wScreen - w) / 2, (hScreen - h) / 2);
}

function dateToInt(strData) {
	// data in formato gg/mm/yyyy
	if(filtro_data.test(strData)) {
		var objDataTemp = new Date(strData.substr(6,4), strData.substr(3,2) - 1, strData.substr(0,2));
		return objDataTemp.valueOf();
	} else return 0;
}

function cleanForm(ilform) {
	for(var i=0; elemento = ilform.elements[i]; i++) {
		if(elemento.type=="text") elemento.value = "";
		if(elemento.type=="select-one") elemento.selectedIndex = 0;
		if(elemento.type=="radio") if(elemento.value == "") elemento.checked = true;
	}
}

function TrasferisciValoriSelezionatiDueSelect(selectFrom, selectTo) {
	if(selectFrom.selectedIndex >= 0) {
		for(i = 0; i < selectFrom.length; i++) {
			if(selectFrom[i].selected) {
				selectTo[selectTo.length] = new Option(selectFrom[i].text, selectFrom[i].value);
				selectFrom[i--] = null;
			}
		}
	}
}

function selezionaTutteLeOption(objSelect) {
	for(i = 0; i < objSelect.options.length; i++)
		objSelect.options[i].selected = true;
}

function selezionaTutteLeCheckbox(objCB, selezionare) {
	if(objCB.length) {
		for(i = 0; i < objCB.length; i++) {
			objCB[i].checked = selezionare;
		}
	} else {
		objCB.checked = selezionare;
	}
}

function radioCheckboxChecked(objElement) {
	// ritorna il numero di checkbox o radio spuntati dello stesso nome
	var trovati = 0;
	if(objElement) {
		if(objElement.length) {
			for(var i=0; i < objElement.length; i++) {
				if(objElement[i].checked)
					trovati++;
			}
		} else {
			if(objElement.checked)
				trovati++;
		}
	}
	return trovati;
}

function apriFinestra(url, nomeFinestra) {
	finestraFiglia = window.open(url, nomeFinestra, "toolbar=0,scrollbars=1,status=0,resizable=1,width=200,height=200");
	finestraFiglia.focus();
}

var XMLHttpRequestObject = false;
if(window.XMLHttpRequest)
	XMLHttpRequestObject = new XMLHttpRequest();
else if(window.ActiveXObject)
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");
function getAjaxData(parametri, cellaTarget, asincrono, append) {
	// solo nella modalità sincrona la funzione può ritornare un valore se cellaTarget è false
	// se append = true in cellaTarget viene aggiunto il responseText, altrimenti normalmente sovrascritto il vecchio contenuto
	if(XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET", "ajax.php?" + parametri, asincrono);
		if(asincrono) {
			XMLHttpRequestObject.onreadystatechange = function() {
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
					if(append) cellaTarget.innerHTML += XMLHttpRequestObject.responseText;
					else cellaTarget.innerHTML = XMLHttpRequestObject.responseText;
				}
			}
			XMLHttpRequestObject.send(null);
		} else {
			XMLHttpRequestObject.send(null);
			if (XMLHttpRequestObject.status == 200) {
				if(cellaTarget) {
					if(append) cellaTarget.innerHTML += XMLHttpRequestObject.responseText;
					else cellaTarget.innerHTML = XMLHttpRequestObject.responseText;
				} else
					return(XMLHttpRequestObject.responseText);
			}
		}
	}
}

function dettagliViaggio(idViaggio, dataViaggio) {
	var urlDettagli = "viaggio-dati.php?idViaggio=" + idViaggio + "&dataViaggio=" + dataViaggio + "#inizioDati";
	if(opener) {
		opener.location.href = urlDettagli;
		opener.focus();
	} else location.href = urlDettagli;
}
function nuovoViaggio(nowin, idHub, idEvento) {
	var urlDettagli = "viaggio-nuovo-edit.php?idHubDestinazione=" + idHub + "&idEvento=" + idEvento+ "#inizioDati";
	if(opener && !nowin) {
		opener.location.href = urlDettagli;
		opener.focus();
		self.close();
	} else location.href = urlDettagli;
}

function getPageSize() {
	// ritorna un array con 6 valori:
	// 0: larghezza totale, inclusa l'ampiezza dello scrolling orizzontale
	// 1: altezza totale, inclusa l'ampiezza dello scrolling verticale
	// 2: larghezza dello spazio utile della finestra
	// 3: altezza dello spazio utile della finestra
	// 4: posizione dello scroll orizzontale attuale
	// 5: posizione dello scroll verticale attuale
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {    
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	var orizScroll, vertScroll;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		orizScroll = window.pageXOffset;
		vertScroll = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
		orizScroll = document.documentElement.scrollLeft;
		vertScroll = document.documentElement.scrollTop;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
		orizScroll = document.body.scrollLeft;
		vertScroll = document.body.scrollTop;
	}    
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight) {
		fullHeight = windowHeight;
	} else {
		fullHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth) {    
		fullWidth = windowWidth;
	} else {
		fullWidth = xScroll;
	}
	arrayPageSize = new Array(fullWidth,fullHeight,windowWidth,windowHeight,orizScroll,vertScroll);
	return arrayPageSize;
}
function getPageCoords(element) {
	var coords = {x: 0, y: 0};
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}
function refreshCaptcha() {
	var objImg = document.images["imgCaptcha"];
	if(objImg) objImg.src = "captcha_image.php?ots=" + escape(Date());
}