function AJAX() {
	this.Updater=carregarDados;
	this.Texto = buscaTexto;
	
	var xmlhttp = getXmlHttp();
	
	// função para pegar o objeto xml
  function getXmlHttp() {
		var xmlhttp;
		try	{
			xmlhttp = new XMLHttpRequest();
		} catch(ee) {
			try	{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(E) {
					xmlhttp = false;
				}
			}
		}
		return xmlhttp;
	}

	function buscaTexto(url, metodo, id, mensagem) {
		xmlhttp.open(metodo.toUpperCase(), url,true);
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				id.innerHtml = mensagem;
				//Lê o texto
				var t=xmlhttp.responseText;
				//Desfaz o urlencode
				t=t.replace(/\+/g," ");
				t=unescape(t);
				id.innerHTML = t;
			}
	}
		xmlhttp.send(null);
	}

	function carregarDados(caminhoRetorno,idResposta,metodo,mensagem) {
		var conteudo = idResposta;
		conteudo.innerHTML= mensagem;
		var xmlhttp = getXmlHttp();

		//Abre a url
		xmlhttp.open(metodo.toUpperCase(), caminhoRetorno,true);
		//Executada quando o navegador obtiver o código
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4){
				//Lê o texto
				var texto=xmlhttp.responseText;
				//Desfaz o urlencode
				texto=texto.replace(/\+/g," ");
				texto=unescape(texto);
				//Exibe o texto no div conteúdo
				var conteudo = idResposta;
				conteudo.innerHTML=texto;
			}
		}
		xmlhttp.send(null);
	}
}

var HttpReq = null;
var dest_combo = null;

function ajaxComboBox(url, idBusca, comboBox){
	dest_combo = comboBox;
	var indice = document.getElementById(idBusca).selectedIndex;
	var valor = document.getElementById(idBusca).options[indice].getAttribute('value');
	url = url + '?cidade='+valor;
	if (document.getElementById) { //Verifica se o Browser suporta DHTML.
		if (window.XMLHttpRequest) {
			HttpReq = new XMLHttpRequest();
			HttpReq.onreadystatechange = XMLHttpRequestChange;
			HttpReq.open("GET", url, true);
			HttpReq.send(null);
		} else if (window.ActiveXObject) {
			HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			if (HttpReq) {
				HttpReq.onreadystatechange = XMLHttpRequestChange;
				HttpReq.open("GET", url, true);
				HttpReq.send(null);
			}
		}
	}
}

function XMLHttpRequestChange() {
	if (HttpReq.readyState == 4 && HttpReq.status == 200){
		var result = HttpReq.responseXML;
		var no = result.getElementsByTagName("nome");
		document.getElementById(dest_combo).innerHTML = "";
		for (var i = 0; i < no.length; i++) {
			new_opcao = create_opcao(no[i]);
			document.getElementById(dest_combo).appendChild(new_opcao);
		}
	}
}

function create_opcao(no) { 
	var new_opcao = document.createElement("option"); 
	var texto = document.createTextNode(no.childNodes[0].data); 
	new_opcao.setAttribute("value",no.getAttribute("id")); 
	new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
	return new_opcao; // Retorna a nova OPTION.
}

