<?php
  $tablehtml = "<table cellpadding='5'>";
  $numtoguess = 0;
  $score = "0";
  $outof = "0";
  $theword = "";
  $theunderline = array("","","","");
  $theword = array("","","","");
  $inorder = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
  $outoforder = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
  $sparewsarray = array("", // "01F311", "0026F3", "0026BD", "00263C", "00263D", "00263E", "01F311", "0026F3", "0026BD", "0026f5", 
  "F09F91A0", "F09F9193", "F09F9098", "F09F9092", "F09F8E89",
  "F09F8DB0", "F09F8DA9", "F09F8DA8", "F09F8DA7", "F09F8DA3",
  "F09F8DB1", "F09F8E88", "F09F8EA7", "F09F8EA8", "F09F8EB5",
  "F09F8F88", "F09F9099", "F09F909E", "F09F90B6", "F09F90BC",
  "F09F90BE", "F09F938A", "F09F93A2", "F09F91BC", "F09F90AB",
  "F09F90A8", "F09F9097", "F09F8F80", "F09F8EB7", "F09F8EA9",
  "F09F8DAE", "F09F8D92", "F09F8F80", "F09F8C88", "F09F9AB2",
  "F09F9AA2", "F09F9A99", "F09F9A87", "F09F9A8F", "F09F9AA7",
  "F09F9AAA", "F09F9A83", "F09F9A87", "F09F9881", "F09F9882",
  "F09F98B8", "F09F9A92", "F09F9A87", "F09F9B80", "F09F8CB4",
  "F09F8CB5", "F09F8CB7", "F09F8C9F", "F09F8CBD", "F09F8CBE",
  "F09F8D84", "F09F8DB7", "F09F8D8F", "F09F8D9F", "F09F8DB5",
  "F09F8C89", "F09F8C8A", "F09F8C8B", "F09F8C87", "F09F8C86",
  "F09F8C81", "F09F8C82", "F09F8C82", "F09F8C83", "F09F8C84",
  "E29BAA", "E29BB2", "E29BBA", "E29B83", "F09F9A80",
  "F09F97BB", "F09F97BC", "F09F97BD", "F09F97BF", "F09F90A7",
  "F09F998B", "F09F9988", "F09F9989", "F09F998C", "F09F9896"); 
  $wsarray = array("", // "01F311", "0026F3", "0026BD", "00263C", "00263D", "00263E", "01F311", "0026F3", "0026BD", "0026f5", 
  "\xF0\x9F\x91\xA0", "\xF0\x9F\x91\x93", "\xF0\x9F\x90\x98", "\xF0\x9F\x90\x92", "\xF0\x9F\x8E\x89",
  "\xF0\x9F\x8D\xB0", "\xF0\x9F\x8D\xA9", "\xF0\x9F\x8D\xA8", "\xF0\x9F\x8D\xA7", "\xF0\x9F\x8D\xA3",
  "\xF0\x9F\x8D\xB1", "\xF0\x9F\x8E\x88", "\xF0\x9F\x8E\xA7", "\xF0\x9F\x8E\xA8", "\xF0\x9F\x8E\xB5",
  "\xF0\x9F\x8F\x88", "\xF0\x9F\x90\x99", "\xF0\x9F\x90\x9E", "\xF0\x9F\x90\xB6", "\xF0\x9F\x90\xBC",
  "\xF0\x9F\x90\xBE", "\xF0\x9F\x93\x8A", "\xF0\x9F\x93\xA2", "\xF0\x9F\x91\xBC", "\xF0\x9F\x90\xAB",
  "\xF0\x9F\x90\xA8", "\xF0\x9F\x90\x97", "\xF0\x9F\x8F\x80", "\xF0\x9F\x8E\xB7", "\xF0\x9F\x8E\xA9",
  "\xF0\x9F\x8D\xAE", "\xF0\x9F\x8D\x92", "\xF0\x9F\x8F\x80", "\xF0\x9F\x8C\x88", "\xF0\x9F\x9A\xB2",
  "\xF0\x9F\x9A\xA2", "\xF0\x9F\x9A\x99", "\xF0\x9F\x9A\x87", "\xF0\x9F\x9A\x8F", "\xF0\x9F\x9A\xA7",
  "\xF0\x9F\x9A\xAA", "\xF0\x9F\x9A\x83", "\xF0\x9F\x9A\x87", "\xF0\x9F\x98\x81", "\xF0\x9F\x98\x82",
  "\xF0\x9F\x98\xB8", "\xF0\x9F\x9A\x92", "\xF0\x9F\x9A\x87", "\xF0\x9F\x9B\x80", "\xF0\x9F\x8C\xB4",
  "\xF0\x9F\x8C\xB5", "\xF0\x9F\x8C\xB7", "\xF0\x9F\x8C\x9F", "\xF0\x9F\x8C\xBD", "\xF0\x9F\x8C\xBE",
  "\xF0\x9F\x8D\x84", "\xF0\x9F\x8D\xB7", "\xF0\x9F\x8D\x8F", "\xF0\x9F\x8D\x9F", "\xF0\x9F\x8D\xB5",
  "\xF0\x9F\x8C\x89", "\xF0\x9F\x8C\x8A", "\xF0\x9F\x8C\x8B", "\xF0\x9F\x8C\x87", "\xF0\x9F\x8C\x86",
  "\xF0\x9F\x8C\x81", "\xF0\x9F\x8C\x82", "\xF0\x9F\x8C\x82", "\xF0\x9F\x8C\x83", "\xF0\x9F\x8C\x84",
  "\xE2\x9B\xAA", "\xE2\x9B\xB2", "\xE2\x9B\xBA", "\xE2\x9B\x83", "\xF0\x9F\x9A\x80",
  "\xF0\x9F\x97\xBB", "\xF0\x9F\x97\xBC", "\xF0\x9F\x97\xBD", "\xF0\x9F\x97\xBF", "\xF0\x9F\x90\xA7",
  "\xF0\x9F\x99\x8B", "\xF0\x9F\x99\x88", "\xF0\x9F\x99\x89", "\xF0\x9F\x99\x8C", "\xF0\x9F\x98\x96"); 
  $azorder = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
  $eazorder = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
  $zazorder = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
  $wssofar = ";";
  $ws = rand(1, (sizeof($wsarray) - 2));
  for ($itr=0; $itr<sizeof($eazorder); $itr++) {
    while (strpos($wssofar, ";" . $ws . ";") !== false || $ws == 0) {
      $ws = rand(1, (sizeof($wsarray) - 2));
    }
    $eazorder[$itr] = $wsarray[$ws];
    $zazorder[$itr] = $sparewsarray[$ws];
    $wssofar .= ($ws . ";");
  }
  $donottouch = rand(0,25);
  if (isset($_POST['word1of6'])) {
    $theword[0] = $_POST['word1of6'];
    $theword[1] = $_POST['word2of6'];
    $theword[2] = $_POST['word3of6'];
    $theword[3] = $_POST['word4of6'];
    $theword[4] = $_POST['word5of6'];
    $theword[5] = $_POST['word6of6'];
  } else if (isset($_GET['huhlarge'])) {
    if (strpos($_GET['huhlarge'], "~") !== false) {
    $theword = explode("~", $_GET['huhlarge']);
    } else {
    $theword[0] = $_GET['huhlarge'];
    }
  } else {
    $theword[0] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y"));
    $theword[1] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y"));
    $theword[2] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y"));
    $theword[3] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y"));
    $theword[4] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y"));
    $theword[5] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y"));
  }
  
  if (isset($_GET['okguesslist'])) {
    if (strpos($_GET['okguesslist'], "~") !== false) {
    $okguesslist = explode("~", $_GET['okguesslist']);
    } else {
    $okguesslist = array($_GET['okguesslist']);
    }
  } else {
    $okguesslist = array($azorder[$donottouch]);
  }
  

  
  for ($m=0; $m<26; $m++) {
    $asis = 0;
    for ($k=0; $k<sizeof($okguesslist); $k++) {
          $proposed = $k;
          if (strtolower($azorder[$m]) == strtolower($okguesslist[$k])) {
            $azorder[$m] = strtolower($azorder[$m]);
            $asis = 1;
            $outoforder[$m] = $inorder[$m];
          }
    }
    if ($asis == 0) {
          if (1 == 1) {
            $ok = 0;
            while ($ok == 0) {
             $ok = 1;
             $proposed = rand(0,25);
             if ($proposed == $m) $ok = 0;
             for ($kk=0; $kk<sizeof($okguesslist); $kk++) {
               if (strtolower($azorder[$proposed]) == strtolower($okguesslist[$kk])) $ok = 0;
             }
             for ($kk=0; $kk<$m; $kk++) {
               if ($outoforder[$kk] == $proposed) $ok = 0;
             }
            }
            $outoforder[$m] = $proposed;  
          }
    }
  }
  
  for ($j=0; $j<6; $j++) {
   $tablehtml .= "<tr>";
   for ($i=0; $i<strlen($theword[$j]); $i++) {
    $asis = 0;
    for ($k=0; $k<sizeof($okguesslist); $k++) {
          if (strtolower(substr($theword[$j], $i, 1)) == strtolower($okguesslist[$k])) {
            $asis = 1;
          }
    }
    $tablehtml .= "<td id='" . ($j + 1) . ($i + 1) . "' align='center' style='background-color:pink;'>";
    if ($asis == 1) {
          $theunderline[$j] .= strtolower(substr($theword[$j], $i, 1));
          $tablehtml .= "<span style='color:red; font-size:20px;'><b><i>" . strtolower(substr($theword[$j], $i, 1)) . "</i></b></span>";
    } else {
          $numtoguess = $numtoguess + 1; 
          $kk = 0;
          for ($m=0; $m<26; $m++) {
             if (strtolower(substr($theword[$j], $i, 1)) == strtolower($azorder[$m])) $kk = $m;
          }
          $theunderline[$j] .= strtoupper($azorder[$outoforder[$kk]]);
          $tablehtml .= '<select id="guess' . ($j + 1) . ($i + 1) . '" name="guess' . ($j + 1) . ($i + 1) . '" onchange=" guessmade(this,' . ($j + 1) . ',' . ($i + 1) . ',1); " style=" text-align:center; font-size:20px; background-color: yellow; ">';
          $ztablehtml = "<option value=''>Letters</option>"; // 
          $ztablehtml .= '<option value="a">a</option>';
          $ztablehtml .= '<option value="b">b</option>';
          $ztablehtml .= '<option value="c">c</option>';
          $ztablehtml .= '<option value="d">d</option>';
          $ztablehtml .= '<option value="e">e</option>';
          $ztablehtml .= '<option value="f">f</option>';
          $ztablehtml .= '<option value="g">g</option>';
          $ztablehtml .= '<option value="h">h</option>';
          $ztablehtml .= '<option value="i">i</option>';
          $ztablehtml .= '<option value="j">j</option>';
          $ztablehtml .= '<option value="k">k</option>';
          $ztablehtml .= '<option value="l">l</option>';
          $ztablehtml .= '<option value="m">m</option>';
          $ztablehtml .= '<option value="n">n</option>';
          $ztablehtml .= '<option value="o">o</option>';
          $ztablehtml .= '<option value="p">p</option>';
          $ztablehtml .= '<option value="q">q</option>';
          $ztablehtml .= '<option value="r">r</option>';
          $ztablehtml .= '<option value="s">s</option>';
          $ztablehtml .= '<option value="t">t</option>';
          $ztablehtml .= '<option value="u">u</option>';
          $ztablehtml .= '<option value="v">v</option>';
          $ztablehtml .= '<option value="w">w</option>';
          $ztablehtml .= '<option value="x">x</option>';
          $ztablehtml .= '<option value="y">y</option>';
          $ztablehtml .= '<option value="z">z</option>';
          if (1 == 1) {
          //$ztablehtml = str_replace("'>Letters", $zazorder[$outoforder[$kk]] . "'>" . $eazorder[$outoforder[$kk]], $ztablehtml);
          $ztablehtml = str_replace("Letters", $eazorder[$outoforder[$kk]], $ztablehtml);
          } else {
          $ztablehtml = str_replace('"' . strtolower($azorder[$outoforder[$kk]]) . '">', '"' . strtolower($azorder[$outoforder[$kk]]) . '" selected="selected">', $ztablehtml);
          }
          $ztablehtml = str_replace('"' . strtolower(substr($theword[$j], $i, 1)) . '"', '"' . strtoupper(substr($theword[$j], $i, 1)) . '"', $ztablehtml);
          $tablehtml .= $ztablehtml;
          $tablehtml .= '</select><input type="hidden" id="ih' . ($j + 1) . ($i + 1) . '" value="' . strtolower($azorder[$outoforder[$kk]]) . '"></input>';
    }
    $tablehtml .= "</td>";
   }
   $tablehtml .= "</tr>";
  }
  $tablehtml .= "</table>";
  if (isset($_GET['score'])) {
    $score=$_GET['score'];
  }
  if (isset($_GET['outof'])) {
    $outof=$_GET['outof'];
  }
  if (isset($_POST['score'])) {
    $score=$_POST['score'];
  }
  if (isset($_POST['outof'])) {
    $outof=$_POST['outof'];
  }
echo "\n" . '<!doctype html>' . "\n";
echo "\n" . '<html>' . "\n";
echo "\n" . '<head>' . "\n";
echo "\n" . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . "\n";
echo "\n" . '<link href="//www.rjmprogramming.com.au/PHP/emboss_h1.css" rel="stylesheet" type="text/css">' . "\n";
echo "\n" .  '<title>Decoding Pictogram Words - RJM Programming - 2015</title>' . "\n";
echo "\n" . '<script type="text/javascript">' . "\n";
echo "\n" . 'var overgoes=0;' . "\n";
echo "\n" . 'var score=0;' . "\n";
echo "\n" . 'var outof=0;' . "\n";
echo "\n" . 'var goes=0;' . "\n";
echo "\n" . 'var guess="";' . "\n";
echo "\n" . 'var lettersdone="";' . "\n";
echo "\n numguess = " . $numtoguess . "; \n";

  $huhl = "\n var ooo = '";
  for ($m=0; $m<26; $m++) {
    $huhl .= " " . $outoforder[$m];
  }
  $huhl .= "'; \n";
  //echo $huhl;
  if (isset($_GET['score'])) {
    echo "\n score=" . $_GET['score'] . "; \n";
  }
  if (isset($_GET['outof'])) {
    echo "\n outof=" . $_GET['outof'] . "; \n";
  }
?>

function golater() {
    location.href = "./decodepictowords.php?score=" + score + "&outof=" + outof;
}

function goagain(pshow) {
    outof = outof + 1;
    if (pshow != 0) {
      document.getElementById('w0of6').style.display = 'block';
      document.getElementById('w1of6').style.display = 'block';
      document.getElementById('w2of6').style.display = 'block';
      document.getElementById('w3of6').style.display = 'block';
      document.getElementById('w4of6').style.display = 'block';
      document.getElementById('w5of6').style.display = 'block';
      document.getElementById('w6of6').style.display = 'block';
      setInterval(golater, pshow);
    } else {
      location.href = "./decodepictowords.php?score=" + score + "&outof=" + outof;
    }
}

function guessmade(thiso, wordi, posi, firstcall) {
  if (thiso.style.backgroundColor == 'red') {
    var ddah = "word" + wordi + "of6";
    var dodah = document.getElementById(ddah);
    var dodahv = dodah.value;
    //alert('here ' + dodah.value);
    //alert(thiso.value + " vs " + dodahv.substring((posi-1),posi).toUpperCase());
    thiso.value = dodahv.substring((posi-1),posi).toUpperCase();
  } else if (thiso.value == "") {
    thiso.value = document.getElementById("ih" + wordi + posi).value;
  } else if (thiso.value == thiso.value.toUpperCase()) {
    if (thiso.style.backgroundColor != 'red') {
       numguess = numguess - 1;
       //alert("numguess=" + numguess);
       thiso.style.backgroundColor = 'red';
       if (firstcall == 1) {
       var ii, jj, kk, sval;
       var origval = document.getElementById("ih" + wordi + posi).value;
       //alert("origval.substring((kk-1),kk)=" + origval);
       for (ii=1; ii<=6; ii++) {
         jj = document.getElementById("word" + ii + "of6").value.length;
       //alert("jj=" + jj);
         for (kk=1; kk<=jj; kk++) {
            sval = document.getElementById("guess" + ii + kk);
       //alert("sval.value=" + sval.value + " for guess" + ii + kk);
            if (sval != null) {
            if (sval.value == origval && sval.style.backgroundColor != 'red') {
       //alert("candidate=" + thiso.value + " for guess" + ii + kk);
              sval.value = thiso.value;
              guessmade(sval, ii, kk, 0);
            }
            }
         } 
       }
       }
    }
    if (numguess == 0) {
      score = score + 1;
      if (1 == 1) {
        goagain(9000);
      } else {
        outof = outof + 1;
        location.href = "./decodepictowords.php?score=" + score + "&outof=" + outof;
      }
    }
  } else {
    thiso.value = document.getElementById("ih" + wordi + posi).value;
  }
  
}
</script>
</head>
<?php
 if (isset($_GET['badluck'])) {
  echo "\n <body style=\"background-color: cyan;\"> \n";
 } else {
  echo "\n <body style=\"background-color: cyan;\"> \n";
 }
?>
<div align="center">
<h1 id="gamename">Decoding Pictogram Words</h1>
<?php
  global $score, $outof;
  if ($outof < $score) {
    $outof = $score;
  }
  echo "\n <h2 id='scoreis'>Score: " . $score . " / " . $outof . "</h2> \n";
  echo "\n" . '<form id="gform" method="POST" action="http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . '/PHP/surprise.php">' . "\n";
?>
<input type="hidden" id="sword" name="sword" value="<?php echo $theword[0]; ?>" />
<input type="hidden" id="word1of6" name="word1of6" value="<?php echo $theword[0]; ?>" />
<input type="hidden" id="word2of6" name="word2of6" value="<?php echo $theword[1]; ?>" />
<input type="hidden" id="word3of6" name="word3of6" value="<?php echo $theword[2]; ?>" />
<input type="hidden" id="word4of6" name="word4of6" value="<?php echo $theword[3]; ?>" />
<input type="hidden" id="word5of6" name="word5of6" value="<?php echo $theword[4]; ?>" />
<input type="hidden" id="word6of6" name="word6of6" value="<?php echo $theword[5]; ?>" />
<input type="hidden" id="score" name="score" value="<?php echo $score; ?>" />
<input type="hidden" id="outof" name="outof" value="<?php echo $outof; ?>" />
<input type="text" onclick=" window.open('http://www.thefreedictionary.com/<?php echo $theword[0]; ?>','<?php echo $theword[0]; ?>',' height=360,width=900,top=370,left=400 '); " style="display:none;" id="w1of6" name="w1of6" value="<?php echo $theword[0]; ?>" />
<input type="text" onclick=" window.open('http://www.thefreedictionary.com/<?php echo $theword[1]; ?>','<?php echo $theword[1]; ?>',' height=360,width=900,top=370,left=400 '); "  style="display:none;" id="w2of6" name="w2of6" value="<?php echo $theword[1]; ?>" />
<input type="text" onclick=" window.open('http://www.thefreedictionary.com/<?php echo $theword[2]; ?>','<?php echo $theword[2]; ?>',' height=360,width=900,top=370,left=400 '); "  style="display:none;" id="w3of6" name="w3of6" value="<?php echo $theword[2]; ?>" />
<input type="text" onclick=" window.open('http://www.thefreedictionary.com/<?php echo $theword[3]; ?>','<?php echo $theword[3]; ?>',' height=360,width=900,top=370,left=400 '); "  style="display:none;" id="w4of6" name="w4of6" value="<?php echo $theword[3]; ?>" />
<input type="text" onclick=" window.open('http://www.thefreedictionary.com/<?php echo $theword[4]; ?>','<?php echo $theword[4]; ?>',' height=360,width=900,top=370,left=400 '); "  style="display:none;" id="w5of6" name="w5of6" value="<?php echo $theword[4]; ?>" />
<input type="text" onclick=" window.open('http://www.thefreedictionary.com/<?php echo $theword[5]; ?>','<?php echo $theword[5]; ?>',' height=360,width=900,top=370,left=400 '); "  style="display:none;" id="w6of6" name="w6of6" value="<?php echo $theword[5]; ?>" />
<p id="w0of6" style="display:none;">Optionally click words above for dictionary meaning links</p>
<?php
 echo $tablehtml;
?>
<!--h2 id="leng" name="leng">Word to guess below is <?php echo strlen($theword); ?> letters</h2>
<h1 id='myh1' name='myh1' style=" color: red;"><?php echo $theunderline; ?></h1-->
<input type="button" onclick="goagain(8000);" value="Please Show"></input>&nbsp;<input type="button" onclick="goagain(0);" value="New Game"></input>
<p id='lsofar' name='lsofar'></p>
</form>
<p id="information"></p>
</div>
</body>
</html>

