<?php
  $score = "0";
  $outof = "0";
  $theword = "";
  $theunderline = "";
  if (isset($_POST['word'])) {
    $theword = $_POST['word'];
  } else {
    $satisfies = "n";
    while ($satisfies == "n") {
      //$theword = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=6&max=12&youllneverfindthis=y"));
      $theword = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . "/PHP/surprise.php?min=6&max=12&youllneverfindthis=y"));
      if (strpos(($theword . "*"), "ent*") !== false || 
          strpos(($theword . "*"), "ant*") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("ant*", "_nt", str_replace("ent*", "_nt", ($theword . "*")));
      } else if (strpos(($theword . "*"), "ly*") !== false || 
          strpos(($theword . "*"), "ry*") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("ly*", "_y", str_replace("ry*", "_y", ($theword . "*")));
      } else if (strpos(($theword . "*"), "ive*") !== false || 
          strpos(($theword . "*"), "ice*") !== false || 
          strpos(($theword . "*"), "ite*") !== false || 
          strpos(($theword . "*"), "ize*") !== false || 
          strpos(($theword . "*"), "ire*") !== false || 
          strpos(($theword . "*"), "ise*") !== false || 
          strpos(($theword . "*"), "ine*") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("ice*", "i_e", str_replace("ize*", "i_e", str_replace("ire*", "i_e", str_replace("ite*", "i_e", str_replace("ise*", "i_e", str_replace("ive*", "i_e", str_replace("ine*", "i_e", ($theword . "*"))))))));
      } else if (strpos(("*" . $theword), "*in") !== false || 
          strpos(("*" . $theword), "*un") !== false || 
          strpos(("*" . $theword), "*on") !== false || 
          strpos(("*" . $theword), "*en") !== false || 
          strpos(("*" . $theword), "*un") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("*en", "_n", str_replace("*an", "_n", str_replace("*on", "_n", str_replace("*in", "_n", str_replace("*un", "_n", ("*" . $theword))))));
      } else if (strpos(("*" . $theword), "*be") !== false || 
          strpos(("*" . $theword), "*ce") !== false || 
          strpos(("*" . $theword), "*je") !== false || 
          strpos(("*" . $theword), "*xe") !== false || 
          strpos(("*" . $theword), "*re") !== false || 
          strpos(("*" . $theword), "*de") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("*be", "_e", str_replace("*xe", "_e", str_replace("*ce", "_e", str_replace("*je", "_e", str_replace("*re", "_e", str_replace("*de", "_e", ("*" . $theword)))))));
      } else if (strpos(("*" . $theword), "*qu") !== false || 
          strpos(("*" . $theword), "*fu") !== false || 
          strpos(("*" . $theword), "*gu") !== false || 
          strpos(("*" . $theword), "*hu") !== false || 
          strpos(("*" . $theword), "*ku") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("*gu", "_u", str_replace("*fu", "_u", str_replace("*qu", "_u", str_replace("*hu", "_u", str_replace("*ku", "_u", ("*" . $theword))))));
      } else if (strpos(("*" . $theword), "*wo") !== false || 
          strpos(("*" . $theword), "*yo") !== false || 
          strpos(("*" . $theword), "*lo") !== false || 
          strpos(("*" . $theword), "*mo") !== false || 
          strpos(("*" . $theword), "*no") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("*wo", "_o", str_replace("*yo", "_o", str_replace("*lo", "_o", str_replace("*mo", "_o", str_replace("*no", "_o", ("*" . $theword))))));
      } else if (strpos(("*" . $theword), "*pa") !== false || 
          strpos(("*" . $theword), "*ra") !== false || 
          strpos(("*" . $theword), "*sa") !== false || 
          strpos(("*" . $theword), "*ta") !== false || 
          strpos(("*" . $theword), "*va") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("*pa", "_a", str_replace("*ra", "_a", str_replace("*sa", "_a", str_replace("*ta", "_a", str_replace("*va", "_a", ("*" . $theword))))));
      } else if (strpos(("*" . $theword), "*shr") !== false || 
          strpos(("*" . $theword), "*str") !== false || 
          strpos(("*" . $theword), "*skr") !== false || 
          strpos(("*" . $theword), "*spr") !== false || 
          strpos(("*" . $theword), "*scr") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("*shr", "s_r", str_replace("*str", "s_r", str_replace("*skr", "s_r", str_replace("*spr", "s_r", str_replace("*scr", "s_r", ("*" . $theword))))));
      } else if (strpos(("*" . $theword), "*cy") !== false || 
          strpos(("*" . $theword), "*ca") !== false || 
          strpos(("*" . $theword), "*ce") !== false || 
          strpos(("*" . $theword), "*co") !== false || 
          strpos(("*" . $theword), "*ci") !== false) {
          $satisfies = "y";
          $theunderline = str_replace("*cy", "c_", str_replace("*ca", "c_", str_replace("*ce", "c_", str_replace("*co", "c_", str_replace("*ci", "c_", ("*" . $theword))))));
      } else if (strpos(substr($theword,1), substr($theword,0,1)) !== false) {
          $satisfies = "y";
          $theunderline = str_replace(substr($theword,0,1), "_", $theword);
      } else if (strpos(substr($theword,2), substr($theword,1,1)) !== false) {
          $satisfies = "y";
          $theunderline = str_replace(substr($theword,1,1), "_", $theword);
      } else if (strpos(substr($theword,3), substr($theword,2,1)) !== false) {
          $satisfies = "y";
          $theunderline = str_replace(substr($theword,2,1), "_", $theword);
      }
    }
  } 
  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'];
  }
?>
<html>
<head>
<title>Spelling and Meaning - RJM Programming - 2014</title>
<script type="text/javascript">
var overgoes=0;
var score=0;
var outof=0;
var goes=0;
var guess="";
var lettersdone="";

<?php
  echo "\n var versus='" . $theunderline . "'; \n";
  if (isset($_GET['score'])) {
    echo "\n score=" . $_GET['score'] . "; \n";
  }
  if (isset($_GET['outof'])) {
    echo "\n outof=" . $_GET['outof'] . "; \n";
  }
?>


function guessmade(thiso) {
  var aform=document.getElementById('gform');
  aform.submit();
}

<?php
  if (isset($_GET['suggestion'])) {
      echo "\n" . ' var ret=prompt("Oops!  Word needed was ' . $_GET['suggestion'] . ' ... do not Cancel for a dictionary lookup","' . $_GET['suggestion'] . '"); ' . "\n";
      echo "\n" . '     if (ret != null) { ' . "\n";
      echo "\n" . '         if (ret == "' . $_GET['suggestion'] . '") { ' . "\n";
      echo "\n" . '           window.open("//www.thefreedictionary.com/' . $_GET['suggestion'] . '","' . $_GET['suggestion'] . '", " height=600,width=900,top=30,left=400 "); ' . "\n";
      echo "\n" . '         } ' . "\n";
      echo "\n" . '     } ' . "\n";
  }
?>

</script>
</head>
<?php
 if (isset($_GET['badluck'])) {
  echo "\n <body style=\"background-color: yellow;\" onload=\" if (score > outof) { alert('Very funny!'); document.getElementById('outof').value = document.getElementById('score').value; outof=score; } \"> \n";
 } else {
  echo "\n <body style=\"background-color: yellow;\" onload=\" if (score > outof) { alert('Very funny!'); document.getElementById('outof').value = document.getElementById('score').value; outof=score; } \"> \n";
 }
?>
<div align="center">
<h1 id="gamename">Spelling and Meaning</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";
  echo "\n" . '<form id="gform" method="POST" action="//' . $_SERVER['SERVER_NAME'] . '/PHP/surprise.php">' . "\n";
  echo "\n" . '<input type="hidden" id="returnto" name="returnto" value="' . $_SERVER['PHP_SELF'] . '" />' . "\n";
?>
<input type="hidden" id="sword" name="sword" value="<?php echo $theword; ?>" />
<input type="hidden" id="word" name="word" value="<?php echo $theword; ?>" />
<input type="hidden" id="theunderline" name="theunderline" value="<?php echo $theunderline; ?>" />
<input type="hidden" id="score" name="score" value="<?php echo $score; ?>" />
<input type="hidden" id="youllneverfindthis" name="youllneverfindthis" value="y" />
<input type="hidden" id="outof" name="outof" value="<?php echo $outof; ?>" />
<table><tr><td>
<p style=" padding-left:20px;padding-right:20px;font-size:32px;font-color:red;border: 2px solid lightgreen; "><?php echo "<br><br><br><br><br><br>" . $theunderline . "<br><br><br><br><br><br>"; ?></p>
</td><td>
<select size=27 id="guess" name="guess" onchange=" guessmade(this); " style=" text-align:center; background-color: pink; ">
<option value="">Select a letter below ...</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
<option value="f">f</option>
<option value="g">g</option>
<option value="h">h</option>
<option value="i">i</option>
<option value="j">j</option>
<option value="k">k</option>
<option value="l">l</option>
<option value="m">m</option>
<option value="n">n</option>
<option value="o">o</option>
<option value="p">p</option>
<option value="q">q</option>
<option value="r">r</option>
<option value="s">s</option>
<option value="t">t</option>
<option value="u">u</option>
<option value="v">v</option>
<option value="w">w</option>
<option value="x">x</option>
<option value="y">y</option>
<option value="z">z</option>
</select>
</td></tr></table>
<h2 id="leng" name="leng">Word with missing letter is shown below</h2>
<h1 id='myh1' name='myh1' style=" color: red;"><?php echo $theunderline; ?></h1>
<p id='lsofar' name='lsofar'></p>
</form>
<p id="information"></p>
</div>
</body>
</html>

