var xmlHttp = createXmlHttpRequestObject();

var serverAddress = "functions/validate.php"; //link do skryptu php, ktory odpali odpowiednia klase uzyty poznej w linijce 73

var showErrors = true;

var cache = new Array();

function createXmlHttpRequestObject()  // tworznie obiektu do odpowiedzi zaleznie czy Ie czy inna przegladarka
{
  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]);  // sprawdza wersje
      } 
      catch (e) {} 
    }
  }
 
  if (!xmlHttp)
    displayError("Błąd podczas tworzenia obiektu XMLHttpRequest.");
  else 
    return xmlHttp;
}

function displayError($message)
{
  
  if (showErrors)
  {
  
    showErrors = false;
   
    //alert("Wystąpił błąd: \n" + $message);
   
    setTimeout("validate();", 10000);  // co jakis czas wywoluje funkcje validate
  }
}
function validate(lang, inputValue, fieldID, inputValue2)  // wartosc i id pola dla jakiego zostala wywolana ta funkcja
{
   if (xmlHttp)
  {
    if (fieldID)  
    {
       inputValue = encodeURIComponent(inputValue);
       inputValue2 = encodeURIComponent(inputValue2);
      fieldID = encodeURIComponent(fieldID);
	  lang = encodeURIComponent(lang);
       cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID +"&inputValue2=" + inputValue2+'&lang='+lang);  //wrzuca do tablicy wartosc i klucz   
      
    }
      try
    {
         if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0)  // jezeli serwer jest wolny (4)
      {
      
        var cacheEntry = cache.shift();
         xmlHttp.open("POST", serverAddress, true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChange;
        xmlHttp.send(cacheEntry);
      }
    }
    catch (e)
    {
       displayError(e.toString());
    }
  }
}
function handleRequestStateChange() 
{
  if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {
      try
      {
        readResponse();  //czyta odpowiedz z pliku php
      }
      catch(e)
 
      {
        displayError(e.toString());
      }
    }
    else
    {
       displayError(xmlHttp.statusText);
    }
  }
}
function readResponse()  //funkcja do czytania odpowiedzi z pliku XML
{
  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0
    || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  responseXml = xmlHttp.responseXML;
   xmlDoc = responseXml.documentElement;
  result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;  // sprawdza wezly  xpliku XML - odpowiednio result - czyli wartosci pola
  fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data; // i fieldid - id pola

  message = document.getElementById(fieldID + "Failed"); // uchwyt do konkretnego obiektu w pliku po id
  message.className = (result == 'ok') ? "hidden" : "error"; // przyposanie odpowiedniej klasy w zaleznosci od wyniku
  obrazek = document.getElementById(fieldID + "Foto");
  message.innerHTML=result;
  obrazek.src=(result == 'ok') ? "Layout/dobrze.gif" : "Layout/zle.gif";
  obrazek.alt=(result == 'ok') ? "dobrze" : "zle";
  obrazek.className ="";
 
   setTimeout("validate();", 500); // kolejne wywolanie funkcji
}
function setFocus()    
{
  document.getElementById("nazwa").focus();
}

