/*AJAX*/
var xmlhttp
var id_element

function carregar(file,identificador,method,qs) {

id_element = identificador;

if (!method) { //verifica se o method foi alterado para POST
  metodo = 'GET'
} else {
  metodo = method;
  var querystring;
  var z = '';
  for (i=0;i<qs.length;i++) { //monta a querystring
    z = z+qs[i]+"="+document.getElementById(qs[i]).value+"&";
  }
  querystring = unescape(z.substring(0,z.length-1));
}

//cria sempre um link difererente com date e time para evitar armazenamento com cache, verifica se há parâmetros na url
if (file.match('=')) {var arquivo = file+"&rand="+new Date().getTime();} else {var arquivo = file+"?rand="+new Date().getTime();}

//exibe a mensagem carregando
var loading=document.getElementById(identificador)
    loading.innerHTML='<div class="carregando">Carregando...</div>'

// code for Mozilla, etc.
if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest()
  xmlhttp.onreadystatechange=xmlhttpChange
  xmlhttp.open(metodo,arquivo,true)
  
  if (!querystring) {
    xmlhttp.send(null)
  } else {
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    xmlhttp.send(querystring)
  }
  
  }
// code for IE
else if (window.ActiveXObject) {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp) {
    xmlhttp.onreadystatechange=xmlhttpChange
    xmlhttp.open(metodo,arquivo,true)

    if (!querystring) {
      xmlhttp.send(null)
    } else {
      xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
      xmlhttp.send(querystring)
    }

    }
  }
}

function xmlhttpChange() {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4) {
  // if "OK"
  if (xmlhttp.status==200) {
  var resultado;
  resultado = xmlhttp.responseText;
  resultado = resultado.replace(/\+/g,' '); //substitui o + por um espaço
  if (metodo == 'POST') {resultado = unescape(resultado);} //desfaz a função urlencode (p/ POST)
  document.getElementById(id_element).innerHTML = resultado;
  }
  else {
    alert("Problem retrieving data:" + xmlhttp.statusText)
  }
  }
}

