function getXMLHTTPRequest() {
  try {
    req = new XMLHttpRequest();
    } catch(err1) {
      try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (err2) {
        try {
          req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (err3) {
          req = false;
        }
      }
    }
  return req;
}

var http = getXMLHTTPRequest();

function dostupnost_mesto() { //zvoleno mesto, chci ulice
  var myurl = '/xml/dostupnost.php';
  myRand = parseInt(Math.random()*999999999999999);
  
  select = document.getElementById("sel_mesto");
  select.options[0].disabled = true;  
  
  var modurl = myurl+"?mesto="+select.options[select.selectedIndex].value+"&rand="+myRand;

  select = document.getElementById("sel_cisdom");
  select.options.length = 0;
  select.options[0] = new Option('-- Vyberte číslo domu --', 0);
  select.selectedIndex = 0;  
  select.disabled = true;      

  http.open("GET", modurl, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.onreadystatechange = useHttpResponse_ulice;  
  http.send(null);    
}

function dostupnost_ulice() { //zvoleno mesto, chci ulice
  var myurl = '/xml/dostupnost.php';
  myRand = parseInt(Math.random()*999999999999999);
  
  select = document.getElementById("sel_ulice");
  var modurl = myurl+"?ulice="+encodeURI(select.options[select.selectedIndex].value)+"&rand="+myRand;

  http.open("GET", modurl, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.onreadystatechange = useHttpResponse_cisdom;  
  http.send(null);
}

function dostupnost_cisdom() {
	// zvolen dum (divny)
	var vybranyDum = document.getElementById('sel_cisdom');
	setCookie('dum',vybranyDum.options[vybranyDum.selectedIndex].value,1);
}

function useHttpResponse_ulice() { //dostavam ulice
  if (http.readyState && http.readyState == 4) {
    if(http.status && http.status == 200) {
        var ulice_id = http.responseXML.getElementsByTagName("id");
        var ulice_nazev = http.responseXML.getElementsByTagName("nazev");
        select = document.getElementById("sel_ulice");
        select.disabled = false;
        select.options.length = 0;
        
        select.options[0] = new Option('-- Vyberte ulici --', 0);
        select.options[0].disabled = true;
        for(i = 0; i < ulice_nazev.length; i++) {
           select.options[i+1] = new Option(ulice_nazev[i].childNodes[0].nodeValue, ulice_id[i].childNodes[0].nodeValue);
        }
	var vybraneMesto = document.getElementById('sel_mesto');
	setCookie('mesto',vybraneMesto.options[vybraneMesto.selectedIndex].value,1);
    }
    else {
    }
  } else { //cekam na odpoved
  }
}

function useHttpResponse_cisdom() { //dostavam cisla domu
  if (http.readyState && http.readyState == 4) {
    if(http.status && http.status == 200) {
	var vybranaUlice = document.getElementById("sel_ulice");
	setCookie('ulice',vybranaUlice.options[vybranaUlice.selectedIndex].value,1);
        var cisdom_id = http.responseXML.getElementsByTagName("id");
        var cisdom_nazev = http.responseXML.getElementsByTagName("nazev");
        select = document.getElementById("sel_cisdom");
        select.disabled = false;
        select.options.length = 0; 
        select.options[0] = new Option('-- Vyberte číslo domu --', 0);
        select.options[0].disabled = true;
        for(i = 0; i < cisdom_nazev.length; i++) {
           select.options[i+1] = new Option(cisdom_nazev[i].childNodes[0].nodeValue, cisdom_id[i].childNodes[0].nodeValue);
        }
    }
    else {
    }
  } else { //cekam na odpoved
  }
}

