var xmlHttp = getXmlHttpObject();

function loadList(tb, id){
var url='request.php?table='+tb+'&id='+id;
xmlHttp.open('GET', url, true);
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.send(null);
}

function addOption(myselect, value, text) {
	//Aggiunge un elemento <option> ad una lista <select>
	var option = document.createElement("option");
	option.value = value;
	option.text = text;
	try {
		myselect.add(option, null);
	} catch(e) {
		//Per Internet Explorer
		myselect.add(option);
	}
}
function getSelected(myselect) {
	//Ritorna il valore dell'elemento <option> selezionato in una lista
	return myselect.options[myselect.selectedIndex].value;
}
function stateChanged() {
	if(xmlHttp.readyState == 4) {
		//Stato OK
		if (xmlHttp.status == 200) {
			var resp = xmlHttp.responseText;
			if(resp) {
				//Le coppie di valori nella striga di risposta sono separate da ;
				var values = resp.split(';');
				//Il primo elemento è l'ID della lista.
				var listId = values.shift();
				var myselect = document.getElementById(listId);
				//Elimina i valori precedenti
				while (myselect.options.length) {
					myselect.remove(0);
				}	
				if(listId == 'lh_regioni') {addOption (myselect, 0, 'Seleziona regione');}
				else{addOption (myselect, 0, 'Seleziona provincia');}
				var limit = values.length;
				
				for(i=0; i < limit; i++) {
					var pair = values[i].split('|');
					//aggiunge un elemento <option>
					addOption(myselect, pair[0], pair[1]);
				}
			}
		} else {
			alert(xmlHttp.responseText);
		}
	}
}

function getXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}
