<?php
// City Guess Country Game
// RJM Programming
// January, 2016
// Thanks to http://www.webserviceX.NET

$citiesarr=[];
$uniquecountries=",";
$countriesarr=[];

foreach (glob("*cities*.xml") as $filename) {
 $fchunk=file_get_contents($filename);
 $bits=explode("<City>", $fchunk);
 for ($i=1; $i<sizeof($bits); $i++) {
  $thistwobits=explode("<", $bits[$i]);
  $citiesarr[sizeof($citiesarr)]=$thistwobits[0];
  $thistwobits=explode("<Country>", $bits[-1 + $i]);
  $thosetwobits=explode("<", $thistwobits[-1 + sizeof($thistwobits)]);
  $countriesarr[sizeof($countriesarr)]=$thosetwobits[0];
  if (!strstr($uniquecountries, "," . urlencode($thosetwobits[0]) . ",")) {
   $uniquecountries .= urlencode($thosetwobits[0]) . ",";
  }
 }
}


function cmp($a, $b) { // alphabetical comparison method
    $ta=explode("title=", $a);
    $tb=explode("title=", $b);
    if ($ta[sizeof($ta) - 1] == $tb[sizeof($tb) - 1]) {
        return 0;
    }
    return ($ta[sizeof($ta) - 1] < $tb[sizeof($tb) - 1]) ? -1 : 1;
}

function showucountries() {
 global $uniquecountries;
 $ret="<select style='background-color:pink;' id='ucountry' onchange='picked(this.value);'><option value=''>Please select a Country answer below ...</option></select>";
 $ucountriesarr=explode(",", $uniquecountries);
 usort($ucountriesarr, "cmp");
 for ($i=0; $i<sizeof($ucountriesarr); $i++) {
   if (strlen($ucountriesarr[$i]) != 0) $ret=str_replace("</select>", "<option value='" . urldecode($ucountriesarr[$i]) . "'>" . urldecode($ucountriesarr[$i]) . "</option></select>", $ret);
 }
 return $ret;
}

function notshowucities() {
 global $citiesarr, $countriesarr;
 $ret="<select id='ucity' style='display:none;'></select>";
 for ($i=0; $i<sizeof($countriesarr); $i++) {
   $ret=str_replace("</select>", "<option id='o" . $i . "' value='" . ($citiesarr[$i]) . "'>" . ($countriesarr[$i]) . "</option></select>", $ret);
 }
 return $ret;
}

function shownextcity() {
 global $citiesarr, $countriesarr;
 $ret="<s" . "cript type='text/javascript'>\n var urlprefix='https://www.google.com.au/search?tbm=isch&q='; \n var firstgo=true; var goes=0; \n var score=0; \n var lcity=''; \n var oucity=null; var numc=" . sizeof($citiesarr) . ";\n var ans=false; var lastg=-1;\n var ccountry=''; \n function picked(gcountry) { goes++; if (gcountry == ccountry) { score++; document.getElementById('score').innerHTML='Score: ' + score + ' Goes: ' + goes; if (firstgo) { ans=confirm('Well done.  OK does a Google (image) search.'); } } else {  document.getElementById('score').innerHTML='Score: ' + score + ' Goes: ' + goes; ans=confirm('Sorry, but answer is ' + ccountry + '.   OK does a Google (image) search.');  } firstgo=false; if (ans == true) { window.open(urlprefix + encodeURIComponent(lcity), '_blank', 'top=50,left=50,width=400,height=400');  } document.getElementById('ucountry').value=''; getnext(); } \n  function getnext() { if (!oucity) { oucity=document.getElementById('ucity'); } var rnum=lastg; while (rnum == lastg) { rnum=Math.floor(Math.random() * numc); } \n lastg=rnum; var ois=document.getElementById('o' + rnum); \n document.getElementById('ucity').value=ois.value; \n lcity=ois.value; \n document.getElementById('thiscity').value=ois.value; ccountry=ois.innerHTML; \n } \n </s" . "cript>";
 return $ret;
}

echo "<!doctype html><html><head><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'><title>City Guess Country Game</title>" . shownextcity() . "</head><body style='background-color: olive;' onload='setTimeout(getnext,1000);'><h1 align='center'>City Guess Country Game</h1><br><h3 style='color: white;' align='center'>Which Country is City In?</h3><br><h4 style='color: white;' id='score' align='center'>Score: 0 Goes: 0</h4><br><div align='centre' style='background-color: #f0f0f0;'>" . notshowucities() . "<table align='center'><tr><th>City</th><th>Country</th></tr><tr><td><input id='thiscity' type='text' value='' style='width: 300px;background-color:yellow;' onchange='this.value=lcity;'></input></td><td>" . showucountries() . "</td></tr></table></div></body></html>";

?>
