var xhr = vytvorXHR();  
var cache = new Array();  

function vytvorXHR()
{  
    var xhr;  
    try
{  
    xhr = new XMLHttpRequest();  
}
catch(e)
{//pro případ starší verze prohlížeče  
     var MSXmlVerze = new Array('MSXML2.XML.Http.6.0','MSXML2.XML.Http.5.0','MSXML2.XML.Http.4.0','MSXML2.XML.Http.3.0','MSXML2.XML.Http.2.0','Microsoft.XMLHttp','Microsoft.XML.Http');
     
     for(var i = 0; i < MSXmlVerze.length; i ++)
     {  
           try
           {  
                 xhr = new ActiveXObject(MSXmlVerze[i]);  
           }
           catch(e)
           {  
               //vzniklou chybu ignoruji a pokračuji nastavením další verze  
           }  
     }  
}  
if(!xhr)  
  alert("Došlo k chybě při vytváření objektu XMLHttpRequest!");  
else  
  return xhr;  
}  

function vytvorPozadavekModalWindow(width,height,name,target,par)
{  
    if(xhr){  
      cache.push("type=params&width="+width+"&height="+height+"&name="+name+"&target="+target+"&par="+par);  
      try
      {  
        //pokračovat budu jen v případě že cache není prázdná a objekt  
        //XHR nemá co na práci  
        if((xhr.readyState == 4 || xhr.readyState == 0)&& cache.length > 0){  
           //z cache načtu další hodnotu  
          var cacheVstup = cache.shift();  
          xhr.open("POST","/modalWindowHandler.php",true);  
          xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
          xhr.onreadystatechange = ajaxModalWindow;  
          xhr.send(cacheVstup);  
        }  
      }catch(e){  
        alert("Nelze se připojik k serveru:\n" + e.toString());  
      }  
    }
    else
    {  
      alert("Funkce \"precitSoubor()\": chybí objekt XMLHttpRequest");  
    }  
}

function vytvorPozadavek(type,text)
{  
    if(xhr){  
      cache.push("type="+type+"&text="+text);  
      try
      {  
        //pokračovat budu jen v případě že cache není prázdná a objekt  
        //XHR nemá co na práci  
        if((xhr.readyState == 4 || xhr.readyState == 0)&& cache.length > 0){  
           //z cache načtu další hodnotu  
          var cacheVstup = cache.shift();  
          xhr.open("POST","/errHandler.php",true);  
          xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
          xhr.onreadystatechange = validace;  
          xhr.send(cacheVstup);  
        }  
      }catch(e){  
        alert("Nelze se připojik k serveru:\n" + e.toString());  
      }  
    }
    else
    {  
      alert("Funkce \"precitSoubor()\": chybí objekt XMLHttpRequest");  
    }  
}


 function validace(){  
   if(xhr.readyState == 4){  
     if(xhr.status == 200){  
       var XMLRes = xhr.responseXML;  
         //zachycení chyb IE a Opery  
         if(!XMLRes || !XMLRes.documentElement){  
           throw("Chybná struktura XML:\n"+xhr.responseText);  
         }  
         //zachycení chyb ohnivé lišky :-)  
         var rootNodeName = XMLRes.documentElement.nodeName;  
         if(rootNodeName == "parsereerror"){  
           throw("Chybná struktura XML:\n"+xhr.responseText);  
         }  
         //čtu dokument, jelikož je vše ok :-)  
         xmlRoot = XMLRes.documentElement;  
         odpoved = xmlRoot.getElementsByTagName("odpoved")[0].firstChild.data;  
         alert(odpoved);  
           
//         setTimeout("vytvorPozadavek();", 500);  
     }else{  
       alert("Požadavek HTTP není v pořádku.")  
     }  
   }  
 }  
 
 function ajaxModalWindow()
 {
    var arrPar = new Array();     
    
    if(xhr.readyState == 4){  
     if(xhr.status == 200){  
       var XMLRes = xhr.responseXML;  
         //zachycení chyb IE a Opery  
         if(!XMLRes || !XMLRes.documentElement){  
           throw("Chybná struktura XML:\n"+xhr.responseText);  
         }  
         //zachycení chyb ohnivé lišky :-)  
         var rootNodeName = XMLRes.documentElement.nodeName;  
         if(rootNodeName == "parsereerror"){  
           throw("Chybná struktura XML:\n"+xhr.responseText);  
         }  
         //čtu dokument, jelikož je vše ok :-)  
         xmlRoot = XMLRes.documentElement;
           
         width = xmlRoot.getElementsByTagName("width")[0].firstChild.data;  
         height = xmlRoot.getElementsByTagName("height")[0].firstChild.data;  
         name = xmlRoot.getElementsByTagName("name")[0].firstChild.data;  
         target = xmlRoot.getElementsByTagName("target")[0].firstChild.data;  
         par = xmlRoot.getElementsByTagName("par")[0].firstChild.data;  
         
         arrPar = par.split(",");
         
         for (i=0;i<arrPar.length;i++)  
         {
             if (i==0)
             {
                target = target + "?";   
             }
             else
             {
                target = target + "&";   
             }
             target = target + arrPar[i];
         }
         
         top = (screen.height-height)/2;
         left = (screen.width-width)/2;
    
         window.showModalDialog(target,name,"dialogLeft:"+left+"px;dialogTop:"+top+"px;dialogWidth:"+width+"px;dialogHeight:"+height+"px");
           
//         setTimeout("vytvorPozadavek();", 500);  
     }else{  
       alert("Požadavek HTTP není v pořádku.")  
     }  
   }      
 }

