var xmlHttp = createXmlHttpRequestObject();

// vytvari instanci xmlhttprequest
function createXmlHttpRequestObject() {
  var xmlHttp;
  
  try {
    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");
    for (var i=0; i < XmlHttpVersions.length && !xmlHttp; i++) {
      try {
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {
        xmlHttp = false;
      }
    }
  }

  if (!xmlHttp) alert("Chyba pi vytvareni pozadavku na objekt");
  else return xmlHttp;
}

// odeslani pozadavku
function process() {
  if (xmlHttp) {
    try {
      xmlHttp.open("GET", "serverscript... vraci odpoved", true); //pokud jde o textovy retezec
      //xmlHttp.open("GET", "neco.xml", true); //pokud jde o XML
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);
    }
    catch (e) {
      alert("Nemohu se pripojit k serveru:\n" + e.toString());
    }
  } else {
    setTimeout('process()', 500);
  }
}

// zpracovani vysledku
function handleRequestStateChange() {
  output = document.getElementById("mujDiv");
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      try {
        var response = xmlHttp.responseText; //pokud jde o textovy retezec
        output.innerHTML = response; //textem naplnime mujDiv
        
        //var xmlResponse = xmlHttp.responseXML; //pokud jde o XML
		//handleServerResponse(); //funkce pro zpracovani XML
      }
      catch (e) {
        alert("chyba pri nacitani odpovedi: " + e.toString());
      }
    }
    else {
      alert("chyba pri vraceni odpovedi: " + xmlHttp.statusText);
    }
  }
}

// priklad funkce zpracovavajici XML
function handleServerResponse() {
	var xmlResponse = xmlHttp.responseXML;
	
	//pro IE a Operu
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Nevalidni struktura XML: \n" + xmlHttp.responseText);
	
	//pro Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror")
		throw("Nevalidni struktura XML: \n" + xmlHttp.responseText);
	
	xmlRoot = xmlResponse.documentElement;
	textArray = xmlRoot.getElementsByTagName("text");
	var html = "";

	for (var i=0; i<textArray.length; i++) {
		html += textArray[i].firstChild.data + "<br />";
	}

	mujDiv = document.getElementById("mujDiv");
	mujDiv.innerHTML = html;
}

