// JavaScript Document
var xmlhttp_state_content;
var xmlhttp_state_content2;

function busca_m(estado)
{
	//alert("Estado" + id_estado);
	xmlhttp_state_content = GetXmlHttpObject();
	if(xmlhttp_state_content==null)
	{
		alert("Browser does not support HTTP Request");
		return;
	}
	var url_content = "../scripts/obtenerMun.php";
	url_content = url_content + "?estado=" + estado;
	url_content = url_content + "&sid=" + Math.random(); 
	xmlhttp_state_content.onreadystatechange = stateChanged_data_content;
	xmlhttp_state_content.open("GET", url_content, true);
	xmlhttp_state_content.send(null);
}

function busca_c(municipio,estado)
{
	//alert("Estado" + estado +"Municipio "+municipio);
	xmlhttp_state_content2 = GetXmlHttpObject();
	if(xmlhttp_state_content2==null)
	{
		alert("Browser does not support HTTP Request");
		return;
	}
	var url_content2 = "../scripts/obtenerCol.php";
	url_content2 = url_content2 + "?municipio=" + municipio + "&estado=" + estado;
	url_content2 = url_content2 + "&sid=" + Math.random(); 
	xmlhttp_state_content2.onreadystatechange = stateChanged_data_content2;
	xmlhttp_state_content2.open("GET", url_content2, true);
	xmlhttp_state_content2.send(null);
}

function stateChanged_data_content()
{
	if(xmlhttp_state_content.readyState==4)
	{
		sel = document.createElement('SELECT');
		sel.name = "municipio";
		sel.id = "municipio";
		sel.style.width = "200px";
		sel.onchange = function(){colonias();}
		
		//para select de colonias
		sel2 = document.createElement('SELECT');
		sel2.name = "colonia";
		sel2.id = "colonia";
		sel2.style.width = "200px";
		

		document.getElementById('MN').appendChild(sel);
		document.getElementById('CL').appendChild(sel2);
		
		var datos = (xmlhttp_state_content.responseText).split(",");
		//alert(datos.length);
		
		opcioncur = document.createElement('OPTION');
     	opcioncur.innerHTML = "Elige";
    	opcioncur.value = "";
		opcioncur.selected = "selected"
     	document.getElementById('municipio').appendChild(opcioncur);
		
		//definir la opcion elige del select colonias
		opcioncur2 = document.createElement('OPTION');
     	opcioncur2.innerHTML = "Elige";
    	opcioncur2.value = "";
		opcioncur2.selected = "selected"
     	document.getElementById('colonia').appendChild(opcioncur2);
		
			
		for(j=0; j<datos.length-1; j=j+2)
		{
			opcioncur = document.createElement('OPTION');
			opcioncur.value = datos[j];
     		opcioncur.innerHTML = datos[j+1];
     		document.getElementById('municipio').appendChild(opcioncur);
		}
	}
}


function stateChanged_data_content2()
{
	if(xmlhttp_state_content2.readyState==4)
	{
		sel = document.createElement('SELECT');
		sel.name = "colonia";
		sel.id = "colonia";
		sel.style.width = "200px";
		sel.onchange = "municipios()";
		document.getElementById('CL').appendChild(sel);
		
		var datos = (xmlhttp_state_content2.responseText).split(",");
		//alert(datos.length);
		
		opcioncur = document.createElement('OPTION');
     	opcioncur.innerHTML = "Elige";
    	opcioncur.value = "";
		opcioncur.selected = "selected"
     	document.getElementById('colonia').appendChild(opcioncur);
			
		for(j=0; j<datos.length-1; j=j+2)
		{
			opcioncur = document.createElement('OPTION');
			opcioncur.value = datos[j];
     		opcioncur.innerHTML = datos[j+1];
     		document.getElementById('colonia').appendChild(opcioncur);
		}
	}
}

function GetXmlHttpObject()
{
	var req = null;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = null;
	}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = null;
			}
		}
	}
	return req;
}
