var xml_distribuidores = loadXml("distribuidores_top.xml");
var xsl_distribuidores = loadXsl("distribuidores_top.xsl");

function ListarZonas()
{
	var nodos = xml_distribuidores.getElementsByTagName("Zona");
	var elementos = new Array();
	for ( var i=0; i<nodos.length; i++ )
	{
		if ( !in_array(nodos[i].firstChild.nodeValue, elementos) )
		{
			elementos.push( nodos[i].firstChild.nodeValue );
		}
	}
	var selector = document.getElementById("selector_zonas");
	var selector_estados = document.getElementById("selector_estados");
	var selector_ciudades = document.getElementById("selector_ciudades");
	selector.options.length = 0;
	selector_estados.options.length = 0;
	selector_ciudades.options.length = 0;
	for ( i=0; i<elementos.length; i++ )
	{
		var opcion = document.createElement("option");
		opcion.setAttribute("value",elementos[i]);
		opcion.innerHTML = elementos[i];
		selector.appendChild(opcion);
	}
	
}

function ListarEstados()
{
	var selector_estados = document.getElementById("selector_estados");
	var selector_zonas= document.getElementById("selector_zonas");
	var selector_ciudades = document.getElementById("selector_ciudades");
	var zona = selector_zonas.options[selector_zonas.selectedIndex].value;
	
	var estados = new Array();
	var nodos = xml_distribuidores.getElementsByTagName("Empresa");
	for ( var i=0; i<nodos.length; i++ )
	{
		if ( nodos[i].getElementsByTagName("Zona")[0].firstChild.nodeValue == zona )
		{
			
			if ( !in_array( nodos[i].getElementsByTagName("Estado")[0].firstChild.nodeValue, estados ) )
			{
				estados.push( nodos[i].getElementsByTagName("Estado")[0].firstChild.nodeValue );
			}
		}
	}
	selector_estados.options.length = 0;
	selector_ciudades.options.length = 0;
	for ( i=0; i<estados.length; i++ )
	{
		var opcion = document.createElement("option");
		opcion.setAttribute("value",estados[i]);
		opcion.innerHTML = estados[i];
		selector_estados.appendChild(opcion);
	}
}

function ListarCiudades()
{
	var selector_zonas = document.getElementById("selector_zonas");
	var selector_estados = document.getElementById("selector_estados");
	var selector_ciudades = document.getElementById("selector_ciudades");
	var estado = selector_estados.options[selector_estados.selectedIndex].value;
	var zona = selector_zonas.options[selector_zonas.selectedIndex].value;
	
	var ciudades = new Array();
	var nodos = xml_distribuidores.getElementsByTagName("Empresa");
	for ( var i=0; i<nodos.length; i++ )
	{
		if ( nodos[i].getElementsByTagName("Estado")[0].firstChild.nodeValue == estado &&
			 nodos[i].getElementsByTagName("Zona")[0].firstChild.nodeValue == zona )
		{
			if ( !in_array( nodos[i].getElementsByTagName("Ciudad")[0].firstChild.nodeValue, ciudades ) )
			{
				ciudades.push( nodos[i].getElementsByTagName("Ciudad")[0].firstChild.nodeValue );
			}
		}
	}
	
	selector_ciudades.options.length = 0;
	for ( i=0; i<ciudades.length; i++ )
	{
		agregarOpcionACombo("selector_ciudades", ciudades[i], ciudades[i])
	}
}


function MostrarEmpresa()
{
	var selector1 = document.getElementById("selector_zonas");
	var selector2 = document.getElementById("selector_estados");
	var selector3 = document.getElementById("selector_ciudades");
	var objetivo = document.getElementById("div-distribuidores");
	var zona = selector1.options[selector1.selectedIndex].value;
	var estado = selector2.options[selector2.selectedIndex].value;
	var ciudad = selector3.options[selector3.selectedIndex].value;
	// code for IE
	
	objetivo.innerHTML = XSLTransformToString( xml_distribuidores, xsl_distribuidores, { seleccion_zona:zona, seleccion_estado:estado, seleccion_ciudad:ciudad } );
	
}