<?php

$dbuser = "root";
$dbpass = "castillejos";
$dbhost = "localhost";
$dbname = "geoyos_jc";
@$con = mysqli_connect($dbhost,$dbuser,$dbpass);
if (!$con){
	die("SIN CONEXION: " . mysqli_error($con));
}
$cag=mysqli_select_db($dbname); 

function CAJselectOptions(&$arrJSON,$valor,$etiqueta){
	$arrJSON[]="{valueOption:'$valor',labelOption:'$etiqueta'}";
}

function CSJselectOptions(&$strJSON,$arrJSON,$separador=","){
		 $strJSON="[".implode($separador,$arrJSON)."]";
}

//->Regresa Paises
if(isset($_POST["getCountries"])){
	$sqPaises=mysqli_query($con ,"SELECT * FROM dxpaises order by cve_pais");
	CAJselectOptions($jsonOptions,'-1','--Seleccione Pais--');
	while($arrPais=mysqli_fetch_array($sqPaises)){
		CAJselectOptions($jsonOptions,$arrPais['cve_pais'],$arrPais['des_pais']);
	}
	CSJselectOptions($jsonOptionsString,$jsonOptions,",");
	echo $jsonOptionsString;
}
//->Regresa Estados de la llave mandada: pais
elseif(isset($_POST["getStates"])){
	$pais=$_POST['codPais'];
	$sqEstados=mysqli_query($con ,"SELECT * FROM dxestado where cve_pais='$pais' order by cve_estado");
	CAJselectOptions($jsonOptions,'-1','--Seleccione Estado--');
	while($arrEstado=mysqli_fetch_array($sqEstados)){
		CAJselectOptions($jsonOptions,$arrEstado['cve_estado'],$arrEstado['des_estado']);
	}
	CSJselectOptions($jsonOptionsString,$jsonOptions,",");
	echo $jsonOptionsString;
}

//Regresa Delegaciones/Municipios de la llave mandada: pais|estado
elseif(isset($_POST["getDelMun"])){
	$pais=$_POST['codPais'];
	$estado=$_POST['codEstado'];
	$sqDelMun=mysqli_query($con ,"SELECT * FROM dxdelmun where cve_pais='$pais' and cve_estado='$estado' order by cve_delmun");
	CAJselectOptions($jsonOptions,'-1','--Seleccione Municipio--');
	while($arrDelMun=mysqli_fetch_array($sqDelMun)){
		CAJselectOptions($jsonOptions,$arrDelMun['cve_delmun'],$arrDelMun['des_delmun']);
	}
	CSJselectOptions($jsonOptionsString,$jsonOptions,",");
	echo $jsonOptionsString;
}

elseif(isset($_POST["keyColCiu"])){
	$llave=explode("|",$_POST["keyColCiu"]);
	$sqDelMun=mysqli_query($con ,"SELECT * FROM dxcoloni where cve_pais='$llave[0]' and cve_estado='$llave[1]' and cve_delmun=$llave[2] order by des_colonia");
	CAJselectOptions($jsonOptions,'-1','--Seleccione Localidad--');
	while($arrDelMun=mysqli_fetch_array($sqDelMun)){
		CAJselectOptions($jsonOptions,$arrDelMun['cve_col'],$arrDelMun['des_colonia']);
	}
	CSJselectOptions($jsonOptionsString,$jsonOptions,",");
	echo $jsonOptionsString;
}

?>