var xmlhttp;

function refreshOptions() {



    var selectElementPrinter = document.getElementById("printer");
    var selectElement = document.getElementById("producer");
    var optionIndex = selectElement.selectedIndex;
    var optionElement = selectElement.options[optionIndex];
    
    if (optionElement.value == -1) {
        var len = selectElementPrinter.childNodes.length;
      
      	while (selectElementPrinter.hasChildNodes())
      	{
      	  selectElementPrinter.removeChild(selectElementPrinter.firstChild);
      	}  
 
       var textNode;
       var option;

       option = document.createElement("option");
       textNode = document.createTextNode("-- Vyberte typ zařízení --"); //printers.item(i).id
       option.appendChild(textNode);
       option.setAttribute('value', "-1");
       selectElementPrinter.appendChild(option);    
             
    }
    else {
        xmlhttp=GetXmlHttpObject(); 
    
        if (xmlhttp==null) {
          alert ("Váš prohlížeč nepodporuje AJAX.");
          return;
        }
        var url="http://www.printline.cz/libraries/framework/ajax.php";
        url=url+"?q="+optionElement.value;
        xmlhttp.onreadystatechange=stateChanged;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);        
    }
}


function stateChanged() {
    if (xmlhttp.readyState==4) {
          handleServerResponse();
    }
}



function handleServerResponse() {

  var xmlResponse = xmlhttp.responseXML;
  var xmlRoot = xmlResponse.documentElement;
  
  var printers = xmlRoot.getElementsByTagName("printer");
  
  var html = "";
  
  var selectElementPrinter = document.getElementById("printer");
/*
  var optionsArray = selectElementPrinter.getElementsByTagName("option");
  alert(optionsArray.length);
  for (var i = 0; i<optionsArray.length; i++) {
  selectElementPrinter.removeChild(optionsArray[i]);
  }
  
  */
/*
   var len = selectElementPrinter.childNodes.length;
   alert(len);
   for(var i = 0; i < len; i++)
   {
      selectElementPrinter.removeChild(selectElementPrinter.childNodes[i]);
   }
*/

  var len = selectElementPrinter.childNodes.length;

	while (selectElementPrinter.hasChildNodes())
	{
	  selectElementPrinter.removeChild(selectElementPrinter.firstChild);
	}

  
  var textNode;
  var option;
  for (var i = 0; i<printers.length; i++) {
     option = document.createElement("option");
     textNode = document.createTextNode(printers[i].firstChild.nodeValue); //printers.item(i).id
     option.appendChild(textNode);
     option.setAttribute('value', printers[i].getAttribute("id"));
     selectElementPrinter.appendChild(option);
  }
  



 //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}




function GetXmlHttpObject() {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
}

