var TextToSend = '';
var st = '1';

function CheckStatus(){
  // 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", 'sync.php'); 
  //InfoBoxObject.setRequestHeader("Content-Type", "text/xml");  
  InfoBoxObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  InfoBoxObject.send(TextToSend);  
  TextToSend = '';
}

function LoadPage(pageID, langID){  
  TextToSend = 'page='+pageID+'&amp;langID='+langID;  
  CheckStatus();
}

// find attribute value in the node
function GetAttribute(attrs, name, _default){  
  for(a=0; a<attrs.length; a++){
    if (attrs[a].nodeName == name) return attrs[a].nodeValue;
  }
  
  return _default ? _default : '';
}

// exchange content in object
function ReadContent(object, content){
  ChangeStyle(GetAttribute(object.attributes, 'style', ''));
  chat = GetAttribute(object.attributes, 'chat', '')
  if (chat == 'start') StartChat();
  else if (chat == 'stop') StopChat(); 

  var id = GetAttribute(object.attributes, 'id'); 
  var text = content.substring(content.indexOf('<!-- begin '+id+' -->'), content.length);
  text = text.substring(text.indexOf('-->')+3, text.length);
  text = text.substring(0, text.indexOf('<!-- end '+id+' -->'));
    
  if (obj = document.getElementById(id)) {
    obj.innerHTML = text;
    obj.className = GetAttribute(object.attributes, 'class', obj.className);  
  }
}

function ChangeStyle(css){
  if (css == '') return ;
  if (document.styleSheets.item(2).ownerNode) document.styleSheets.item(2).ownerNode.attributes[1].nodeValue = css;
  else document.styleSheets.item(2).href = css;
}
