function assegnaXMLHttpRequest(){
	//alert("entrato costr");
	// lista delle variabili locali
	var
	// variabile di ritorno, nulla di default
	XHR = null,
	// informazioni sul nome del browser
	browserUtente = navigator.userAgent.toUpperCase();
	// browser standard con supporto nativo
	// non importa il tipo di browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		XHR = new XMLHttpRequest();
	// browser Internet Explorer
	// è necessario filtrare la versione 4
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0){
		// la versione 6 di IE ha un nome differente
		// per il tipo di oggetto ActiveX
		if(browserUtente.indexOf("MSIE 5") < 0)
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		// le versioni 5 e 5.5 invece sfruttano lo stesso nome
		else
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return XHR;
}

function elaborazione_subquery(idSelectSub,idCampo,valoreCampo,valoreCondizionale,lingua,sid){
	//alert(valoreCondizionale);
	if(typeof valoreCondizionale == 'string'){}
	else{
		var strTemp='';
		for(t=0;t<valoreCondizionale.length;t++){
			if(strTemp != '') strTemp+='%23n%23';
			if(valoreCondizionale[t].selected){
				if(valoreCondizionale[t].value=='#NULL#') strTemp+='%23NULL%23';
				else strTemp+=valoreCondizionale[t].value;
			}
		}
		valoreCondizionale=strTemp;
	}
	//alert(valoreCondizionale);
	var data = new Date();
	var returned = "";
	var req = assegnaXMLHttpRequest();
	if(req){
		if(valoreCondizionale=="#NULL#") valoreCondizionale="%23NULL%23";
		var url=PATH2ROOT+'backend/mutant_table_dati/subquery.php?idCampo='+idCampo+'&valoreCondizionale='+valoreCondizionale+'&idLingua='+lingua+'&time='+data.getTime()+'&'+sid;
		//alert(url);
		req.open("get",url,true);
		req.onreadystatechange = function(){
			if(req.readyState==4){
				if(req.status==200){
					returned = req.responseText;
					//alert(returned);
					//document.open();
					//document.write(returned);
					//pulisco la select
					pulisciSelect(idSelectSub);
					//parsing dell'output
					var strExpl = returned.split("#<#br#>#");
					//alert(strExpl.length);
					for(i=0;i<(strExpl.length - 1);i++){
						var rigaExpl = strExpl[i].split("#<#,#>#");
						//creo le option
						if(valoreCampo == rigaExpl[0])
							var optObj = new Option(rigaExpl[1],rigaExpl[0],false,true);
						else
							var optObj = new Option(rigaExpl[1],rigaExpl[0],false,false);
						document.getElementById(idSelectSub).options[i]=optObj;
					}
				}
			}
		}
		req.send(null);   
	}
}

function myAddEventListener(obj,ev,cbFunc)
{
        if(obj!==null)
        {
                if(obj.attachEvent)
                {
                        obj.attachEvent('on'+ev,cbFunc);
                }
                else if(obj.addEventListener)
                {
                        obj.addEventListener(ev,cbFunc,false);
                }
                else
                {
                        obj['on'+ev] = cbFunc;
                }
        }
}


function myRemoveEventListener(obj,ev,cbFunc)
{
        if(obj!==null)
        {
                if(obj.detachEvent)
                {
                        obj.detachEvent('on'+ev,cbFunc);
                }
                else if(obj.removeEventListener)
                {
                        obj.removeEventListener(ev,cbFunc,false);
                }
                else
                {
                        obj['on'+ev] = null;
                }
        }
}

function pulisciSelect(idElement){
	var selectObj = document.getElementById(idElement);
	for(i=selectObj.options.length;i>=0;i--){
		selectObj.options[i] = null;
	}
}
