var xml = loadXml("distribuidores.xml");
var xsl = loadXsl("distribuidores.xsl");

function ListarPaises()
{
	var nodos = xml.getElementsByTagName("Provincia");
	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_provincias");
	var selector_ciudades = document.getElementById("selector_ciudades");
	var selector_empresas = document.getElementById("selector_empresas");
	selector.options.length = 0;
	selector_ciudades.options.length = 0;
	selector_empresas.options.length = 0;
	for ( i=0; i<elementos.length; i++ )
	{
		var opcion = document.createElement("option");
		opcion.setAttribute("value",elementos[i]);
		opcion.innerHTML= (elementos[i]=='-') ? 'Capital Federal' : elementos[i];
		selector.appendChild(opcion);
	}
}

function ListarCiudades()
{
	var selector_ciudades = document.getElementById("selector_ciudades");
	var selector_paises = document.getElementById("selector_provincias");
	var selector_empresas = document.getElementById("selector_empresas");
	var pais = selector_paises.options[selector_paises.selectedIndex].value;
	
	var ciudades = new Array();
	var nodos = xml.getElementsByTagName("Empresa");
	for ( var i=0; i<nodos.length; i++ )
	{
		if ( nodos[i].getElementsByTagName("Provincia")[0].firstChild.nodeValue == pais )
		{
			//alert( nodos[i].getElementsByTagName("Ciudad")[0].firstChild.nodeValue+" - "+nodos[i].getElementsByTagName("Pais")[0].firstChild.nodeValue+" - "+in_array(nodos[i].getElementsByTagName("Ciudad")[0].firstChild.nodeValue, ciudades) );
			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;
	selector_empresas.options.length = 0;
	for ( i=0; i<ciudades.length; i++ )
	{
		var opcion = document.createElement("option");
		opcion.setAttribute("value",ciudades[i]);
		opcion.innerHTML = ciudades[i];
		selector_ciudades.appendChild(opcion);
	}
}

function ListarEmpresas()
{
	var selector_paises = document.getElementById("selector_provincias");
	var selector_ciudades = document.getElementById("selector_ciudades");
	var selector_empresas = document.getElementById("selector_empresas");
	var ciudad = selector_ciudades.options[selector_ciudades.selectedIndex].value;
	var pais = selector_paises.options[selector_paises.selectedIndex].value;
	
	var empresas = new Array();
	var nodos = xml.getElementsByTagName("Empresa");
	for ( var i=0; i<nodos.length; i++ )
	{
		if ( nodos[i].getElementsByTagName("Ciudad")[0].firstChild.nodeValue == ciudad &&
			 nodos[i].getElementsByTagName("Provincia")[0].firstChild.nodeValue == pais )
		{
			if ( !in_array( nodos[i].getElementsByTagName("Nombre")[0].firstChild.nodeValue, empresas ) )
			{
				empresas.push( nodos[i].getElementsByTagName("Nombre")[0].firstChild.nodeValue );
			}
		}
	}
	selector_empresas.options.length = 0;
	for ( i=0; i<empresas.length; i++ )
	{
		var opcion = document.createElement("option");
		opcion.setAttribute("value",empresas[i]);
		opcion.innerHTML = empresas[i];
		selector_empresas.appendChild(opcion);
	}
}


function MostrarEmpresa()
{
	var selector = document.getElementById("selector_empresas");
	var objetivo = document.getElementById("tabla-distribuidores");
	var empresa = selector.options[selector.selectedIndex].value;
	// code for IE
	if (window.ActiveXObject)
	{
		var xsltThread = new ActiveXObject("Msxml2.XSLTemplate.4.0");
		xsltThread.stylesheet = xsl;
		var xsltProc = xsltThread.createProcessor();
		xsltProc.input = xml;
		//parameters
		xsltProc.addParameter("p_seleccion", empresa);
		xsltProc.transform();

		objetivo.innerHTML=xsltProc.output;
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation 
	&& document.implementation.createDocument)
	{
		xsltProcessor=new XSLTProcessor();
		xsltProcessor.importStylesheet(xsl);
		xsltProcessor.setParameter(null, "p_seleccion", empresa);
		resultDocument = xsltProcessor.transformToFragment(xml,document);
		objetivo.innerHTML = "";
		objetivo.appendChild(resultDocument);
	}
	/*var filas = objetivo.childNodes[0].childNodes[1].childNodes;
	var i = 1;
	while ( filas[i] != null )
	{
		if ( i % 2 == 0 )
		{
			filas[i].setAttribute( (window.ActiveXObject ? "className" : "class"), "gris" );
		}
		i++;
	}*/
}