﻿var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject() //vytvorenie objektu - XmlHttpRequest
{	
  var xmlHttp;
  if(window.ActiveXObject)	//ak je browser Internet Explorer - pouzije sa komponeneta ActiveX
  {
    try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  else	//Mozilla alebo ine prehliadace - pouzije sa JavaScript objekt
  {
    try { xmlHttp = new XMLHttpRequest(); }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  if (!xmlHttp) alert("Error creating the XMLHttpRequest object.");
  else return xmlHttp;
}

function handleServerResponse() //spusti sa automaticky po prijati spravy zo serveru
{       
  if (xmlHttp.readyState == 4) 	//pokracuj ak je transakcia dokoncena
  {
    if (xmlHttp.status == 200) //200 - uspesne ukoncenie tranzakcie
    { 
    //alert(xmlHttp.responseText);
      var xmlResponse = xmlHttp.responseXML; //vrati textovu odpoved zo servera
      var selectParfemAjax = document.getElementById('cbIdZakaznikAjax');
                  
      xmlRoot = xmlResponse.documentElement;
      linkaArray = xmlRoot.getElementsByTagName('linka');
      nazovArray = xmlRoot.getElementsByTagName('nazov');
			for( a = selectParfemAjax.length - 1; a >= 0; a--){
				selectParfemAjax.options[a] = null;
			}

			for (var i = 0; i < linkaArray.length; i++) {
				selectParfemAjax.options[i] = new Option(nazovArray.item(i).firstChild.data, linkaArray.item(i).firstChild.data);
				
			}                                                                        
			if (linkaArray.length > 0) {
				selectParfemAjax.style.display = "block";
			} else {
				selectParfemAjax.style.display = "none";
			}
    } 
    else alert("There was a problem accessing the server: " + xmlHttp.statusText);
  }
} 
