<?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
  
  $ashare="Share";
  $ahref="href";
  $atitle="href";
  $withletters=" that is 4-9 characters long and has to have ";
  $htype="1";
  $thirty="30";
  $thirtyagain="30";
  $nameonblur="onblur";
  $wording="Propose a Word";
  
  $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");  
  $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");  
  $blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");  
  $ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");  
  
  if ($iphone || $android || $ipod || $blackberry) { // thanks to http://www.phpmysqlscript.com/2012/03/detecting-users-screen-size-and.html
    $nameonblur="onclick=\"within='y';\" onblur";
    $thirty="10";
    $thirtyagain="20";
    if ($android) {
      $thirty="4";
      $thirtyagain="12";
      $wording="New Word";
    }
    $htype="2";
    $ahref="href='#' onclick='analyze(this);' title";
    $atitle="alt";
    $ashare="<input id='emailee' type='text' value='fill.in@email' " . $nameonblur . "='updemail(this);'></input>";
  }
  $tletter="";
  $startword="";
  $wordlist="";
  $wlistd="none";
  $why="";
  $score=0;
  $emailhrefjl = "";
  $emailhrefet = "";
  $preemailhrefjl = "";
  $preemailhrefet = "";
  $postemailhrefjl = "";
  $postemailhrefet = "";
  $wordstable="<tr><td><input type='button' id='paw' value='" . $wording . "' onclick='ask();'></input>&nbsp;&nbsp;<input type='text' onblur='validate(this,this.value);' id='aword' size=10></input></td></tr>";
  $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>";
  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);
    $withletters .= (strtoupper(substr($letwrd, (5 * 5) - 1, 1)) . " and could contain");
    for ($j=0; $j<9; $j++) {
      if ($j == 5) {
          $tletter = strtolower(substr($letwrd, ($j * 5) - 1, 1));
      } else {
          $withletters .= (" " . strtoupper(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);
     $withletters .= (strtoupper($tletter) . " and could contain");
     $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)));
          $withletters .= (" " . 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 within='';
var tletter='<?php echo $tletter; ?>';
var startword='<?php echo $startword; ?>';
var wordlist='<?php echo $wordlist; ?>';
var emailee='';

function updemail(ithis) {
  //emailee='';
  //alert(athis.id);
  //alert(athis.title);
  var was=document.getElementById('altemail').value;
  //if (was == ithis.value) within='';
  if (within != '' && ithis.value.replace("fill.in@email","") != "" && ithis.value.replace("fill.in@email","").indexOf("@") != -1) {
      if (was != ithis.value) document.getElementById('iemail').src='<?php echo str_replace("target_share_challenge", "target_emailer", $emailhref); ?>' + '?set=' + ithis.value;
      if (was != ithis.value) document.getElementById('altemail').value=ithis.value;
      //var ans=confirm("Email " + ithis.value + " a link to a new Target Share Challenge Game link (which does not share letters)?"); //,document.getElementById('ashare').title.replace(emailee + '?', ithis.value + '&').replace('?', '&'));
      //if (ans == true) {
      //  document.getElementById('iemail').src='<?php echo str_replace("target_share_challenge", "target_emailer", $emailhref); ?>' + '?mt=' + document.getElementById('ashare').title.replace(emailee + '?', ithis.value + '&').replace('?', '&');
      //} 
      var ansp=prompt("Email " + ithis.value + " a link to a new Target Share Challenge Game link (which does not share letters)?  Else enter the new email address.  Add blank somewhere for no email just now.", was); //,document.getElementById('ashare').title.replace(emailee + '?', ithis.value + '&').replace('?', '&'));
      if (ansp != null) {
        if (ansp != was && ansp.indexOf("@") != -1) {
          document.getElementById('iemail').src='<?php echo str_replace("target_share_challenge", "target_emailer", $emailhref); ?>' + '?set=' + ansp.replace(/ /g, "");
          document.getElementById('altemail').value=ansp.replace(/ /g, "");
          ithis.value=ansp.replace(/ /g, "");
        }
        if (ansp.indexOf("@") != -1 && ansp.indexOf(" ") == -1) preask();
      }
  }
  within='';
}

function analyze(athis) {
  var xemailee='';
  //alert(athis.id);
  //alert(athis.title);
  if (athis.title.replace("fill.in@email","").indexOf("mailto:?") != -1) {
    xemailee = prompt("Please enter your To: email address", "fill.in@email");
    if (xemailee == null) xemailee = "fill.in@email";
    if (xemailee.replace("fill.in@email","") != "" && xemailee.replace("fill.in@email","").indexOf("@") != -1) {
      document.getElementById('iemail').src='<?php echo str_replace("target_share_challenge", "target_emailer", $emailhref); ?>' + '?set=' + xemailee;
      document.getElementById('emailee').value=xemailee;
      preask();
      emailee = xemailee;
    } 
  }
  document.getElementById('iemail').src='<?php echo str_replace("target_share_challenge", "target_emailer", $emailhref); ?>' + '?mt=' + athis.title.replace('?', xemailee + '&').replace('?', '&');
}

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() {
  var mtv;
  document.getElementById('aword').focus();
  mtv=document.getElementById('altemail').value.replace("fill.in@email", "");
  if (mtv != "") {
    document.getElementById('ashare').title=document.getElementById('ashare').title.replace("mailto:" + emailee + "?", "mailto:" + mtv + "?");
    document.getElementById('aletters').title=document.getElementById('aletters').title.replace("mailto:" + emailee + "?", "mailto:" + mtv + "?");
    document.getElementById('aeverything').title=document.getElementById('aeverything').title.replace("mailto:" + emailee + "?", "mailto:" + mtv + "?");
    document.getElementById('ashare').href=document.getElementById('ashare').href.replace("mailto:" + emailee + "?", "mailto:" + mtv + "?");
    document.getElementById('aletters').href=document.getElementById('aletters').href.replace("mailto:" + emailee + "?", "mailto:" + mtv + "?");
    document.getElementById('aeverything').href=document.getElementById('aeverything').href.replace("mailto:" + emailee + "?", "mailto:" + mtv + "?");
    document.getElementById('emailee').value=mtv;
    emailee=mtv;
  }
}

function ask() {
  var res=false;
  var pword=prompt("Please enter your proposed word<?php echo $withletters; ?>", "");
  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;'>
<h<?php echo $htype; ?> id='myh1' align='center'>Target Share Challenge <a title='New Game with new letters' href='./target_share_challenge.php'>Game</a> <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 id='ashare' <?php echo $ahref; ?>='mailto:?subject=Target%20Share%20Challenge&body=<?php echo urlencode($emailhref); ?>' <?php echo $atitle; ?>='Target Share Challenge ... New Game'><?php echo $ashare; ?></a><?php echo $preemailhrefjl; ?><a id='aletters' <?php echo $ahref; ?>='mailto:?subject=Target%20Share%20Challenge&body=<?php echo urlencode($emailhrefjl); ?>' <?php echo $atitle; ?>='Target Share Challenge ... Same Letters'><?php echo $postemailhrefjl; ?></a><?php echo $preemailhrefet; ?><a id='aeverything' <?php echo $ahref; ?>='mailto:?subject=Target%20Share%20Challenge&body=<?php echo urlencode($emailhrefet); ?>' <?php echo $atitle; ?>='Target Share Challenge ... Same Letters and Word List So Far'><?php echo $postemailhrefet; ?></a></h<?php echo $htype; ?>>
<div align='center'>
<form id='myform' action='./target_share_challenge.php' method='GET'>
<table><tbody><tr><td>
<table id='letters' cellpadding=<?php echo $thirty; ?>>
<tbody id='tbletters'>
<?php echo $letterstable; ?>
</tbody>
</table>
</td><td style='display:<?php echo $wlistd; ?>;'><textarea readonly id='wlist' rows=<?php echo $thirtyagain; ?> 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>
<iframe id='iemail' style='display:none;height:1px;width=1px;' src='target_emailer.php?get=' height=1 width=1></iframe>
<input type='hidden' id='altemail' value=''></input>
</div>
</body>
</html>

