<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once (PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
include_once (PATH_TO_ROOT."CRM/class/class.checkString.php");

$LangPack = new LanguagePack($db, $aLanguage);

$table = "_TelephoneCodeList";

if($_POST['action'] == 'get'){
	//Assistent
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".$table WHERE codeColumn = '". StringChecker::Check($_POST['zip']) ."'";
	$res = $db->SQLexecute($sql);
	$data = $db->fetchAssoc($res);
	
	echo $data['textColumn'];
}else if($_POST['action'] == "getCity"){
	if(isset($_POST['query']) && $_POST['query'] != null){
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".$table WHERE textColumn LIKE '".StringChecker::Check($_POST['query'])."%'";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$data[] = $row;
		}
		$rez['success'] = true;
		$rez['results'] = sizeof($data);
		$rez['rows'] = $data;
		echo json_encode($rez);
	}else{
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".$table";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$data[] = $row;
		}
		$rez['success'] = true;
		$rez['results'] = sizeof($data);
		$rez['rows'] = $data;
		echo json_encode($rez);
	}
}
