function procPaises(modalidade){
	var HttpReq = createXMLHTTP();
	if (modalidade == "nada"){
		document.getElementById('mostraPaises').style.display = "none";
		procEstados("nada");
		procCidades("nada");
		procBairros("nada");
	} else {
	HttpReq.open("GET", "http://www.sistemasparaimobiliarias.com.br/buscador/busca_paises.php?cliente=" + cliente + "&modalidade=" + modalidade + "&idioma=" + idioma, true);
	HttpReq.onreadystatechange=function(){
		if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					if (subcategorias.length == 2){
						document.getElementById('paises').innerHTML = "";
						document.getElementById('mostraPaises').style.display = "none";
						procEstados(subcategorias[1].attributes[0].value,idioma,cliente);
					} else {
						document.getElementById('paises').innerHTML = "";
						document.getElementById('mostraPaises').style.display = "block";
        				for (var i = 0; i < subcategorias.length; i++) {
            				new_opcao = create_opcao(subcategorias[i]);
            				document.getElementById('paises').appendChild(new_opcao);
        				}
						procEstados("nada",idioma,cliente);
					}
					procCidades("nada",idioma,cliente);
					procBairros("nada",idioma,cliente);
    	}
	}
	HttpReq.send(null);
	}
}
function procEstados(modalidade){
	var idioma = document.getElementById('idioma').value;
	var cliente = document.getElementById('cliente').value;
	procCidades("nada");
	procBairros("nada");
	procTipos();
	var HttpReq = createXMLHTTP();
	HttpReq.open("GET","http://www.sistemasparaimobiliarias.com.br/buscador/busca_estados-2010.php?modalidade=" + modalidade + "&cliente=" + cliente + "&idioma=" + idioma, true);
	HttpReq.onreadystatechange=function(){
		if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
        			document.getElementById('estado').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('estado').appendChild(new_opcao);
        			}

    	}

	}

	HttpReq.send(null);

}

function procCidades(estado){

	var idioma = document.getElementById('idioma').value;

	var cliente = document.getElementById('cliente').value;

	var modalidade = document.getElementById('modalidade').value;

	procBairros("nada");

	procTipos();

	var HttpReq = createXMLHTTP();

	HttpReq.open("GET", "http://www.sistemasparaimobiliarias.com.br/buscador/busca_cidades.php?estado=" + estado + "&cliente=" + cliente + "&modalidade=" + modalidade + "&idioma=" + idioma, true);

	HttpReq.onreadystatechange=function(){

		if (HttpReq.readyState == 4 && HttpReq.status == 200){

        			var result = HttpReq.responseXML;

        			var subcategorias = result.getElementsByTagName("nome");

        			document.getElementById('cidade').innerHTML = "";

					

        			for (var i = 0; i < subcategorias.length; i++) {

            			new_opcao = create_opcao(subcategorias[i]);

            			document.getElementById('cidade').appendChild(new_opcao);

        			}

    	}

	}

	HttpReq.send(null);

}

function procBairros(cidade){

	var idioma = document.getElementById('idioma').value;

	var cliente = document.getElementById('cliente').value;

	var modalidade = document.getElementById('modalidade').value;

	procTipos();

	var HttpReq = createXMLHTTP();

	modalidade = document.getElementById('modalidade').value;

	HttpReq.open("GET", "http://www.sistemasparaimobiliarias.com.br/buscador/busca_bairros.php?destino=" + cidade + "&cliente=" + cliente + "&modalidade=" + modalidade + "&idioma=" + idioma, true);

	HttpReq.onreadystatechange=function(){

		if (HttpReq.readyState == 4 && HttpReq.status == 200){

        			var result = HttpReq.responseXML;

        			var subcategorias = result.getElementsByTagName("nome");

        			document.getElementById('bairro').innerHTML = "";

        			for (var i = 0; i < subcategorias.length; i++) {

            			new_opcao = create_opcao(subcategorias[i]);

            			document.getElementById('bairro').appendChild(new_opcao);

	       			}

    	}

	}

	HttpReq.send(null);

}

function procTipos(){

	var varsArray = Array('modalidade','estado','cidade','bairro','cliente');

	var url = "http://www.sistemasparaimobiliarias.com.br/buscador/busca_tipos_2010.php?idioma=" + document.getElementById('idioma').value;

	for(var ii=0;ii<=4;ii++){

		url = url + "&" + varsArray[ii] + "=" + document.getElementById(varsArray[ii]).value;

	}

	var HttpReq = createXMLHTTP();

	HttpReq.open("GET",url,true);

	HttpReq.onreadystatechange=function(){

		if (HttpReq.readyState == 4 && HttpReq.status == 200){

        			var result = HttpReq.responseXML;

        			var subcategorias = result.getElementsByTagName("nome");

        			document.getElementById('tipoImo').innerHTML = "";

        			for (var i = 0; i < subcategorias.length; i++) {

            			new_opcao = create_opcao(subcategorias[i]);

            			document.getElementById('tipoImo').appendChild(new_opcao);

        			}

    	}

	}

	HttpReq.send(null);

}


