<?php
// what_is_the_english_word.php
// RJM Programming
// February, 2022
set_time_limit(58);

$h1prefix="";
$ddname="";
if (isset($_GET['myname'])) {
 $ddname="<input type=hidden name=myname value='" . str_replace(',&nbsp;,',',',str_replace(', ,',',',str_replace(',,',',',trim(str_replace('&nbsp;',' ',str_replace('+',' ',urldecode($_GET['myname'])))) . ''))) . "'></input>";
}
$score=0;
$goes=0;
$blurb='';
$dblurb="Score: 0/0";
$totalj=0;
$fortyeight="48";
$fortyeighti="";
$fortyeightj="";
$ob="";
$okp="";
$sh3="";
$aic="Am I Correct?";
$bc="background-color:yellow;";
if (isset($_GET['fortyeight'])) {
  $fortyeight=$_GET['fortyeight'];
  $fortyeighti="<input type=hidden name=fortyeight value='" . $fortyeight . "'></input>";
  $fortyeightj="&fortyeight=" . $fortyeight;
  $aic="Correct?";
  $ob=" onblur=\" if (this.value.trim() != '' && eval('' + this.value.length) >= eval('' + document.getElementById('wl').innerHTML)) { document.getElementById('subm').style.backgroundColor='orange'; document.getElementById('subm').click(); } \" ";
  $okp=" onkeydown=\" return isitnum(event); \" title=\"Type numerics to focus on that player number textbox.\" ";
  $sh3=" style='display:none;'";
  $bc="background:linear-gradient(270deg, #00D7B9, #B95DD7 50%, #FFB367 100%);"; //linear-gradient(180deg, cornflowerblue, purple);";
  if (isset($_GET['myname'])) {
  $fortyeightj="&fortyeight=" . $fortyeight . '&myname=' . $_GET['myname'];
  $h1prefix="<span style='border:1px dashed transparent;' contenteditable=true title='Your name?' id=sname onblur=\" document.getElementById('dname').innerHTML='<input type=hidden id=myname name=myname value=' + String.fromCharCode(39) + this.innerHTML.split('&')[0] + String.fromCharCode(39) + '></input>';  \">" . str_replace(',&nbsp;,',',',str_replace(', ,',',',str_replace(',,',',',trim(str_replace('&nbsp;',' ',str_replace('+',' ',urldecode($_GET['myname'])))) . ', '))) . "</span>";
  } else {
  $h1prefix="<span style='border:1px dashed pink;' contenteditable=true title='Your name?' id=sname onblur=\" document.getElementById('dname').innerHTML='<input type=hidden id=myname name=myname value=' + String.fromCharCode(39) + this.innerHTML.split('&')[0] + String.fromCharCode(39) + '></input>';  \"></span>";
  }
}

if (isset($_GET['numplayers'])) {
  $preifs="<html>
<head>
<title>What Is The English Word - RJM Programming - February, 2022</title>
<meta id='myviewport' name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.1, maximum-scale=8, user-scalable=yes' >
<script type='text/javascript'>
 var gl=-1;
 
 function setgl(inv) {
   gl=inv;
   //location.href='#if' + inv;
 }
 
 function tisitnum(event) {
  if (eval('' + event.keyCode) >= 48 && eval('' + event.keyCode) <= 57) {
    top.setgl(eval(-48 + eval('' + event.keyCode)));
    return false;
  }
  return true;
 }
  
 function everyh() {
    var mygl=gl;
    var iframes = document.getElementsByTagName('iframe');
    if (eval('' + iframes.length) > 0) {
     var rn=Math.floor(Math.random() * eval('' + iframes.length));
     if (eval('' + gl) >= 1 && eval('' + gl) <= eval('' + iframes.length)) { 
       rn=eval(-1 + eval('' + gl));   
       gl=-1; 
       var elmntq = iframes[rn].contentWindow.document.getElementById('q');
       var elmnt = iframes[rn].contentWindow.document.getElementById('youranswer');
       if (elmnt) { 
    if (elmntq.innerHTML == '?' && document.getElementById('if' + mygl) && (1 == 1 || navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))) {
    //alert(document.getElementById('playerth').innerHTML);
    elmntq.innerHTML += '&nbsp;&nbsp;<font size=1>' + document.getElementById('playerth').innerHTML.replace('Player','').replace('>' + mygl + '<','><') + '</font>';
    //document.getElementById('if' + mygl).scrollIntoView(); //location.href='#if' + mygl;
    //alert('1 ' + document.getElementById('if' + mygl));
    //var rect=document.getElementById('if' + mygl).getBoundingClientRect();
    //alert('' + rect.top);
    //document.getElementById('playerth').style.top=('' + eval(window.scrollY) + 'px').replace('-','');
    //document.getElementById('playerth').style.bottom=('' + eval(window.scrollY + rect.height) + 'px').replace('-','');
    //document.getElementById('playerth').scrollIntoView(); 
    }
         if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { location.href='#if' + mygl; } 
         elmnt.focus(); 
       }
       gl=-1;
     }
    }
    setTimeout(everyh, 200);
  }
  
  function tonl() {
    if (!navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { setgl(1); }
    if (!navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
    var rect=document.getElementById('playerth').getBoundingClientRect();
    document.getElementById('playerth').style.left='' + rect.left + 'px';
    document.getElementById('playerth').style.right='' + eval(rect.left + rect.width) + 'px';
    document.getElementById('playerth').style.width='' + rect.width + 'px';
    if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
    //document.getElementById('playerth').style.position='absolute';
    document.getElementById('playerth').style.position='fixed';
    //document.getElementById('playerth').style.height='100%';
    document.getElementById('playerth').style.height='' + rect.height + 'px';
    //document.getElementById('playerth').style.overflow='auto';
    //document.getElementById('playerth').style.top='200px';
    //document.getElementById('playerth').style.bottom=('' + eval(200 + rect.height) + 'px').replace('-','');
    document.getElementById('playerth').style.top=('' + rect.y + 'px').replace('-','');
    document.getElementById('playerth').style.bottom=('' + eval(rect.y + rect.height) + 'px').replace('-','');
    //document.getElementById('playerth').style.overflowY='scroll';
    //document.getElementById('playerth').style.overflowX='hidden';
    } else {
    document.getElementById('playerth').style.position='fixed';
    document.getElementById('playerth').style.height='' + rect.height + 'px';
    document.getElementById('playerth').style.top=('' + rect.y + 'px').replace('-','');
    document.getElementById('playerth').style.bottom=('' + eval(rect.y + rect.height) + 'px').replace('-','');
    }
    document.getElementById('playerth').style.borderRadius='100px';
    //document.getElementById('playerth').style.overflowY='scroll';
    //document.getElementById('playerth').style.overflowX='hidden';
    //document.getElementById('playerth').style.zIndex='99';
    //document.getElementById('playerth').style.opacity='0.8';
    //alert(document.getElementById('playerth').outerHTML);
    }
  }
  
  setTimeout(everyh, 5000);
</script>
</head><body onload=tonl(); style='background-color:yellow;'><table style='width:100%;background:linear-gradient(180deg, lightgreen, lightblue 50%, yellow);' border=20><tr><th id=playerth colspan=2>Player </TH></tr><tr><th>Number</th><th><a target=_self style='cursor:pointer;text-decoration:none;' title=Reload href=./what_is_the_english_word.php>Game</a></th></tr></table></body></html>";
  for ($ih=1; $ih<=$_GET['numplayers']; $ih++) {
     $preifs=str_replace("</table>", "<tr><td style='font-size:28px;width:10%;border-radius:100px;cursor:pointer;text-decoration:none;text-align:center;background-color:lightblue;' onclick=' top.setgl(" . $ih . "); ' id=ltd" . $ih . " title=\"Click numeric to focus on that player number textbox.\">" . $ih . "</td><td><iframe id=if" . $ih . " src='./what_is_the_english_word.php?fortyeight=12' style='width:100%;height:200px;'></iframe></table>", $preifs); 
     $preifs=str_replace("</TH>", "&nbsp;&nbsp;&nbsp;<span style='font-size:18px;width:12%;border-radius:100px;cursor:pointer;text-decoration:none;text-align:center;background-color:lightblue;' onclick=' top.setgl(" . $ih . "); ' id=std" . $ih . " title=\"Click numeric to focus on that player number textbox.\">" . $ih . "</span></TH>", $preifs); 
  }
  echo $preifs;
  exit;
}

function ask() {
  global $score, $goes, $blurb, $fortyeightj;
  $letters=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
  $rl=rand(0,25);
  $lr=rand(5,12);
  $pr=rand(1,$lr);
  header('Location: ' . str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . explode('?',$_SERVER['REQUEST_URI'])[0] . '?position=' . $pr . '&length=' . $lr . '&letter=' . $letters[$rl] . '&score=' . $score . '&goes=' . $goes . $fortyeightj . '&blurb=' . urlencode($blurb))));
  exit;
}

if (isset($_GET['youranswer']) && isset($_GET['theanswer'])) {
  if (isset($_GET['score'])) { $score=$_GET['score'];  }
  if (isset($_GET['goes'])) { $goes=$_GET['goes'];  }
  if (isset($_GET['blurb'])) { $dblurb=str_replace('+',' ',urldecode($_GET['blurb']));  }
  $goes++;
  $blurb=str_replace('+',' ',urldecode($_GET['explanation'])) . "<font title='Thanks to The Free Dictionary, if you click' style=cursor:pointer;text-decoration:underline; onclick=\"window.open('//www.thefreedictionary.com/" . $_GET['theanswer'] . "', '_blank', 'top=100,left=100,width=500,height=600');  \" color=blue>" . $_GET['theanswer'] . "</font> ... Score: " . $score . "/" . $goes;
  if (trim(strtolower($_GET['youranswer'])) == trim(strtolower($_GET['theanswer']))) {
    $score++;
    $blurb="Congratulations! ... Score: " . $score . "/" . $goes;
  }
  ask();
} else if (isset($_GET['letter']) && isset($_GET['length']) && isset($_GET['position'])) {
  if (isset($_GET['score'])) { $score=$_GET['score'];  }
  if (isset($_GET['goes'])) { $goes=$_GET['goes'];  }
  if (isset($_GET['blurb'])) { $dblurb=str_replace('+',' ',urldecode($_GET['blurb']));  }
  $w14a=$_GET['letter'];
  $wzero="";
  $wz="";
  $dwz="_";
  for ($ii=1; $ii<=$_GET['length']; $ii++) {
    if ($ii == $_GET['position']) {
      $wzero.=$w14a;
      $wz.=$w14a;
    } else {
      $wzero.="0";
      $wz.=$dwz;
      if ($dwz == "_") { $dwz="-";  } else {  $dwz="_";  }
    }
  }
  $w10d="";
  $jj=0;
  while (strpos($w10d, ' ') === false) {
    $w10d = file_get_contents(str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/surprise.php?min=" . (1 + $_GET['length']) . "&max=" . (1 + $_GET['length']) . "&hastobe=" . $wzero . "&needclue=y&youllneverfindthis=y")));
    $jj++;
    $totalj++;
    if ($totalj > 100) { 
        exit; //header('Location: ' . str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . explode('?',$_SERVER['REQUEST_URI'])[0])));
    }
    if ($jj > 10) { ask(); }
    sleep(2);
  }
  
  echo "<html>
<head><title>What Is The English Word - RJM Programming - February, 2022</title>
<style>
  #subm:active { background-color:orange; }
  #subm:focus { background-color:lightgreen; }
</style>
<script type='text/javascript'>
 function isitnum(event) {
  if (eval('' + event.keyCode) >= 48 && eval('' + event.keyCode) <= 57) {
    top.setgl(eval(-48 + eval('' + event.keyCode)));
    return false;
  }
  return true;
 }
</script>
</head><body style='" . $bc . "' onload=\"  document.getElementById('youranswer').focus(); \">
<h1" . str_replace(",&nbsp;</span>W",", </span>w",str_replace(", </span>W",", </span>w",">" . $h1prefix . "What Is <span id=wl>")) . $_GET['length'] . "</span> Letter English Word With \"" . $_GET['letter'] . "\" position " . $_GET['position'] . "<span title='" . substr($w10d, strlen(explode(' ',$w10d)[0])) . "' id=q>?</span></h1>
<h3" . $sh3 . ">RJM Programming <a title='How many players?' style='cursor:pointer;text-decoration:underline;' onclick=\" var hm=prompt('How many players?  Maximum is 9.','1'); if (hm == null) { hm=''; } if (hm.trim().replace('1','') != '') { location.href=document.URL.split('?')[0].split('#')[0] + '?fortyeight=12&numplayers=' + encodeURIComponent((hm + ' ').substring(0,1).trim());  } \">&#10133;</a> February, 2022</h3>
<h4>" . $dblurb . "</h4>
<form style='font-size:" . $fortyeight . "px;background-color:pink;padding: 5 5 5 5;width:100%;text-align:center;' method=GET action=./what_is_the_english_word.php>
<input type=hidden name=score value=" . $score . "></input>" . $fortyeighti . "
<input type=hidden name=theanswer value=" . explode(' ',$w10d)[0] . "></input>
<input type=hidden name=explanation value='" . "Sorry, last answer for " . $_GET['length'] . " Letter English Word With \"" . $_GET['letter'] . "\" at position " . $_GET['position'] . " with clue \"" . trim(substr($w10d, strlen(explode(' ',$w10d)[0])))  . "\" was " . "'></input>
<input type=hidden name=goes value=" . $goes . "></input><span style=font-size:" . $fortyeight . "px;>&nbsp;&nbsp;" . substr($w10d, strlen(explode(' ',$w10d)[0])) . " ... &nbsp;&nbsp;</span><input style=display:inline-block;font-size:" . $fortyeight . "px;width:30%; type=text" . $ob . $okp . " id=youranswer name=youranswer placeholder='" . $wz . "' value=''></input><span>&nbsp;&nbsp;</span><input id=subm onclick=\" this.style.cursor='progress'; document.body.style.cursor='progress';  \" style=display:inline-block;font-size:" . $fortyeight . "px; type=submit value='" . $aic . "'></input>
<div id=dname style=display:none;>" . $ddname . "</div></form>
</body></html>
";
  
} else {
 ask();
}


?>
