var acao_pesquisa = "";
var id_pesquisa = "";

function valor_tipo(){
	
	if(document.getElementById('busca_tipo0') != undefined){
		if(document.getElementById('busca_tipo0').checked)
			return "";
		if(document.getElementById('busca_tipo1').checked)
			return "N";
		if(document.getElementById('busca_tipo2').checked)
			return "U";
	}else{
		return "";	
	}
	return "";
}		
var xmldoc;
try{
    xmldoc = new xmlDocument();
}
catch(eee){
    try{
        xmldoc = new ActiveXObject("Msxml2.DOMDocument");
    }catch(eeee){
        try{
            xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        }catch(EE){
            xmldoc = null
            
        }
    }
}
var	objRegiao = null; 


function Hoje(){
	
	var objData = new Date();
	var dia = new String(objData.getDate());
	var mes = new String(objData.getMonth() +1);
	var ano = new String(objData.getFullYear());
	var seg = new String(objData.getSeconds());
	
	if(parseInt(dia.length) == 1){
		dia = "0" + dia;
	}
	if(parseInt(mes.length) == 1){

		mes = "0" + mes;
	}

	return dia + "" + mes + "" + ano + "" + seg;

	
}

function efetua_busca_filtros(){
	
}	



function carrega_regionalizacao(pais,estado,regiao,cidade,subregiao,bairro){
	var strConsulta = "";
	if(pais != ""){
		strConsulta = strConsulta + "qs_pais=" + pais;
		if(estado != ""){
			strConsulta = strConsulta + "&qs_estado=" + estado;
			if(regiao != ""){
				strConsulta = strConsulta + "&qs_regiao=" + regiao;
				if(cidade != ""){
					strConsulta = strConsulta + "&qs_cidade=" + cidade;
					if(subregiao != ""){
						strConsulta = strConsulta + "&qs_subregiao=" + subregiao;
						if(bairro != ""){
							strConsulta = strConsulta + "&qs_bairro=" + bairro;
						}
					}
				}
			}
		}
	}else{
		strConsulta = "pais=00000000001";
	}
	objRegiao = null; 
	// Procura por um objeto nativo (Mozilla/Safari) 
    if (window.XMLHttpRequest) {
		objRegiao = new XMLHttpRequest(); 
        objRegiao.open("GET","gera_mapa_new.php?" + strConsulta  + "&tmpdatahora=" + pega_datahora(), true); 		
		objRegiao.onreadystatechange = pross_carrega_regionalizacao; 		
        objRegiao.send(null); 
    // Procura por uma versao ActiveX (IE) 
    } else if (window.ActiveXObject) { 
        objRegiao = new ActiveXObject("Microsoft.XMLHTTP"); 
        if (objRegiao) { 
			objRegiao.onreadystatechange = pross_carrega_regionalizacao; 
			objRegiao.open("GET","gera_mapa_new.php?" + strConsulta  + "&tmpdatahora=" + pega_datahora(), true);
			objRegiao.send(strConsulta);
        } 
    } 
}
function pross_carrega_regionalizacao(){
	if (objRegiao.readyState == 4){
		if (objRegiao.status == 200) { 
			/*x = window.open();
			x.document.write(objRegiao.responseText)*/
			if (window.ActiveXObject){
				ret = xmldoc.loadXML(objRegiao.responseText);
				if (ret!=true){
					alert("Não foi possvel ler arquivo.");
					return(0);
				}
			}else{
				var xmlParser = new DOMParser();
				xmldoc = xmlParser.parseFromString(objRegiao.responseText,"text/xml");
			}		
			xmldoc.resolveExternals=false;
			xmldoc.async=false;
			var root = xmldoc.documentElement;
			/*############################################
			# COMBOS DE REGIONALIZAÇÃO                   #
			#############################################*/
			/*##########################
			#COMBO DE PAIS (busca_pais)#
			###########################*/
			c_busca_pais = document.getElementById('busca_pais');
			if(c_busca_pais){
				// Limpa o Campo
				while (c_busca_pais.length > 0)
				    c_busca_pais.remove(0);
				for(i = 0; i < root.childNodes.item(0).childNodes.length; i++){
					var newElem = 	document.createElement("option");
					newElem.text =  root.childNodes.item(0).childNodes.item(i).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(0).childNodes.item(i).attributes.getNamedItem('value').value;
					c_busca_pais.options.add(newElem);
					if(root.childNodes.item(0).childNodes.item(i).attributes.getNamedItem('selected').value == 'true'){
						c_busca_pais.value = root.childNodes.item(0).childNodes.item(i).attributes.getNamedItem('value').value;
					}
				} 
			}
			/*###############################
			#COMBO DE ESTADOS (busca_estado)#
			###############################*/
			c_busca_estado = document.getElementById('busca_estado');
			if(c_busca_estado){
				while (c_busca_estado.length > 0)
				    c_busca_estado.remove(0);
				var newElem = document.createElement("option");
				newElem.text = "todos os estados";
				newElem.value = "";
				c_busca_estado.options.add(newElem);
				c_busca_estado.value = "";
				for(i = 0; i < root.childNodes.item(1).childNodes.length; i++){
					var newElem = document.createElement("option");
					newElem.text = root.childNodes.item(1).childNodes.item(i).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(1).childNodes.item(i).attributes.getNamedItem('value').value;
					c_busca_estado.options.add(newElem);
					if(root.childNodes.item(1).childNodes.item(i).attributes.getNamedItem('selected').value == 'true'){
						c_busca_estado.value = root.childNodes.item(1).childNodes.item(i).attributes.getNamedItem('value').value;
					}
				} 
				if(c_busca_estado.options.length < 2){
					c_busca_estado.style.display = 'none';
					document.getElementById('apontador1').style.display = 'none';
				}else{
					c_busca_estado.style.display = '';
					document.getElementById('apontador1').style.display = '';				
				}
			}
			/*###############################
			#COMBO DE REGIAO  (busca_cidade)#
			###############################*/
			c_busca_regiao = document.getElementById('busca_regiao');
			if(c_busca_regiao){
				while (c_busca_regiao.length > 0)
					c_busca_regiao.remove(0);
				if(root.childNodes.item(2).attributes.getNamedItem('value') != null){
					var newElem = document.createElement("option");
					newElem.text = "todas as  cidades";
					newElem.value = "";
					c_busca_regiao.options.add(newElem);
	
					var newElem = document.createElement("option");
					newElem.text = "Regiao de " + root.childNodes.item(2).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(2).attributes.getNamedItem('value').value;
					c_busca_regiao.options.add(newElem);
					
				}else{
					var newElem = document.createElement("option");
					newElem.text = "todas as cidades";
					newElem.value = "";
					c_busca_regiao.options.add(newElem);
				}
				for(i = 0; i < root.childNodes.item(2).childNodes.length; i++){
					var newElem = document.createElement("option");
					newElem.text = root.childNodes.item(2).childNodes.item(i).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(2).childNodes.item(i).attributes.getNamedItem('value').value;
					c_busca_regiao.options.add(newElem);
					if(root.childNodes.item(2).attributes.getNamedItem('value') == null){
						if(root.childNodes.item(2).childNodes.item(i).attributes.getNamedItem('selected').value == 'true'){
							c_busca_regiao.value = root.childNodes.item(2).childNodes.item(i).attributes.getNamedItem('value').value;
						}
					}else{
						c_busca_regiao.value = root.childNodes.item(2).attributes.getNamedItem('value').value;
					}
				} 
				if(c_busca_regiao.options.length < 2){
					c_busca_regiao.style.display = 'none';
				}else{
					c_busca_regiao.style.display = '';
				}
			}
			/*######################################
			#COMBO DE CIDADE  (busca_cidade_regiao)#
			######################################*/
			c_busca_cidade = document.getElementById('busca_cidade');
			if(c_busca_cidade){
				while (c_busca_cidade.length > 0)
					c_busca_cidade.remove(0);
				var newElem = document.createElement("option");
				newElem.text = "todas as cidades da região";
				newElem.value = "";
				c_busca_cidade.options.add(newElem);
				c_busca_cidade.value = "";
				for(i = 0; i < root.childNodes.item(3).childNodes.length; i++){
					var newElem = document.createElement("option");
					newElem.text = root.childNodes.item(3).childNodes.item(i).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(3).childNodes.item(i).attributes.getNamedItem('value').value;
					c_busca_cidade.options.add(newElem);
					if(root.childNodes.item(3).childNodes.item(i).attributes.getNamedItem('selected').value == 'true'){
						c_busca_cidade.value = root.childNodes.item(3).childNodes.item(i).attributes.getNamedItem('value').value;
					}
				} 
				if(c_busca_cidade.options.length < 2){
					c_busca_cidade.style.display = 'none'; document.getElementById('apontador2').style.display = 'none';
				}else{
					if(c_busca_cidade.options.length == 2){
						c_busca_cidade.options[1].selected = true;
					}
					c_busca_cidade.style.display = ''; document.getElementById('apontador2').style.display = '';
				}
			}
			/*######################################
			#COMBO DE BAIRRO  (busca_bairro)       #
			######################################*/
			c_busca_subregiao = document.getElementById('busca_subregiao');
			if(c_busca_subregiao){
				while (c_busca_subregiao.length > 0)
					c_busca_subregiao.remove(0);
				if(root.childNodes.item(4).attributes.getNamedItem('value') != null){
					var newElem = document.createElement("option");
					newElem.text = "todos os bairros";
					newElem.value = "";
					c_busca_subregiao.options.add(newElem);
					
					var newElem = document.createElement("option");
					newElem.text = "SubRegião de " + root.childNodes.item(4).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(4).attributes.getNamedItem('value').value;
					c_busca_subregiao.options.add(newElem);
				}else{
					var newElem = document.createElement("option");
					newElem.text = "todos os bairros";
					newElem.value = "";
					c_busca_subregiao.options.add(newElem);
				}
				c_busca_subregiao.value = "";
				for(i = 1; i < root.childNodes.item(4).childNodes.length; i++){
					var newElem = document.createElement("option");
					newElem.text = root.childNodes.item(4).childNodes.item(i).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(4).childNodes.item(i).attributes.getNamedItem('value').value;
					c_busca_subregiao.options.add(newElem);
					if(root.childNodes.item(4).childNodes.item(i).attributes.getNamedItem('selected').value == 'true'){
						c_busca_subregiao.value = root.childNodes.item(4).childNodes.item(i).attributes.getNamedItem('value').value;
					}
				} 
				if(c_busca_subregiao.options.length < 2){
					c_busca_subregiao.style.display = 'none';
	
				}else{
					c_busca_subregiao.style.display = '';
				}
			}
			/*######################################
			#COMBO DE BAIRRO  (busca_bairro_subregiao)#
			######################################*/
			c_busca_bairro = document.getElementById('busca_bairro');
			if(c_busca_bairro){
				while (c_busca_bairro.length > 0)
					c_busca_bairro.remove(0);
				var newElem = document.createElement("option");
				newElem.text = "todos os bairros da sub-regiao";
				newElem.value = "";
				c_busca_bairro.options.add(newElem);
				c_busca_bairro.value = "";
				for(i = 1; i < root.childNodes.item(5).childNodes.length; i++){
					var newElem = document.createElement("option");
					newElem.text = root.childNodes.item(5).childNodes.item(i).attributes.getNamedItem('text').value;
					newElem.value = root.childNodes.item(5).childNodes.item(i).attributes.getNamedItem('value').value;
					c_busca_bairro.options.add(newElem);
					if(root.childNodes.item(5).childNodes.item(i).attributes.getNamedItem('selected').value == 'true'){
						c_busca_bairro.value = root.childNodes.item(5).childNodes.item(i).attributes.getNamedItem('value').value;
					}
				} 
				if(root.childNodes.item(5).childNodes.length < 2){
					c_busca_bairro.style.display = 'none'; document.getElementById('apontador3').style.display = 'none';
				}else{
					c_busca_bairro.style.display = ''; document.getElementById('apontador3').style.display = '';
				}
			}
			/*############################################
			# MAPA                                       #
			#############################################*/
			//alert(root.childNodes.item(6).childNodes.item(0).attributes.getNamedItem('src').value);
			if(root.childNodes.item(6).childNodes.item(0).attributes.getNamedItem('src').value != ""){
				if(document.getElementById('iframe_mapa')){
					document.getElementById('iframe_mapa').src = root.childNodes.item(6).childNodes.item(0).attributes.getNamedItem('src').value
					return true;
				}
			}
		}else{ 
			alert("Houve um problema ao obter os dados:\n" + objRegiao.statusText); 
		} 	
	}
	return true;
}

function abreestados(cod){
	pais = "";
	estado = "";
	regiao = "";
	cidade = "";
	subregiao = "";
	bairro = "";

	if(document.getElementById('busca_pais'))
		pais = 		document.getElementById('busca_pais').options[(document.getElementById('busca_pais').selectedIndex)].value;
	if(document.getElementById('busca_estado'))
		estado = 	document.getElementById('busca_estado').options[(document.getElementById('busca_estado').selectedIndex)].value;
	/*if(document.getElementById('busca_regiao'))
		regiao = 	document.getElementById('busca_regiao').options[(document.getElementById('busca_regiao').selectedIndex)].value;
	/*if(document.getElementById('busca_cidade'))
		cidade = 	document.getElementById('busca_cidade').options[(document.getElementById('busca_cidade').selectedIndex)].value;
	if(document.getElementById('busca_subregiao'))
		subregiao = document.getElementById('busca_subregiao').options[(document.getElementById('busca_subregiao').selectedIndex)].value;
	if(document.getElementById('busca_bairro'))
		bairro = 	document.getElementById('busca_bairro').options[(document.getElementById('busca_bairro').selectedIndex)].value;*/
//		alert(pais+","+cod+","+regiao+","+cidade+","+subregiao+","+bairro)
	carrega_regionalizacao(pais,cod,regiao,cidade,subregiao,bairro);
}
function abrecidades(cod){
	pais = "";
	estado = "";
	regiao = "";
	cidade = "";
	subregiao = "";
	bairro = "";

	if(document.getElementById('busca_pais'))
		pais = 		document.getElementById('busca_pais').options[(document.getElementById('busca_pais').selectedIndex)].value;
	if(document.getElementById('busca_estado'))
		estado = 	document.getElementById('busca_estado').options[(document.getElementById('busca_estado').selectedIndex)].value;
	if(document.getElementById('busca_regiao'))
		regiao = 	document.getElementById('busca_regiao').options[(document.getElementById('busca_regiao').selectedIndex)].value;
	/*if(document.getElementById('busca_cidade'))
		cidade = 	document.getElementById('busca_cidade').options[(document.getElementById('busca_cidade').selectedIndex)].value;
	if(document.getElementById('busca_subregiao'))
		subregiao = document.getElementById('busca_subregiao').options[(document.getElementById('busca_subregiao').selectedIndex)].value;
	if(document.getElementById('busca_bairro'))
		bairro = 	document.getElementById('busca_bairro').options[(document.getElementById('busca_bairro').selectedIndex)].value;*/

	carrega_regionalizacao(pais,estado,cod,cidade,subregiao,bairro);
}
function abrecidades_regiao(cod){
	pais = "";
	estado = "";
	regiao = "";
	cidade = "";
	subregiao = "";
	bairro = "";

	if(document.getElementById('busca_pais'))
		pais = 		document.getElementById('busca_pais').options[(document.getElementById('busca_pais').selectedIndex)].value;
	if(document.getElementById('busca_estado'))
		estado = 	document.getElementById('busca_estado').options[(document.getElementById('busca_estado').selectedIndex)].value;
	if(document.getElementById('busca_regiao'))
		regiao = 	document.getElementById('busca_regiao').options[(document.getElementById('busca_regiao').selectedIndex)].value;
	if(document.getElementById('busca_cidade'))
		cidade = 	document.getElementById('busca_cidade').options[(document.getElementById('busca_cidade').selectedIndex)].value;
	/*if(document.getElementById('busca_subregiao'))
		subregiao = document.getElementById('busca_subregiao').options[(document.getElementById('busca_subregiao').selectedIndex)].value;
	if(document.getElementById('busca_bairro'))
		bairro = 	document.getElementById('busca_bairro').options[(document.getElementById('busca_bairro').selectedIndex)].value;*/
	carrega_regionalizacao(pais,estado,regiao,cod,subregiao,bairro);
}
function abrebairros(cod){
	
	pais = "";
	estado = "";
	regiao = "";
	cidade = "";
	subregiao = "";
	bairro = "";

	if(document.getElementById('busca_pais'))
		pais = 		document.getElementById('busca_pais').options[(document.getElementById('busca_pais').selectedIndex)].value;
	if(document.getElementById('busca_estado'))
		estado = 	document.getElementById('busca_estado').options[(document.getElementById('busca_estado').selectedIndex)].value;
	if(document.getElementById('busca_regiao'))
		regiao = 	document.getElementById('busca_regiao').options[(document.getElementById('busca_regiao').selectedIndex)].value;
	if(document.getElementById('busca_cidade'))
		cidade = 	document.getElementById('busca_cidade').options[(document.getElementById('busca_cidade').selectedIndex)].value;
	if(document.getElementById('busca_subregiao'))
		subregiao = document.getElementById('busca_subregiao').options[(document.getElementById('busca_subregiao').selectedIndex)].value;
	/*if(document.getElementById('busca_bairro'))
		bairro = 	document.getElementById('busca_bairro').options[(document.getElementById('busca_bairro').selectedIndex)].value;*/
	carrega_regionalizacao(pais,estado,regiao,cidade,cod,bairro);
}	
function abrebairros_subregiao(cod){
	pais = "";
	estado = "";
	regiao = "";
	cidade = "";
	subregiao = "";
	bairro = "";

	if(document.getElementById('busca_pais'))
		pais = 		document.getElementById('busca_pais').options[(document.getElementById('busca_pais').selectedIndex)].value;
	if(document.getElementById('busca_estado'))
		estado = 	document.getElementById('busca_estado').options[(document.getElementById('busca_estado').selectedIndex)].value;
	if(document.getElementById('busca_regiao'))
		regiao = 	document.getElementById('busca_regiao').options[(document.getElementById('busca_regiao').selectedIndex)].value;
	if(document.getElementById('busca_cidade'))
		cidade = 	document.getElementById('busca_cidade').options[(document.getElementById('busca_cidade').selectedIndex)].value;
	if(document.getElementById('busca_subregiao'))
		subregiao = document.getElementById('busca_subregiao').options[(document.getElementById('busca_subregiao').selectedIndex)].value;
	if(document.getElementById('busca_bairro'))
		bairro = 	document.getElementById('busca_bairro').options[(document.getElementById('busca_bairro').selectedIndex)].value;
	carrega_regionalizacao(pais,estado,regiao,cidade,subregiao,cod);
}	
function pesquisa_palavra_chave(valor){
		
}