<?php
 $score=0;
 $goes=0;
 $lastans="";
 $lastq="";
 $to="";
 $explanation="";
 $onlis="";
 $qis="0";
 $pto="Email";
 $ansarr=["\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\"","\" style=\"display:none;\""];
 $questions=["What is the name that we give to the meat we get from pigs?beef|venison| pork|veal",
                       "What is the name of the metal alloy we get from zinc and copper? bronze|stainless steel|sterling silver|pewter|duralumin",
                       "What is the capital of Australia?Ottawa|Vienna| Canberra|Wellington|Sydney",
                       "Who wrote The Tale of Two Cities?Austen| Dickens|Thackeray|Gaskell|Eliot",
                       "How many legs does the Legs of Man have? three|two|four|one",
"Which country had Tijuana Brass? Brazil|Australia|Norway|Morocco|France|Argentina",
"Which country is the land of the brave? United States|Australia|China|Morocco|Argentina",
"Which country was home for William Shakespeare? United Kingdom|Russia|Norway|Morocco|Argentina|Germany",
"Which country has Abu Dhabi as its capital city? United Arab Emirates|Russia|Norway|Morocco|Argentina|Germany",
"Which country here is a member of OPEC? Iran|Russia|Norway|Morocco|Argentina|Germany",
"Which country has a maple leave on its flag?Iran|Russia|Norway|Morocco| Canada|Germany",
"Which country has the fastest sprinters of the early 21st century?Finland|Hungary|Brazil|China|United States|Cuba| Jamaica",
"Which city in Italy has the Colosseum?Milan|Turin| Rome|Florence|Genoa|Naples|Florence",
"Which city has The Gabba?Canberra|Melbourne|Perth|Adelaide|Sydney|Hobart| Brisbane",
"Which city is the Nevernight City?Chongqing|Hong Kong|Tianjin|Beijing| Shanghai|Canton",
"Which city is the highest city in Mexico out of these?Cancun| Mexico City|Guadalajara|Tijuana|Acapulco",
"Where is Hollywood?San Francisco|Chicago| Los Angeles|Boston|New York",
"Which city is the administrative capital of South Africa?Capetown|Durban| Pretoria|Johannesburg",
"Which city had the football team to win the Champion League in 2016?London| Leicester|Liverpool|Birmingham~|Manchester",
"Where is the Kremlim?Leningrad|Novosibirsk| Moscow|Minsk|Vladivostok|Volvograd",
"Where is the biggest centre for the movie industry of India?Chennai|Kolkata|New Delhi| Mumbai|Jaipur|Varanasi",
"Where is the Eiffel Tower?Marseille|Lyon| Paris|Nice|Nantes|Toulouse",
"What is the largest city of Bavaria? Munich|Frankfurt|Hamburg|Cologne|Nuremburg|Stuttgart",
"What is the largest city on Sumatra?Makassar|Semarang|Depok|Bandung|Jakarta| Medan",
"What is the largest city in Nigeria?Benin City| Lagos|Kaduna|Ibadan",
"Where is the Sagrada Familia in Spain?Madrid|Valencia| Barcelona|Seville|Zaragoza",
"Where is the remotest Opera House in Brazil?Brasilia|Rio de Janeiro| Manaus|Recife|Campinas",
"Where is the National Flag Memorial?Buenos Aires| Rosario|La Plata|Salta|Mar del Plata",
"Where is the French more likely to be heard?Montreal| Quebec City|Toronto|Vancouver|Winnipeg",
"Which airport would you get to to see The Giant Staircase?Liverpool|Manchester| Belfast|London|Glasgow",
                       "How many tails does a Manx cat have?one| none|two",
                       "How many teeth does an aardvark have?64|48| none|128", 
                       "Which sea creature has three hearts?sea lion|walrus|blue whale| octopus",
                       "Which instrument has forty-seven strings and seven pedals?piano| harp|harpsichord|dulcimer",
                       "In the traditional rhyme, how many mice were blind? three|two|four|one",
                       "How many pedals do most modern pianos have? three|two|four|one"];
 
 if (strpos(('?' . $_SERVER['QUERY_STRING']), '?ans') !== false) {
   $score=str_replace('+',' ',urldecode($_GET['score']));
   $goes=str_replace('+',' ',urldecode($_GET['goes']));
   $lastans=str_replace('+',' ',urldecode($_GET['lastans']));
   $lastq=str_replace('+',' ',urldecode($_GET['lastq']));
   $to=str_replace('+',' ',urldecode($_GET['to']));
   $vsans=intval(explode('&', explode("?ans", ('?' . $_SERVER['QUERY_STRING']))[1])[0]);
   $sq=str_replace('??','?',('?' . $_SERVER['QUERY_STRING']));
   $sq=str_replace('&goes=' . $goes, '&goes=' . (1 + intval($goes)), $sq);
   $goes='' . (1 + intval($goes));
   if ($vsans == intval($lastans)) {
    if (1 == 1) {
     $sq=str_replace('?ans' . $vsans . '=', '?scorediff=1&ans' . $vsans . '=', $sq);
    } else {
     $sq=str_replace('&score=' . $score, '&score=' . (1 + intval($score)), $score);
     $score='' . (1 + intval($score));
    }
   } else {
    $sq=str_replace('?ans' . $vsans . '=', '?scorediff=0&ans' . $vsans . '=', $sq);
   }
   header('Location: ./simple_inline_html_email_quiz.php' . $sq);
   exit;
 } else if (strpos($_SERVER['QUERY_STRING'], '?score') !== false) {
   $score=str_replace('+',' ',urldecode($_GET['score']));
   $goes=str_replace('+',' ',urldecode($_GET['goes']));
   $lastans=str_replace('+',' ',urldecode($_GET['lastans']));
   $lastq=str_replace('+',' ',urldecode($_GET['lastq']));
   $to=str_replace('+',' ',urldecode($_GET['to']));
   exit;
 }
 
 if (isset($_GET['score']) && isset($_GET['goes']) && isset($_GET['lastans']) && isset($_GET['lastq']) && isset($_GET['to'])) {
   $score=str_replace('+',' ',urldecode($_GET['score']));
   $goes=str_replace('+',' ',urldecode($_GET['goes']));
   $lastans=str_replace('+',' ',urldecode($_GET['lastans']));
   $lastq=str_replace('+',' ',urldecode($_GET['lastq']));
   $to=str_replace('+',' ',urldecode($_GET['to']));
   $scorediff=intval(str_replace('+',' ',urldecode($_GET['scorediff'])));
   $score='' . (intval($score) + intval($scorediff));
   $vsq=0;
   if ($scorediff == 0) {
     for ($i=0; $i<sizeof($questions); $i++) {
       if (strpos($questions[$i], str_replace('+',' ',urldecode($lastq))) !== false) {
         $vsq=$i;
       }
     }
     $explanation=' ... correct answer to ' . str_replace('+',' ',urldecode($lastq)) . ' was ' . trim(explode('|', explode('?', $questions[intval($vsq)])[1])[intval($_GET['lastans'])]);
   }
   $lastq="";
 } else if (isset($_GET['score']) && isset($_GET['goes'])) {
   $score=str_replace('+',' ',urldecode($_GET['score']));
   $goes=str_replace('+',' ',urldecode($_GET['goes']));
   if (isset($_GET['to'])) {
     $to=str_replace('+',' ',urldecode($_GET['to']));
   }
 }
 
 if ($lastq == "") {
   if (isset($_GET['qnum'])) {
   $qis=str_replace('+',' ',urldecode($_GET['qnum']));
   } else {
   $qis=rand(0, (-1 + sizeof($questions)));
   }
   $lastq=explode('?', $questions[$qis])[0] . '?';
   $anslist=explode('|', explode('?', $questions[$qis])[1]);
   for ($i=0; $i<sizeof($anslist); $i++) {
     if (trim($anslist[$i]) != $anslist[$i]) {
       $ansarr[$i]=trim($anslist[$i]);
       $lastans='' . $i;
     } else {
       $ansarr[$i]=trim($anslist[$i]);
     }
   }
   if (!isset($_GET['viaemail'])) {
    $onlis=(strpos(trim($to),"@") === false ? "" : " onload=sendit();");
   } else {
    if (strpos(trim($to),"@") !== false) {
      $pto=$to;
    }
    $to="";
   }
 }
 
 $ajaxis="\n function sendit() {
  var zhr = new XMLHttpRequest();
  var zform=new FormData();
  var mydate = new Date();
  alturl=document.URL.split('?')[0].split('#')[0] + '?goes=' + document.getElementById('goes').value + '&score=' + document.getElementById('score').value + '&to=' + encodeURIComponent(document.getElementById('to').value) + '&viaemail=y&qnum=" . $qis . "';
  altto=document.getElementById('to').value;
  zform.append('inline', mydate.toLocaleString());
  zform.append('to', document.getElementById('to').value);
  zform.append('subj', 'Multiple Choice Quiz');
  zform.append('waitforreturn', 'y');
  zform.append('altmailto', alturl);
  zform.append('htmlis', '<html><body>' + document.body.innerHTML.replace(' onblur=\"dojs(this);\" type=\"text\"',' type=\"hidden\"').replace('=\"to\" value=\"\"', '=\"to\" value=\"' + document.getElementById('to').value + '\"').replace('</form>', '<input type=hidden name=viaemail value=y></input></form>').replace(' ondblclick=', ' data-ondblclick=') + '</body></html>');
  zhr.open('post', '//www.rjmprogramming.com.au/HTMLCSS/emailhtml.php', true);
  zhr.send(zform);
  document.getElementById('to').style.cursor='progress';
  document.getElementById('to').placeholder='Email sent to ' + document.getElementById('to').value;
  document.getElementById('to').title='Double click can deliver the email via the alternative a mailto: link method rather than default inline HTML way.';
  document.getElementById('to').ondblclick=function(){ emailit(); };
  //setTimeout(function(){ document.getElementById('to').value=''; }, 12000);
  setTimeout(function(){ document.getElementById('to').style.cursor='pointer'; }, 12000);
  } 
  
  function emailit() {
         var anchor = document.createElement('a');
         anchor.href = 'mailto:' + altto + '?subject=' + encodeURIComponent('Multiple Choice Email') + '&body=' + encodeURIComponent(alturl);
         anchor.style.display='none';
         anchor.innerHTML='Email';
         anchor.target='_top';
         anchor.click();
  }
  
  ";
 
 
 echo "<html>
 <head><title>Multiple Choice Quiz - RJM Programming - December, 2026</title><script type=\"text/javascript\"> var alturl='', altto='';  function dojs(oinp) { if (oinp.value.indexOf('@') != -1) {  sendit();    }   } " . $ajaxis . "  </script></head>
 <body" . $onlis . ">
 <h1>Multiple Choice Quiz</h1>
 <h3>RJM Programming - December, 2026</h3>
 <h4>Score: " . $score . "/" . $goes . $explanation . "</h4>
 
 <form method=\"GET\" action=\"https://www.rjmprogramming.com.au/simple_inline_html_email_quiz.php\">
 <input type=\"submit\" name=\"ans0\" id=\"ans0\" value=\"" . $ansarr[0] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans1\" id=\"ans1\" value=\"" . $ansarr[1] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans2\" id=\"ans2\" value=\"" . $ansarr[2] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans3\" id=\"ans3\" value=\"" . $ansarr[3] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans4\" id=\"ans4\" value=\"" . $ansarr[4] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans5\" id=\"ans5\" value=\"" . $ansarr[5] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans6\" id=\"ans6\" value=\"" . $ansarr[6] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans7\" id=\"ans7\" value=\"" . $ansarr[7] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans8\" id=\"ans8\" value=\"" . $ansarr[8] . "\"></input>&nbsp;<input type=\"submit\" name=\"ans9\" id=\"ans9\" value=\"" . $ansarr[9] . "\"></input>&nbsp;
 <br><br>
 <input style=\"width:70%;\" type=\"text\" placeholder=\"Question\" id=\"lastq\" name=\"lastq\" value=\"" . $lastq . "\"></input><br>
 <input style=\"width:70%;\" type=\"hidden\" id=\"lastans\" name=\"lastans\" value=\"" . $lastans . "\"></input><br>
 <input style=\"width:70%;\" ondblclick=\"if (this.value == '' && this.placeholder.indexOf('@') != -1) { this.value=this.placeholder.split(' ')[eval(-1 + this.placeholder.split(' ').length)];  }\" title=\"The default method used for delivery is the inline HTML way.\" onblur=\"dojs(this);\" type=\"text\" placeholder=\"" . $pto . "\" id=\"to\" name=\"to\" value=\"" . $to . "\"></input><br>
 <input style=\"width:70%;\" type=\"hidden\" id=\"score\" name=\"score\" value=\"" . $score . "\"></input><br>
 <input style=\"width:70%;\" type=\"hidden\" id=\"goes\" name=\"goes\" value=\"" . $goes . "\"></input><br>

 
 </form>
 </body>
 </html>";
 
 ?>
 
 