<?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);
  $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");
  $donottouch = rand(0,25);
  if (isset($_POST['word1of4'])) {
    $theword[0] = $_POST['word1of4'];
    $theword[1] = $_POST['word2of4'];
    $theword[2] = $_POST['word3of4'];
    $theword[3] = $_POST['word4of4'];
  } 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(":443/","/",":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y")));
    $theword[1] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . str_replace(":443/","/",":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y")));
    $theword[2] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . str_replace(":443/","/",":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=4&max=10&youllneverfindthis=y")));
    $theword[3] = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . str_replace(":443/","/",":" . $_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<4; $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>';
          $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" . '<html>' . "\n";
echo "\n" . '<head>' . "\n";
echo "\n" . '<title>Decoding the Scrambled Words - RJM Programming - 2014</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 = "./decodewords.php?score=" + score + "&outof=" + outof;
}

function goagain(pshow) {
    outof = outof + 1;
    if (pshow != 0) {
      document.getElementById('w0of4').style.display = 'block';
      document.getElementById('w1of4').style.display = 'block';
      document.getElementById('w2of4').style.display = 'block';
      document.getElementById('w3of4').style.display = 'block';
      document.getElementById('w4of4').style.display = 'block';
      setInterval(golater, pshow);
    } else {
      location.href = "./decodewords.php?score=" + score + "&outof=" + outof;
    }
}

function guessmade(thiso, wordi, posi, firstcall) {
  if (thiso.style.backgroundColor == 'red') {
    var ddah = "word" + wordi + "of4";
    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<=4; ii++) {
         jj = document.getElementById("word" + ii + "of4").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 = "./decodewords.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: yellow;\"> \n";
 } else {
  echo "\n <body style=\"background-color: yellow;\"> \n";
 }
?>
<div align="center">
<h1 id="gamename">Decoding the Scrambled 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="word1of4" name="word1of4" value="<?php echo $theword[0]; ?>" />
<input type="hidden" id="word2of4" name="word2of4" value="<?php echo $theword[1]; ?>" />
<input type="hidden" id="word3of4" name="word3of4" value="<?php echo $theword[2]; ?>" />
<input type="hidden" id="word4of4" name="word4of4" value="<?php echo $theword[3]; ?>" />
<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="w1of4" name="w1of4" 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="w2of4" name="w2of4" 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="w3of4" name="w3of4" 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="w4of4" name="w4of4" value="<?php echo $theword[3]; ?>" />
<p id="w0of4" 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>

