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

$egoes="0";
$escore="0";
$emdef="";

if (isset($_POST['thiscity']) && isset($_POST['ucountry']) && isset($_POST['eanswer'])) {
  if ($_POST['ucountry'] == $_POST['eanswer']) {
    $egoes="1  Well done!  " . str_replace("+"," ",urldecode($_POST['thiscity'])) . " is indeed in " . str_replace("+"," ",urldecode($_POST['eanswer'])) . ".";
    $escore="1";
  } else {
    $egoes="1  Sorry, <a title='Google image search' style=cursor:pointer;text-decoration:underline; onclick=woit(this.innerHTML);>" . str_replace("+"," ",urldecode($_POST['thiscity'])) . "</a> is in " . str_replace("+"," ",urldecode($_POST['eanswer'])) . " but is not in " . str_replace("+"," ",urldecode($_POST['ucountry'])) . ".";
  }
  if (isset($_POST['emailfrom'])) {
    $emdef=str_replace("+"," ",urldecode($_POST['emailfrom']));
  }
} else if (isset($_GET['thiscity']) && isset($_GET['ucountry']) && isset($_GET['eanswer'])) {
  if ($_GET['ucountry'] == $_GET['eanswer']) {
    $egoes="1  Well done!  " . str_replace("+"," ",urldecode($_GET['thiscity'])) . " is indeed in " . str_replace("+"," ",urldecode($_GET['eanswer'])) . ".";
    $escore="1";
  } else {
    $egoes="1  Sorry, <a title='Google image search' style=cursor:pointer;text-decoration:underline; onclick=woit(this.innerHTML);>" . str_replace("+"," ",urldecode($_GET['thiscity'])) . "</a> is in " . str_replace("+"," ",urldecode($_GET['eanswer'])) . " but is not in " . str_replace("+"," ",urldecode($_GET['ucountry'])) . ".";
  }
  if (isset($_GET['emailfrom'])) {
    $emdef=str_replace("+"," ",urldecode($_GET['emailfrom']));
  }
}

$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;' name='ucountry' 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 name='ucity' 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, $egoes, $escore, $emdef;
 $ret="<s" . "cript type='text/javascript'>\n var urlprefix='//www.google.com.au/search?tbm=isch&q='; \n var emdef='" . $emdef . "'; \n var firstgo=true; \n var goes=" . explode(" ", $egoes)[0] . "; \n var score=" . $escore . "; \n var lcity=''; \n var oucity=null; var numc=" . sizeof($citiesarr) . ";\n var ans=false; var lastg=-1;\n var ccountry=''; \n 
 function askemail() {
 if (document.getElementById('emailto').value.indexOf('@') == -1) {
 var emis=prompt('Please enter Email To address?',emdef);
 if (emis) {
 if (emis.indexOf('@') != -1) {
 document.getElementById('emailto').value=emis;
 }
 }
 }
 if (document.getElementById('emailto').value.indexOf('@') != -1) {
 document.getElementById('htmlis').value=document.getElementById('htmlis').value.replace(' value=\" \"', ' value=\"' + emis + '\"');
 document.getElementById('contact').click();
 }
 }
 function woit(incity) {
 window.open(urlprefix + encodeURIComponent(incity), '_blank', 'top=50,left=50,width=400,height=400');  
 }
 function gethtmlready(incval) {
 var huhsel='<input' + document.getElementById('cgcform').outerHTML.split('<input')[eval(-1 + document.getElementById('cgcform').outerHTML.split('<input').length)].split('>')[0] + '>';
 document.getElementById('htmlis').value='<html><body><h1>City Guess Country Game</h1><h3>Which Country is City In?</h3>' + document.getElementById('cgcform').outerHTML.replace(' action=\"//',' action=\"https://').replace(' value=\"\"',' value=\"' + incval + '\"').replace(huhsel + '</input>','').replace(huhsel,'').replace('display:none','display:inline-block').replace('>','><input name=eanswer type=hidden value=\"' + ccountry + '\"></input><input name=emailfrom type=hidden value=\"' + ' ' + '\"></input>') + '</body></html>';
 }
 function picked(gcountry) { 
 if (document.getElementById('bemail')) { 
 document.getElementById('bemail').style.display='inline-block'; 
 }  
 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 (document.getElementById('bemail')) { 
 document.getElementById('bemail').style.display='inline-block'; 
 }  
 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; 
 gethtmlready(ois.value); \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: " . $escore . " Goes: " . $egoes . "</h4><br>
 <div align='centre' style='background-color: #f0f0f0;'>
 <form target=iemail style=display:none; action='//www.rjmprogramming.com.au/HTMLCSS/emailhtml.php' method=POST>
 <input type=hidden name=inline value=''></input>
 <input type=hidden name=emailto id=emailto value=''></input>
 <input type=hidden name=subject id=subject value='City Guess Country Game'></input>
 <textarea style=display:none; name=htmlis id=htmlis value=''></textarea>
 <input type=submit style=display:none; id=contact value=Email></input>
 </form>" . notshowucities() . "<form target=_top id=cgcform action='//www.rjmprogramming.com.au/Games/CityGuessCountry/city_guess.php' method=GET><table align='center'><tr><th>City</th><th>Country</th></tr><tr><td><input name='thiscity' id='thiscity' type='text' value='' style='width: 300px;background-color:yellow;' onchange='this.value=lcity;'></input></td><td>" . showucountries() . "&nbsp;<input name=check style=display:none;background-color:pink; value=Check type=submit></input>&nbsp;<input onclick=askemail(); name=email id=bemail style=display:none;background-color:yellow; value=Email type=button></input></form></td></tr></table></div>
 <iframe name=iemail src=//www.rjmprogramming.com.au/HTMLCSS/emailhtml.php style=display:none;></iframe>
 </body></html>";

?>
