
// function to return the xml http object
function GetXmlHttp() {
	var XmlHttp=false;
	try {
		XmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		try {
			XmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			try {
				XmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e1){
				XmlHttp=false;
			}
		}
	}
	return XmlHttp;
}

function SelectCustomField(Category,CustomFieldId,NumCustomFields,Selected) {
	// build url
	var Url="ajax_search.php?category="+Category+"&customfield="+CustomFieldId; //+"&selected="+Selected;
	var i;
	for (i=0; i<=CustomFieldId; i++) {
		// get selected value for the customfield i
		var Element=document.getElementById("sel_customfield"+i);
		var Value=Element.options[Element.selectedIndex].value;
		// add to url
		Url=Url+"&selected"+i+"="+Value;
	}
	// request
	var Req=GetXmlHttp();
	if (Req) {
		Req.onreadystatechange=function() {
			if (Req.readyState==4) {
				// unset loading state
				var Element=document.getElementById("div_loading");
				Element.style.visibility="hidden";
				// parse req
				if (Req.status==200) { // status ok
					// set custom field values
					var ElementId="div_customfield"+(CustomFieldId+1);
					//alert(ElementId);
					var Element=document.getElementById(ElementId);
					Element.innerHTML=Req.responseText;
					// reset values of other custom fields after this
					for (i=CustomFieldId+1; i<NumCustomFields; i++) {
						ElementId="div_customfield"+(i+1);
						var Element=document.getElementById(ElementId);
						Element.innerHTML="<select id=\"sel_"+ElementId+"\" name=\""+ElementId+"\" size=\"10\" style=\"width: 90%\" disabled onchange=\"this.form.submit();\"></select>";
					}
				} else {
					alert("Errore di connessione con il server:\n"+Req.statusText);
				}
			}
		}
		// loading
		var Element=document.getElementById("div_loading");
		Element.style.visibility="visible";
		// issue request
		Req.open("GET",Url,true);
		Req.send(null);
	}
}

