<?php 
sleep( 3 );
require_once "../clases/modelo.php";
// no term passed - just exit early with no response
if (empty($_GET['term'])) exit ;
$q = strtolower($_GET["term"]);
// remove slashes if they were magically added
if (get_magic_quotes_gpc()) $q = stripslashes($q);

$q =str_replace('.','',$q);
$q =str_replace('-','',$q);

$clientes = new general();
$cli = new clientes();
$listaclientes = $clientes->getcliente("");
$result = array();


foreach ($listaclientes as $val) {
	$items[str_replace('-','',str_replace('.','',$val['rut']))] = $val['id'];
}

foreach ($items as $key=>$value) {
	if (strpos(strtolower($key), $q) !== false) {
		//$cliente = $clientes->getcliente(rut($key));
		$cliente = $cli->getClientesbyID($value);
		array_push($result, array("id"=>$value, "label"=>rut($key), "value" => strip_tags(rut($key)),"nombre"=>$cliente['nombre'],"apepat"=>$cliente['apepat'],"apemat"=>$cliente['apemat'],"tipo_cliente"=>$cliente['tipo_cliente']));
	}
	if (count($result) > 11)
		break;
}
// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions
function rut( $rut ) {
    return number_format( substr ( $rut, 0 , -1 ) , 0, "", ".") . '-' . substr ( $rut, strlen($rut) -1 , 1 );
}
echo json_encode($result);
?>