 g_ceka_se_na_odpoved_serveru = false;
 g_httpRequest = null;

 function PosliPozadavekNaURL_Zpracovani(funkce) {
   if (g_httpRequest.readyState == 4) {
     g_ceka_se_na_odpoved_serveru = false;
     if (g_httpRequest.status == 200) {
       if (g_httpRequest.responseXML == null) {
         if (navigator.appVersion.indexOf('Konqueror') != -1) alert('Bohužel, Vámi používaný prohlížeč není podporován.');
         else { alert('Chyba XML!\n'+g_httpRequest.responseText); funkce(null); }
        }
       if (g_httpRequest.responseXML != null) {
         if (VratXMLPoleVPozadavku(g_httpRequest.responseXML,'debug') != '') alert('Debug info:\n'+g_httpRequest.responseText);
         funkce(g_httpRequest.responseXML.documentElement);
        }
      }
     else {
       if (g_httpRequest.status == 0) funkce(null);
       else alert('Chyba při komunikaci! Načtěte znova tuto stránku.\n'+g_httpRequest.status);
      }
    }
  }

 function PosliPozadavek(pozadavek,funkce) {
   PosliPozadavekNaURL('/api',pozadavek,funkce);
  }

 function PosliPozadavekNaURL(url,pozadavek,funkce) {
   if (g_ceka_se_na_odpoved_serveru) {    //pri vice pozadavcich pada Opera (ale sem tam pada i tak)
     setTimeout(function() { PosliPozadavekNaURL(url,pozadavek,funkce); },100);
     return;
    }

   g_ceka_se_na_odpoved_serveru = true;

   if (window.ActiveXObject) g_httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
   else g_httpRequest = new XMLHttpRequest();

   g_httpRequest.open('POST',url,true);
   g_httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
   g_httpRequest.onreadystatechange = function () { PosliPozadavekNaURL_Zpracovani(funkce); };
   g_httpRequest.send(pozadavek);
  }

 function VratXMLPoleVPozadavku(xml,nazev) {
   var elementy = xml.getElementsByTagName(nazev);
   if (elementy.length < 1) return '';
   elementy = elementy[0].childNodes;
   if (elementy.length < 1) return '';
   return elementy[0].nodeValue;
  }


 /********** funkce pro prihlaseni ke hre **************/

 g_vybrana_moznost = null;
 g_formular_odeslan = false;

 function ZobrazMoznostiPrihlaseni() {   //heslo zatim neni pouzito
   if (g_formular_odeslan) return;

   var form = document.getElementById('prihlaseniform');
   g_vybrana_moznost = null;
   for (var i = 0; i < 3; i++) {
     if (form.hrac[i].checked) { g_vybrana_moznost = i; break; }
    }

   document.getElementById('label_jmeno_hrace').style.display = (g_vybrana_moznost == null || !g_nutne_udaje[g_vybrana_moznost][0] ? 'none' : 'block');
   document.getElementById('label_heslo').style.display = (g_vybrana_moznost == null || !g_nutne_udaje[g_vybrana_moznost][1] ? 'none' : 'block');
   document.getElementById('label_odeslani').style.display = (g_vybrana_moznost == null ? 'none' : 'block');
   if (g_vybrana_moznost != null) {
     if (g_nutne_udaje[g_vybrana_moznost][0]) document.getElementById('label_jmeno_hrace').getElementsByTagName('input')[0].focus();
     else if (g_nutne_udaje[g_vybrana_moznost][1]) document.getElementById('label_heslo').getElementsByTagName('input')[0].focus();
    }
  }

 function OdesliFormular(form) {
   if (g_vybrana_moznost == null) return;
   var jmeno_hrace_objekt = document.getElementById('label_jmeno_hrace').getElementsByTagName('input')[0];
   if (g_nutne_udaje[g_vybrana_moznost][0] && jmeno_hrace_objekt.value == '') {
     alert('Prosím vyplňte jméno.');
     jmeno_hrace_objekt.focus();
     return;
    }
   g_formular_odeslan = true;
   var odeslanilabel = document.getElementById('label_odeslani');
   if (odeslanilabel.firstChild.getAttribute('type') != 'submit') return;   //tlacitko neexistuje, uz jednou bylo odeslano
   odeslanilabel.appendChild(document.createTextNode('Přihlašuji se…'));  //dam tam text
   odeslanilabel.removeChild(odeslanilabel.firstChild);                     //...misto tlacitka
   PrijetiPrihlaseni(null);
  }

 function PrijetiPrihlaseni() {
   var jmeno_hrace = document.getElementById('label_jmeno_hrace').getElementsByTagName('input')[0].value;
   var heslo = document.getElementById('label_heslo').getElementsByTagName('input')[0].value;
   setTimeout(function() { PosliPozadavek('pozadavek=prihlasse&id_hry='+g_id_hry+'&cislo_hrace='+g_vybrana_moznost+'&jmeno_hrace='+encodeURIComponent(jmeno_hrace)+'&heslo='+encodeURIComponent(heslo),PrijetiPrihlaseni2); },0);
   return;
  }

 function PrijetiPrihlaseni2(xml) {
   if (xml == null) alert('Něco se nepovedlo!');
   else {
     var id_hrace = VratXMLPoleVPozadavku(xml,'idHrace');
     if (id_hrace == '' || VratXMLPoleVPozadavku(xml,'chyba') != '') {
       alert('Chyba přihlášení! Obnovte tuto stránku a zkuste to znova.');
       return;
      }

     document.location = id_hrace+'/';
    }
  }


 /********** funkce pro vytvoreni nove hry **************/


 function ZalozNovouHru(rezim_hry) {
   if (g_formular_odeslan) return;
   var nazev = document.getElementById('nazev').value;
   var popis = document.getElementById('popis').value;
   var poradi_barev = document.getElementById('poradi_barev').value;
   var zacina_hrac = document.getElementById('zacina_hrac').value;
   var postaveni_kralovny = document.getElementById('postaveni_kralovny').value;
   var hlasit_garde = document.getElementById('hlasit_garde').value;
   var zobrazit_verejne = document.getElementById('zobrazit_verejne').checked;
   var odhlasovat_hrace_po = document.getElementById('odhlasovat_hrace_po').value;

   if (rezim_hry == 'normalni' && nazev.length == 0) {
     alert('Prosím zadejte jméno hry (např. podle lidí, místa, času, události…)');
     document.getElementById('nazev').focus();
     return;
    }

   var tlacitko = document.getElementById('zalozit'+rezim_hry+'tlacitko');
   var tlacitko_rodic = tlacitko.parentNode;
   tlacitko_rodic.removeChild(tlacitko);
   tlacitko_rodic.appendChild(document.createTextNode('Zakládám…'));

   g_formular_odeslan = true;
   g_rezim_hry = rezim_hry;

   var pozadavek = 'pozadavek=zalozhru&rezim_hry='+encodeURIComponent(rezim_hry)+'&nazev='+encodeURIComponent(nazev)+'&popis='+encodeURIComponent(popis)+'&poradi_barev='+encodeURIComponent(poradi_barev)+'&zacina_hrac='+encodeURIComponent(zacina_hrac)+'&postaveni_kralovny='+encodeURIComponent(postaveni_kralovny)+'&hlasit_garde='+(hlasit_garde == 'ano' ? 1 : 0)+'&zobrazit_verejne='+(zobrazit_verejne ? 1 : 0)+'&odhlasovat_hrace_po='+encodeURIComponent(odhlasovat_hrace_po);
   PosliPozadavek(pozadavek,ZalozNovouHru_Vysledek);
  }

 function ZalozNovouHru_Vysledek(xml) {
   if (xml == null) {
     alert('Bohužel, hru se momentálně nepodařilo založit. Zkuste to znovu.');
     return;
    }

   var chyba = VratXMLPoleVPozadavku(xml,'chyba');
   var id_hry = VratXMLPoleVPozadavku(xml,'idHry');
   if (chyba != '' || id_hry == '') {
     alert('Chyba zakládání! Obnovte tuto stránku a zkuste to znova.'+(chyba != null ? ' ('+chyba+')' : ''));
     return;
    }

   if (g_rezim_hry == 'normalni') alert('Gratulujeme, hra byla úspěšně založena. Adresa hry je\n\nhttp://hraj.trojsachy.cz/hra-'+id_hry+'/\n\nNyní na tuto adresu také budete přesměrován(a), kde se přihlásíte do hry za hráče zvolené barvy.\n\nTuto adresu pak pošlete přátelům, kde se stejně přihlásí i oni.');
   document.location = 'hra-'+id_hry+'/';
  }
