<?php
  // Target Share Challenge Game
  // RJM Programming
  // March, 2015
  // Inspiration: Thanks to Sydney Morning Herald http://www.smh.com.au
  //
  // Rules: 4 to 9 letter words
  //        Word must contain the yellow middle letter
  //        No plurals please
  //        Ten point score bonus for 9 letter words
  
  $tletter="";
  $startword="";
  $wordlist="";
  $wlistd="none";
  $why="";
  $score=0;
  $emailhrefjl = "";
  $emailhrefet = "";
  $preemailhrefjl = "";
  $preemailhrefet = "";
  $postemailhrefjl = "";
  $postemailhrefet = "";
  $letterstable="<tr><td></td><td></td><td></td></tr><tr><td></td><td id='keyl'></td><td></td></tr><tr><td></td><td></td><td></td></tr>";
  $wordstable="<tr><td><input type='button' id='paw' value='Propose a Word' onclick='ask();'></input>&nbsp;&nbsp;<input type='text' onblur='validate(this,this.value);' id='aword'></input></td></tr>";
  if (strlen($_SERVER['QUERY_STRING']) > 0) {
    $emailhref="http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI']; // . "?" . $_SERVER['QUERY_STRING'];
    $emailhrefet=$emailhref;
    $stuffsa=explode("&wordlist", $emailhref);
    $emailhrefjl=$stuffsa[0];
    $stuffs=explode("?", $emailhref);
    $emailhref=$stuffs[0];
    $preemailhrefjl = "/";
    $postemailhrefjl = "Letters";
    if (sizeof($stuffsa) > 1) {
     $preemailhrefet = "/";
     $postemailhrefet = "Everything";
    }

    $letwrd="&" . substr($_SERVER['QUERY_STRING'], 0, 44);
    for ($j=0; $j<9; $j++) {
      if ($j == 5) $tletter = strtolower(substr($letwrd, ($j * 5) - 1, 1));
      $startword = $startword . strtolower(substr($letwrd, ($j * 5) - 1, 1));
    }
  } else {
    $emailhref="http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  }
  $done = "n";
  $tl = -1;
  $delim = "?";
  if (1 == 1) {
    if ($startword == "") $startword = strtolower(file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=10&max=10"));
    $prestartword = $startword;
    if ($tletter == "") {
     $tl = rand(0,8);
     $tletter = substr($startword, $tl, 1);
     $letterstable=str_replace(" id='keyl'>", " id='keyl' style='background-color:yellow;'><b><h2 style='color:red;'>" . strtoupper($tletter) . "</h2></b><input type='hidden' name='l5' value='" . strtoupper($tletter) . "'></input>", $letterstable);
     for ($j=0; $j<15; $j++) {
      $xtl = rand(2,8);
      $thisletter = substr($startword, $xtl, 1);
      $prefix = substr($startword, 0, ($xtl));
      $suffix = substr($startword, ($xtl + 1));
      $startword=$thisletter . $prefix . $suffix;
     }
    } else {
     $letterstable=str_replace(" id='keyl'>", " id='keyl' style='background-color:yellow;'><b><h2 style='color:red;'>" . strtoupper($tletter) . "</h2></b><input type='hidden' name='l5' value='" . strtoupper($tletter) . "'></input>", $letterstable);
    }
    $k = 1;
    for ($j=0; $j<strlen($startword); $j++) {
      if ($done == "n" && substr($startword, $j, 1) == $tletter) {
        $done = "y";
      } else {
        $place = strpos($letterstable, "<td></td>");
        $prefix = substr($letterstable, 0, ($place + 4));
        $suffix = substr($letterstable, ($place + 4));
        $letterstable=$prefix . "<b><h4 style='color:black;'>" . strtoupper(substr($startword, $j, 1)) . "</h4><input type='hidden' name='l" . $k  . "' value='" . strtoupper(substr($startword, $j, 1)) . "'></input>" . $suffix;
        if ($tl >= 0) {
          $preemailhrefjl = "/";
          $postemailhrefjl = "Letters";
          if ($emailhrefjl == "") $emailhrefjl="http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI']; // . "?" . $_SERVER['QUERY_STRING'];
          $emailhrefjl .= ($delim . "l" . $k . "=" . strtoupper(substr($startword, $j, 1)));
          $delim = "&";
          if ($k == 4) $emailhrefjl .= ($delim . "l" . ($k + 1) . "=" . strtoupper($tletter));
        } 
        $k = $k + 1;
        if ($k == 5) {
          $k = $k + 1;
        } 
      }
    }
  }
  
  if (isset($_GET['wordlist'])) {
    $wordlist=$_GET['wordlist'];
  } else if (isset($_POST['wordlist'])) {
    $wordlist=$_POST['wordlist'];
  }
  
  if ($wordlist != "") {
    $words=explode(";", $wordlist);
    $fwlist="";
    $sofarlist=";;";
    for ($jk=0; $jk<sizeof($words); $jk++) {
       $pw=strtolower($words[$jk]);
       if (strpos($sofarlist, ";" . $pw . ";") === false) {
        $wordis = file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?huh=" . $pw . "");
        if ($wordis != "false") {
         if (strlen($pw) == 9) {
           $score = $score + 11 + (strlen($pw) / 1000.0);
         } else {
           $score = $score + 1 + (strlen($pw) / 1000.0);
         }
         $sofarlist=str_replace(";;", ";" . $pw . ";;", $sofarlist);
         if ($fwlist == "") {
           $fwlist = $pw;
         } else {
           $sparelist = $pw . ";" . $fwlist;
           $fwlist = $sparelist;
         }
        }
       }
    }
    $wordlist = $fwlist;
  }
  
  if ($wordlist != "") $wlistd="block";


?>
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.25, maximum-scale=8, user-scalable=yes" />
<title>Target Share Challenge Game - RJM Programming - March, 2015</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<style>
 td {  border: 4px solid green; background-color: white;  }
 input {  background-color: pink;  }
</style>
<script type='text/javascript'>
var tletter='<?php echo $tletter; ?>';
var startword='<?php echo $startword; ?>';
var wordlist='<?php echo $wordlist; ?>';

function canreset() {
  var ans=confirm('Rules: 4 to 9 letter words. Word must contain the yellow middle letter. No plurals please. Ten point score bonus for 9 letter words.  You can hit the Cancel button to get different letter set.  Inspiration: Thanks to Sydney Morning Herald http://www.smh.com.au');
  if (ans == true) {
    ans=ans;
  } else {
    location.href='./target_share_challenge.php';
  }
}

function preask() {
  document.getElementById('aword').focus();
}

function ask() {
  var res=false;
  var pword=prompt("Please enter your proposed word", "");
  if (pword != null) {
    if (pword.length > 0) {
      res = validate(document.getElementById('aword'), pword.toLowerCase());
    }
  }
}

function validate(iois, pval) {
  var proposedword="", compword=startword, ourletter;
  var ij;
  var retval=true;
  if (pval != "") {
   proposedword=pval.toLowerCase();
  } else {
   proposedword=iois.value.toLowerCase();
  }
  if (proposedword == "") {
    iois.value="";
    return false;
  } else if (proposedword.length > 9) {
    iois.value="";
    return false;
  } else if (proposedword.length < 4) {
    iois.value="";
    return false;
  } else if (proposedword.indexOf(tletter) == -1) {
    iois.value="";
    return false;
  } else {
    for (ij=0; ij<proposedword.length; ij++) {
      ourletter=proposedword.substring(ij,(ij + 1));
      if (compword.indexOf(ourletter) == -1) {
        iois.value="";
        retval=false;
      } else {
        compword=compword.replace(ourletter, "");
      }
    }
  }
  if (retval == true) {
    iois.value=proposedword;
<?php
    if ($wordlist == "") {
      echo "\n document.getElementById('wordlist').value = proposedword; \n";
      echo "\n document.getElementById('submit').click(); \n";
    } else {
      echo "\n document.getElementById('wordlist').value = document.getElementById('wordlist').value + ';' + proposedword; \n";
      echo "\n document.getElementById('submit').click(); \n";
    }
?>
  }
  return retval;
}


</script>
</head>
<body onload='setTimeout(preask,2000);' style='background-color:lightblue;'>
<h1 align='center'>Target Share Challenge Game <a href='#' title='Rules: 4 to 9 letter words. Word must contain the yellow middle letter. No plurals please. Ten point score bonus for 9 letter words.  Inspiration: Thanks to Sydney Morning Herald http://www.smh.com.au' onclick='canreset();'>Help</a> ... Score: <?php echo $score; ?> ... Email <a href='mailto:?subject=Target%20Share%20Challenge&body=<?php echo urlencode($emailhref); ?>' title='Target Share Challenge ... New Game'>Share</a><?php echo $preemailhrefjl; ?><a href='mailto:?subject=Target%20Share%20Challenge&body=<?php echo urlencode($emailhrefjl); ?>' title='Target Share Challenge ... Same Letters'><?php echo $postemailhrefjl; ?></a><?php echo $preemailhrefet; ?><a href='mailto:?subject=Target%20Share%20Challenge&body=<?php echo urlencode($emailhrefet); ?>' title='Target Share Challenge ... Same Letters and Word List So Far'><?php echo $postemailhrefet; ?></a></h1>
<div align='center'>
<form id='myform' action='./target_share_challenge.php' method='GET'>
<table><tbody><tr><td>
<table id='letters' cellpadding=30>
<tbody id='tbletters'>
<?php echo $letterstable; ?>
</tbody>
</table>
</td><td style='display:<?php echo $wlistd; ?>;'><textarea id='wlist' rows=30 column=10 style='background-color:lightgreen;'><?php echo str_replace(";", "\n", $wordlist); ?></textarea></td></tr></tbody></table>
<h2 id='mywords'>Your words: Score: <?php echo $score; ?></h2>
<table id='words'>
<tbody id='tbwords'>
<?php echo $wordstable; ?>
</tbody>
</table>
<input type='hidden' name='wordlist' id='wordlist' value='<?php echo $wordlist; ?>'></input>
<input type='submit' id='submit' value='Check Your Score' style='display:none;'></input>
</form>
</div>
</body>
</html>

