/* *********************************************************************************************** */

// Oggetto per interazioni MEAjax

function getMEAjaxObj(){
    MEAjax = new Object(); 
    MEAjax.isUpdating = true; 
    
    MEAjax.Request = function(method, url, callback) 
    { 
	this.isUpdating = true; 
	this.callbackMethod = callback; 
	this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP"); 
	this.request.onreadystatechange = function() { MEAjax.checkReadyState(); }; 
	this.request.open(method, url, true); 
	this.request.send(url); 
    } 
    
    MEAjax.checkReadyState = function(_id){ 
	switch(this.request.readyState) 
	{ 
	    case 1: break; 
	    case 2: break; 
	    case 3: break; 
	    case 4: 
		this.isUpdating = false; 
		this.callbackMethod(this.request); 
	} 
    }
    

    MEAjax.updateHTML = function(method, url, idElement){
    
    this.isUpdating = true; 
    this.callbackMethod = function(doc){document.getElementById(idElement).innerHTML = doc.responseText}; 
    this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP"); 
    this.request.onreadystatechange = function() { MEAjax.checkReadyState(); }; 
    this.request.open(method, url, true); 
    this.request.send(url); 

    }
    return MEAjax;
}

/* *********************************************************************************************** */


function addEvent(obj,ev,fn){
	if(obj.addEventListener) {
		// metodo w3c
		obj.addEventListener(ev, fn, false);
	} else if(obj.attachEvent) {
		// metodo IE
		obj.attachEvent('on'+ev, fn);
	} else {
		// se i suddetti metodi non sono applicabili
		// se esiste gia' una funzione richiamata da quel gestore evento
		if(typeof(obj['on'+ev])=='function'){
			// salvo in variabile la funzione gia' associata al gestore
			var f=obj['on'+ev];
			// setto per quel gestore una nuova funzione 
			// che comprende la vecchia e la nuova
			obj['on'+ev]=function(){if(f)f();fn()}
		}
		// altrimenti setto la funzione per il gestore
		else obj['on'+ev]=fn;
	}
}

/* *********************************************************************************************** */
//ricerca elementi in base alla classe

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
		
	return classElements;
}
/* *********************************************************************************************** */


function hide(el){
    el.style.display='none';
}
function show(el){
    el.style.display='block';
}


/* ********************************************************************************************** */

// elimina spazi prima e dopo di una 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;
}

/* ********************************************************************************************** */

function appendCss(cssPath){
	if (!document.getElementById(cssPath)){
		var headID = document.getElementsByTagName("head")[0];         
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.id = cssPath;
		cssNode.href = cssPath;
		cssNode.media = 'screen';
		headID.appendChild(cssNode);
	}
}
