var xmlHttp = createXmlHttpRequestObject();
var scriptAddress = 'model/validate.php';
var cache = new Array();
var showErrors = true;

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) {} // ignore potential error
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    displayError("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// function that displays an error message
function displayError($message)
{
  // ignore errors if showErrors is false
  if (showErrors)
  {
    // turn error displaying Off
    showErrors = false;
    // display error message
 
    alert("Nalezena chyba: \n" + $message);
    // retry validation after 10 seconds
    setTimeout("validate();", 10000);
  }
}

function validate(inputValue, fieldID, type)
{
  // pokracuj kdyz je xmlHttp prazdne
  if (xmlHttp)
  {
    // kdyz je nastaveno id
    if (fieldID)
    {
      // zakoduj hodnoty 
      inputValue = encodeURIComponent(inputValue);
      fieldID = encodeURIComponent(fieldID);
      type = encodeURIComponent(type);
      // dej inputy do fronty
      cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID + "&type="+type);
    }
    // zkus se pripojit k serveru
    try
    {
      // jen kdzy neni xmlHttp zaneprazdnen
      // a neni prazdna cache
      if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
         && cache.length > 0)
      {
        // dostan parametry z cache
        var cacheEntry = cache.shift();
        // pozadavak na server l validovani
        xmlHttp.open("POST", scriptAddress, true);
        xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChange;
        xmlHttp.send(cacheEntry);
      }
    }
    catch (e)
    {
      // display an error when failing to connect to the server
      displayError(e.toString());
    }
  }
}

function handleRequestStateChange()
{
  if(xmlHttp.readyState == 4)
  {
    
    if(xmlHttp.status == 200)
    {
      try
      {
        readResponse();
      }
      
      catch (e)
      {
        displayError(e.toString());
      }
    }
    
  }
}

function readResponse()
{
  var response = xmlHttp.responseText;
  
  // nejaka chyba
  if(response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
    throw(response.length == 0 ? "Chyba na serveru." : response); 
    
  responseXml = xmlHttp.responseXML;
  xmlDoc = responseXml.documentElement;
  result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
  fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
  type = xmlDoc.getElementsByTagName("type")[0].firstChild.data;
  
  message = document.getElementById(fieldID + "Failed");
  message.className = (result == "0") ? "error" : "hidden";
  setTimeout("validate();", 500); 
}

function setFocus(startElement)
{
  document.getElementById(startElement).focus();
}

