
//Global XMLHTTP Request object
var XmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//Gets called when country combo box selection changes
function CountryListOnChange() 
{
	var countryList = document.getElementById("countryList");

	//Getting the selected country from country combo box.
	var selectedCountry = countryList.options[countryList.selectedIndex].value;
	
	// URL to get states for a given country
	var requestUrl = AjaxServerPageName + "?SelectedCountry=" + encodeURIComponent(selectedCountry);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

function cmbDptoOnChange() 
{
	var cmbDpto = document.getElementById("cmbDpto");

	//Getting the selected country from country combo box.
	var selectedDpto = cmbDpto.options[cmbDpto.selectedIndex].value;
	
	// URL to get states for a given country
	var requestUrl = "AjaxServer.aspx?ff=dp&tipo=" + encodeURIComponent(selectedDpto);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponseDP;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

function cmbProvOnChange() 
{
	var cmbDpto = document.getElementById("cmbDpto");
	var cmbProv = document.getElementById("cmbProv");

	//Getting the selected country from country combo box.
	var selectedDpto = cmbDpto.options[cmbDpto.selectedIndex].value;
	var selectedProv = cmbProv.options[cmbProv.selectedIndex].value;
	
	// URL to get states for a given country
	var requestUrl = "AjaxServer.aspx?ff=dd&tipo=" + encodeURIComponent(selectedDpto+selectedProv);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponseDD;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

function solicitarProductos(tipo) 
{
	var selectedTipo = tipo;
	
	// URL to get states for a given country
	var requestUrl = "AjaxServer.aspx?ff=pr&tipo=" + encodeURIComponent(selectedTipo);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponsePR;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

function cmbCategoriasOnChange() 
{
	var cmbCategoria = document.getElementById("cmbCategoria");
    var tipo="";
	//Getting the selected country from country combo box.
	var selectedCategoria = cmbCategoria.options[cmbCategoria.selectedIndex].value;
	if(selectedCategoria=="TU"){
	    tipo="PT";
	}else{
	    tipo="PA";
	}
	// URL to get states for a given country
	var requestUrl = "AjaxServer.aspx?ff=pr&tipo=" + encodeURIComponent(tipo);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponsePRT;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

//Called when response comes back from server
function HandleResponsePR()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			LlenarProductos(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("Problemas con el servidor Ajax." );
		}
	}
}

function HandleResponsePRT()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			LlenarProductosT(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("Problemas con el servidor Ajax." );
		}
	}
}

function HandleResponseDP()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			LlenarProvincias(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("Problemas con el servidor Ajax." );
		}
	}
}

function HandleResponseDD()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			LlenarDistritos(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("Problemas con el servidor Ajax." );
		}
	}
}

//Called when response comes back from server
function HandleResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			ClearAndSetStateListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetStateListItems(countryNode)
{
    var stateList = document.getElementById("stateList");
	//Clears the state combo box contents.
	for (var count = stateList.options.length-1; count >-1; count--)
	{
		stateList.options[count] = null;
	}

	var stateNodes = countryNode.getElementsByTagName('state');
	var textValue; 
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < stateNodes.length; count++)
	{
   		textValue = GetInnerText(stateNodes[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		stateList.options[stateList.length] = optionItem;
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function LlenarProductos(NodoProductos)
{
    var listaProductos = document.getElementById("cmbProductos");
	//Clears the state combo box contents.
	for (var count = listaProductos.options.length-1; count >-1; count--)
	{
		listaProductos.options[count] = null;
	}

	var NodoProducto = NodoProductos.getElementsByTagName('producto');
	var textValue; 
	var textTitulo;
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < NodoProducto.length; count++)
	{
	    var NodoValor=NodoProducto[count].getElementsByTagName('valor');	    
   		textValue = GetInnerText(NodoValor[0]);
   		var NodoTitulo=NodoProducto[count].getElementsByTagName('titulo');	    
   		textTitulo = GetInnerText(NodoTitulo[0]);
		optionItem = new Option( textTitulo, textValue,  false, false);
		listaProductos.options[listaProductos.length] = optionItem;
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function LlenarProductosT(NodoProductos)
{
    var listaProductos = document.getElementById("cmbProducto");
	//Clears the state combo box contents.
	for (var count = listaProductos.options.length-1; count >-1; count--)
	{
		listaProductos.options[count] = null;
	}

	var NodoProducto = NodoProductos.getElementsByTagName('producto');
	var textValue; 
	var textTitulo;
	var optionItem;
	//Add new states list to the state combo box.
	optionItem = new Option( "- Todos -", "",  false, false);
    listaProductos.options[listaProductos.length] = optionItem;
    
	for (var count = 0; count < NodoProducto.length; count++)
	{
	    var NodoValor=NodoProducto[count].getElementsByTagName('valor');	    
   		textValue = GetInnerText(NodoValor[0]);
   		var NodoTitulo=NodoProducto[count].getElementsByTagName('titulo');	    
   		textTitulo = GetInnerText(NodoTitulo[0]);
		optionItem = new Option( textTitulo, textValue,  false, false);
		listaProductos.options[listaProductos.length] = optionItem;
	}
}

function LlenarProvincias(NodoProvincias)
{
    var listaProvincias = document.getElementById("cmbProv");
	//Clears the state combo box contents.
	for (var count = listaProvincias.options.length-1; count >-1; count--)
	{
		listaProvincias.options[count] = null;
	}

	var NodoProvincia = NodoProvincias.getElementsByTagName('provincia');
	var textValue; 
	var textTitulo;
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < NodoProvincia.length; count++)
	{
	    var NodoValor=NodoProvincia[count].getElementsByTagName('valor');	    
   		textValue = GetInnerText(NodoValor[0]);
   		var NodoTitulo=NodoProvincia[count].getElementsByTagName('titulo');	    
   		textTitulo = GetInnerText(NodoTitulo[0]);
		optionItem = new Option( textTitulo, textValue,  false, false);
		listaProvincias.options[listaProvincias.length] = optionItem;
	}
	cmbProvOnChange();
}

function LlenarDistritos(NodoDistritos)
{
    var listaDistritos = document.getElementById("cmbDist");
	//Clears the state combo box contents.
	for (var count = listaDistritos.options.length-1; count >-1; count--)
	{
		listaDistritos.options[count] = null;
	}

	var NodoDistrito = NodoDistritos.getElementsByTagName('distrito');
	var textValue; 
	var textTitulo;
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < NodoDistrito.length; count++)
	{
	    var NodoValor=NodoDistrito[count].getElementsByTagName('valor');	    
   		textValue = GetInnerText(NodoValor[0]);
   		var NodoTitulo=NodoDistrito[count].getElementsByTagName('titulo');	    
   		textTitulo = GetInnerText(NodoTitulo[0]);
		optionItem = new Option( textTitulo, textValue,  false, false);
		listaDistritos.options[listaDistritos.length] = optionItem;
	}
	
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}