var xmlHttp = sortuXmlHttpRequestObjektua();
function sortuXmlHttpRequestObjektua(){
  var xmlHttp;
  if(window.ActiveXObject){
    try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (e) { xmlHttp = false; }
  }
  else {
    try { xmlHttp = new XMLHttpRequest(); }
    catch (e) { xmlHttp = false; }
  }
  if (!xmlHttp)
    alert("XMLHttpRequest sortzean errorea gertatu da.");
  else
    return xmlHttp;
}

function cargarMunicipios(){
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    provincia = encodeURIComponent(document.getElementById("provincias").value);
    xmlHttp.open("GET", "buscarmunicipios.php?prov="+ provincia, true);
    xmlHttp.onreadystatechange = prozesatuZerbitzariarenErantzuna;
    xmlHttp.send(null);
  }
}

function prozesatuZerbitzariarenErantzuna() {
  if (xmlHttp.readyState == 4){
    if (xmlHttp.status == 200){// HTTP status-a ez bada 200, errorea gertatu da
      xmlErantzuna = xmlHttp.responseXML;
      xmlDocumentElementua = xmlErantzuna.documentElement;
	  filmak = xmlDocumentElementua.getElementsByTagName('elem');
	  selek = document.getElementById('municipios');
	  selek.innerHTML=" ";
	  nodoa = document.createTextNode('Denak');
	  opt = document.createElement('option');
	  opt.appendChild(nodoa);
	  selek.appendChild(opt);
	  for (i = 0; i < filmak.length; i++){ 
        izenb = filmak[i].firstChild.data;
		nodoa = document.createTextNode(izenb);
		opt = document.createElement('option');
		opt.appendChild(nodoa);
		selek.appendChild(opt);
      }
    }
    else {
      alert("Zerbitzaria atzitzean arazoren bat dago: " + xmlHttp.statusText);
    }
  }
}