var tick = 0;
var TTS = '';
var FREQ = 20;
var chatI = null;

function LoadChat(){
  tick++;
  if (tick < FREQ) return ;
  
  tick = 0;  
  // if Mozilla, IE7, Safari etc
  if (window.XMLHttpRequest) InfoBoxObject = new XMLHttpRequest()
  else if (window.ActiveXObject){ // if IE
      try {
        InfoBoxObject = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e){
        try{
          InfoBoxObject = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e){}
      }
  }
  else return false;
  
  InfoBoxObject.onreadystatechange = function(){                      
    if (InfoBoxObject.readyState > 0){      
      window.status = 'Read - '+InfoBoxObject.readyState;      
    }  
    
    if(InfoBoxObject.readyState == 4)
    {     
      window.status = 'Sync succeed';
      
      var doc = InfoBoxObject.responseXML;   // Assign the XML file to a var
      var element = doc.getElementsByTagName('root').item(0);   // Read the first element          
      if (!element) return;
      
      var object = element.firstChild;
      while (object){              
        switch(object.nodeName){
          case 'content': ReadContent(object, InfoBoxObject.responseText); break;
        }
        object = object.nextSibling;
      }                
    }
  };
  
  if (!InfoBoxObject) return ;
  window.status = 'Ajax active';
      
  InfoBoxObject.open("POST", 'chat.php'); 
  //InfoBoxObject.setRequestHeader("Content-Type", "text/xml");  
  InfoBoxObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  InfoBoxObject.send(TTS);  
  TTS = '';
}

function SendChatMessage(name, message){
  TTS += '&msg='+encodeURIComponent(message)+'&name='+encodeURIComponent(name);
  tick = FREQ - 1;
}


function StartChat(){
  tick = FREQ - 1;
  chatI = setInterval(LoadChat, 500);
} 

function StopChat(){
  clearInterval(chatI);
}

