<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 
set_magic_quotes_runtime(0);

include("include/dbcommon.php");
include("include/pf_proceso_variables.php");


$conn=db_connect();	

$response = array();
$fields = array();

$strSQL = "SELECT * FROM ".AddTableWrappers($strOriginalTableName)." WHERE 1<0";
$rs=db_query($strSQL,$conn);

for($i=0; $i < db_numfields($rs); $i++) {
	$fields[] = db_fieldname($rs,$i);
}

if (isset($_GET['searchFor']) && postvalue('searchFor') != '') {

	$searchFor = postvalue('searchFor');
	$searchField = postvalue('searchField');
	$whereCondition = ($suggestAllContent) ? " like '%".str_replace("'","''",$searchFor)."%'" : " like '".str_replace("'","''",$searchFor)."%'";
	
		if ( $searchField == '' ) 
	{
		$searchField="fecha_inicio";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="id_proceso";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="id_camara";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_fin";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_ultcam";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="estado";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="frecuencia";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="creador_inicial";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_inicio_giro1";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_fin_giro1";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_inicio_giro2";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_fin_giro2";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_inicio_giro3";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="fecha_fin_giro3";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="Pallets";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="Puchos";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="bins";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="origen";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="destino";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="id_especie";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="id_envase";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="condicion_inicial";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="num_giros";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="creador_final";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
		$searchField="condicion_final";
		if(in_array($searchField, $fields)) {
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}
		}
	} else 
	{
	  if(in_array($searchField, $fields)) {
		$origfield=GetFieldByGoodFieldName($searchField);
		if($origfield!="") { $searchField=$origfield; }
		$type=GetFieldType($searchField);
		if((IsNumberType($type) || IsCharType($type)) && !IsTextType($type) && CheckFieldPermissions($searchField)) 
		{
			$strSQL = "SELECT DISTINCT TOP 10 ".AddFieldWrappers($searchField)." FROM ".AddTableWrappers($strOriginalTableName)." WHERE ".AddFieldWrappers($searchField).$whereCondition." ORDER BY 1";		
			$rs=db_query($strSQL,$conn);
			while ($row = db_fetch_array($rs)) {
				$pos = strpos($row[$searchField],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[$searchField],0,$pos);
				} else {
					$response[] = $row[$searchField];
				}
			}		
		}
	  }
	}
	db_close($conn);
}

sort($response);

if ($output = array_chunk(array_unique($response),10)) {
	foreach( $output[0] as $value ) {
		echo ($suggestAllContent) ? preg_replace("/(".$searchFor.")/i","<b>$1</b>",substr($value,0,50),1)."\n" : substr($value,0,50)."\n";
	}
}
?>