<?php
  // no_body.php
  // RJM Programming
  // October, 2019
  // Speech bubbles with no webpage body tag.
  $one="";
  $two="";
  $three="";
  $four="";
  
  $nextone=1;
  $innards="";
  $title=" ... ";
  $emailto="";
  $gats="Got anything to say?";
  
  foreach( $_GET as $name=>$val ) {
    if ($one == "") {
      $one=str_replace("+", " ", urldecode($val));
      if (strpos($one, "@") !== false) {
        $emailto=$one;
      } else {
        $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
        $title.="|" . $val;
        $nextone++;
      }
    } else if ($two == "") {
      $two=str_replace("+", " ", urldecode($val));
      if (strpos($two, " ... |") !== false) {
        $one="";
        $fields=explode("|", explode("... |", $two)[1]);
        $two="";
        $gats.="  You can email to an entered email address what you have so far.";
        for ($ii=0; $ii<sizeof($fields); $ii++) {
          if ($one == "") {
            $one=$fields[$ii];
          } else if ($two == "") {
            $two=$fields[$ii];
          } else if ($three == "") {
            $three=$fields[$ii];
          } else if ($four == "") {
            $four=$fields[$ii];
          } else {
            $one=$two;
            $two=$three;
            $three=$four;
            $four=$fields[$ii];
          }          
          $innards.="\nform.append('infld" . $nextone . "', \"" . $fields[$ii] . "\");\n";
          $title.="|" . $fields[$ii];
          $nextone++;
        }
      } else {
        $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
        $title.="|" . $val;
        $nextone++;
      }
    } else if ($three == "") {
      $three=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    } else if ($four == "") {
      $four=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    } else {
      $one=$two;
      $two=$three;
      $three=$four;
      $four=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    }
  }
  
  foreach( $_POST as $name=>$val ) {
    if ($one == "") {
      $one=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    } else if ($two == "") {
      $two=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    } else if ($three == "") {
      $three=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    } else if ($four == "") {
      $four=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    } else {
      $one=$two;
      $two=$three;
      $three=$four;
      $four=str_replace("+", " ", urldecode($val));
      $innards.="\nform.append('infld" . $nextone . "', \"" . $val . "\");\n";
      $title.="|" . $val;
      $nextone++;
    }
  }
  

  $htmlis = "
<!doctype html>
<head>
<title>No Body - RJM Programming - October, 2019" . $title . "</title>
<style>

  html::before {
    border: 2px solid red;
    content: \"" . $one . "\";
	height: 150px;
	width: 90%;
	min-width: 100px;
	background: ivory;
	display: inline-block;
	margin: 0 auto;
	border-radius: 0px 0px 130px 0px;
	margin-top: 50px;
    margin-left: 20px;
	font-size: 60px;
	text-align: center;
  }
  
  body::before {
    border: 2px solid blue;
    content: \"" . $two . "\";
	height: 150px;
	width: 40%;
	min-width: 100px;
	background: ivory;
	display: inline-block;
	margin: 0 auto;
	border-radius: 120px 0px 130px;
	margin-top: 50px;
    margin-left: 20px;
	font-size: 60px;
	text-align: center;
  }
  
  body::after {
    border: 2px solid brown;
    content: \"" . $three . "\";
	height: 150px;
	width: 40%;
	min-width: 100px;
	background: ivory;
	display: inline-block;
	margin: 0 auto;
	border-radius: 120px 0px 130px;
	margin-top: 50px;
    margin-left: 20px;
	font-size: 60px;
	text-align: center;
  }
  
  html::after {
    border: 2px solid purple;
    content: \"" . $four . "\";
	height: 150px;
	width: 90%;
	min-width: 100px;
	background: ivory;
	display: inline-block;
	margin: 0 auto;
	border-radius: 0px 0px 0px 130px;
	margin-top: 49px;
    margin-left: 20px;
	font-size: 60px;
	text-align: center;
  } 
  
</style>

<scr" . "ipt type='text/javascript'>

  var nextone='" . $nextone . "';
  var zhr=null, form=null;
  var ezhr=null, eform=null;
  var nextsb='';
  var emailto='';
  var level=1;
  var gats='" . $gats . "';
  
  function showEStuff(evt) {
  if (ezhr.readyState == 4) {
    if (ezhr.status == 200) {
      alert('Emailed to " . $emailto . " this webpage contents as an HTML attachment.');  
    }
  }
  }
  
  function showStuff(evt) {
  if (zhr.readyState == 4) {
    if (zhr.status == 200) {
      document.title+='|' + nextsb;
      document.write(zhr.responseText);
      if (gats.indexOf('You can ') == -1) {
      gats+='  You can email to an entered email address what you have so far.';
      }
    }
  }
  }
  
  function doemail() {
  ezhr=new XMLHttpRequest();
  eform=new FormData();
  eform.append('to', '" . $emailto . "');
  eform.append('subject', 'Speech Bubbles');
  eform.append('html', '<html><head>' + document.head.innerHTML.split('<sc' + 'ript')[0].replace(' 49px',' -400px') + '</head><!--body></body--></html>');
  ezhr.open('post', '//www.rjmprogramming.com.au/HTMLCSS/emailhtml.php', true);
  ezhr.onreadystatechange = showEStuff;
  ezhr.send(eform);
  }
  
  function donext(nextv) {
  zhr=new XMLHttpRequest();
  form=new FormData();
  " . $innards . " 
  form.append('infld' + nextone, nextv);
  zhr.open('post', document.URL.split('#')[0].split('?')[0], true);
  zhr.onreadystatechange = showStuff;
  zhr.send(form);
  }

  function andthen() {
  if (level == 1) {
  nextsb=prompt(gats, '');
  if (nextsb != null) { 
  if (nextsb.indexOf('@') != -1 && nextsb.indexOf(' ') == -1 && document.title.indexOf('|') != -1) { emailto=nextsb; level++; nextsb=''; }
  }
  } else if (level == 2) {
  emailto=prompt('Email to?', '');
  if (emailto == null) { emailto=''; }
  if (emailto.indexOf('@') == -1) { emailto=''; }
  }
  if (nextsb == null) { level++; nextsb=''; }
  if (nextsb.trim() != '') {
  donext(nextsb); 
  } else if (level == 2) {
  if (emailto == '') { emailto=prompt('Email to?', ''); }
  if (emailto == null) { emailto=''; }
  if (emailto.indexOf('@') == -1) { emailto=''; }
  }
  if (emailto.indexOf('@') != -1) {
  location.href=document.URL.split('#')[0].split('?')[0] + '?to=' + encodeURIComponent(emailto) + '&rest=' + encodeURIComponent(document.title);
  }
  }
  
  function nothing() {
  }

  setTimeout(andthen, eval(nextone * 2000));
</scr" . "ipt>
</head>
<!--body>
</body-->
</html>";
  
  if ($emailto != "") {
    echo str_replace("setTimeout(", "\n doemail(); \n setTimeout(", $htmlis);
  } else {
    echo $htmlis;
  }
?>
