function criaXMLHTTP(){
	var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
	"MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
	"Microsoft.XMLHTTP"];
	var retorno = false;
	try{
		return new XMLHttpRequest();
	}catch(ee){
		try{
			for (var i=0; !retorno && i < arrSignatures.length; i++) {
				try {
					retorno = new ActiveXObject(arrSignatures[i]);
				} catch (oError) {
					retorno = false;
				}
			}
		}catch(e){
			return false;
		}
	}
	return retorno;
}

function get(obj) {
	// come from: http://www.captain.at/howto-ajax-form-post-get.php
  var getstr = "";
  for (i=0; i<obj.childNodes.length; i++) {
	 if (obj.childNodes[i].tagName == "INPUT") {
		if (obj.childNodes[i].type == "text") {
		   getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
		}
		if (obj.childNodes[i].type == "hidden") {
		   getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
		}
		if (obj.childNodes[i].type == "checkbox") {
		   if (obj.childNodes[i].checked) {
			  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
		   } else {
			  getstr += obj.childNodes[i].name + "=&";
		   }
		}
		if (obj.childNodes[i].type == "radio") {
		   if (obj.childNodes[i].checked) {
			  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
		   }
		}
	 }   
	 if (obj.childNodes[i].tagName == "SELECT") {
		var sel = obj.childNodes[i];
		getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
	 }
	 
  }
  return getstr;
}
/*
function createXMLHTTP(){
	var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
	for (var i=0; i < arrSignatures.length; i++) {
		try {
			var oRequest = new ActiveXObject(arrSignatures[i]);
			return oRequest;
		} catch (oError) {	}
	}
	throw new Error("MSXML is not installed on your system.");
}
*/
function get_ajax(url,id){
    xmlhttp = criaXMLHTTP();

    //Obtém o objeto HTML
	if(id!="0") objetoHTML=document.getElementById(id);
	// recebeu um objeto para envio
	if(id=="obj"){
		campo = arguments[2];
	}

	funcao = '';
	if(id!="0" && id!="obj" && arguments.length==3) {
		funcao = arguments[2];
	}

    //Abre a conexão
    xmlhttp.open("GET",url);
	xmlhttp.setRequestHeader("Cache-Control", "no-cache");
	xmlhttp.setRequestHeader("Pragma", "no-cache");

    // para solicitacoes utilizando o metodo post deve ser acrescentado 
    // oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    //Função para tratamento do retorno
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			conteudo = xmlhttp.responseText;
            //Mostra o HTML recebido
            if(id!="0") {
				if(id=="obj"){
					campo.value = conteudo;
				}else{
					objetoHTML.innerHTML = conteudo;
					if(funcao!='') eval(funcao);
				}
			}else{
				return conteudo;
			}
        }
    }

    //Executa
    xmlhttp.send(null);
}


function post_ajax(mydiv, str, formu) {
	request = criaXMLHTTP();
	if(request.readyState == 4 || request.readyState == 0) {
		var form_string = get(formu);
		request.open("POST", str, true);
		request.setRequestHeader("Cache-Control", "no-cache");
		request.setRequestHeader("Pragma", "no-cache");
		request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		request.onreadystatechange = function(){	if (request.readyState == 4) {
														var conteudo = request.responseText;
														mydiv.innerHTML = conteudo;
													}

												}
		request.send(form_string);
	}
}


// url_encode version 1.0  
function url_encode(str) {  
    var hex_chars = "0123456789ABCDEF";  
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/;  
    var n, strCode, hex1, hex2, strEncode = "";  

    for(n = 0; n < str.length; n++) {  
        if (noEncode.test(str.charAt(n))) {  
            strEncode += str.charAt(n);  
        } else {  
            strCode = str.charCodeAt(n);  
            hex1 = hex_chars.charAt(Math.floor(strCode / 16));  
            hex2 = hex_chars.charAt(strCode % 16);  
            strEncode += "%" + (hex1 + hex2);  
        }  
    }  
    return strEncode;  
}  

// url_decode version 1.0  
function url_decode(str) {  
    var n, strCode, strDecode = "";  

    for (n = 0; n < str.length; n++) {  
        if (str.charAt(n) == "%") {  
            strCode = str.charAt(n + 1) + str.charAt(n + 2);  
            strDecode += String.fromCharCode(parseInt(strCode, 16));  
            n += 2;  
        } else {  
            strDecode += str.charAt(n);  
        }  
    }  
    return strDecode;  
}  