/**********************************************************************
* 2007-09
* 
* Sang
*
***********************************************************************/

var req; 
var fila = new Array();
var atual;
var animate = new Array();
var idFocus;
function processNext() {
   //pegar na fila
   if (fila.length > 0) {
      if (atual == null || atual == '') {
         var aux = fila.pop();
         atual = aux[1];
         //encontrado
         loadXMLDoc(aux[0], aux[1], aux[2], aux[3]);
      }
   }
}

function loadXMLDoc(url, d, a, c) {
   req = null;
   div = d;
   anime = a;
   concat = c;
   if (window.XMLHttpRequest) {
      // Procura por um objeto nativo (Mozilla/Safari)
      req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
      // Procura por uma versao ActiveX (IE)
      req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   if (req) {
      req.onreadystatechange = processReqChange;
      req.open("POST", url, true);
      req.setRequestHeader('Content-Type', "application/x-www-form-urlencoded; charset=Windows-1252");
      req.send(null);
   } else {
      alert("Seu browser não sporta AJAX.");
   }
   
}

function processReqChange() {
   // apenas quando o estado for "completado"
   try {
      if (req.readyState == 4) {
         // apenas se o servidor retornar "OK"
         switch(anime) {
            case 'slide':
               //slide
               ElementSlideUpDown(div, 1, 'slow');
               ElementClear(div);
            break;
            case 'fade':
               //fade
               ElementFadeInOut(div, 1, 'slow');
               ElementClear(div);
            break;
            default:
               //default
               ElementShowHiddenSimple(div, '1');
               ElementClear(div);
            break;
         }
         if (req.status == 200) {
            if (div != null) {
               // retornado nela, como texto HTML
               if (concat != 1) document.getElementById(div).innerHTML = req.responseText;
               else document.getElementById(div).innerHTML += req.responseText+"<br>\r\n";
            } else {
               return req.responseText;
            }
            //receber focu
            if (idFocus) ElementFocus(idFocus);
         } else {
            if (div != null) {
               if (concat != 1) document.getElementById(div).innerHTML = "Houve um problema ao obter os dados<br>"+document.getElementById(div).innerHTML;
               else document.getElementById(div).innerHTML += "Houve um problema ao obter os dados<br>"+document.getElementById(div).innerHTML+"<br>\r\n";
            } else {
               return "Houve um problema ao obter os dados";
            }
         }
         //remover lista de atual
         atual = '';
         //processar proximo da fila
         processNext();
      }
   } catch (e) {
      alert('Houve um problema ao obter os dados');
   }
   
}

//chamada normal
function refreshPage(url, d, animate, focusIdField, concatText) {
   idFocus = focusIdField;
   if (concatText == null) concatText = 0;
   //exibir idLoading
   if (document.getElementById(d) != null) {
      var content = new Array(url, d, animate, concatText);
      //adiciona na fila
      fila.push(content);
      if (animate!=0) document.getElementById(d).innerHTML = "<div style='text-align:left;'><div id=idLoadingImg>Carregando...</div></div>"+document.getElementById(d).innerHTML;
      //inicia loadXMLDoc
      processNext();
   }
}

//chamada com looping
function refreshPageLooping(url, d, animate, time, concatText) {
   //exibir idLoading
   if (concatText == null) concatText = 0;
   if (document.getElementById(d) != null) {
      var content = new Array(url, d, animate, concatText);
      //adiciona na fila
      fila.push(content);
      //adiciona na fila
      processNext();
   }
   if (time != null && time >= 500) setTimeout("refreshPageLooping('"+url+"','"+d+"','"+animate+"', '"+time+"', '"+concatText+"')", time);
   
}