function ajax()
{
	var ajax;
	try 
	{
		ajax = new XMLHttpRequest();
	} 
	catch(ee) 
	{
		try 
		{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E) 
			{
				ajax = false;
			}
		}
	}
	return ajax;
}

function ajaxSubtipos(tipo,destino)
{

	var Ajax = ajax();

	Ajax.open('GET', 'tipoSubtipo.php?tipo='+tipo, true);
	Ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	Ajax.onreadystatechange = function() {

		var campo = get(destino);
		campo.options.length = 1;
		var opcao = campo.options[0];

		if (Ajax.readyState == 1) {
			opcao.text = "Carregando ...";
		}

		if (Ajax.readyState == 4) {
			if (Ajax.status == 200) {
				var xml = Ajax.responseXML;
				var subtipos = xml.getElementsByTagName('subtipo');

				if (subtipos.length > 0) {
					opcao.text = " - Selecione - ";
					opcao.value = "";
					for ( var i = 0; i < subtipos.length; i++) {
						var nod = subtipos[i];
						var nome = nod.getElementsByTagName("nome")[0].firstChild.nodeValue;
						var codigo = nod.getElementsByTagName("codigo")[0].firstChild.nodeValue;

						var novo = document.createElement("option");
						novo.value = codigo;
						novo.text = nome;

						campo.options.add(novo);
					}
				} 
				else {
					opcao.text = "Nenhum subtipo encontrado";
					opcao.value= ""
				}
			}
		}
	}

	Ajax.send(null);

}

function estadoCidades(uf, destino)
{

	var Ajax = ajax();

	Ajax.open('GET', 'ajax.php?tipo=estadoCidades&uf='+uf, true);
	Ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	Ajax.onreadystatechange = function() {

		var campo = get(destino);
		campo.options.length = 1;
		var opcao = campo.options[0];

		if (Ajax.readyState == 1) {
			opcao.text = "Carregando ...";
		}

		if (Ajax.readyState == 4) {
			if (Ajax.status == 200) {
				var xml = Ajax.responseXML;
				var cidades = xml.getElementsByTagName('cidade');

				if (cidades.length > 0) {
					opcao.text = " - Selecione - ";
					opcao.value = "";
					for ( var i = 0; i < cidades.length; i++) {
						var nod = cidades[i];
						var nome = nod.getElementsByTagName("nome")[0].firstChild.nodeValue;

						var novo = document.createElement("option");
						novo.value = nome;
						novo.text = nome;

						campo.options.add(novo);
					}
				} 
				else {
					opcao.text = "Nenhuma cidade encontrada";
					opcao.value= ""
				}
			}
		}
	}

	Ajax.send(null);

}

function cidadeBairros(uf, cidade, destino)
{
	if (!uf)
	{
		var x = cidade.split(" - ");
		cidade = x[0];
		uf = x[1];
	}
	
	var Ajax = ajax();

	Ajax.open('GET', 'ajax.php?tipo=cidadeBairros&uf='+uf+'&cidade='+cidade, true);
	Ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	Ajax.onreadystatechange = function() {

		var campo = get(destino);
		campo.options.length = 1;
		var opcao = campo.options[0];

		if (Ajax.readyState == 1) {
			opcao.text = "Carregando ...";
		}

		if (Ajax.readyState == 4) {
			if (Ajax.status == 200) {
				var xml = Ajax.responseXML;
				var bairros = xml.getElementsByTagName('bairro');

				if (bairros.length > 0) {
					opcao.text = " - Selecione - ";
					opcao.value = "";
					for ( var i = 0; i < bairros.length; i++) {
						var nod = bairros[i];
						var nome = nod.getElementsByTagName("nome")[0].firstChild.nodeValue;

						var novo = document.createElement("option");
						novo.value = nome;
						novo.text = nome;

						campo.options.add(novo);
					}
				} 
				else {
					opcao.text = "Nenhum bairro encontrado";
					opcao.value= ""
				}
			}
		}
	}

	Ajax.send(null);

}
