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][0-9][0-9]$/;
filtro_video_valido = /\.(mp4)|(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_video_valido		// 5
						, filtro_jpg);		// 6

function doAction(strAzione) {
	document.forms[0].azione.value = strAzione;
	document.forms[0].submit();
}

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.selectedIndex >= 0)
		return objSelect.options[objSelect.selectedIndex].value;
	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) {
	var objDataTemp = new Date(strData.substr(6,4), strData.substr(3,2) - 1, strData.substr(0,2));
	return objDataTemp.valueOf();
}

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;
		if(elemento.type=="checkbox") if(elemento.value == "") elemento.checked = true; else elemento.checked = false;
	}
}

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) {
	// solo nella modalità sincrona 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 getPageCoords(element) {
	var coords = {x: 0, y: 0};
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}

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 mostraOggettoCentrato(obj) {
	var aDimensioniPag = getPageSize();
	larghezza = parseInt(obj.style.width,10);
	altezza = parseInt(obj.style.height,10);
	obj.style.marginLeft = aDimensioniPag[4] - Math.floor(larghezza / 2) + "px";
	obj.style.marginTop = aDimensioniPag[5] - Math.floor(altezza / 2) + "px";
	obj.style.display = "block";
}