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 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 strDecode(stringa) {
	stringa = stringa.replace(/&s;/g, '\\');
	stringa = stringa.replace(/&dq;/g, '"');
	stringa = stringa.replace(/&q;/g, "'");
	stringa = stringa.replace(/&l;/g, '<');
	stringa = stringa.replace(/&g;/g, '>');
	stringa = stringa.replace(/&p;/g, '.');
	stringa = stringa.replace(/&bs;/g, ' ');
	stringa = stringa.replace(/&a;/g, '&'); // sempre per ultimo
	return stringa;
}

function dateToInt(strData) {
	// data in formato gg/mm/yyyy
	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;
	}
}

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);
			}
		}
	}
}