<?php
// country_capital_quiz.php
// RJM Programming
// November, 2016
// Country capital quiz ... thanks to TikiWiki regarding flag country list, and to https://www.countries-ofthe-world.com/capitals-of-the-world.html for capitals themselves

// Eg. <tr class="grey"><td>Bahamas</td><td>Nassau</td></tr> ... thanks
$capbits=@file_get_contents("https://www.countries-ofthe-world.com/capitals-of-the-world.html");


$thiscap="";

function countryid($cinis) {
  return str_replace('.', '_', str_replace(' ', '_', str_replace('"', '_', str_replace("'", "_", $cinis))));
}

function mapcapital($cincountry) {
  global $capbits, $thiscap;
  $retcap="";
  $thiscap="";
  $ctrys=explode(" title='", $cincountry);
  if (sizeof($ctrys) > 1) {
    $ctryiss=explode("'", $ctrys[1]);
    $huhbits=explode(">" . $ctryiss[0], $capbits);
    if (sizeof($huhbits) == 1) $huhbits=explode("(" . $ctryiss[0], $capbits);
    if (sizeof($huhbits) > 1) {
      $posthuhbits=explode("</td><td>", $huhbits[1]);
      if (sizeof($posthuhbits) > 1) {
       $preposthuhbits=explode("</td>", $posthuhbits[1]);
       $thiscap=str_replace('.', '_', str_replace(' ', '_', str_replace('"', '_', str_replace("'", "_", str_replace("'", "`", $preposthuhbits[0])))));
       $retcap=str_replace("title='" . $ctryiss[0] . "", "title='" . str_replace("'", "`", $preposthuhbits[0]) . "", $cincountry);
      }
    }
  }
  return $retcap;
}

$num=0;
$countries=[];
$capitals=[];
$countryids=[];
$htmlis="<!doctype html><html><head></head><body onload=choose();><div style='position:absolute;z-index:4;top:0px;left:600px;height:280px;width:400px;overflow:hidden;' id='dgeochart'></div><h1>Country Capital Quiz</h1><h2 id=score>Score: 0 Goes: 0</h2><br><div id=xssuffix></div><br><h3>RJM Programming</h3><h3>November, 2016</h3><br><br><br><table style='width:100%;'><tbody style='background-color:#f0f0f0;'><tr><th style='text-align:right;'>Country</th><th style='text-align:left;'><select onchange=' location.href=\"http://www.rjmprogramming.com.au/PHP/country_\" + this.value.toLowerCase() + \"_quiz.php\"; '><option value=Capital>Capital</option><option value=Currency>Currency</option><option value=Flag>Flag</option></select>?</th></tr><tr><td id=tdcountry style='background-color:pink;text-align:right;'></td><td id=tdcapital style='text-align:left;'><input id=icapital onblur='check(this);' type=text value=></input></td></tr></tbody></table><div id=mydpost style=diplay:none;></div></body></html>";
$scriptis="<meta charset='UTF-8'><title>Country Capital Quiz</title> \n<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'> \n <style> body { background-color:lightblue; }   </style> \n <scri" . "pt type='text/javascript'> var sprefix='', choice=-1, score=0, goes=0, num=0, capitals=[], countries=[], countryids=[];   var uprefix=\"http://www.rjmprogramming.com.au/PHP/GeoChart/geo_chart.php?title=Countries&onclick=y&width=298&height=194&country=Country&popularity=Intensity&guess=&data=\";  function iframeit() { document.getElementById('dgeochart').innerHTML='<iframe name=\"myipost\" id=\"myipost\" style=\"height:260px;width:350px;\" src=\"' + checkforpost(uprefix,null) + '\" title=Context></iframe>';   }  function checkforpost(insg, owhere) {
  var outs=insg, fbits='<form style=display:none; target=\"myipost\" method=\"POST\" action=\"http://www.rjmprogramming.com.au/PHP/GeoChart/geo_chart.php\"><input type=submit id=myspost value=Submit></input><input name=wellinever value=y type=hidden></input></form>';
  if (insg.length > 950) {
    var outarr=insg.split('#')[0].split('?');
    if (outarr.length > 1) {
       var outarrtwo=outarr[1].split('&'), oath;
       for (var im=0; im<outarrtwo.length; im++) {
         oath=outarrtwo[im].split('=');
         fbits=fbits.replace('</form>','<input type=hidden name=' + oath[0] + ' value=\"' + (oath[1]) + '\"></input></form>');
       }
    }
    if (owhere == null) {
      owhere=document.getElementById('mydpost');
      if (owhere == null) {
        if (document.getElementById('mydpost')) {
          document.getElementById('mydpost').innerHTML=fbits;
          setTimeout(andlater,1500); 
          outs=\"#\";
        } else {
          document.body.innerHTML+='<div id=mydpost>' + fbits + '</div>';
          setTimeout(andlater,1500); 
          outs=\"#\";
        }
      }  else {
        owhere.innerHTML=fbits;
        setTimeout(andlater,1500); 
        outs=\"#\";
      }
    } else {
      owhere.innerHTML=fbits;
      setTimeout(andlater,1500); 
      outs=\"#\";
    }
  }
  return outs;
} function andlater() { document.getElementById('myspost').click();  }  function setup(ins) {  uprefix+=sprefix + '%20[~' + encodeURIComponent(ins) + '~,66]%20'; sprefix=','; iframeit();    }  function ourcomp(c1,c2) { var xc1=c1.replace(/_/g,' ').replace(/`/g,' '); var xc2=c2.split('-')[1].replace(/_/g,' ').replace(/`/g,' ');  if (xc2.toLowerCase().indexOf(xc1.toLowerCase()) != -1 && xc1.toLowerCase().length > 3) { return true; } return false; }  \n function check(inv) { if (inv.value != '') { var suffix=''; goes++; if (ourcomp(inv.value,document.getElementById('country').value)) { score++; } else {  suffix=' The country ' + document.getElementById(countryids[choice]).id.replace(/_/g,' ') + ' <BR>represented by <img src=' + document.getElementById(countryids[choice]).src + '></img> <BR>has the capital city ' + document.getElementById(countryids[choice]).title; } if (document.getElementById('ssuffix')) { document.getElementById('ssuffix').innerHTML=suffix; suffix=''; }    document.getElementById('score').innerHTML='Score: ' + score + ' Goes: ' + goes + suffix; choose(); } } \n function choose() {  choice = Math.floor(Math.random() * num); document.getElementById('country').value=countryids[choice] + '-' + capitals[choice];  setup(countryids[choice].replace(/_/g,' ')); document.getElementById('icapital').value=''; setTimeout(fit, 1500);  } function fit() {  document.getElementById('icapital').focus();  } \n</scr" . "ipt><scr" . "ipt type='text/javascript' src='country_quiz.js' defer='defer'></scr" . "ipt>";
$seloneis="<select onchange=\"uprefix+=sprefix + '%20[~' + encodeURIComponent(this.options[this.selectedIndex].text) + '~,66]%20'; sprefix=','; iframeit(); \" id=country><option value=>Country shown below ...</option></select>";
$divis="<div id=flags></div>";

foreach (glob("../tikiwiki/img/flags/*.gif") as $filename) {
  $bits=explode("/", $filename);
  $postbits=explode(".", $bits[-1 + sizeof($bits)]);
  $idis=countryid($postbits[0]);
  $outcapital=mapcapital("<img src='http://www.rjmprogramming.com.au/tikiwiki/img/flags/" . $postbits[0] . ".gif' style='display:none;' id='" . $idis . "' title='" . str_replace("_", " ", $postbits[0]) . "'></img>");
  if ($outcapital != "") {
   $countries[$num]=$postbits[0];
   $scriptis=str_replace("</scr" . "ipt>", " countries.push(\"" . str_replace("_", " ", $postbits[0]) . "\");  countryids.push(\"" . $idis . "\");  capitals.push(\"" . $thiscap . "\"); num++; \n</scr" . "ipt>", $scriptis);
   $countryids[$num]=$idis;
   $seloneis=str_replace("</select>", "<option value=" . $idis . "-" . $thiscap . ">" . str_replace("_", " ", $postbits[0]) . "</option></select>", $seloneis);
   $divis=str_replace("</div>", $outcapital . "</div>", $divis);
   $num++;
  }
}

echo  str_replace("</body>", $divis . "<input type='text' style='margin-left:-6786px;width:1px;height:1px;' value=''></input></body>", str_replace("<head></head>", "<head>" . $scriptis . "</head>", str_replace("</td><td", $seloneis . "</td><td", $htmlis)));

?>

