<!--
// #############################################################################
// funzioni per aggiungere eventi a determinati oggetti:
// per attivare JS automaticamente e in modo trasparente (graceful degradation)
// #############################################################################
//
function addEvent(elm,evType,fn,useCapture){
	if(elm.addEventListener){
		elm.addEventListener(evType,fn,useCapture); 
		return true;
	}else if(elm.attachEvent){ 
		var r=elm.attachEvent('on'+evType,fn); 
		EventCache.add(elm,evType,fn);
		return r; 
	}else{
		elm['on'+evType]=fn;
	};
};
//
function getEventSrc(e){
	if(!e) e=window.event;
	if(e.originalTarget){
		return e.originalTarget;
	}else if(e.srcElement){
		return e.srcElement;
	};
};
//
function addLoadEvent(func){
	var oldonload=window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload=function(){
			oldonload();
			func();
		};
	};
};
//
var EventCache=function(){
	var listEvents = [];
	return {
			listEvents : listEvents,
			add : function(node, sEventName, fHandler, bCapture){
				listEvents.push(arguments);
	},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
//
// #############################################################################
// funzione per disinfettare i dati del form
// #############################################################################
//
function sanitize(input){
	// ogni carattere diverso a "0-9" "a-z" "A-Z" e " " (spazio) viene eliminato
	return input.replace(/[^a-z0-9\s]/gi,"");
};
//
// #############################################################################
// funzione per comporre dai dati del form una stringa da inviare tramite ajax
// #############################################################################
//
function strdatiform(idform){
	var stringa="";
	var frm=document.forms[idform];
	var numElem=frm.elements.length;
	for(var i=0;i<numElem;i++){
		var tipo=frm.elements[i].type;
		if(tipo=="checkbox" || tipo=="hidden" || tipo=="radio" || tipo=="text"){
			// aggiungo tutti gli elementi che contengono qualcosa e che appartengono ad un tipo ammesso
			var nome=sanitize(frm.elements[i].name);
			var valore=sanitize(frm.elements[i].value);
			if(nome!="" && valore!=""){
				stringa+=nome+"="+encodeURIComponent(valore)+"&";
			};
		};
	};
	// tolgo l'ultimo &
	stringa=stringa.replace(/\&$/,"");
	return stringa;
};
//
// #############################################################################
// funzione per creare un oggetto XMLHttp
// #############################################################################
//
function getxmlhttp(){
	// preimposto una variabile booleana per fare il check su Internet Explorer
	var xmlhttp=false;
	// controllo se si sta usando IE.
	try{
		// Se la versione di JS è maggiore di 5
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			// altrimenti si sta usando un activex meno recente
			try{
				// se stiamo usando IE
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					// altrimenti il browser non è IE
					xmlhttp=false;
				};
	};
	// Se stiamo usando un browser non IE allora creo una istanza JS
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp=new XMLHttpRequest();
	};
	return xmlhttp;
};
//
// #############################################################################
// funzione per inviare i dati ad una certa pagina utilizzanto GET o POST
// #############################################################################
//
function processajax(serverPage,objID,getOrPost,str){
	// individuo l'elemento obj
	var obj=document.getElementById(objID);
	// creo un oggetto XMLHttpRequest
	xmlhttp=getxmlhttp();
	if(getOrPost=="get"){
		// allora utilizzo il metodo GET
		xmlhttp.open("GET",serverPage);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				obj.innerHTML = xmlhttp.responseText;
			};
		};
		xmlhttp.send(null);
	}else{
		// utilizzo il metodo POST
		xmlhttp.open("POST",serverPage,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				obj.innerHTML = xmlhttp.responseText;
			};
		};
		xmlhttp.send(str);
	};	
};
//
// #############################################################################
// funzioni per visualizzare o nascondere il blocco timer animazione
// #############################################################################
function showContactTimer () {
	var loader = document.getElementById('loadBar');
	loader.style.display = 'block';
	sentTimer = setTimeout("hideContactTimer()",6000);
};
//
function hideContactTimer () {
	var loader = document.getElementById('loadBar');
	var success = document.getElementById('emailSuccess');
	var fieldArea = document.getElementById('contactFormArea');
	var inputs = fieldArea.getElementsByTagName('input');
	var inputsLen = inputs.length;
	var tAreas = fieldArea.getElementsByTagName('textarea');
	var tAreasLen = tAreas.length;
	// Hide the load bar alas! Done Loading
	loader.style.display = "none";
	success.style.display = "block";
	success.innerHTML = '<strong style="color:green;">'+grabPosXML("confirmation")+'</strong>';
	// Now Hijack the form elements
	for ( i=0;i<inputsLen;i++ ) {
		if ( inputs[i].getAttribute('type') == 'text' ) {
			inputs[i].value = '';
		};
	};
	for ( j=0;j<tAreasLen;j++ ) {
		tAreas[j].value = '';
	};
};
//
// #############################################################################
// funzione per fare il bookmark tra i siti preferiti
// #############################################################################
//
function bookmarkSite(title, url){
	if (document.all) window.external.AddFavorite(url, title);
    else if (window.sidebar) window.sidebar.addPanel(title, url, "");
}
//
// #############################################################################
//	funzione per impostare la classe className al div con identificatore=id 
// #############################################################################
//
function setClassName(id, className) {
	var element;
	if(document.getElementById && (element = document.getElementById(id))){
		element.className = className;
	}
}
//
// #############################################################################
// Setta la classe di default default_class
// in modo ricorsivo per i div con id nella forma label_ini1,2,3 fino a max_items
// es. n1 n2 n3 n4...
// Utilizzata nelle pagine con blocchi a comparsa in base al click
// es. ""Dove siamo" o "News"//
// #############################################################################
//
function setDefaultClass(label_ini,default_class,max_items){
	for(var i=1;i<=max_items;i++){
		setClassName(label_ini+i,default_class);
	}
}
//
addEvent(window,'unload',EventCache.flush, false);
//-->

