// JavaScript Document
function createRequestObj(){ // creo un'istanza XMLHttpRequest
	var re;
	var browser=navigator.appName;
	if (browser=="Microsoft Internet Explorer"){ // sniff browser
		re=new ActiveXObject("Microsoft.XMLHTTP");
	} else re=new XMLHttpRequest();
	return re;
}

var http=createRequestObj();

function AjaxSelect(pagina_fonte,parametri,id_select) {
	document.getElementById(id_select).disabled = true;
	testo_etichetta = document.getElementById(id_select).options[0].text;
	document.getElementById(id_select).options[0].text = '<? echo CARICAMENTO_IN_CORSO ?>';
	url = pagina_fonte+'?'+parametri+"&rnd="+Math.random();
	http.open('get',url , true);
	http.onreadystatechange=function(){
		if (http.readyState==4 || http.readyState=="complete"){
			if (http.status == 200){
				text = http.responseText;
				appo = text.split('|');
				n_opt = document.getElementById(id_select).length;
				document.getElementById(id_select).options[0].text = testo_etichetta;
				document.getElementById(id_select).disabled = false;
				for (i=0; i < document.getElementById(id_select).length; i++) { //svuoto la select
					document.getElementById(id_select).options[1] = null;
				}
				if (id_select == 'id_provincia') {
					for (i=0; i < document.getElementById('id_comune').length; i++) { //svuoto la select dei comuni
						document.getElementById('id_comune').options[1] = null;
					}
				}
				for (i=1; i<=appo.length; i++) { //riempio con i nuovi dati
					cur_record = appo[i-1].split(',');
					document.getElementById(id_select).options[i] = new Option(cur_record[0],cur_record[1]);
				}
			} else {
				alert("Attenzione!! Errore di caricamento dati. Riprovare.");
			}
		}
	}
	http.send(null);
}
