var req=null;
var consoleData=null;
var consoleMsg=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

function loadXMLDoc(url) {
   req = false;
   if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){}
		}
    }
	
	
	
    if (req) {
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    }

}

function processReqChange(){
  var ready=req.readyState;
  var data=null;
  if (ready==READY_STATE_COMPLETE){
    toConsole("",consoleMsg);
	toConsole(req.responseText,consoleData);
  }else{
    toConsole("Carregando...",consoleMsg);
  }
}


function toConsole(data,console){
  if (console!=null){
    console.innerHTML = data;
  }
}

function Request(url,outData,form,outMsg){
  if(!outMsg) outMsg = outData;
  if(!form) form = '';
  
  consoleData=document.getElementById(outData);
  consoleMsg=document.getElementById(outMsg);
  sParam = '';
  
  if (form != '') sParam = getRequestBody(form);
  if (sParam != '') {
     loadXMLDoc(url+'?'+sParam); }
  else {   
     loadXMLDoc(url);
  }
}

function validType(obj) {
   var r = false;
   if (obj.type == "checkbox" && obj.checked) { r = true; }
   if (obj.type == "text" || obj.type == "password" || obj.type == "hidden" || obj.type == "file" || obj.type == "select-one" || obj.type == "textarea") { r = true; }
   return r;
}

function getRequestBody(Form) {
   oForm = document.getElementById(Form);

   var aParams = new Array();
   for (var i=0 ; i < oForm.elements.length; i++) {
      if (validType(oForm.elements[i])) {
        var sParam = encodeURIComponent(oForm.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
        aParams.push(sParam);
      }
   }
   return aParams.join("&");

}