//***********************
//Primany function AJAX comunication
//***********************
 var updInterval = 4000;
 function newIntroduce(url, rodzaj, nazwafunkcji, podaj)
 {
   var xmlHttp = false;

   if (window.XMLHttpRequest) 
   {
     xmlHttp = new XMLHttpRequest();
     if (xmlHttp.overrideMimeType) {
       xmlHttp.overrideMimeType('text/xml');   }
   }
   else if (window.ActiveXObject) 
   {
      try {
          xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
      }     
   }

   xmlHttp.onreadystatechange = function ()
   {
     try 
     { 
       
       if (xmlHttp.readyState == 4) 
       {
         if (xmlHttp.status == 200) 
         {
           if (rodzaj == 'xml')
           {
             if (window.XMLHttpRequest) 
               var xmldoc = xmlHttp.responseXML;
             else if (window.ActiveXObject) 
             {
               var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
               xmldoc.loadXML(xmlHttp.responseText);
             }
           }
           else
             var xmldoc = xmlHttp.responseText;
           if (nazwafunkcji.length>0 && eval('window.'+nazwafunkcji))
             eval(nazwafunkcji+'('+((podaj=='')?'xmldoc':podaj)+');');
         }
         else if (xmlHttp.status == 12152)
         {
           setTimeout(function () { newIntroduce(url, rodzaj, nazwafunkcji, podaj); }, 0); 
           ;
         }
         else if (xmlHttp.status == 202)
         {
     
         }
         else 
         {
//           showInform(1, 'Aktualizacja nie powiodÅ?a siÄ?');
//           alert('WystÄ?piÅ? problem podczas Å?Ä?czenia. Dane nie zostaÅ?y zaktualizowane. PonÃ³w ostatniÄ? operacjÄ?.');
           setTimeout(function () { newIntroduce(url, rodzaj, nazwafunkcji, podaj); }, 0); 
         }
       }
     } 
     catch(e) 
     {
       newIntroduce(url, rodzaj, nazwafunkcji, podaj); 
     };
   }

   xmlHttp.open("GET", url, true);
   xmlHttp.send(null);
 }

//*************************
//Beafore start chat window 
//*************************
 var isHelpStart=true;
 var chat=null;
 var v_isHelp=false;
 var redirect_site='';
 var timer=null;
 var xCheck=true; 

 function getHelp()
 {
   setChPanelId(panel_id);
   newIntroduce('chat.php?action=chat&subact=call_help', 'xml', 'updateWait', 'xmldoc');
 }

 function isHelp()
 {
   newIntroduce('chat.php?action=chat&subact=is_help', 'xml', 'messWindow', 'xmldoc');
 }

 function req_help() 
 {
//   alert(parseInt(isHelpStart)+' '+parseInt(v_isHelp));
   if (isHelpStart==true && !(v_isHelp)&&xCheck)
   {
     isHelpStart=false;
     isHelp();
   }
   if (!(v_isHelp))
   {
     checkChPanelId(false);
     timer=setTimeout('req_help();',updInterval);  
   }
//   var shoutbox = document.getElementById('shoutbox');
 }

 function updateWait(xmldoc)
 {
   if (xmldoc.getElementsByTagName('data').length>0)
   {
     var status = parseInt(xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('status').item(0).firstChild.data);
     var op_count = parseInt(xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('op_count').item(0).firstChild.data);

     if (status==1)
     {
       if (op_count==0)
       {
         alert(langV.getLV('ch_no_operator'));
         finishChat();
       }
       else
       {
         setInfoData(langV.getLV('ch_waiting')+': '+op_count, 1);
         req_help();
       }
     }
   }
 }

 function setChLink(id, linkk, nazwa, pokaz)
 {
    document.getElementById(id).display=(pokaz?'':'none');
    document.getElementById(id).href=linkk;
    document.getElementById(id).innerHTML='<span class="font_red"><b>'+nazwa+'</b></span>';
 }

 function setInfoData(str, pokaz)
 {
    document.getElementById('infoData').style.display=(pokaz?'':'none');
    document.getElementById('infoData').innerHTML=(str);
 }

 function messWindow(xmldoc)
 {
   if (xmldoc.getElementsByTagName('data').length>0)
   {
     v_isHelp = parseInt(xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('isHelp').item(0).firstChild.data);
     var op_count = parseInt(xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('op_count').item(0).firstChild.data);

     if (v_isHelp==1)
     {
       setInfoData('',0);

       setChLink('chat_link', 'javascript: finishChat()', langV.getLV('ch_finish'), 1);
       document.getElementById('chatWindow').style.display='';
       
       req_new_msg();
     }
     else if (v_isHelp==-1)
     {
       reset();
     }
     else
     {
       setChLink('chat_link', '', '', 0);
       if (op_count==0)
       {
         alert(langV.getLV('ch_no_operator'));
         finishChat();
       }
       else
       {
         setInfoData(langV.getLV('ch_waiting')+': '+op_count, 1);
       }
     }
//     isHelpStart=true;
   }
   
 }

//**********************
//In chat window
//**********************
 var md5=0;
 var write_col='black';

 function getMess(idp)
 {
   newIntroduce('chat.php?action=chat&subact=get_mess&md5='+(md5), 'xml', 'rysujMess', 'xmldoc');
 }

 function send_msg()
 {
   var shout=document.getElementById('shout');
   if (shout.value.length>0)
   {
//     alert(encodeURIComponent(shout.value));
     var vallll='<font color="'+write_col+'">'+shout.value+'</color>';
     
     newIntroduce('chat.php?action=chat&subact=send&info='+encodeURI(vallll).replace('+', '%2b'), 'xml', 'rysujMess', 'xmldoc');
     shout.value='';
   }
 }

 function req_new_msg() 
 {
   if (v_isHelp && xCheck)
   {
     getMess();
     checkChPanelId(false);
     setTimeout('req_new_msg();',updInterval);  
     var shoutbox = document.getElementById('shoutbox');
   }
//   shoutbox.scrollTop = shoutbox.scrollHeight;
 }


 function rysujMess(xmldoc)
 {
   var shoutbox = document.getElementById('shoutbox');
   var dane='';
   var div=null;
   if (xmldoc.getElementsByTagName('data').length>0)
   {
     if (xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('close').item(0))
     {
       var close=xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('close').item(0).firstChild.data;
       if (parseInt(close)==1) 
       {
         finishChat();
       }
     }
     else
     {
       var md5_new=xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('md5').item(0).firstChild.data;
       if (md5_new!=md5)
       {
         var messages = xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('messages');
         shoutbox.innerHTML='';
         for (var i=0, m=messages.length; i < m; i++)
         {
           if (messages.item(i).getElementsByTagName('redirect').item(0))
           {
             if (i+1==m)
             {
               newIntroduce('chat.php?action=chat&subact=redirect', 'xml', 'redirect', 'xmldoc');
               redirect_site = messages.item(i).getElementsByTagName('redirect').item(0).firstChild.data;
             }
           }
           else
           {
             div = document.createElement('span');
             var str = messages.item(i).getElementsByTagName('czas').item(0).firstChild.data;
             str += ' <b>'+messages.item(i).getElementsByTagName('kto').item(0).firstChild.data+'</b>: ';
             str += ((messages.item(i).getElementsByTagName('info').item(0).firstChild)?messages.item(i).getElementsByTagName('info').item(0).firstChild.data:'')+'<br/>';
             div.innerHTML = str;
             shoutbox.appendChild(div);
           }
         }
         shoutbox.scrollTop = shoutbox.scrollHeight;
         md5=md5_new;
       }
     }
   }
 }

 function redirect(xmldoc)
 {
   if (xmldoc.getElementsByTagName('data').length>0)
   {
     var status = parseInt(xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('status').item(0).firstChild.data);

     if (status==1 && redirect_site.length>0)
     {
       document.location.href=redirect_site;
       redirect_site='';
     }
   }

 }

 function closeChat(xmldoc)
 {
   if (xmldoc.getElementsByTagName('data').length>0)
   {
     var status = parseInt(xmldoc.getElementsByTagName('data').item(0).getElementsByTagName('status').item(0).firstChild.data);

     if (status==1)
     {
       reset();
       setCookie('ch_panel_id','0');
     }
   }
 }

 function reset()
 {
   isHelpStart=true;
   v_isHelp=false;
   clearTimeout(timer);
   setInfoData('',0);
   document.getElementById('chatWindow').style.display='none';

   setChLink('chat_link', 'javascript: getHelp()', langV.getLV('ch_call_help'), 1);
 }

 function finishChat()
 {
   newIntroduce('chat.php?action=chat&subact=finish', 'xml', 'closeChat', 'xmldoc');
 }

 function ch_write_color(col)
 {
   document.getElementById('shout').style.color=col;
   document.getElementById('shout').focus();
   write_col=col;
 }
 
 function setChPanelId(pan_id)
 {
   ch_panel_id=pan_id;
   var date=new Date();

   setCookie('ch_panel_id', ch_panel_id, new Date(date.getTime()+30000));
 }

 function checkChPanelId(req_start)
 {
   ch_panel_id = getCookie('ch_panel_id');
   tmp_panel_id = getCookie('panel_id');
//   document.getElementById('xxxy').innerHTML+='<br/>'+ch_panel_id+' '+panel_id+ ' '+document.cookie+' - '+tmp_panel_id;

   if (tmp_panel_id!=null&&tmp_panel_id!=''&&ch_panel_id==null)
   {
     setCookie('panel_id','');
//     ch_panel_id=null;
   }

   if (ch_panel_id==null) 
     setChPanelId(panel_id);
   else if (ch_panel_id==0) 
   {
     reset();  return;
   }
   else if (tmp_panel_id==null||tmp_panel_id=='')
     setChPanelId(ch_panel_id);

   xCheck=(ch_panel_id==panel_id?true:false);
   isHelpStart=xCheck;

   if (!(isHelpStart)) 
   {
     setChLink('chat_link', 'javascript: takePanel()', langV.getLV('ch_take_panel'), 1);
     setInfoData('', 0);
     document.getElementById('chatWindow').style.display='none';
   }

//   document.getElementById('xxxy').innerHTML+=' - '+(isHelpStart==true?1:0);
   if (req_start||(v_isHelp==true&&xCheck==0)) 
   {
     v_isHelp=false;
     req_help();
   }
 }

 function takePanel()
 {
   setChPanelId(panel_id);
   checkChPanelId(false);
 }

