// przechowuje obiekt XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// przechowuje adres i parametry zdalnego serwera
var serverAddress = "/go/_search_engine/list.php";
// zmienne określające częstotliwość łączenia z serwerem
var updateInterval = 10; // ile sekund oczekiwać na pobranie nowej wiadomości
var errorRetryInterval = 10; // ile sekund czekać po wystąpieniu błędu serwera
// parametr true pozwala wyświetlić szczegóły błędu
var debugMode = true;

var section="mySecondElement";

// tworzy obiekt XMLHttpRequest
function createXmlHttpRequestObject() 
{
  // przechowa odwołanie do obiektu XMLHttpRequest
  var xmlHttp;
  // powinno działać dla wszystkich przeglądarek z wyjątkiem IE6 i starszych
  try
  {
    // próbuje stworzyć obiekt XMLHttpRequest
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // zakładając, że IE6 lub starsza
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
      "MSXML2.XMLHTTP.5.0",
      "MSXML2.XMLHTTP.4.0",
      "MSXML2.XMLHTTP.3.0",
      "MSXML2.XMLHTTP",
      "Microsoft.XMLHTTP");
    // sprawdza każdy prog id aż któryś zadziała
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // próbuje stworzyć obiekt XMLHttpRequest
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // zwraca stworzony obiekt albo wyświetla komunikat o błędzie
  if (!xmlHttp)
    alert("Błąd podczas tworzenia obiektu XMLHttpRequest.");
  else 
    return xmlHttp;
}

// funkcja wyświetla nową wiadomość na stronie
function display($message)
{
  // pobiera odwołanie do elementu <div> na stronie
  myDiv = document.getElementById(section);
  // wyświetla komunikat
  myDiv.innerHTML = $message + "<br/>";
}

// funkcja wyświetlająca komunikat o błędzie
function displayError($message)
{
  // wyświetla szczegóły techniczne błędu jeśli debugMode ma wartość true
  display("Błąd podczas otrzymywania wiadomości! Spróbuję za " +
    errorRetryInterval + " sekund." + (debugMode ? "<br/>" + $message : ""));
  // ponawia sekwencję
  setTimeout("process();", errorRetryInterval * 1000);
}

// asynchroniczne wywołanie serwera
function processFirst()
{
  // kontynuuje, jeśli xmlHttp nie jest pusty
  if (xmlHttp)
  {
    // próbuje połączyć się z serwerem
    try
    {
      oListboxFirst=document.getElementById("first");
      if(oListboxFirst) {	
	  	valueFirst=oListboxFirst.options[oListboxFirst.selectedIndex].getAttribute("value");
	  	valueFirst1="?action=getFirst&first="+valueFirst;
	  	section="mySecondElement";
	  	xmlHttp.open("GET", serverAddress+valueFirst1, true);
      	xmlHttp.onreadystatechange = handleGettingNews;
      	xmlHttp.send(null);
      }	else {
	  	valueFirst1='';
	  }
     }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

// asynchroniczne wywołanie serwera
function processSecond()
{
  // kontynuuje, jeśli xmlHttp nie jest pusty
  if (xmlHttp)
  {
    // próbuje połączyć się z serwerem
    try
    {
      oListboxFirst=document.getElementById("first");
      oListboxSecond=document.getElementById("second");
      if(oListboxSecond) {	     
	  	valueFirst=oListboxFirst.options[oListboxFirst.selectedIndex].getAttribute("value");
	  	valueFirst1="?action=getSecond&first="+valueFirst;
      	valueSecond=oListboxSecond.options[oListboxSecond.selectedIndex].getAttribute("value");
	  	valueSecond1="&second="+valueSecond;
	  	section="myThirdElement";
	  	xmlHttp.open("GET", serverAddress+valueFirst1+valueSecond1, true);
      	xmlHttp.onreadystatechange = handleGettingNews;
      	xmlHttp.send(null);
	  } else {
	  	valueSecond1='';
	  }
     }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

// asynchroniczne wywołanie serwera
function processThird()
{
  // kontynuuje, jeśli xmlHttp nie jest pusty
  if (xmlHttp)
  {
    // próbuje połączyć się z serwerem
    try
    {
      oListboxFirst=document.getElementById("first");
      oListboxSecond=document.getElementById("second");
      oListboxThird=document.getElementById("third");
      if(oListboxThird) {	     
	  	valueFirst=oListboxFirst.options[oListboxFirst.selectedIndex].getAttribute("value");
	  	valueFirst1="?action=getThird&first="+valueFirst;
      	valueSecond=oListboxSecond.options[oListboxSecond.selectedIndex].getAttribute("value");
	  	valueSecond1="&second="+valueSecond;
      	valueThird=oListboxThird.options[oListboxThird.selectedIndex].getAttribute("value");
	  	valueThird1="&third="+valueThird;
	  	section="myFourthElement";
	  	xmlHttp.open("GET", serverAddress+valueFirst1+valueSecond1+valueThird1, true);
      	xmlHttp.onreadystatechange = handleGettingNews;
      	xmlHttp.send(null);
	  } else {
	  	valueThird1='';
	  }
     }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

// asynchroniczne wywołanie serwera
function processFourth()
{
  // kontynuuje, jeśli xmlHttp nie jest pusty
  if (xmlHttp)
  {
    // próbuje połączyć się z serwerem
    try
    {
      oListboxFirst=document.getElementById("first");
      oListboxSecond=document.getElementById("second");
      oListboxThird=document.getElementById("third");
      oListboxFourth=document.getElementById("fourth");
      if(oListboxFourth) {	     
	  	valueFirst=oListboxFirst.options[oListboxFirst.selectedIndex].getAttribute("value");
	  	valueFirst1="?action=getFourth&first="+valueFirst;
      	valueSecond=oListboxSecond.options[oListboxSecond.selectedIndex].getAttribute("value");
	  	valueSecond1="&second="+valueSecond;
      	valueThird=oListboxThird.options[oListboxThird.selectedIndex].getAttribute("value");
	  	valueThird1="&third="+valueThird;
      	valueFourth=oListboxFourth.options[oListboxFourth.selectedIndex].getAttribute("value");
	  	valueFourth1="&fourth="+valueFourth;
	  	section="myFifthElement";
	  	xmlHttp.open("GET", serverAddress+valueFirst1+valueSecond1+valueThird1+valueFourth1, true);
      	xmlHttp.onreadystatechange = handleGettingNews;
      	xmlHttp.send(null);
	  } else {
	  	valueFourth1='';
	  }
     }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

// funkcja wywoływana przy zmianie statusu żądania HTTP
function handleGettingNews() 
{
  // kiedy readyState ma wartość 4, jesteśmy gotowi do odebrania odpowiedzi z Sewera
  if (xmlHttp.readyState == 4) 
  {
    // kontynuuje tylko, gdy status HTTP ma wartość "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // przetwarza wiadomość z serwera
        getNews();
      }
      catch(e)
      {
        // komunikat o błędzie
        displayError(e.toString());
      }
    } 
    else
    {
      // komunikat o błędzie
      displayError(xmlHttp.statusText);   
    }
  }
}

// obsługuje odpowiedź otrzymaną z serwera
function getNews()
{
  // pobiera odpowiedź serwera
  var response = xmlHttp.responseText;
  // błąd serwera?
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0
    || response.length == 0)
    throw(response.length == 0 ? "Błąd serwera." : response);
  // wyświetla wiadomość
  display(response);
  // ponawia sekwencję
  //setTimeout("process();", updateInterval * 1000);
}


