<?php
header("Content-Type: text/html; charset=iso-8859-1");
ViewResponse::getInstance()->setMethodAjax();

if( $_GET['input']){

	$aUsers = array(
		"Artimes, Juan C.",
		"Almada, Marisa",
		"Archimo, Jimena",
		"Ausman, Carlos",
		"Barreto, Miguel A.",
		"Baller, Esteban",
		"Bartez, Cesar",
		"Basil, Orlando",
		"Beail, Leonardo",
		"Blanco, Luis M.",
		"Bloise, Eugenia",
		"Blant, Nora",
		"Bollin, Carmen",
		"Burnot, Javier",
		"Carden, Presentada",
		"Carter, Mirhna",
		"Charatea, Aanabel",
		"Churchi, Miranda E.",
		"Conkler, Ermindo Juan",
		"Countri, Anabela B.",
		"Courner, Edgardo",
		"Couher, Antonio P.",
		"Craig, Carlos",
		"Cramo, Lautaro",
		"Cressmin, Teodoro",
		"Cressmin, Jesus",
		"Davis, Pdero",
		"Dominguez, Eusevio",
		"Escobar, Marcos",
		"Escobar, Marcelo",
		"Eisenman, Mamerto",
		"Fitzman, Timoteo",
		"Fleming, Francisco",
		"Fuchs, Estela",
		"Fulton, Rosalia A.",
		"Fernandez, Florencia",
		"Fernandez, Fiorela R.",
		"Genes, Raul",
		"Gremol, Marcos",
		"Greis, Juan",
		"Guest, Sara",
		"Hardei, Prisila",
		"Hildan, Ernesto",
		"Hoens, Eulalia",
		"Isasi, Marcelo L.",
		"Javiere, Domingo",
		"Jenco, Mirtha",
		"Jubeno, Berta",
		"Kava, Mariana",
		"Kern, Beatriz",
		"Klockman, Joaquin",
		"Lacon, Jimena",
		"Laurenzi, Leandro",
		"Leiva, Javier O.",
		"Leiva, Karlos M.",
		"Lester, Enzo",
		"Llora, Roxana",
		"Lombardi, Paola",
		"Llorin, Luisa",
		"Mayans, Ernesto J.",
		"Mocule, Bernardo",
		"McAlister, Cristian",
		"Meyers, Hector",
		"Monahan, Penelope",
		"Mull, Orlando P.",
		"Newing, Pedro",
		"Nickolson, Alfredo",
		"Paulin, Javier",
		"Panter, Andrea",
		"Pinneda, Walter",
		"Pratto, Ricardo",
		"Prietto, Estefania",
		"Reamo, Teresa",
		"Rumbaugh, Noelia",
		"Rial, Tania",
		"Sailor, Leonora",
		"Schofield, Danna V.",
		"Schuck, Juan",
		"Scannia, Claudia",
		"Smith, Estela",
		"Smothers, Macos",
		"Stainfor, Mauricio",
		"Stephenson, Paola",
		"Stemart, Hector",
		"Stough, Mariela",
		"Strickland, Benjamin",
		"Sullivan, Griselda",
		"Swink, Stefania",
		"Tavoularis, Teodoro",
		"Taylor, Karla",
		"Thigpen, Amir",
		"Treeby, Jimena",
		"Trevin, Jaime",
		"Waldron, Alberto",
		"Wheeler, Vanesa",
		"Whishaw, Dalberto",
		"Whitehead, Lorena",
		"Wilkins, Daniel",
		"Winre, karina",
		"Woodman, Alejandra",
		"Zanicontor, Jimena"
	);
	
	
	$aInfo = array(
		"Afganistan",
		"Africa del Sur",
		"Alemania",
		"Albania",
		"Andorra",
		"Angola",
		"Arabia Saudita",
		"Argelia", 
		"Argentina", 
		"Belgica", 
		"Benin",
		"Bermudas",
		"Bolivia",
		"Brasil",
		"Canada", 
		"Chile", 
		"Colombia",
		"Costa Rica",
		"Cuba",
		"Egipto",
		"Eslovenia",
		"Francia", 
		"Guatemala", 
		"Holanda",
		"Irlanda",
		"Libano",
		"Mexico",
		"Monaco",
		"Nigeria", 
		"Noruega",
		"Panama",
		"Rumania",
		"Turquia",
		"Ucrana", 
		"Uganda", 
		"Union Europea",
		"Uruguay",
		"Venezuela",
		"Vietman",
		"Yemen", 
		"Zambia", 
		"Formosa",
		"Chaco",
		"Salta",
		"Jujuy",
		"Corrientes",
		"Misiones", 
		"Entre Rios", 
		"Buenos Aires",
		"Santa Fe",
		"Cordoba",
		"Mendoza",
		"Tucuman",
		"Catamarca", 
		"Santiago del Estero",
		"San Luis",
		"Tierra del Fuego",
		"Santa Cruz",
		"Neuquen", 
		"Formosa", 
		"Chaco",
		"Corrientes",
		"Misiones",
		"Buenos Aires",
		"Mexico", 
		"Panama", 
		"Neuquen",
		"Chaco",
		"EE UU",
		"Salta",
		"Santa Cruz",
		"Canada", 
		"Ecuador", 
		"El Salvador",
		"Mexico",
		"Brasil",
		"Colombia",
		"Peru",
		"Paraguay", 
		"Bolivia",
		"Mexico",
		"Puerto Rico",
		"Argentina",
		"Mexico", 
		"Canada", 
		"Frnacia",
		"Alemania",
		"Rusia",
		"China",
		"Japon", 
		"Austria", 
		"Jamaica",
		"Rumania",
		"Colombia",
		"Nueva Zelanda",
		"Reino Unido",
		"Nicaragua", 
		"Nepal", 
		"EE UU"
	);
	
	
	$input = strtolower( $_GET['input'] );
	$len = strlen($input);
	$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
	
	
	$aResults = array();
	$count = 0;
	
	if ($len)
	{
		for ($i=0;$i<count($aUsers);$i++)
		{
			// had to use utf_decode, here
			// not necessary if the results are coming from mysql
			//
			if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
			{
				$count++;
				$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
			}
			
			if ($limit && $count==$limit)
				break;
		}
	}
	
	
	
	
	
	header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header ("Pragma: no-cache"); // HTTP/1.0
	
	sleep(2);
	
	if (isset($_REQUEST['json']))
	{
		header("Content-Type: application/json");
	
		echo "{\"results\": [";
		$arr = array();
		for ($i=0;$i<count($aResults);$i++)
		{
			$arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\":\"".$aResults[$i]['info']."\"}";
		}
		echo implode(", ", $arr);
		echo "]}";
	}
	else
	{
		header("Content-Type: text/xml");

		echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
		for ($i=0;$i<count($aResults);$i++)
		{
			echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
		}
		echo "</results>";
	}

}else{
switch ($type){
  
  case 'nameDoc':

    $o_ObjectBean = new DocenteBean();
    if($value!=""){ 
      $o_ObjectBean->setCodigoDocente($value);
      
      $o_class = new DocenteBL();
      $a_ObjectDocenteBean = $o_class->busquedaDocente($o_ObjectBean);

      if(is_array($a_ObjectDocenteBean) && count($a_ObjectDocenteBean) > 0){
        $tmp = array($nombreElm=>$a_ObjectDocenteBean[0]->getNombre());
      }else{
        $tmp = array($nombreElm=>"<b><font color=red>No se encontro el registro</font></b>");
      }
      
      $scadena = utilJson::generateJsonElment($tmp);
      ////echo "<pre>";
      //print_r($scadena);
      //echo "<pre>";
    }
       
    header("X-JSON: ".$scadena);
  break;
  case 'NombreColaborador':

    $o_ObjectBean = new ColaboradorBean();
    if($value!=""){ 
      $o_ObjectBean->setIdColaborador($value);
      
      $o_class = new ColaboradorBL();
      $a_ObjectColaboradorBean = $o_class->busquedaColaborador($o_ObjectBean);

      if(is_array($a_ObjectColaboradorBean) && count($a_ObjectColaboradorBean) > 0){
        $tmp = array($nombreElm=>$a_ObjectColaboradorBean[0]->getNombreCompleto());
      }else{
        $tmp = array($nombreElm=>"<b><font color=red>No se encontro el registro</font></b>");
      }
      
      $scadena = utilJson::generateJsonElment($tmp);
      ////echo "<pre>";
      //print_r($scadena);
      //echo "<pre>";
    }
       
    header("X-JSON: ".$scadena);
  break;
  
  case 'NombreMercado':

    $o_ObjectBean = new MercadoBean();
    if($value!=""){ 
      $o_ObjectBean->setIdMercado($value);
      
      $o_class = new MercadoBL();
      $a_ObjectMercadoBean = $o_class->busquedaMercado($o_ObjectBean);

      if(is_array($a_ObjectMercadoBean) && count($a_ObjectMercadoBean) > 0){
        $tmp = array($nombreElm=>$a_ObjectMercadoBean[0]->getDescripcion());
      }else{
        $tmp = array($nombreElm=>"<b><font color=red>No se encontro el registro</font></b>");
      }
      
      $scadena = utilJson::generateJsonElment($tmp);
      ////echo "<pre>";
      //print_r($scadena);
      //echo "<pre>";
    }
       
    header("X-JSON: ".$scadena);
  break;
  
    case 'getRepresentante':

    $o_ObjectBean = new MercadoRepresentanteBean();
    if($value!=""){ 
      $value = (int)$value;
      $o_ObjectBean->setIdMercado($value);
      
      $o_class = new MercadoRepresentanteBL();
      $a_ObjectMercadoRepresentanteBean = $o_class->busquedaMercadoRepresentanteDetalle($o_ObjectBean);
		
      
      if(array($a_ObjectMercadoRepresentanteBean) && count($a_ObjectMercadoRepresentanteBean)>0){
        foreach ($a_ObjectMercadoRepresentanteBean as $key => $ObjectMercadoRepresentanteBean){
        	$a_IdColaborador[] = $ObjectMercadoRepresentanteBean->getIdColaborador();
        }
      }else{
      	$a_IdColaborador[0] = null ;
      }
      
      $o_Colaborador = new ColaboradorBean();
      $o_Colaborador->setIdColaboradores($a_IdColaborador);
      
      $o_ColaboradorBL = new ColaboradorBL();
      $a_o_Colaborador = $o_ColaboradorBL->busquedaColaborador($o_Colaborador);
      foreach ($a_o_Colaborador as $iKey => $o_Colaborador){
          $a_ValuesElm[] = $o_Colaborador->getIdColaborador()."_;_".$o_Colaborador->getNombreCompleto();
      }
      
      $a_Resultado     = array();
      foreach ($a_ValuesElm as $key => $value){
          $valueElm = $value;
          $a_Resultado[]  = '["'.$nombreElm.'","'.$valueElm.'"]';
      }
        
      $salida = "[".implode(",",$a_Resultado).']';
    
      header("X-JSON: ".$salida);
    }
       
    
  break;
  
  case 'getRepresentanteUser':

    $o_ObjectBean = new MercadoRepresentanteBean();
    if($value!=""){ 
      $value = (int)$value;
      $o_ObjectBean->setIdMercado($value);
      
      $o_class = new MercadoRepresentanteBL();
      $a_ObjectMercadoRepresentanteBean = $o_class->busquedaMercadoRepresentanteDetalle($o_ObjectBean);
		
      
      if(array($a_ObjectMercadoRepresentanteBean) && count($a_ObjectMercadoRepresentanteBean)>0){
        foreach ($a_ObjectMercadoRepresentanteBean as $key => $ObjectMercadoRepresentanteBean){
        	$a_IdColaborador[] = $ObjectMercadoRepresentanteBean->getIdColaborador();
        }
      }else{
      	$a_IdColaborador[0] = null ;
      }
      
      $o_Colaborador = new ColaboradorBean();
      $o_Colaborador->setIdColaboradores($a_IdColaborador);
      
      $o_ColaboradorBL = new ColaboradorBL();
      $a_o_Colaborador = $o_ColaboradorBL->busquedaColaborador($o_Colaborador);
      foreach ($a_o_Colaborador as $iKey => $o_Colaborador){
          $a_ValuesElm[] = $o_Colaborador->getUsuario()."_;_".$o_Colaborador->getNombreCompleto();
      }
      
      $a_Resultado     = array();
      foreach ($a_ValuesElm as $key => $value){
          $valueElm = $value;
          $a_Resultado[]  = '["'.$nombreElm.'","'.$valueElm.'"]';
      }
        
      $salida = "[".implode(",",$a_Resultado).']';
    
      header("X-JSON: ".$salida);
    }
       
    
  break;
  
  case 'getMercado':

    $o_ObjectBean = new SucursalMercadoBean();
    if($value!=""){ 
      $value = (int)$value;
      $o_ObjectBean->setIdSucursal($value);
      
      $o_class = new SucursalMercadoBL();
      $a_ObjectSucursalMercadoBean = $o_class->busquedaSucursalMercadoDetalle($o_ObjectBean);
		
      
      if(array($a_ObjectSucursalMercadoBean)){
        foreach ($a_ObjectSucursalMercadoBean as $key => $ObjectSucursalMercadoBean){
        	$a_IdMercados[] = $ObjectSucursalMercadoBean->getIdMercado();
        }
      }
      
      $o_Mercado = new MercadoBean();
      if(count($a_IdMercados) == '0') $a_IdMercados['X'] = 'X'; 
      $o_Mercado->setIdMercados($a_IdMercados);
      
      $o_MercadoBL = new MercadoBL();
      $a_o_mercado = $o_MercadoBL->busquedaMercado($o_Mercado);
      foreach ($a_o_mercado as $iKey => $o_mercado){
          $a_ValuesElm[] = $o_mercado->getIdMercado()."_;_".$o_mercado->getDescripcion();
      }
      
      $a_Resultado     = array();
      foreach ($a_ValuesElm as $key => $value){
          $valueElm = $value;
          $a_Resultado[]  = '["'.$nombreElm.'","'.$valueElm.'"]';
      }
        
      $salida = "[".implode(",",$a_Resultado).']';
    
      header("X-JSON: ".$salida);
    }
       
    
  break;
  
  
 
  case 'getEspecialidad':
    
      if(!$value) $value = "99999999";
      $o_especialidadBean = new EspecialidadBean();
      $o_especialidadBean->setFacultadId($value);
  
      $o_class = new EspecialidadBL();
      $a_ObjectBean = $o_class->getEspecialidad($o_especialidadBean);

      if(is_array($a_ObjectBean) && count($a_ObjectBean) > 0){
        foreach ($a_ObjectBean as $iKey => $oValue){
          $a_ValuesElm[] = $oValue->getEspecialidadId()."_;_".$oValue->getEspecialidadDescripcion();
        }
        
        $a_Resultado     = array();
        foreach ($a_ValuesElm as $key => $value){
          $valueElm = $value;
          $a_Resultado[]  = '["'.$nombreElm.'","'.$valueElm.'"]';
        }
        
      }else{
        $a_Resultado = array($nombreElm=>"");
      }
      
      
      $salida = "[".implode(",",$a_Resultado).']';
     /* echo "<pre>";
      print_r($salida);
      echo "<pre>";*/
      header("X-JSON: ".$salida);
    break;
  
}
}
?>