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 apriFinestra(url, nomeFinestra) {
	finestraFiglia = window.open(url, nomeFinestra, "toolbar=no,scrollbars=yes,status=yes,resizable=yes,width=500,height=500,top=100,left=100");
	finestraFiglia.focus();
}

function valSelect(objSelect) {
	if(objSelect) {
		if(objSelect.selectedIndex >= 0)
			return objSelect.options[objSelect.selectedIndex].value;
		else
			return "";
	} else return "";
}

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 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 SELEZIONI IL CAMPO " + descrizione);
				oggetto.focus();
				return false;
			}
		} else {
			if(filtro_spazi.test(oggetto.value) && obbligatorio) {
				alert("PREGO COMPILI IL CAMPO "+descrizione);
				oggetto.focus();
				return false;
			} else if(indice_filtri >= 0 && oggetto.value != "" && !filtri[indice_filtri].test(oggetto.value)) {
				alert("ATTENZIONE, CAMPO " + 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) {
	var objDataTemp = new Date(strData.substr(6,4), strData.substr(3,2) - 1, strData.substr(0,2));
	return objDataTemp.valueOf();
}

var XMLHttpRequestObject = false;
if (window.XMLHttpRequest)
	XMLHttpRequestObject = new XMLHttpRequest();
else if (window.ActiveXObject)
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");

function getAjaxData(parametri, cellaTarget, asincrono) {
	// solo nella modalità sincrona (asincrono=false) la funzione può ritornare un valore se cellaTarget è false
	if(XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET", "ajax.php?" + parametri, asincrono);
		if(asincrono) {
			XMLHttpRequestObject.onreadystatechange = function() {
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
					cellaTarget.innerHTML = XMLHttpRequestObject.responseText;
				}
			}
			XMLHttpRequestObject.send(null);
		} else {
			XMLHttpRequestObject.send(null);
			if (XMLHttpRequestObject.status == 200) {
				if(cellaTarget)
					cellaTarget.innerHTML = XMLHttpRequestObject.responseText;
				else
					return(XMLHttpRequestObject.responseText);
			}
		}
	}
}

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 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 mostraOverboxIframe(iframeUrl) {
	var aDimensioniPag = getPageSize();
	var objPannelloOscurante = document.getElementById("pannelloOscurante");
	var objIframe = document.getElementById("overbox_frame_id");
	objPannelloOscurante.style.width = aDimensioniPag[0] + "px";
	objPannelloOscurante.style.height = aDimensioniPag[1] + "px";
	objPannelloOscurante.style.display = "block";
	window.overbox_frame.location.href = iframeUrl;
	objIframe.style.marginLeft = aDimensioniPag[4] - Math.floor(parseInt(objIframe.style.width,10) / 2) + "px";
	objIframe.style.marginTop = aDimensioniPag[5] - Math.floor(parseInt(objIframe.style.height,10) / 2) + "px";
	objIframe.style.display = "block";
}
function mostraFoto(fotoPathOrIndex, aElencoFoto) {
	var imgMostraFoto;
	var aDimensioniPag = getPageSize();
	var objPannelloOscurante = document.getElementById("pannelloOscurante");
	var objPannelloFoto = document.getElementById("pannelloFoto");
	var objFotoWait = document.getElementById("mostraFotoWait");
	objPannelloFoto.style.display = "none";
	if(document.getElementById("imgMostra_next")) {
		document.getElementById("imgMostra_next").style.display = "none";
		document.getElementById("imgMostra_prev").style.display = "none";
	}
	if(filtro_digit.test(fotoPathOrIndex)) {
		fotoPath = aElencoFoto[fotoPathOrIndex];
		if(document.getElementById("imgMostra_next")) {
			document.getElementById("imgMostra_next").style.display = "none";
			document.getElementById("imgMostra_prev").style.display = "none";
			if(aElencoFoto[fotoPathOrIndex+1]) {
				document.getElementById("imgMostra_next").onclick = function() {
					mostraFoto(fotoPathOrIndex+1, aElencoFoto);
				}
				document.images["imgMostra"].onclick = document.getElementById("imgMostra_next").onclick;
				document.getElementById("imgMostra_next").style.display = "inline";
			} else
				document.images["imgMostra"].onclick = null;
			if(aElencoFoto[fotoPathOrIndex-1]) {
				document.getElementById("imgMostra_prev").onclick = function() {
					mostraFoto(fotoPathOrIndex-1, aElencoFoto);
				}
				document.getElementById("imgMostra_prev").style.display = "inline";
			}
		}
	} else
		fotoPath = fotoPathOrIndex;
	imgMostraFoto = new Image();
	imgMostraFoto.onload = function() {
		var aDimensioniPag = getPageSize();
		document.images["imgMostra"].width = imgMostraFoto.width;
		document.images["imgMostra"].height = imgMostraFoto.height;
		objPannelloFoto.style.marginLeft = aDimensioniPag[4] - Math.floor(parseInt(imgMostraFoto.width, 10) / 2) + "px";
		objPannelloFoto.style.marginTop = aDimensioniPag[5] - Math.floor(parseInt(imgMostraFoto.height, 10) / 2) - 30 + "px";
		objPannelloFoto.style.display = "block";
	}
	imgMostraFoto.src = fotoPath;
	document.images["imgMostra"].src = imgMostraFoto.src;
	objPannelloOscurante.style.width = aDimensioniPag[0] + "px";
	objPannelloOscurante.style.height = aDimensioniPag[1] + "px";
	objPannelloOscurante.style.display = "block";
	objFotoWait.style.marginLeft = (aDimensioniPag[4] - 10) + "px";
	objFotoWait.style.marginTop = (aDimensioniPag[5] - 10) + "px";
	objFotoWait.style.display = "block";
}

function nascondiOverboxes() {
	document.getElementById("mostraFotoWait").style.display = "none";
	document.getElementById("pannelloOscurante").style.display = "none";
	document.getElementById("pannelloFoto").style.display = "none";
	window.overbox_frame.location.href = "about:blank";
	document.getElementById("overbox_frame_id").style.display = "none";
}
