<?php
require('includes/cron_application_top.php');
$ajax_result_schema = "%s###%s";
if(isset($_GET['letters'])){
	$letters = preg_replace("/[^a-z0-9 ]/si","",$_GET['letters']);
	$data = array();
	if(isset($_GET['getCompaniesByLetters'])){
		$res = $conn->CacheExecute( DB_CACHE_EXPIRE,sprintf("select companies_id,name,city,countries_id from %s where name like '%s%%' or company_code like '%s%%'",TABLE_COMPANIES,$letters,$letters));
		if($res){
			foreach($res as $k=>$row){
				// CITY UND COUNTRY NACH ::CITY::
				// WIRD IN ajax-dynamic-list.js als mouse-over-title geparsed
				//$text_value = $row['name'].'::TITLE::'.$row['city'].', '.xtc_get_country_name($row['countries_id']);
				//$data[] = sprintf($ajax_result_schema,$row['companies_id'],$text_value);
				$country_data = yes_get_country_data($row['countries_id']);
				$text_value = sprintf('%s, %s (%s)',$row['name'],$row['city'],$country_data['countries_iso_code_3']);
				$data[] = sprintf($ajax_result_schema,$row['companies_id'],$text_value);
			}
		}
	}
	if(isset($_GET['getAirportsByLetters'])){
		$res = $conn->CacheExecute( DB_CACHE_EXPIRE,sprintf("select airports_id,name from %s where name like '%s%%' or code like '%s%%'",TABLE_AIRPORTS,$letters,$letters));
		if($res){
			foreach($res as $k=>$row){
				$data[] = sprintf($ajax_result_schema,$row['airports_id'],$row['name']);
			}
		}
	}
	if(isset($_GET['getAirlinesByLetters'])){
		$res = $conn->CacheExecute( DB_CACHE_EXPIRE,sprintf("select airlines_id,name from %s where name like '%s%%' or code like '%s%%'",TABLE_AIRLINES,$letters,$letters));
		if($res){
			foreach($res as $k=>$row){
				$data[] = sprintf($ajax_result_schema,$row['airlines_id'],$row['name']);
			}
		}
	}
	if(isset($_GET['getAirportsCodeByLetters'])){
		$res = $conn->CacheExecute( DB_CACHE_EXPIRE,sprintf("select code,name from %s where name like '%s%%'",TABLE_AIRPORTS,$letters));
		if($res){
			foreach($res as $k=>$row){
				$data[] = sprintf($ajax_result_schema,$row['code'],$row['name']);
			}
		}
	}
	
	// OUTPUT
	if(sizeOf($data)>0){
		$string = implode('|',$data);
		echo $string;
	}
}