function EDAJAX() {
  if (!EDAJAX.objetosAtivos) {
    EDAJAX.objetosAtivos = new Array();
  }
  this.xmlHttp = this.getXMLHttp();
}

EDAJAX.cancelarRequisicoes = function() {
  if (!EDAJAX.objetosAtivos) return;
  for (var i=0; i<EDAJAX.objetosAtivos.length; i++) {
    EDAJAX.objetosAtivos[i].xmlHttp.abort();
  }
  EDAJAX.objetosAtivos = new Array();
  var displayCarregando = document.getElementById("displayCarregando");
  if (displayCarregando)
    displayCarregando.style.display = "none";
}

EDAJAX._contCarregando = 0;
EDAJAX.ultimaAcao = null;
EDAJAX.prototype.xmlHttp = null;

EDAJAX.isIE = function(event) {
  return (window.ActiveXObject!=null);
}

//Retorna o objeto XmlHttp
EDAJAX.prototype.getXMLHttp = function(event) {

  this.xmlHttp = null;

  // Mozilla, Safari, ...
  if (window.XMLHttpRequest)
    this.xmlHttp = new XMLHttpRequest();

  //Versões para Internet Explorer
  else {
    var versoes = [ "MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];

    for (var i=0; i<versoes.length; i++) {
      try {
        this.xmlHttp = new ActiveXObject(versoes[i]);
      } catch (ex) {}
    }
  }
  EDAJAX.objetosAtivos.push(this);
  return this.xmlHttp;
}

EDAJAX.carregando = function() {
  document.body.style.cursor = "wait";
  var displayCarregando = document.getElementById("displayCarregando");
  if (!displayCarregando) {
    displayCarregando = document.createElement("div");
    displayCarregando.id = "displayCarregando";
    displayCarregando.style.position = "absolute";
    displayCarregando.style.display = "none";
    displayCarregando.style.backgroundColor = "#FFFFC0";
    displayCarregando.style.padding = "4px";
    displayCarregando.style.borderWidth = "1px";
    displayCarregando.style.borderColor = "#000033";
    displayCarregando.style.borderStyle = "solid";
    displayCarregando.style.right = "5px";
//    displayCarregando.innerHTML = '<img src="imagens/load.gif" align="absmiddle"> Carregando... ';
    displayCarregando.innerHTML = '<script language="JavaScript">criarFlash("", "18", "18", "Loading", "v=1", "flash/load.swf", "#FFFFFF", "transparent");</script>';
    document.body.appendChild(displayCarregando);
  }
  displayCarregando.style.display = "";
  displayCarregando.style.top = (document.body.scrollTop+5)+"px";
  EDAJAX._contCarregando++;
}

EDAJAX.carregado = function() {
  document.body.style.cursor = "";
  EDAJAX._contCarregando--;
  if (EDAJAX._contCarregando==0) {
    var displayCarregando = document.getElementById("displayCarregando");
    if (displayCarregando)
      displayCarregando.style.display = "none";
  }
}

EDAJAX.prototype.getParametros = function(form) {
  //Recebe as variáveis do form
  var parametros = "";
  for (var i=0;i<form.length;i++)
    if (form.elements[i].type=="radio"||form.elements[i].type=="checkbox") {
      if (form.elements[i].checked)
        parametros += escape(form.elements[i].name) + "=" + escape(form.elements[i].value).replaceAll("+","%2B") + '&';
    }
    else
      parametros += escape(form.elements[i].name) + "=" + escape(form.elements[i].value).replaceAll("+","%2B") + '&';
  return parametros;
}

EDAJAX.prototype.abrirURL = function(metodo, url, parametros, funcao, tipoRetorno, fCarregando, fCarregado) {
  fCarregando = (fCarregando)?fCarregando:EDAJAX.carregando;
  fCarregado = (fCarregado)?fCarregado:EDAJAX.carregado;
  var xmlHttp = this.xmlHttp;
  if (parametros)
    url = (metodo.toUpperCase()=="GET")?((url.indexOf("?")>0)?url+"&"+parametros:url+"?"+parametros):url;
  xmlHttp.open(metodo.toUpperCase(), url, true);
  if (metodo.toUpperCase()=="POST") {
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
  }
  xmlHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  xmlHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
  xmlHttp.setRequestHeader("Pragma", "no-cache");
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState==4) {
      try {
        if (xmlHttp.status==200||xmlHttp.status==0) {
            if (typeof(funcao)=="function")
              if (tipoRetorno&&tipoRetorno.toUpperCase()=="XML")
                funcao(xmlHttp.responseXML);
              else
                funcao(xmlHttp.responseText);
        } else if (xmlHttp.status!=0) {
          setErro(xmlHttp.status, "URL: "+url+"\nSTATUS: "+xmlHttp.status);
        }
      } catch( e ) {
        if (e.name!="NS_ERROR_NOT_AVAILABLE") setErro(e.name,e.message);
      } finally {

        fCarregado();

        for (var i=0; i<EDAJAX.objetosAtivos.length; i++)
          if (EDAJAX.objetosAtivos[i].xmlHttp==xmlHttp)
            EDAJAX.objetosAtivos.splice(i,1);

      }
    }
  }
  fCarregando();
  xmlHttp.send(parametros);
}

EDAJAX.prototype.carregaURL = function(metodo, url, parametros, divID) {
  var callback = function(txt) {
    document.getElementById(divID).innerHTML = txt;
  }
  var aux = function() {callbackLocal();callback()}
  this.abrirURL(metodo, url, null, callback, "text");
}






















if (!EDAJAX.historico) {
  EDAJAX.historico = new Array();
  EDAJAX.iHistorico = 0;
}

//Retorna uma posição no vetor de histórico que deve ser gravado
EDAJAX.posHistorico = function() {
  if (EDAJAX.iHistorico==9) {
    EDAJAX.iHistorico = 8;
    for (var i=1;i<9;i++)
      EDAJAX.historico[i-1] = EDAJAX.historico[i];
  }
  return EDAJAX.iHistorico++;
}

//Grava variáveis de histórico com o conteúdo carregado
EDAJAX.gravaHistorico = function(acao) {
    if (!window.iHistoryAjax) return false;
    if (EDAJAX._contCarregando!=0) {
      setTimeout("EDAJAX.gravaHistorico('" + acao.replaceAll("'","\\'") + "');",400);
      return false;
    }
//    if (EDAJAX.ultimaAcao!=acao) {
      EDAJAX.historico[EDAJAX.posHistorico()] = document.getElementById("corpo").innerHTML;
      window.iHistoryAjax.location.href="includes/_historico.asp?iHistorico="+EDAJAX.iHistorico;
      EDAJAX.historico = EDAJAX.historico.slice(0,EDAJAX.iHistorico);
//    } else {
//      EDAJAX.historico[EDAJAX.iHistorico] = document.getElementById("corpo").innerHTML;
//    }
    EDAJAX.ultimaAcao = acao;
}

//Carrega uma posição do histórico
EDAJAX.carregaHistorico = function(i) {
  if (i) {
    if (i>(EDAJAX.iHistorico))
      EDAJAX.avancarAjax();
    else if (i<(EDAJAX.iHistorico))
      EDAJAX.voltarAjax();
  }
}

//Volta para uma página anterior
EDAJAX.voltarAjax = function() {
  if (EDAJAX.iHistorico>1) {
    document.getElementById("corpo").innerHTML = EDAJAX.historico[(EDAJAX.iHistorico-2)];
    EDAJAX.iHistorico--;
  }
  else
    alert("Página inicial!");

  return false;
}

//Avança uma página a frente
EDAJAX.avancarAjax = function() {
  if ((EDAJAX.iHistorico)==EDAJAX.historico.length) {
    alert("Não existe página adiante!");
  } else {
    document.getElementById("corpo").innerHTML = EDAJAX.historico[(EDAJAX.iHistorico)];
    EDAJAX.iHistorico++;
  }
  return false;
}

//Atualiza o conteúdo da página
EDAJAX.atualizarAjax = function() {
  if (EDAJAX.ultimaAcao!=null) {
    eval(EDAJAX.ultimaAcao);
  }
  return false;
}

function setErro(msg) {
  alert(msg);
}