<?php
include("includes/site_info.php");
mysql_query("SET character_set_results=utf8");

$q = strtolower($_GET["q"]);
if (!$q) return;
$cid = strtolower($_GET["cid"]);
$sid = strtolower($_GET["sid"]);

	switch ($_REQUEST["submit"]) {
	case "country"; 
		country($q);
		break;
	case "state"; 
		state($q, $cid);
		break;
	case "city"; 
		city($q, $sid);
		break;
	default; 
		echo "";
		break;
}

function country($q) {
$countryr=mysql_query("select CountryId, Country from Geo_Countries where Country like '$q%' order by Country");
while($rst= mysql_fetch_array($countryr))
{
	echo $rst["Country"]."\n";
}
}

function state($q, $cid) {
$stater=mysql_query("select Region from Geo_Regions r, Geo_Countries c where Region like '%$q%' and Country='$cid' and r.CountryId=c.CountryId order by Region");
while($rst= mysql_fetch_array($stater))
{
	echo $rst["Region"]."\n";
}
}

function city($q, $sid) {
list($region, $country) = explode(":", $sid);
$cityr=mysql_query("select City from Geo_Cities ci, Geo_Regions r, Geo_Countries c where City like '%$q%' and Country='$country' and Region='$region' and r.CountryId=c.CountryId and ci.RegionId=r.RegionId order by City");
while($rst= mysql_fetch_array($cityr))
{
	echo $rst["City"]."\n";
}
}
?>