var g_departamento='';
var g_provincia='';
var g_distrito='';

// Declaro los selects que componen el documento HTML. Su atributo ID debe figurar aqui.
var listadoSelects=new Array();
listadoSelects[0]="cmbPais";
listadoSelects[1]="cmbDepartamento";
listadoSelects[2]="cmbProvincia";
listadoSelects[3]="cmbDistrito";

function buscarEnArray(array, dato)
{
	// Retorna el indice de la posicion donde se encuentra el elemento en el array o null si no se encuentra
	var x=0;
	while(array[x])
	{
		if(array[x]==dato) return x;
		x++;
	}
	return null;
}

function cargaContenido(idSelectOrigen, valueEdit, filenameAjaxPhp)
{		
	//alert('idSelectOrigen');
	
	var posicionSelectDestino	=	buscarEnArray(listadoSelects, idSelectOrigen)+1;		// comienzo desde el combo despues hacia arriba para desabilitarlos
	var x						=	posicionSelectDestino;

	var selectOrigen			=	document.getElementById(idSelectOrigen);				// combo origen
	var opcion_origen			=	selectOrigen.options[selectOrigen.selectedIndex].value; // value

//	onPais(selectOrigen);


/*
	if(selectOrigen.value=='Elija')	// Opcion Elije
	{
		/*
		alert('23');
		var selectActual = null;
		while(listadoSelects[x])
		{
			selectActual=document.getElementById(listadoSelects[x]);
			selectActual.length=0; // Borra todos los items
			var nuevaOpcion=document.createElement("option"); 
			nuevaOpcion.value=0; 
			nuevaOpcion.innerHTML="Selecciona Opci&oacute;n...";
			selectActual.appendChild(nuevaOpcion);
			selectActual.disabled=true;
			selectActual.className='box_normal';
			x++;
		}
		*/
//	}
//	else
//	{
////		x++;
//		while(listadoSelects[x])
//		{
//			selectActual=document.getElementById(listadoSelects[x]);
//			selectActual.length=0; // Borra todos los items
//			var nuevaOpcion=document.createElement("option"); 
//			nuevaOpcion.value=0; 
//			nuevaOpcion.innerHTML="Selecciona Opci&oacute;n...";
//			selectActual.appendChild(nuevaOpcion);
//			selectActual.disabled=true;
//			x++;
//		}

		var idSelectDestino		=	listadoSelects[x];
		var selectDestino		=	document.getElementById(idSelectDestino);

		if (valueEdit!='')
		{
			if (idSelectOrigen=='cmbPais')
			{
				opcion_origen  		= 	'51';
				opcion_destin		=	valueEdit;
				g_departamento		= 	opcion_destin;
			}
			
			if (idSelectOrigen=='cmbDepartamento')
			{
//				alert(valueEdit);

				opcion_origen 		= 	g_departamento;
				opcion_destin		=	valueEdit;
				g_provincia			=	opcion_destin;
			}
			
			if(idSelectOrigen=='cmbProvincia')
			{
				departamento 		= 	g_departamento;
				opcion_origen		=	g_provincia;
				opcion_destin		=	valueEdit;
				g_distrito			=	opcion_destin;
			}

			if (idSelectOrigen=='cmbDistrito')
			{
				departamento 		= 	g_departamento;
				provincia			= 	g_provincia;
				opcion_origen		=	g_distrito;
				opcion_destin		=	valueEdit;
			}
		}
		else
		{
			var departamento	=	document.getElementById('cmbDepartamento').options[document.getElementById('cmbDepartamento').selectedIndex].value;
			var provincia		=	document.getElementById('cmbProvincia').options[document.getElementById('cmbProvincia').selectedIndex].value;
			var distrito		=	document.getElementById('cmbDistrito').options[document.getElementById('cmbDistrito').selectedIndex].value;
			opcion_destin		=	0;
		}
		
	var params = "select="+idSelectOrigen+"&opcion_origen="+opcion_origen+"&opcion_destin="+opcion_destin;
	params=params+"&departamento="+departamento+"&provincia="+provincia+"&distrito="+distrito+"&url="+filenameAjaxPhp;
	//alert(params);
		
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	
   	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
   		xmlhttp = new XMLHttpRequest();
   	}try{
		xmlhttp.open("POST", filenameAjaxPhp,true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.send(params);
    	xmlhttp.onreadystatechange=function() {

    		//alert(xmlhttp.responseText);
    		
			if (xmlhttp.readyState==1)
			{
				selectDestino.length=0;
				var nuevaOpcion=document.createElement("option"); 
				nuevaOpcion.value=0;
				nuevaOpcion.innerHTML="Cargando...";
				selectDestino.appendChild(nuevaOpcion); 
				selectDestino.disabled=true;	
			}
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.responseText!='')
				{
					//alert(selectDestino);
					if (selectDestino!=null)
					{
						selectDestino.parentNode.innerHTML=xmlhttp.responseText;
					}
				}
			} 
    	}
	} catch(ee) {
	    alert(ee);
	}		
	//}
}

/*
function createCboDep(pPais, valueEdit, )
{
	
}
*/
function loadUbiColombia(pSelectId, valueEdit, filenameAjaxPhp, pDis, pDisVal)
{
	//alert(pSelectId);
	
	var departamento 	= document.getElementById('cmbDepartamento').value;
	var provincia 		= 0;
	var distrito 		= document.getElementById('cmbDistrito').value;	
	var params 			= "pSelectId="+pSelectId+"&valueEdit="+valueEdit+"&url="+filenameAjaxPhp+"&departamento="+departamento+"&provincia="+provincia+"&distrito="+distrito+"&pDis="+pDis+"&pDisVal="+pDisVal;

	var xmlhttp=false;
	try 
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try 
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}

   	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
   	{
   		xmlhttp = new XMLHttpRequest();
   	}
   	try
   	{
		xmlhttp.open("POST", filenameAjaxPhp,true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    	xmlhttp.send(params);
    	xmlhttp.onreadystatechange=function() 
    	{    		
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.responseText!='')
				{
					if (pSelectId=='cmbPais')
					{
						document.getElementById('divDep').innerHTML = xmlhttp.responseText;
					}

					if (pSelectId=='cmbDepartamento')
					{
//						alert('a');
//						document.getElementById('divPro').innerHTML = xmlhttp.responseText;
					}
					
					if (pSelectId=='cmbProvincia')
					{
						document.getElementById('divDis').innerHTML = xmlhttp.responseText;
					}
				}
    		}
		}
   	}
	catch(ee)
	{
	   	alert(ee);
	}
}
