var aux = "";
var count = 0;
function xmlLoader(url){
	if(window.ActiveXObject){
		 var Loader = new ActiveXObject("Msxml2.DOMDocument.3.0");
		//var Loader = new ActiveXObject("Microsoft.XMLHTTP");
		Loader.async = false;
		Loader.load(url);
		return Loader;
	}else if(window.XMLHttpRequest){
		var Loader = new XMLHttpRequest();
		Loader.open("GET", url ,false);
		Loader.send(null);
		return Loader.responseXML;
	}
}
function selecionaCidades(estado){
	try{
		xml = xmlLoader("/sobre_losango/lojas/lojas_"+ estado +".xml");
		count = 0;
		aux = "";
		var divCidades = document.getElementById("divCidades");
		var divBairros = document.getElementById("divBairros");
		divBairros.innerHTML = "";
		divCidades.innerHTML = "";
		divCidades.innerHTML = "<br /><label>Cidade:</label><br /><select name='cidades' id='cidades' onchange='selecionaBairros(this.value)'></select>";
		xmlCidade(xml,"Cidade");
	} catch(e){
		return;
	}
}
function selecionaBairros(bairro){
	try{
		xml = xmlLoader("/sobre_losango/lojas/lojas_"+ document.getElementById("estados").value +".xml");
		count = 0;
		aux = "";
		var divBairros = document.getElementById("divBairros");
		var divLojas = document.getElementById("divLojas");
		var divAutorizada = document.getElementById("divAutorizada");
		divBairros.innerHTML = "";
		divLojas.innerHTML = "";
		divAutorizada.innerHTML = "";
		divBairros.innerHTML = "<br /><label>Bairro:</label><br /><select name='bairros' id='bairros' onchange='selecionaLoja(this.value)'></select>";
		xmlBairros(xml);
	} catch(e){
		return;
	}
}
function selecionaLoja(loja){
	try{
		xml = xmlLoader("/sobre_losango/lojas/lojas_"+ document.getElementById("estados").value +".xml");
		count = 0;
		aux = "";
		var divLojas = document.getElementById("divLojas");
		var divAutorizada = document.getElementById("divAutorizada");
		divLojas.innerHTML = "";
		divAutorizada.innerHTML = "";
		xmlLojas(xml);
	} catch(e){
		return;
	}
}
function xmlCidade(xmlNode){
	var objList = xmlNode.getElementsByTagName("Table");	
	for(i=0;i<objList.length;i++){
		if(window.ActiveXObject){
			cidade = objList[i].childNodes[5].firstChild.nodeValue;
		}else{
			cidade = objList[i].childNodes[11].firstChild.nodeValue;
		}
		if(aux.indexOf(cidade) == -1){
			aux += cidade;
			var x = document.getElementById("cidades");
			if(count == 0){
				x.options[count] = new Option("-----","");
				count++;
			}
			x.options[count] = new Option(cidade);
			count++;
		}
	}
}

function xmlBairros(xmlNode){
	var objList = xmlNode.getElementsByTagName("Table");
	var indexSelect = document.getElementById("cidades").selectedIndex;
	var valueSelected = document.getElementById("cidades").getElementsByTagName("option")[indexSelect].text; 
	//alert("Cidade" + valueSelected);
	//var selectCidades = document.getElementById("cidades");
	//var selectBairros = document.getElementById("bairros");
	//var valorSelecionado = selectBairros.options[selectBairros.selectedIndex];
	//alert(selectCidades)
	
	//alert(objList[i].childNodes[2].nodeName)
	//alert(selectBairros.value);
	
	for(i=0;i<objList.length;i++){
		if(window.ActiveXObject){
			cidade = objList[i].childNodes[5].firstChild.nodeValue;
			bairro = objList[i].childNodes[2].firstChild.nodeValue;
		}else{
			cidade = objList[i].childNodes[11].firstChild.nodeValue;
			bairro = objList[i].childNodes[5].firstChild.nodeValue;
		}
			//alert(selectCidades.value)
		//alert("cidade: "+cidade +"\n select: "+ valueSelected);
		if(cidade == valueSelected && aux.indexOf(bairro) == -1){
			aux += bairro;
			y= document.getElementById("bairros")
			if(count == 0){
				y.options[count] = new Option("-----","");
				count++;
			}
			y.options[count] = new Option(bairro);
			count++;
		}
	 }
 }

function xmlLojas(xmlNode){
	var objList = xmlNode.getElementsByTagName("Table");
	var indexSelect = document.getElementById("cidades").selectedIndex;
	var valueSelected = document.getElementById("cidades").getElementsByTagName("option")[indexSelect].text;
	var indexBairros = document.getElementById("bairros").selectedIndex;
	var valueBairros = document.getElementById("bairros").getElementsByTagName("option")[indexBairros].text; 

	var outFilial = "";
	var outAutorizada = "";
	
	for(i=0;i<objList.length;i++){
		if(window.ActiveXObject){
			cidade = objList[i].childNodes[5].firstChild.nodeValue;
			bairro = objList[i].childNodes[2].firstChild.nodeValue;
			tipo = objList[i].childNodes[9].firstChild.nodeValue;
			filial = objList[i].childNodes[0].firstChild.nodeValue;
			endereco = objList[i].childNodes[1].firstChild.nodeValue;
			cep = objList[i].childNodes[4].firstChild.nodeValue;
			telefone = objList[i].childNodes[7].firstChild.nodeValue;
			horario = objList[i].childNodes[10].firstChild.nodeValue;
		}
		else{
			cidade = objList[i].childNodes[11].firstChild.nodeValue;
			bairro = objList[i].childNodes[5].firstChild.nodeValue;
			tipo = objList[i].childNodes[19].firstChild.nodeValue;
			filial = objList[i].childNodes[1].firstChild.nodeValue;
			endereco = objList[i].childNodes[3].firstChild.nodeValue;
			cep = objList[i].childNodes[9].firstChild.nodeValue;
			telefone = objList[i].childNodes[15].firstChild.nodeValue;
			horario = objList[i].childNodes[21].firstChild.nodeValue;
		}
		if( telefone == 0)
			telefone = ""
		if(cidade == valueSelected && bairro == valueBairros){
			if(tipo == 0){
				if(outFilial == "")
					outFilial = '<dl id="filial">';
				outFilial += '<dt><strong>'+ filial +'</strong></dt>';
				outFilial += '<dd>'+ endereco +'</dd>';
				outFilial += '<dd>CEP: '+ cep +'</dd>';
				outFilial += '<dd>Tel: '+ telefone +'</dd>';
				outFilial += '<dd>Horário de Funcionamento: '+ horario +'</dd>';
			}
			else{
				if(outAutorizada == "")
					outAutorizada = '<dl id="autorizada">';
				outAutorizada += '<dt><strong>'+ filial +'</strong></dt>';
				outAutorizada += '<dd>'+ endereco +'</dd>';
				outAutorizada += '<dd>CEP: '+ cep +'</dd>';
				outAutorizada += '<dd>Tel: '+ telefone +'</dd>';
				outAutorizada += '<dd>Horário de Funcionamento: '+ horario +'</dd>';
			}
		}

	}
	outFilial += "</dl>";
	outAutorizada += "</dl>";
	if(outFilial.length > 20)
		document.getElementById("divLojas").innerHTML = outFilial;
	if(outAutorizada.length > 25)
		document.getElementById("divAutorizada").innerHTML = outAutorizada;
}