<?php

include_once(dirname(__FILE__).'../../../config/config.inc.php');
include_once(dirname(__FILE__).'/venteflash.php');

$venteflash = new venteflash();
function getProductList()
{
         $id_lang = Configuration::get('PS_LANG_DEFAULT');
         $sql  = Db::getInstance()->Execute('
         SELECT  * FROM `'._DB_PREFIX_.'product_lang`
         WHERE id_lang = "'.($id_lang).'"
         ');
         while($result = mysql_fetch_row($sql))
         {
             $keys[] = $result[0];
             $value[] = $result[8]." ID:".$result[0];
             $return = array_combine($keys,$value);
         }
         return $return;
     }
function getProductList2()
{
         $sql  = Db::getInstance()->Execute('
         SELECT  * FROM `'._DB_PREFIX_.'product_lang`
         ');
         while($result = mysql_fetch_row($sql))
         {
             $keys[] = $result[0];
             $value[] = $result[3];
             $return = array_combine($keys,$value);
         }
         return $return;
}
$aUsers = getProductList();

$aInfo = getProductList2();


	$input = strtolower( $_GET['input'] );
	$len = strlen($input);
	$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;


	$aResults = array();
	$count = 0;

    if ($len)
	{

		foreach($aUsers as $key => $value)
		{

            if (strtolower(substr(utf8_decode($value),0,$len)) == $input)
			{
				$aResults[] = array( "id"=>( $key) ,"value"=>htmlspecialchars( $value), "info"=>htmlspecialchars($aInfo[$key]) );

            }

			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


	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\": \"\"}";
		}
		echo implode(", ", $arr);
		echo "]}";
	}

?>
