/*

  Ajax

*/

var MaxAjaxIterations = 10;
var ParamsToSend = new Array();
var AjaxIterations = 0; 
var InfoBoxObject = null;

function Ajax(){
  if (++AjaxIterations < MaxAjaxIterations) return;
  AjaxIterations = 0;
  
  /*was = false;
  for(i in ParamsToSend){
    was = true;
    break;
  }
  
  if (!was) return;*/
   
  // if Mozilla, IE7, Safari etc
 // if (InfoBoxObject == null){
    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
      //window.alert(InfoBoxObject.responseText);
      var element = doc.getElementsByTagName('root').item(0);   // Read the first element               
      if (!element) return;
            
      var object = element.firstChild;      
      while (object){
        switch(object.nodeName){          
          case 'setids': set_ids(object); break;
          case 'refresh': window.location.reload(); break;
          case 'display': set_object_display(object); break;
          case 'setlist': set_list(object); break;
          case 'set_form_value': set_form_value(object); break;
        }
        object = object.nextSibling;
      }                
    }
  };
  
  if (!InfoBoxObject) return ;
  window.status = 'AJAX active';
      
  InfoBoxObject.open("POST", '/ajax.php' + ajax_url_ad);
  InfoBoxObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");     
  InfoBoxObject.send(array_to_str(ParamsToSend));  
  ParamsToSend = new Array();
}

// send command using Ajax
function command(command, value, ask, single){
  if (ParamsToSend[command] == undefined) ParamsToSend[command] = Array(value);
  else if (single) ParamsToSend[command] = Array(value);
  else ParamsToSend[command].push(value);
  
  if (ask == true) AjaxIterations = MaxAjaxIterations;
}

// convert array to url form
function array_to_str(array){  
  result = '';
  
  for(var item in array){
    for (var subitem in array[item]){
      result += '&' + encodeURIComponent(item + '[]') + ((array[item][subitem] != '') ? '=' + encodeURIComponent(array[item][subitem]) : '');
    }
  }
  
  return result;
}

// find attribute value in the node
function get_attribute(attrs, name, _default){  
  for(a=0; a<attrs.length; a++){
    if (attrs[a].nodeName == name) return attrs[a].nodeValue;
  }
  
  return _default ? _default : '';
}

/*function read_user_stats(Node){  
  if (obj = document.getElementById('men_online')) obj.innerHTML = get_attribute(Node.attributes, 'menonline', 0);
}*/

// find objects with IDs and set their values
function set_ids(Node){    
  for(a=0; a<Node.attributes.length; a++){
    if (obj = document.getElementById(Node.attributes[a].nodeName)) {
      obj.innerHTML = Node.attributes[a].nodeValue;
      obj.style.position = 'relative';
    }
  }
}

function set_object_display(Node){    
  for(a=0; a<Node.attributes.length; a++){
    if (obj = document.getElementById(Node.attributes[a].nodeName)) {
      obj.style.display = Node.attributes[a].nodeValue;
    }
  }
}

function set_list(Node){
  if (obj = document.getElementById(get_attribute(Node.attributes, 'id', ''))){
    var object = Node.firstChild;
    
    obj.innerHTML = '';
          
    while (object){
      var new_option = document.createElement('OPTION');
      new_option.value = get_attribute(object.attributes, 'value', '');
      new_option.text = new_option.value;
      obj.options.add(new_option);
      object = object.nextSibling;
    }
    
    obj.size = Math.min(6, obj.options.length);
  }
}

function set_form_value(Node){
  for(a=0; a<Node.attributes.length; a++){
    if (obj = document.getElementById(Node.attributes[a].nodeName)) {
      obj.value = Node.attributes[a].nodeValue;
    }
  }
}

function init(){
  setInterval(Ajax, 1000); // every 10sec  
}

window.onLoad = init();
