var aEventiFF = {
	"click": "click",
	"keyup": "keyup",
	"keydown": "keydown",
	"keypress": "keypress",
	"focus": "focus",
	"change": "change",
	"load": "DOMContentLoaded",
	"mouseover": "mouseover",
	"mouseout": "mouseout"
}
var aEventiIE = {
	"click": "onclick",
	"keyup": "onkeyup",
	"keydown": "onkeydown",
	"keypress": "onkeypress",
	"focus": "onfocus",
	"change": "onchange",
	"load": "DOMContentLoaded",
	"mouseover": "onmouseover",
	"mouseout": "onmouseout"
}

//VOID: Aggiunta sull'evento sEvento (label delle variabili precedenti) della funzione fFunzione all'oggetto oObj
function aggiungiListener(sEvento, fFunzione, oObj) {
	if (oObj.addEventListener) {
		oObj.addEventListener(eval("aEventiFF." + sEvento), fFunzione, true);
	}
	else {
		oObj.attachEvent(eval("aEventiIE." + sEvento), fFunzione);
	}
}

//STRING: Estrazione dei caratteri dall'inizio della stringa sStr alla prima posizione del carattere sChar
function substring_before(sStr, sChar) {
	var indice = sStr.indexOf(sChar);
	if (indice != -1) { return sStr.substring(0, indice); }
	else { return sStr; }
}

//STRING: Estrazione dei caratteri dalla prima posizione del carattere sChar alla fine della stringa sStr
function substring_after(sStr, sChar) {
	var indice = sStr.indexOf(sChar);
	if (indice != -1) { return sStr.substring(indice + 1); }
	else { return sStr; }
}

//STRING: Estrazione dei caratteri dalla prima posizione del carattere sLeft alla prima posizione del carattere sRight della stringa sStr
function substring_between(sStr, sLeft, sRight) {
	return substring_before(substring_after(sStr, sLeft), sRight);
}

//OBJECT DOCUMENT: Caricamento di un XML (anche XSL) dall'indirizzo dname
function loadXMLDoc(dname) {
	if (window.XMLHttpRequest) {
		xhttp = new XMLHttpRequest();
	}
	else {
		xhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhttp.open("GET", dname, false);
	xhttp.send("");
	return (xhttp.responseXML != null) ? xhttp.responseXML : xhttp.responseText;
}

//VOID: Trasformazione di sXml con sXsl e visualizzazione nell'elemento container
function displayResult(sXml, sXsl, container) {
	xml = loadXMLDoc(sXml);
	xsl = loadXMLDoc(sXsl);
	// code for IE
	if (window.ActiveXObject) {
		document.getElementById(container).innerHTML = xml.transformNode(xsl);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument) {
		xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xsl);
		resultDocument = xsltProcessor.transformToFragment(xml, document);
		document.getElementById(container).innerHTML = "";
		document.getElementById(container).appendChild(resultDocument);
	}
}

//STRING: Stampa della data odierna, utilizzando o il separatore passato alla funzione o il "-" di default
function dataOggi(sSep) {
	var data = new Date();
	sSep = (sSep != null) ? sSep : "-";
	return data.getFullYear() + sSep + (data.getMonth() + 1) + sSep + data.getDate();
}

//VOID: Modifica la src dell'immagine utilizzando sTxtSel come indicatore della "versione selezionata". E' dinamico sull'estensione dell'immagine.
function imgRollover(oImg, sTxtSel){
	var sExt = oImg.src.substring(oImg.src.length - 4);
	var sSel = sTxtSel + sExt;
	oImg.src = (oImg.src.indexOf(sSel) != -1) ? oImg.src = oImg.src.replace(sSel, sExt) : oImg.src.replace(sExt, sSel);
}

function bindRollover(oImg, sTxtSel) {
	if(oImg.getAttribute("loaded") == null) {
		var fFunzione = function() { imgRollover(oImg, sTxtSel); };
		aggiungiListener("mouseover", fFunzione, oImg);
		aggiungiListener("mouseout", fFunzione, oImg);
		settaAttributo(oImg, "loaded", "1");
	}
}

//[INT, INT]: Calcola la posizione [left, top] dell'elemento obj
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//[INT, INT]: Calcola la dimensione in pixel [width, height] della finestra del browser dell'utente
function getScreenSize() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight]
}

//[INT, INT]: Calcola il valore in pixel [x, y] della quantita' di scroll della pagina
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if (typeof (window.pageYOffset) == 'number') {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [scrOfX, scrOfY];
}

//STRING: Emula la funzione di trim sulla stringa
function trim(stringa) {
	while (stringa.substring(0, 1) == ' ') {
		stringa = stringa.substring(1, stringa.length);
	}
	while (stringa.substring(stringa.length - 1, stringa.length) == ' ') {
		stringa = stringa.substring(0, stringa.length - 1);
	}
	return stringa;
}

//VOID: setta un cookie di nome "name", valore "value" per un tempo definito da "days". 
//ATTENZIONE: i cookie di sessione si creano assegnano null a "days"
function setCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

//STRING: ritorna il valore del cookie "name" passato alla funzione
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

//[int, int]: ritorna un array con le dimensioni [larghezza, altezza] dell'elemento in analisi
function getDOMsize(oObj) {
	return [parseInt(oObj.offsetWidth), parseInt(oObj.offsetHeight)];
}

//VOID: setta l'altezza dell'elemento oObj a nMin, se l'altezza attuale di oObj &#195;&#168; minore di nMin
function minHeight(oObj, nMin) {
	if (oObj != null) {
		if (getDOMsize(oObj)[1] < nMin) {
			oObj.style.height = nMin + 'px';
		}
	}
}

function checkForm(objForm) {
	var oForm = objForm;
	var aInput = oForm.getElementsByTagName("input");
	var aSelect = oForm.getElementsByTagName("select");
	var aTextArea = oForm.getElementsByTagName("textarea");

	for (var i = 0; i < aInput.length; i++) {
		switch (aInput[i].type) {
			case "text":
				if ((aInput[i].getAttribute("obbligo") == 1) && (aInput[i].value == '')) {
					alert(aInput[i].getAttribute("alert"));
					aInput[i].focus();
					return false;
					break;
				}
				else {
					if ((aInput[i].value != '')) {
						if ((aInput[i].getAttribute("chkEmail") == 1) && (!isValidEmail(aInput[i].value))) {
							alert("L'indirizzo e-mail inserito non e' valido");
							aInput[i].focus();
							return false;
						}
						if ((aInput[i].getAttribute("chkTel") == 1) && (!isValidTel(aInput[i].value))) {
							alert("Il numero di telefono inserito non e' valido");
							aInput[i].focus();
							return false;
						}
						if ((aInput[i].getAttribute("chkCodFiscale") == 1) && (!isValidCodFiscale(aInput[i].value))) {
							alert("Il codice fiscale inserito non e' valido");
							aInput[i].focus();
							return false;
						}
						if (!isValidTel(aInput[i].value)) {
							if (aInput[i].getAttribute("cambioTelCell") == "1") {
								if (aInput[i].value.substr(0, 1) == "0") {
									aInput[i].name = "Telefono";
								}
								else {
									aInput[i].name = "Cellulare";
								}
							}
						}
					}
					break;
				}
			case "radio":
				if ((aInput[i].getAttribute("obbligo") == 1) && !controlloCheck(aInput[i].name, aInput)) {
					alert(aInput[i].getAttribute("alert"));
					return false;
					break;
				}
				else { break; }
			case "checkbox":
				if ((aInput[i].getAttribute("obbligo") == 1) && !controlloCheck(aInput[i].name, aInput, 0)) {
					alert(aInput[i].getAttribute("alert"));
					return false;
					break;
				}
				else { break; }
		}
	}

	for (var i = 0; i < aSelect.length; i++) {
		if ((aSelect[i].getAttribute("obbligo") == 1) && (aSelect[i].options[aSelect[i].selectedIndex].value == '')) {
			alert(aSelect[i].getAttribute("alert"));
			aSelect[i].focus();
			return false;
			break;
		}
	}

	for (var i = 0; i < aTextArea.length; i++) {
		if ((aTextArea[i].getAttribute("obbligo") == 1) && (aTextArea[i].value == '')) {
			alert(aTextArea[i].getAttribute("alert"));
			aTextArea[i].focus();
			return false;
			break;
		}
	}

	return true;
}

function controlloCheck(sNome, aCampi, nLimite) {
	var cnt = false;
	for (var k = 0; k != aCampi.length; k++) {
		if ((aCampi[k].getAttribute("name") == sNome) && ((aCampi[k].checked == true) || ((aCampi[k].value != "") && (aCampi[k].type == "text")))) {
			cnt = true;
		}
	}
	return cnt;
}

function isValidEmail(emailAddress) {
	var address = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return address.test(emailAddress);
}

function isValidTel(sValore) {
	return ((sValore.substr(0, 1) == "0") || (sValore.substr(0, 1) == "3"));
}

function isValidCodFiscale(codFiscale) {
	var re = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	return re.test(codFiscale.toUpperCase());
}

//VOID: setta l'attributo sAttrName al valore sAttrValue. In automatico decide se obj &#195;&#168; un oggetto. In caso contrario tenta prima una getElementsByName e poi una getElementById
function settaAttributo(obj, sAttrName, sAttrValue) {
	var aObj = null;
	if (typeof obj !== "object") {
		aObj = document.getElementsByName(obj);
		if (aObj.length == 0) {
			obj = document.getElementById(obj);
			aObj = null;
		}
	}
	if (aObj != null) {
		for (var x = 0; x != aObj.length; x++) {
			aObj[x].setAttribute(sAttrName, sAttrValue);
		}
	}
	else {
		obj.setAttribute(sAttrName, sAttrValue);
	}
}

function getNextSibling(startBrother){
	var endBrother = startBrother.nextSibling;
	while(endBrother.nodeType != 1){
		endBrother = endBrother.nextSibling;
		if(endBrother == null) {
			break;
		}
	}
	return endBrother;
}
