﻿function CargarMunicipios(listaEstados, listaMunicipios)
{
    var ddlEstados = document.getElementById(listaEstados);
    var ddlMunicipios = document.getElementById(listaMunicipios);
    var valores = [];
    var http = null;
    
    var cargando = new Option('cargando...',0);
    ddlMunicipios.options[0] = cargando;
    
	if(typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			http = new XMLHttpRequest();
		}
		catch (e) { http = null; }
	}
	else
	{
		try
		{
			http = new ActiveXObject("Msxml2.XMLHTTP") ;
		}
		catch (e)
		{
			try
			{
				http = new ActiveXObject("Microsoft.XMLHTTP") ;
			}
			catch (e) {	http = null; }
		}
	}

	if(http)
	{
		if(http.overrideMimeType)
			http.overrideMimeType('text/xml');

		http.open("GET", "ajax/municipios.aspx?estado=" + ddlEstados.value, true);

		var obj = this;
		http.onreadystatechange = function(n)
		{
			if(http.readyState == 4)
			{
				if((http.status == 200) || (http.status == 0))
				{
					var xmlDocument = null, tmpinfo = null;

					try
					{
						xmlDocument = http.responseXML;
						tmpinfo = xmlDocument.getElementsByTagName('listdata').item(0).firstChild.data;
					}
					catch(e)
					{
						try
						{
							xmlDocument = (new DOMParser()).parseFromString(http.responseText, "text/xml");
							tmpinfo = xmlDocument.getElementsByTagName('listdata').item(0).firstChild.data;
						}
						catch(ee) {}
					}

                    //Procesamos los resultados
					valores = tmpinfo.split('|');
					ddlMunicipios.options.length = valores.length;
					ddlMunicipios.options[0] = new Option('Selecciona un Municipio','0');
					if(valores.length>0)
					{
					    for (var i=1;i<=valores.length;i++) {
                        option = new Option(valores[i-1],valores[i-1]);
                        ddlMunicipios.options[i] = option;
                        }
					}
				}
			}
		}

		http.send(null);
	}
}

function CargarMunicipiosSeleccion(listaEstados, listaMunicipios, seleccion)
{
    var ddlEstados = document.getElementById(listaEstados);
    var ddlMunicipios = document.getElementById(listaMunicipios);
    var valores = [];
    
    var http = null;
	if(typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			http = new XMLHttpRequest();
		}
		catch (e) { http = null; }
	}
	else
	{
		try
		{
			http = new ActiveXObject("Msxml2.XMLHTTP") ;
		}
		catch (e)
		{
			try
			{
				http = new ActiveXObject("Microsoft.XMLHTTP") ;
			}
			catch (e) {	http = null; }
		}
	}

	if(http)
	{
		if(http.overrideMimeType)
			http.overrideMimeType('text/xml');

		http.open("GET", "ajax/municipios.aspx?estado=" + ddlEstados.value, true);

		var obj = this;
		http.onreadystatechange = function(n)
		{
			if(http.readyState == 4)
			{
				if((http.status == 200) || (http.status == 0))
				{
					var xmlDocument = null, tmpinfo = null;

					try
					{
						xmlDocument = http.responseXML;
						tmpinfo = xmlDocument.getElementsByTagName('listdata').item(0).firstChild.data;
					}
					catch(e)
					{
						try
						{
							xmlDocument = (new DOMParser()).parseFromString(http.responseText, "text/xml");
							tmpinfo = xmlDocument.getElementsByTagName('listdata').item(0).firstChild.data;
						}
						catch(ee) {}
					}

                    //Procesamos los resultados
					valores = tmpinfo.split('|');
					ddlMunicipios.options.length = valores.length;
					if(valores.length>0)
					{
					    for (var i=0;i<valores.length;i++) {
                        option = new Option(valores[i],valores[i]);
                        ddlMunicipios.options[i] = option;
                        }
					}
					ddlMunicipios.value = seleccion;
				}
			}
		}

		http.send(null);
	}
}

function fijaMunicipio()
{
    if(txtMunicipio!=null)
        document.getElementById(txtMunicipio).value = document.getElementById('ddlMunicipio').value;
}

function usaMismaDireccion(direccion, campos)
{
    var valores = direccion.split('|');
    var valoresC = campos.split('|');
    
    if(valores.length==valoresC.length)
    {
        for (var i=0;i<valores.length;i++)
        {
            document.getElementById(valoresC[i]).value = valores[i];
        }
        CargarMunicipios(listaEstados, listaMunicipios);
    }   
}
