<?php
  $score = "0";
  $outof = "0";
  $theword = "";
  $theunderline = "";
  $wordlist = "";
  
  function expanded($inwl) {
     $wl = explode(" ", rtrim(ltrim($inwl)));
     if (sizeof($wl) >= 1 && rtrim(ltrim($inwl)) != "") {
       $outw = " ";
       for ($i=0; $i<sizeof($wl); $i++) {
         $outw .= "<a target=_blank title='" . $wl[$i] . "' href='http://www.thefreedictionary.com/" . $wl[$i] . "'>" . $wl[$i] . "</a> ";
       }
       return $outw;
     } else {
       return $inwl;
     }
  }

  if (isset($_GET['wordlist'])) {
    $wordlist = $_GET['wordlist'];
  }
  if (isset($_POST['word'])) {
    $theword = $_POST['word'];
  } else if (isset($_GET['huhbig'])) {
    if (strpos($_GET['huhbig'], "~") !== false) {
    $thewords = explode("~", $_GET['huhbig']);
    $theword = $thewords[0];
    $wordlist = str_replace("!", " ", str_replace("wordlist=", "", $thewords[1]));
    $score = str_replace("score=", "", $thewords[2]);
    } else {
    $theword = $_GET['huhbig'];
    }
  } else {
    $theword = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=4&max=12"));
  } 
  for ($i=0; $i<strlen($theword); $i++) {
    $theunderline .= "-";
  }
  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>Words Within Words - RJM Programming - 2014</title>
<script type="text/javascript">
var overgoes=0;
var score=0;
var outof=0;
var goes=0;
var guess="";
var lettersdone="";
var ci=null;

<?php
  echo "\n var bword='" . $theword . "'; \n";
  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 addtoword(intd) {
  document.getElementById("huhsmall").value = document.getElementById("huhsmall").value + intd.innerHTML.replace("<b>","").replace("<i>","").replace("</b>","").replace("</i>","").replace("&nbsp;","");
  intd.style.backgroundColor = 'yellow';
  if (ci) { clearInterval(ci); ci=null; }
}

function shuffle() { 
   //alert(bword);
   var i = bword.length;
   var j,k,m,kcr;
   var col, row;
   var sofar = "";
   for (j=1; j<=5; j++) {
    for (k=1; k<=5; k++) {
       m = j*10 + k;
       document.getElementById(m).innerHTML = "&nbsp;";
    }
   }
   for (j=1; j<=i; j++) {
       col = Math.floor((Math.random()*5)+1);
       row = Math.floor((Math.random()*5)+1);
       kcr = col*10 + row;
       while (sofar.indexOf(kcr) != -1) {
        col = Math.floor((Math.random()*5)+1);
        row = Math.floor((Math.random()*5)+1);
        kcr = col*10 + row;
       }
       //alert("kcr=" + kcr);
       sofar = sofar + " " + kcr + " ";
       document.getElementById(kcr).innerHTML = "<b><i>" + bword.substring((j-1),j) + "</i></b>";       
   }

}
</script>
</head>
<?php
 if (isset($_GET['badluck'])) {
  echo "\n <body style=\"background-color: yellow;\" onload=\" shuffle(); ci=setInterval(shuffle,5000); document.getElementById('huhsmall').focus(); \"> \n";
 } else {
  echo "\n <body style=\"background-color: yellow;\" onload=\" shuffle(); ci=setInterval(shuffle,5000); document.getElementById('huhsmall').focus(); \"> \n";
 }
?>
<div align="center">
<?php
  global $score, $outof, $wordlist;
  if ($wordlist == "") {
  echo "\n" . '<h1 id="gamename">Words Within Words</h1>' . "\n";
  } else {
  echo "\n" . '<h1 id="gamename">Words Within Words ... Words so far: ' . expanded($wordlist) . '</h1>' . "\n";
  }
  if ($outof < $score) {
    $outof = $score;
  }
  echo "\n <h2 id='scoreis'>Score: " . $score . "</h2> \n";
  echo "\n" . '<form id="gform" method="POST" action="http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . '/PHP/surprise.php">' . "\n";
  echo "\n" . '<input type="hidden" id="returnto" name="returnto" value="' . $_SERVER['PHP_SELF'] . '" />' . "\n";
?>
<input type="hidden" id="wordlist" name="wordlist" value="<?php echo $wordlist; ?>" />
<input type="hidden" id="sword" name="sword" value="<?php echo $theword; ?>" />
<input type="hidden" id="huhbig" name="huhbig" value="<?php echo $theword; ?>" />
<input type="hidden" id="word" name="word" value="<?php echo $theword; ?>" />
<input type="hidden" id="score" name="score" value="<?php echo $score; ?>" />
<table id="guess" name="guess" cellpadding=10 border=5 style=" font-size:18px; text-align:center; background-color: pink; ">
<tr>
<td onclick=" addtoword(this); " id="11">&nbsp;</td>
<td onclick=" addtoword(this); " id="12">&nbsp;</td>
<td onclick=" addtoword(this); " id="13">&nbsp;</td>
<td onclick=" addtoword(this); " id="14">&nbsp;</td>
<td onclick=" addtoword(this); " id="15">&nbsp;</td>
</tr>&nbsp;<tr>
<td onclick=" addtoword(this); " id="21">&nbsp;</td>
<td onclick=" addtoword(this); " id="22">&nbsp;</td>
<td onclick=" addtoword(this); " id="23">&nbsp;</td>
<td onclick=" addtoword(this); " id="24">&nbsp;</td>
<td onclick=" addtoword(this); " id="25">&nbsp;</td>
</tr>&nbsp;<tr>
<td onclick=" addtoword(this); " id="31">&nbsp;</td>
<td onclick=" addtoword(this); " id="32">&nbsp;</td>
<td onclick=" addtoword(this); " id="33">&nbsp;</td>
<td onclick=" addtoword(this); " id="34">&nbsp;</td>
<td onclick=" addtoword(this); " id="35">&nbsp;</td>
</tr>&nbsp;<tr>
<td onclick=" addtoword(this); " id="41">&nbsp;</td>
<td onclick=" addtoword(this); " id="42">&nbsp;</td>
<td onclick=" addtoword(this); " id="43">&nbsp;</td>
<td onclick=" addtoword(this); " id="44">&nbsp;</td>
<td onclick=" addtoword(this); " id="45">&nbsp;</td>
</tr>&nbsp;<tr>
<td onclick=" addtoword(this); " id="51">&nbsp;</td>
<td onclick=" addtoword(this); " id="52">&nbsp;</td>
<td onclick=" addtoword(this); " id="53">&nbsp;</td>
<td onclick=" addtoword(this); " id="54">&nbsp;</td>
<td onclick=" addtoword(this); " id="55">&nbsp;</td>
</tr>
<tr><td colspan=5><input type="button" onclick=" if (ci) { clearInterval(ci); } ci=null; shuffle(); " value="Manually Shuffle"></input></td></tr>
<?php
echo "\n" . '<tr><td colspan=5><a href="mailto:?subject=Please help me with my Words Within Words Game with the Link at http://' . str_replace(":80/", "/", $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] .  $_SERVER['PHP_SELF']) . '?huhbig=' . $theword . '&body=http://' . str_replace(":80/", "/", $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] .  $_SERVER['PHP_SELF']) . '?huhbig=' . $theword . '~wordlist=' . str_replace(" ", "!", $wordlist) . '~score=' . $score . '" title="Email Game Link to Friend">Email Game Link to Friend</a></td></tr>' . "\n";
?>
<tr><td colspan=5><input type="button" onclick=" document.getElementById('myj').style.display='block'; document.getElementById('myj').src= 'http://www.rjmprogramming.com.au/plus'; " value="Chat Room"></input><iframe src="javascript:false;" id="myj" style="display:none;width:650px;height:800px;"></iframe></td></tr>
</table><br>
<h2 id="leng" name="leng">Big word below has <?php echo strlen($theword); ?> letters for you to work with</h2>
<h1 id='myh1' name='myh1' style=" color: red;"><?php echo $theword; ?></h1>
<p id='lsofar' name='lsofar'></p>
Your word (if blank, starts new game): <input type="text" id="huhsmall" name="huhsmall" value=""></input>
<input type="submit" id="submit" name="submit" value="Check Your Word" title="Words should be at least 4 characters long. Some plurals will not count."></input>
</form>
<p id="information"></p>
</div>
</body>
</html>

