var req;
var id_html,OForm;

function Acha(tela,filtro,id_htm){
    id_html = null;
    id_html = id_htm;
    //alert(tela);
    loadXMLDoc(tela,filtro);
}
function loadXMLDoc(url,filtro){
    req = null;
    //document.getElementById("aguarde").style.display='block';
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processa;
        req.open("GET", url+'?'+filtro, true);
        req.send(null);
    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processa;
            req.open("GET", url+'?'+filtro, true);
            req.send();
        }
    }
}
function processa(){
  // apenas quando o estado for "completado"
  if (req.readyState == 4) {
      // apenas se o servidor retornar "OK"
      if (req.status == 200) {
          // retornado nela, como texto HTML
          document.getElementById(id_html).innerHTML = req.responseText;
      } else {
          alert("Você precisa logar no sistema\n");
      }
  }
}
//POR POST
function Acha1(tela,filtro,id_htm){
    id_html = null;
    id_html = id_htm;
    //alert(filtro);
    loadXMLDoc1(tela,filtro);
}
function loadXMLDoc1(url,filtro){
    req = null;
    //document.getElementById('carregando').style.display='block';
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processa;
        req.open("GET", url+'?'+filtro, true);
        req.send(null);
    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processa;
            req.open("GET", url+'?'+filtro, true);
            req.send();
        }
    }
}
function processa1(){
  // apenas quando o estado for "completado"
  if (req.readyState == 4) {
      // apenas se o servidor retornar "OK"
      if (req.status == 200) {
          // retornado nela, como texto HTML
          document.getElementById(id_html).innerHTML = req.responseText;
      } else {
          //alert("Houve um problema ao obter os dados:\n" + req.statusText);
          alert("Você precisa logar no sistema\n");
      }
  }
}
//FIM
function strin(str,codigo,valor) {
  if(str) {
      str = str + '&' + codigo + '=' + valor;
  } else {
      str = codigo + '=' + valor;
  }
  return str;
}
function funcRev(){
  this.pos = "";
  for (var i=this.length; i>-1; i--){
          this.pos += this.charAt(i);
  }
  return this.pos;
}
String.prototype.reversao = funcRev;
function acerta_campos(OForm,IdRetorno, TelaRetorno, Limpar) {
    var grupo = document.getElementById(OForm).getElementsByTagName('input');
    var stri = "";
    var codigo = "";
    var descricao = "";
    var i;
    for (i = 0; i < grupo.length; i++) {
        //if (grupo[i].getAttribute('type') != 'button') {
            codigo = "";
            codigo = grupo[i].name;
            if (grupo[i].getAttribute('type') == 'checkbox') {
              //alert(codigo+' - '+document.getElementById(codigo).checked);
              descricao = document.getElementById(codigo).checked;
              stri = strin(stri,codigo,descricao);
            }else{
              if (document.getElementById(codigo)){
                //alert(codigo+' - '+document.getElementById(codigo).value);
                descricao = document.getElementById(codigo).value;
                stri = strin(stri,codigo,descricao);
              }
            }
          //}
        //}
    }
    var grupo = document.getElementById(OForm).getElementsByTagName('select');
    for (i = 0; i < grupo.length; i++) {
        codigo    = grupo[i].name;
        descricao = document.getElementById(codigo).value;
        stri = strin(stri,codigo,descricao);
    }
    var grupo = document.getElementById(OForm).getElementsByTagName('textarea');
    for (i = 0; i < grupo.length; i++) {
        codigo    = grupo[i].name;
        descricao = document.getElementById(codigo).value;
        stri = strin(stri,codigo,descricao);
    }
    if (Limpar){document.getElementById(IdRetorno).innerHTML = "";}
    Acha(TelaRetorno , stri , IdRetorno)
}
