<?php
 // word_association.php
 // Word Associate via Yahoo YQL Thesaurus Until It Peters Out
 // RJM Programming
 // January, 2015
 
 $persist = "n";
 $gpy = 30;
 $instructions = 'context.strokeText("",5,30); ';
 $startword = "";
 $rest = "</select>";
 $myp = "";
 $mypp = "";
 $mypbefore = "";
 $mypmiddle = "";
 $mypafter = "";
 $one = "1";
 $callbit = "";
 $prefixbit = "";
 
 $language = "en_US";

 $score = 1;
 $goes = 1;

 if (isset($_GET['score'])) $score = $_GET['score'] + 1;
 if (isset($_GET['goes'])) $goes = $_GET['goes'] + 1;
 if (isset($_POST['score'])) $score = $_POST['score'] + 1;
 if (isset($_POST['goes'])) $goes = $_POST['goes'] + 1;

 $cols = array('lime','orange','blue','purple','navy','teal','fuchsia','olive','maroon','gray','green');
 $colpick = rand(0, (sizeof($cols) - 1));
 $mycol = $cols[$colpick];

 if (isset($_GET['1'])) {
   $thewordis = urldecode($_GET['1']);
   $myp = urldecode($_GET['1']) . " -> ";
   $mypp = urldecode($_GET['1']) . " -> ";
   $mypbefore = urldecode($_GET['1']) . " -> ";
   $callbit = "?1=" . urlencode($_GET['1']);
   $one = $one + 1;
   while (isset($_GET[$one])) {
     if ($one > 2) {
       $mypmiddle = " ... ";
       $prefixbit = " -> ";
     } else if ($one == 2) {
       $rest = str_replace("</select>", "</select>&nbsp;&nbsp;&nbsp;<a href='mailto:change.email@address?subject=Word%20Association%20Game&body=" . "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("scoxre=" . ($score - 1), "score=" . $score, str_replace("goxes=" . ($goes - 1), "goes=" . $goes, str_replace("?", "%3f", str_replace("&", "%26", $_SERVER['REQUEST_URI'])))) . "'>Optionally Email Snapshot of Game</a>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;<a onclick=' document.getElementById(\"mychat\").src = \"http://www.rjmprogramming.com.au/plus\"; document.getElementById(\"mychat\").style.display = \"block\"; document.getElementById(\"achat\").style.display = \"block\"; ' title='Chat' href='#achat'>Chat</a>", $rest);
     }
     $thewordis = urldecode($_GET[$one]);
     $myp .= urldecode($_GET[$one]) . " -> ";
     if (($one % 6) == 0) {
       $instructions = str_replace('""', '"' . $mypp . '"', $instructions);
       $instructions = str_replace($gpy . "); ", $gpy . "); " . 'context.strokeText("",5,' . ($gpy + 30) . '); ', $instructions);
       $gpy = $gpy + 30;
       $mypp = "";
     }
     $mypp .= urldecode($_GET[$one]) . " -> ";
     $mypafter = $prefixbit . urldecode($_GET[$one]) . " -> ";
     $callbit .= "&" . $one . "=" . urlencode($_GET[$one]);
     $one = $one + 1;
   }
   $callbit = "&" . $one . "=";
   if (strpos($_SERVER['QUERY_STRING'], "score=") === false) $callbit = str_replace("&", "?score=1&goes=1&", $callbit);
   
   
 } else {
   if ($startword == "") {
     $persist = "y";
     $startword = file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=4&max=8");
   }
   $callbit = "?score=1&goes=1&1=" . urlencode($startword) . "&2=";
   $rest = "</select>&nbsp;&nbsp;&nbsp;<a href='mailto:change.email@address?subject=Word%20Association%20Game&body=" . "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("scoxre=" . ($score - 1), "score=" . $score, str_replace("goxes=" . ($goes - 1), "goes=" . $goes, str_replace("?", "%3f", str_replace("&", "%26", $_SERVER['REQUEST_URI'])))) . "%3fscore=0%26goes=0%261=" . urlencode($startword) . "'>Optionally Email Snapshot of Game</a>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=' document.getElementById(\"mychat\").src = \"http://www.rjmprogramming.com.au/plus\"; document.getElementById(\"mychat\").style.display = \"block\"; document.getElementById(\"achat\").style.display = \"block\"; ' title='Chat' href='#achat'>Chat</a>";
   $myp = $startword . " -> ";
   $mypp = $startword . " -> ";
   $mypbefore = $startword . " -> ";
   $thewordis = $startword;
 }
 
 
$selname = urldecode($thewordis);
$mylist = explode('x', 'y');

while ($selname != "") {  // look up Yahoo YQL thesaurus below
 
 $pageContent = file_get_contents("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20thesaurus%20where%20key%3D%22test_only%22%20and%20word%3D%22".urlencode($selname)."%22%20and%20language%3D%22" . $language . "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
 //$json_output = json_decode($pageContent); 
 
 //echo $pageContent;
 
 $mylist = explode('"synonyms":"', $pageContent);
 
 if (sizeof($mylist) > 1 || $persist == "n") {
 for ($i=1; $i<sizeof($mylist); $i++) {
   $myline = explode('"', $mylist[$i]);
   $mywords = explode("|", $myline[0]);
   for ($ii=0; $ii<sizeof($mywords); $ii++) {
    $preword = explode(" (", $mywords[$ii]);
    $proposed = str_replace(" (antonym)", "", str_replace(" (related term)", "", str_replace(" (similar term)", "", $mywords[$ii])));
    if (strlen(str_replace(" -> " . $proposed . " -> ", "", " -> " . $myp . " -> ")) == strlen(" -> " . $myp . " -> ") && strlen(str_replace(">" . $proposed . "<", "", ">" . $rest . "<")) == strlen(">" . $rest . "<")) {
    if (strpos($mywords[$ii], " (antonym)") !== false) {
      $selname = "";
      $rest = str_replace("</select>", "<option value=\"" . $proposed . "\">" . $proposed . "</option></select>", $rest);
	  //echo "<tr><td style='background-color:white;'></td><td style='background-color:white;'><a style='text-decoration:none;' target=_blank title='" . str_replace(" (antonym)", "", $mywords[$ii]) . "' href='" . $dprefix . str_replace(" (antonym)", "", $preword[0]) . "'>" . str_replace(" (antonym)", "", $mywords[$ii]) . "</a></td></tr>";
    } else {
      $selname = "";
      $rest = str_replace("</select>", "<option value=\"" . $proposed . "\">" . $proposed . "</option></select>", $rest);
	  //echo "<tr><td style='background-color:white;'><a style='text-decoration:none;' target=_blank title='" . str_replace(" (antonym)", "", $mywords[$ii]) . "' href='" . $dprefix . str_replace(" (antonym)", "", $preword[0]) . "'>" . str_replace(" (antonym)", "", $mywords[$ii]) . "</a></td><td style='background-color:white;'></td></tr>";
	}
	}
   }
 }
 if ($selname != "") {
   $selname = "";
   $rest .= "<" . "script type='text/javascript'> alert('Oh well ... it is the end of the line ... and you scored " . $score . "'); </" . "script" . ">";
   $myp = str_replace("~", "", str_replace(" -> ~", "", ($myp . "~")));
   $mypp = str_replace("~", "", str_replace(" -> ~", "", ($mypp . "~")));
 }
 } else {
   $startword = file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=4&max=8");
   $callbit = "?score=1&goes=1&1=" . $startword . "&2=";
   $rest = "</select>&nbsp;&nbsp;&nbsp;<a href='mailto:change.email@address?subject=Word%20Association%20Game&body=" . "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("scoxre=" . ($score - 1), "score=" . $score, str_replace("goxes=" . ($goes - 1), "goes=" . $goes, str_replace("?", "%3f", str_replace("&", "%26", $_SERVER['REQUEST_URI'])))) . "%3fscore=0%26goes=0%261=" . urlencode($startword) . "'>Optionally Email Snapshot of Game</a>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=' document.getElementById(\"mychat\").src = \"http://www.rjmprogramming.com.au/plus\"; document.getElementById(\"mychat\").style.display = \"block\"; document.getElementById(\"achat\").style.display = \"block\"; ' title='Chat' href='#achat'>Chat</a>";
   $myp = $startword . " -> ";
   $mypp = $startword . " -> ";
   $mypbefore = $startword . " -> ";
   $thewordis = $startword;
   $selname = urldecode($thewordis);
 }
  
}   
   

 
 //if ($rest == "</select>") $rest = "</select>";
 $instructions = str_replace('""', '"' . $mypp . '"', $instructions);
?>
<!doctype HTML>
<html>
<head>
<title>English Word Association Game - RJM Programming - January, 2015</title>
<meta charset="UTF-8" />
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<style>
 
#myh1:before {
    content: 'English Word Association Game';
}
 
#myh2:before {
    content: 'RJM Programming - January, 2015';
}
 
#myh3:before {
    content: '';
}
 
#myh4:before {
    content: 'Score: <?php echo $score; ?> Goes: <?php echo $goes; ?>';
}
 
#myp:before {
    content: '<?php echo $mypbefore; ?><?php echo $mypmiddle; ?><?php echo $mypafter; ?>';
}
 
#myh1:after {
    content: '';
}
 
#myh2:after {
    content: '';
}
 
#myh3:after {
    content: '';
}
 
#myh4:after {
    content: '';
}
 
#myp:after {
    content: '';
}
</style>
<script type='text/javascript'>
var callbit = '<?php echo $callbit; ?>';

function checkit(ois) {
 if (ois.value == "") {
   location.href="./word_association.php";
 } else if (document.URL.indexOf("?") == -1) {
   location.href = document.URL.replace("#achat", "").replace("#", "").replace("score=" + (score - 1), "score=" + score).replace("goes=" + (goes - 1), "goes=" + goes) + callbit + encodeURIComponent(ois.value);
 } else {
   location.href = document.URL.replace("#achat", "").replace("#", "").replace("score=" + (score - 1), "score=" + score).replace("goes=" + (goes - 1), "goes=" + goes) + callbit.replace("?", "&") + encodeURIComponent(ois.value);
 }
}
</script>

<script type='text/javascript'>
  var callbit = '<?php echo $callbit; ?>';
  var thewordis = '<?php echo $thewordis; ?>';
  var score = '<?php echo $score; ?>';
  var goes = '<?php echo $goes; ?>';
</script>
</head>
<body style='background-color:lightgray;' onload=' var elem = document.getElementById("canvaselement"); var context = elem.getContext("2d"); context.font = "22px Verdana"; <?php echo $instructions; ?>  '>
<div align='center'>
<h1 title='Click for dictionary lookup of <?php echo $thewordis; ?>' onclick=' window.open("http://www.thefreedictionary.com/" + thewordis, thewordis, " height=600,width=900,top=30,left=400 "); ' id='myh1'></h1>
<h2 title='Click for RJM Programming Landing Page' onclick=' window.open("http://www.rjmprogramming.com.au", "RJM Programming Landing Page", " height=600,width=900,top=30,left=400 "); ' id='myh2'></h2>
<h3 id='myh3'><a href="#" onclick="var neww=prompt('Enter your word of interest.',''); if (neww.length > 0) { location.href='./word_association.php?1=' + encodeURIComponent(neww);  }">Click to specify your word</a></h3>
<h4 id='myh4'></h4>
<p title="<?php echo $myp; ?>" id='myp'><select onchange='checkit(this);' id='myps'><option value=''>...</option><option value=''>New Root Word</option><?php echo $rest; ?></p>
<canvas title='Click for a new starting word' onclick='location.href="./word_association.php";' style='border: 5px solid green; background-color: <?php echo $mycol; ?>; opacity: 0.8;' title='Click for your next sentence to solve' id="canvaselement" width="1350" height="400" style="border: 1px solid green;" />
</div>
<div align='center'>
<a id='achat' style='display:none;' href='#' title='Back to game'>Back to game</a><br><iframe id='mychat' style='display:none;width:100%;height:900px;' src='javascript:none;' title='Chat'></iframe>
</div>
</body>
</html>
