function goAjax(url, metodo, modo, tagRetorno, parametros, exibeCarregando) {
	document.getElementById(tagRetorno).style.display = '';
	
	if (exibeCarregando){
		document.getElementById(tagRetorno).innerHTML = '<div align="center" class="carregando"><img src="imagens/carregando.gif"><br /><br />carregando...</div>';
	}
	
	function createXMLHTTP() {
		try {
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Msxml2.XMLHTTP");
				alert(ajax);
			} catch(ex) {
				try {
					ajax = new XMLHttpRequest();
				} catch(exc) {
					alert("Esse browser não tem recursos para uso do Ajax");
					ajax = null;
				}
			}
			return ajax;
		}
		
		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.");
	}
	
	var xmlhttp = createXMLHTTP();
	
	if (metodo == "GET") {
		xmlhttp.open("GET", url, modo);
	} else {
		xmlhttp.open("POST", url, modo);
		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) {
			retorno=xmlhttp.responseText;
			document.getElementById(tagRetorno).innerHTML=retorno;
			findScript(tagRetorno);
		}
	}
	
	if (metodo == "GET") {
		xmlhttp.send(null);
	} else {
		xmlhttp.send(parametros);
	}
}

function montaParametros(param, formulario) {
	parametros = param;
	for (var i=0;i<formulario.length;i++) {
		if (formulario.elements[i].type == "radio" || formulario.elements[i].type == "checkbox"){
			if (formulario.elements[i].checked){
				parametros += "&" + formulario.elements[i].name + "=" + encodeMyHtml(formulario.elements[i].id);
			}
		} else {
			parametros += "&" + formulario.elements[i].name + "=" + encodeMyHtml(formulario.elements[i].value);
		}
  }
	return parametros;
}

function findScript(tagRetorno){
	objRetorno = document.getElementById(tagRetorno);
	var scripts = objRetorno.getElementsByTagName('script');
	for (var i=0; i<scripts.length; i++){
		if (scripts[i].innerHTML != ""){
			eval(scripts[i].innerHTML);
		}
	}
}

function limpaTagRetorno(tagRetorno) {
	document.getElementById(tagRetorno).innerHTML = '';
	document.getElementById(tagRetorno).style.display = 'none';
}

function atualizaCampoAjax(valor, campoForm, tagRetorno){
	document.getElementsByName(campoForm)[0].value = valor;
	limpaTagRetorno(tagRetorno);
}

function setPropriedades(campoForm, tagRetorno) {
	document.getElementsByName(campoForm)[0].onblur = function() { limpaTagRetorno(tagRetorno); }
	document.getElementById(tagRetorno).onmouseover = function() { document.getElementsByName(campoForm)[0].onblur = function (){  }; }
	document.getElementById(tagRetorno).onmouseout = function() { document.getElementsByName(campoForm)[0].onblur = function(){ limpaTagRetorno(tagRetorno); }; }
}

function ativaAjaxTags(campoForm, tagRetorno, nomeTabela, campoTabela){
	document.getElementsByName(campoForm)[0].onkeyup = function() { goAjaxTags(campoForm, tagRetorno, nomeTabela, campoTabela); }
}

function goAjaxTags(campoForm, tagRetorno, nomeTabela, campoTabela){
	texto = document.getElementsByName(campoForm)[0].value;
	if (texto == ""){
		limpaTagRetorno(tagRetorno);
		return;
	}
	texto = encodeMyHtml(texto);
	parametros = 'texto='+texto+'&nomeTabela='+nomeTabela+'&campoTabela='+campoTabela+'&campoForm='+campoForm+'&tagRetorno='+tagRetorno;
	goAjax('ajaxTags.asp', 'POST', false, tagRetorno, parametros);
}

function encodeMyHtml(texto) {
	encodedHtml = escape(texto);
	encodedHtml = encodedHtml.replace(/\//g,"%2F");
	encodedHtml = encodedHtml.replace(/\?/g,"%3F");
	encodedHtml = encodedHtml.replace(/\+/g,"%2B");
	encodedHtml = encodedHtml.replace(/=/g,"%3D");
	encodedHtml = encodedHtml.replace(/&/g,"%26");
	encodedHtml = encodedHtml.replace(/@/g,"%40");
	return encodedHtml;
}
