var xmlHttp = getXmlHttpObject();

function checkComune(id){
	xmlHttp.open('GET', '/it/copertura/request.php?comune='+id, true);
	xmlHttp.onreadystatechange = showResult;
	xmlHttp.send(null);
}

function loadList(tb, id){
	xmlHttp.open('GET', '/it/copertura/request.php?table='+tb+'&id='+id, true);
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.send(null);
}

function addOption(select, value, text) {
	// Aggiunge un elemento <option> ad una lista <select>
	var option = document.createElement("option");
	option.value = value,
	option.text = text;
	try {
		select.add(option, null);
	} catch(e) {
		// Per Internet Explorer
		select.add(option);
	}
}
function getSelected(select) {
	// Ritorna il valore dell'elemento <option> selezionato in una lista
	// return select.options[select.selectedIndex].value;
	return select.value;
	
}

function showResult() {
	if(xmlHttp.readyState == 4) {
		// Stato OK
		if (xmlHttp.status == 200) {
			var resp = xmlHttp.responseText;
			
			if(resp) {
				
				document.getElementById('spazio_risultati').innerHTML = resp;
				
						}
		} else {
			alert(xmlHttp.responseText);
		}
	}
}

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 e' l'ID della lista.
				var listId = values.shift();
				var select = document.getElementById(listId);
				// Elimina i valori precedenti
				var stateZero = '';
				while (select.options.length) {
					select.remove(0);
					document.getElementById('spazio_risultati').innerHTML = stateZero;
				} 
				
				if(listId == 'regioni') {
					addOption (select, 0, 'Selezionare regione');
				}
				if(listId == 'province') {
					addOption (select, 0, 'Selezionare provincia');
				}
				if(listId == 'comune') {
					addOption (select, 0, 'Selezionare comune');
				}
				
				var limit = values.length;
				
				for(i=0; i < limit; i++) {
					var pair = values[i].split('|');
					// aggiunge un elemento <option>
					addOption(select, pair[0], pair[1]);
				}
				
				var selRegId = getSelected(document.getElementById('regioni'));
				var selProvId = getSelected(document.getElementById('province'));
				var selComId = getSelected(document.getElementById('comune'));
				
				if (listId == 'province') {
					if (selRegId != 0) { 
					document.getElementById(listId).disabled = false;
					document.getElementById('comune').disabled = true;
					document.getElementById('comune').selectedIndex = 0;
					document.getElementById('spazio_risultati').innerHTML = stateZero;
					} else { 
					document.getElementById('province').disabled = true;
					document.getElementById('province').selectedIndex = 0;
					document.getElementById('comune').disabled = true;
					document.getElementById('comune').selectedIndex = 0;
					document.getElementById('spazio_risultati').innerHTML = stateZero;
					}
				}
				
				if (listId == 'comune') {
					if ((selProvId != 0) && (selRegId != 0)) { 
					document.getElementById(listId).disabled = false;
					} else { 
					document.getElementById(listId).disabled = true;
					}
				}
					
			}
		} 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;
}
