// JavaScript Document

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {	
	// zde bude reference na objek XMLHttpRequest
	var xmlHttp;
	// toto by melo fungovat ve vsech prohlizecich mimo IE6 a starsi
	try 
	{
		// pokusi se vyvorit objekt XMLHttpRequest
		xmlHttp = new XMLHttpRequest();		
	}
	catch (e) 
	{
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');
		//vyzkousej vsechna prog id, dokud nektere nebude funkcni
		for(var i=0; i < XmlHttpVersions.lenght && !xmlHttp; i++) {
			try {
				//pokusi se vytvorit objekt XmlHttpRequest
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e) {}
		}
	}
	// vrati vytvoreny objekt nebo zobrazi chybovou zpravu
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else 
		return xmlHttp;
}

function process1(id_firma) 
{
	//pokracuj jen neni-li xmlHttp prazdne
	if(xmlHttp) 
	{
		try 
		{
			//pokus se pripojit k serveru
			//vytvori retezec parametru
			var params = "id_firma=" + id_firma;
			//inicijuje asynchronni pozadavek HTTP
			xmlHttp.open("GET", "../functions/ajax-stat-firmy.php?" + params, true);
			
			xmlHttp.onreadystatechange = handleRequestStateChange1;
			
			xmlHttp.send(null);
			
		}
		// v pripade selhani zobraz chybu
		catch(e) 
		{
			alert("Can`t connect to server: \n" + e.toString());
		}
	}
}

function handleRequestStateChange1() 
{
	//je li readyState 4 muzeme precist odpoved ze serveru
	if(xmlHttp.readyState == 4) 
	{		
		//pokracuj pouze, je-li stav HTTP "OK"
		if(xmlHttp.status == 200) 
		{			
			try 
			{
				//zpracuj odpoved ze serveru
				//handleServerResponse1();
			}
			catch(e) 
			{
				//zobraz chybovou zpravu
				alert ("Error reading the response: " + e.toString());
			}
		}
		else 
		{
			// zobraz zpravu o stavu
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse1() 
{
	//precte zpravu ze serveru
	var xmlResponse = xmlHttp.responseXML;
	
	//ziska element dokumentu XML
	xmlRoot = xmlResponse.documentElement;
	
	// ziska pole se jmeny knih a jejich  ISBN
	titulekArray = xmlRoot.getElementsByTagName("titulek");
	idArray = xmlRoot.getElementsByTagName("id");
	//autorArray = xmlRoot.getElementsByTagName("autor");
	
	//generuje vystup HTML
	var html="<select name=\"id_kategorie\" id=\"id_kategorie\" class=\"select1\">";
	html += "<option value=\"0\">nekategorizováno</option>";
	
	var _id_kategorie = document.getElementById("_id_kategorie").value;
	// prochazi polem a tvori strukturu HTML
	for(var i=0; i < idArray.length; i++) 
	{
	  if(_id_kategorie == idArray.item(i).firstChild.data) {
		  selected=" selected=\"selected\"";
	  }
	  else {
		  selected="";
	  }
	  html += "<option value='" + idArray.item(i).firstChild.data + "'" + selected + ">" + titulekArray.item(i).firstChild.data + "</option>";		
	}
	html += "</select>";
	html += "<input name=\"_id_kategorie\" id=\"_id_kategorie\" type=\"hidden\" value=\"0\" />";
	//ziska odkaz na element <div> na strance		
	myDiv = document.getElementById("AjaxIdKategorie");
	
	//pridej obsah do elementu <div>
	myDiv.innerHTML = html;	
}