  var ajax_queue = new Array();
  var ajax_max_queue = 20;
  var ajax_queue_reset_warning_treshold = 5;
  var ajax_queue_warned = false;
  var current_ajax = null;

  //httpr holds request creator and default settings
  var httpr = new SZN.HTTPRequest();
  httpr.setMethod('get');
  httpr.setFormat('xml');
  httpr.setMode('async');

  function ajax_working() {
    var img=SZN.gEl('ajax_state_image');
    if(img) {
        img.src='user/img/skin/work.gif';  // Dotaz uspesne proveden
    }
  }

  function ajax_done() {
    if (ajax_queue.length > 0) return;

    var img=SZN.gEl('ajax_state_image');
    if(img) {
        img.src='user/img/skin/done.gif';  // Dotaz uspesne proveden
    }
  }

  function ajaxstate(target){
    var stav = SZN.gEl(target);
    stav.innerHTML='<div class="ajaxstate">Probíhá dotaz</div>';
  }

  //vytvori zadost
  function vytvoritZadost(url) {
      if (ajax_queue.length >= ajax_max_queue) {
          if (!ajax_queue_warned) {
            alert("AJAX queue is full. Please wait until some request finishes.");
            ajax_queue_warned = true;
          }
          return false;
      }

      ajax_queue.push(url);
      ajaxProcessQueue();
      return false;
  }

  function ajaxProcessQueue() {
      if (ajax_queue.length < ajax_queue_reset_warning_treshold) ajax_queue_warned = false;

      if (ajax_queue.length == 0) return;

      setTimeout("ajaxProcessQueue()","200");

      if (current_ajax == null) {
          var url = ajax_queue[0];
          ajax_queue.shift();

          current_ajax = httpr.send(url, window, "processReqChange");

          if (!current_ajax) {
              alert("Error when sending AJAX request.\nPlease check your connection.");
              current_ajax = null;
              return;
          }

          ajax_working();
      }
  }

      // Original JavaScript code by Chirp Internet: www.chirp.com.au
      // vrati hodnotu vlastnosti
      function getNodeValue(parent, tagName) {
          if (!parent) return false;
          var tags = parent.getElementsByTagName(tagName);
          if (!tags || !tags.length) return false;
          var node = tags[0];
          return (node && node.firstChild) ? node.firstChild.nodeValue : false;
      }

      function processReqChange() {
          if (current_ajax.readyState == 4) {
              var ajax = current_ajax;
              current_ajax = null;

              if (ajax.status == 200) {
                  try {
                      var response = ajax.responseXML.documentElement;
                      var commands = response.getElementsByTagName('command');
                  } catch(err) {
                      window.alert("Server returned invalid response.");
                      ajax_done();
                      return;
                  }

                  processCommands(commands);
                  ajax_done();
              } else if(ajax.status != 0) {
                  alert("Temporary server error. Status code = " + ajax.status);
                  ajax_done();
              }
          }
      }

      function update_form(IFrame) {
              var doc;

              try {
                if( IFrame.contentDocument )
                      // For NS6
                      doc = IFrame.contentDocument;
                else if( IFrame.contentWindow )
                      // For IE5.5 and IE6
                      doc = IFrame.contentWindow.document.XMLDocument;
                else if( IFrame.document )
                      // For IE5
                      doc = IFrame.document;
                else //other browser
                      doc = IFrame.document;
              } catch(err) {
                window.alert("Server vrátil neplatnou odpověď.");
                ajax_done();
                return;
              }

              if (!doc) return;
              var commands = doc.getElementsByTagName('command');
              processCommands(commands);
              ajax_done();
      }


      function processCommands(commands) {

        for(var i=0; i < commands.length; i++) {
            var method = commands[i].getAttribute('method');

            try {
                switch(method) {

                    case 'alert':
                      var message = getNodeValue(commands[i], 'message');
                      window.alert(message);
                      break;

                    case 'setvalue':
                      var target = getNodeValue(commands[i], 'target');
                      var value = getNodeValue(commands[i], 'value');
                      if(target && value) { document.getElementById(target).value = value; }
                      break;

                    case 'setdefault':
                      var target = getNodeValue(commands[i], 'target');
                      if(target) { document.getElementById(target).value = document.getElementById(target).defaultValue; }
                      break;

                    case 'focus':
                      var target = getNodeValue(commands[i], 'target');
                      if(target) { document.getElementById(target).focus(); }
                      break;

                    case 'setcontent':
                      var target = getNodeValue(commands[i], 'target');
                      var content = getNodeValue(commands[i], 'content');
                      document.getElementById(target).innerHTML = content;
                      break;

                    case 'setstyle':
                      var target = getNodeValue(commands[i], 'target');
                      var property = getNodeValue(commands[i], 'property');
                      var value = getNodeValue(commands[i], 'value');
                      if(target && property && value) { document.getElementById(target).style[property] = value; }
                      break;

                    case 'setproperty':
                      var target = getNodeValue(commands[i], 'target');
                      var property = getNodeValue(commands[i], 'property');
                      var value = getNodeValue(commands[i], 'value');
                      if(value == "true") value = true;
                      if(value == "false") value = false;
                      if(target) { document.getElementById(target)[property] = value; }
                      break;

                    case 'addscript':
                      var content = getNodeValue(commands[i], 'content');
                      var headID = document.getElementsByTagName("head")[0];
                      var script = document.createElement('script');
                      script.type = 'text/javascript';
                      script.src = content;
                      headID.appendChild(script);
                      break;

                    case 'runscript':
                      var content = getNodeValue(commands[i], 'content');
                      eval(content);
                      continue;
                      break;

                    default:
                      window.console.log("Error: unrecognised method '" + method + "' in processReqChange()");
              }
           } catch(err) {
              //do nothing
           }
        }
      }

