<?php
// favourites_midair.php
// Favourites inline HTML email midair database
// November, 2020

$sofar="";
$proposedemail="";
$rval="|" . rand(19876543,39876543) . "|";
$titlebit="";
$anyextras="";
$ourlistc="Book,Movie,Play,Song,Video,Audio";
$ourlist=["Book","Movie","Play","Song","Video","Audio"];
$mytype="";
$umytype="";

function jn($insms) {
  $outsms="";
  for ($qas=0; $qas<strlen($insms); $qas++) {
    if (substr($insms, $qas, 1) >= '0' && substr($insms, $qas, 1) <= '9') {
      $outsms.=substr($insms, $qas, 1);
    }
  }
  return $outsms;
}


function oururlencode($inw) {
  return $inw;
}

function ourgetenv($ine) {
  return getenv($ine);
}

function maybelinkvia($ih, $plnk) {
  if (strpos(strtolower($plnk), "https:") !== false || strpos(strtolower($plnk), "http:") !== false) {
    return '<a target=_blank href=' . $plnk . '>' . $ih . '</a>';
  } else if (strpos(strtolower($plnk), "@") !== false) {
    return '<a target=_blank href=mailto:' . $plnk . '?subject=' . str_replace("+","%20",urlencode($ih)) . '>' . $ih . '</a>';
  } else if (isset($_POST['htimestamp']) && trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))]) != explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))] && trim($plnk) != '') { //  && trim(str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",$plnk))))))))))) != ''
    return '<a target=_blank href=sms:' . jn($plnk) . '&body=' . str_replace("+","%20",urlencode($ih)) . '>' . $ih . '</a>';
  } else if (strlen($plnk) == 11 && str_replace(' ','',$plnk) == $plnk) {
    return '<a target=_blank href=//www.youtube.com/watch?v=' . $plnk . '>' . $ih . '</a>';
  } else if ((strlen(str_replace('-','',$plnk)) == 10 || strlen(str_replace('-','',$plnk)) == 13) && str_replace(' ','',str_replace('-','',$plnk)) == str_replace('-','',$plnk) && trim(str_replace("-","",str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",$plnk)))))))))))) == '') {
    return '<a target=_blank href=//www.abebooks.com/servlet/SearchResults?sts=t&isbn=' . $plnk . '>' . $ih . '</a>';
  }
  return $ih;
}

function maybelink($plnk) {  
  if (strpos(strtolower($plnk), "https:") !== false || strpos(strtolower($plnk), "http:") !== false) {
    return '<a target=_blank href=' . $plnk . '>' . $plnk . '</a>';
  } else if (strpos(strtolower($plnk), "@") !== false) {
    return '<a target=_blank href=mailto:' . $plnk . '>' . $plnk . '</a>';
  } else if (isset($_POST['htimestamp']) && trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))]) != explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))] && trim($plnk) != '') { //  && trim(str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",$plnk))))))))))) != ''
    return '<a target=_blank href=sms:' . jn($plnk) . '>' . $plnk . '</a>';
  } else if (strlen($plnk) == 11 && str_replace(' ','',$plnk) == $plnk) {  // https://www.youtube.com/watch?v=GuJQSAiODqI
    return '<a target=_blank href=//www.youtube.com/watch?v=' . $plnk . '>' . $plnk . '</a>';
  } else if ((strlen(str_replace('-','',$plnk)) == 10 || strlen(str_replace('-','',$plnk)) == 13) && str_replace(' ','',str_replace('-','',$plnk)) == str_replace('-','',$plnk) && trim(str_replace("-","",str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",$plnk)))))))))))) == '') {
    return '<a target=_blank href=//www.abebooks.com/servlet/SearchResults?sts=t&isbn=' . $plnk . '>' . $plnk . '</a>';
  }
  return $plnk; // reDGraTP3qU
}

function maybemailto($lto, $psubj) {
  if (strpos($lto, "@") !== false) {
    return '<a title=Email href=mailto:' . trim($lto) . '?subject=' . oururlencode(urlencode(str_replace(" ","\t",'Re: ' . $psubj . ' ... have moderated and accepted ...'))) . '&body=' . oururlencode(urlencode('http://www.rjmprogramming.com.au/PHP/favourites_midair.php'))  . '>' . $lto . '</a>';
  } else if (jn(trim($lto)) != "" && trim(str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",jn(trim($lto))))))))))))) == '') {
    return '<a title=SMS href=sms:' . jn(trim($lto)) . '&body=' . urlencode(urlencode(str_replace(" ","\t",'Re: ' . $psubj . ' ... have moderated and accepted ... ' . "\n"))) . urlencode(urlencode('http://www.rjmprogramming.com.au/PHP/favourites_midair.php')) . '>' . $lto . '</a>';
  }
  return $lto;
}


if (isset($_POST['favouritename']) && isset($_POST['favouritetype']) && isset($_POST['favouritetimestamp']) && isset($_POST['favouritevalue']) && isset($_POST['favouriteemail']) && isset($_POST['favouriteshare'])) {
  $rval="";
  $titlebit.=" post";

if (isset($_POST['htimestamp'])) {
  //$titlebit.=" 2";

if (jn(trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))])) != trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))])) {
$umytype=explode(jn(trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))])), trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))]))[1];
if (strpos(($ourlistc . $anyextras), $umytype) === false) {
  $anyextras.='<option value=\"' . $umytype . '\" selected>' . $umytype . '</option>';
}
$mytype=$umytype;
$rval="|" . jn(trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))])) . "|";
$sval=str_replace("|","",$rval . $umytype) . "|";
} else {
$rval="|" . trim(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))]) . "|";
$sval=str_replace("|","",$rval) . "|";
}
$smval=str_replace("|","",$rval) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritetype'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritename'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritevalue'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouriteemail'])) . "|";
$sval.=str_replace("\r"," ... ",str_replace("\n"," ... ",str_replace("\r\n"," ... ",str_replace("+"," ",urldecode($_POST['favouriteshare'])))));

$smval.=str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "|";
$smval.=str_replace(">" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", " selected>" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", "<select><option value=Book>Book</option><option value=Movie>Movie</option><option value=Play>Play</option><option value=Song>Song</option><option value=Video>Video</option><option value=Audio>Audio</option>" . $anyextras . "</select>") . "|";
$smval.=maybelinkvia(str_replace("+"," ",urldecode($_POST['favouritename'])),str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "|";
$smval.=maybelink(str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "|";
$smval.=maybemailto(str_replace("+"," ",urldecode($_POST['favouriteemail'])),str_replace("+"," ",urldecode($_POST['favouritename']))) . "|";
$smval.=str_replace("+"," ",urldecode($_POST['favouriteshare']));

  //$titlebit.=" " . $sval;

$ssval="<h1>Favourite<a target=_blank href=//www.rjmprogramming.com.au/PHP/favourites_midair.php title=Webpage>s</a></h1>";
$ssval.="<p>Timestamp: " . str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "</p>";
$ssval.="<p>Type: " . str_replace(">" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", " selected>" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", "<select><option value=Book>Book</option><option value=Movie>Movie</option><option value=Play>Play</option><option value=Song>Song</option><option value=Video>Video</option><option value=Audio>Audio</option>" . $anyextras . "</select>") . "</p>";
$ssval.="<p>Title (by Creator): " . maybelinkvia(str_replace("+"," ",urldecode($_POST['favouritename'])),str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "</p>";
$ssval.="<p>Link: " . maybelink(str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "</p>";
$ssval.="<p>Email: " . maybemailto(str_replace("+"," ",urldecode($_POST['favouriteemail'])),str_replace("+"," ",urldecode($_POST['favouritename']))) . "</p>";
$qssval=$ssval;
$ssval.="<p>Comments: " . str_replace("\n","<br>",str_replace("+"," ",urldecode($_POST['favouriteshare']))) . "</p>";
$qssval.="<div>Comments: <textarea>" . str_replace("\n","' + String.fromCharCode(10) + '",str_replace("+"," ",urldecode($_POST['favouriteshare']))) . "</textarea></div>";
$qssval=str_replace("\r","",str_replace("\n","",str_replace("\r\n","",$qssval)));

$sssval=str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouritetype'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouritename'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouritevalue'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouriteemail'])) . "|";
$sssval.=str_replace("\n","<br>",str_replace("+"," ",urldecode($_POST['favouriteshare'])));


$ssxval="<h1>Favourites</h1>";
$ssxval.="<p>Timestamp: " . str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "</p>";
$ssxval.="<p>Type: " . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "</p>";
$ssxval.="<p>Title (by Creator): " . str_replace("+"," ",urldecode($_POST['favouritename'])) . "</p>";
$ssxval.="<p>Link: " . str_replace("+"," ",urldecode($_POST['favouritevalue'])) . "</p>";
$ssxval.="<p>Email: " . str_replace("+"," ",urldecode($_POST['favouriteemail'])) . "</p>";
$ssxval.="<p>Comments: " . str_replace("\n","<br>",str_replace("+"," ",urldecode($_POST['favouriteshare']))) . "</p>";


$proposedemail="  if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { \n 
  zhr = new XMLHttpRequest(); \n
  zform=new FormData(); \n
  zform.append('inline', 'y'); \n
  zform.append('to', 'rmetcalfe15@gmail.com'); /" . "/ 'rmetcalfe@rjmprogramming.com.au'); \n
  zform.append('cc', 'rmetcalfe@rjmprogramming.com.au'); \n
  zform.append('subj', 'Please moderate Favourite ...'); \n
  zform.append('body', '<body>" . $qssval . "<form action=' + document.URL.split('#')[0].split('?')[0] + ' method=GET><input type=hidden name=okyes value=\"" . $sval . "\"></input><input type=submit value=Okay?></input></form></body>');
  zhr.open('post', 'http://www.rjmprogramming.com.au/HTMLCSS/emailhtml.php', true); \n
  zhr.onreadystatechange = showStuff;
  zhr.send(zform); \n 
  } else if (document.URL.toLowerCase().indexOf('/localhost') != -1) { \n 
  var moreu='" . urlencode(str_replace("</h1>"," ... ",str_replace("</p>"," ... ",str_replace("<p>","",str_replace("<h1>","",$ssxval))))) . "';
  var morev='';
  document.getElementById('aemail').href+=moreu + '&body=' + encodeURIComponent(morev + document.URL.split('#')[0].split('?')[0] + '?okyes=' + encodeURIComponent('" . $sval . "')); \n
  document.getElementById('aemail').click(); \n 
  } \n 
  \n";
}

} else if (isset($_GET['okyes'])) {
  $rval="";
  $newrow="<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>";
  //echo "here";
  //exit;
  if ($_GET['okyes'] != '') {
   $oks=explode("|", str_replace("+"," ",urldecode($_GET['okyes'])));
   //echo '' . sizeof($oks);
   //exit;
   if (sizeof($oks) >= 7 && file_exists(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html")) {
    $lastone=str_replace($oks[0] . "|" . $oks[1] . "|" . $oks[2] . "|" . $oks[3] . "|" . $oks[4] . "|" . $oks[5] . "|", "", str_replace("+"," ",urldecode($_GET['okyes'])));
    if ($oks[0] != jn($oks[0])) {
      $mytype=explode(jn($oks[0]), $oks[0])[1];
      $oks[0]=jn($oks[0]);
      if (strlen($mytype) <= 1) {
        $umytype=strtoupper($mytype);
      } else {
        $umytype=strtoupper(substr($mytype,0,1)) . substr($mytype,1);
      }
      if (strpos(($ourlistc . $anyextras), $umytype) === false) {
        $anyextras.='<option value=\"' . $umytype . '\" selected>' . $umytype . '</option>';
      }
    }
    $bigsofar=file_get_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html");
    if (strpos($bigsofar, "|" . $oks[0] . "|") !== false) {
      $bigsofar=str_replace("|" . $oks[0] . "|", "", $bigsofar);
      $newrow="<tr><td>" . $oks[1] . "</td><td>" . $oks[2] . "</td><td>" . maybelinkvia($oks[3],$oks[4]) . "</td><td>" . maybelink($oks[4]) . "</td><td>" . maybemailto($oks[5],$oks[3]) . "</td><td>" . str_replace(" ... ","<br>",$lastone) . "</td></tr>";
      if (strpos($bigsofar, "<tr><td") !== false) {
        $coks=explode("<tr><td", $bigsofar);
        $bigsofar=str_replace($coks[0], $coks[0] . $newrow, $bigsofar);
        file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", $bigsofar);
      } else if (strpos($bigsofar, "</table>") !== false) {
        $coks=explode("</table>", $bigsofar);
        $bigsofar=str_replace($coks[0], $coks[0] . $newrow, $bigsofar);
        file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", $bigsofar);
      }
    }
   }
  }
} 

if (!file_exists(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html")) {
  file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", "<!doctype html>
<html>
<head>
<title>Favourites - RJM Programming - November, 2020</title>
<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.25, maximum-scale=8, user-scalable=yes' />
<meta charset='utf-8' />
<style>
 input { margin-left: 2%; width:95%; }
 textarea { margin-left: 2%; width:95%; }
 select { margin-left: 2%; }
 td:nth-child(3) a { text-decoration:none; }
</style>
</head>
<body>
<h1>Favourites</h1>
<h3>Moderated by <a href='mailto:rmetcalfe@rjmprogramming.com.au?Subject=Favourites'>RJM Programming</a> - November, 2020</h3>
<h4 style=display:none;>" . $rval . "</h4>
<table border=1 style='width:100%;'>
<tr><th colspan=6 style='background-color:yellow;'><br><br><br><br>Past Favourites (as moderated)</th></tr>
<tr><th id='thfavouritetimestamp'>Datetime</th><th id='thfavouritetype'>Type</th><th id='thfavouritename'>Title (by Creator)</th><th id='thfavouritevalue' title='http: or https: URL or 11 character YouTube ID or 10 or 13 digit ISBN'>Link</th><th id='thfavouriteemail' title='Email or SMS number'>Email</th><th id='thfavouriteshare''>Comments</th></tr>
</table>
</body>
</html>");
} else if ($rval != "") {
    $bigsofar=file_get_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html");
    if (strpos($bigsofar, "</h4>") !== false) {
      $bigsofar=str_replace("</h4>", $rval . "</h4>", $bigsofar);
      file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", $bigsofar);
    }
}

if (file_exists(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html")) {
  //echo "1 \n";
  $bigsofar=file_get_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html");
  //echo "11 \n";
  if (strpos($bigsofar, "<table") !== false && strpos($bigsofar, "</table>") !== false) {
  //echo "111 \n";
    $sofar="<table" . explode("<table", explode("</table>", $bigsofar)[0])[1] . "</table>";
    // <tr><td>2020-11-12 19:56:24</td><td>Book</td>
    $sofaropts=explode("<tr><td>", $sofar);
  //echo "" . (-1 + sizeof($sofaropts)) . " \n";
  //echo " " . str_replace("<","&lt;",str_replace(">","&gt;",$sofar));
    for ($itr=1; $itr<(0 + sizeof($sofaropts)); $itr++) {
      $tone=explode("</td>", explode("</td><td>", $sofaropts[$itr])[1])[0];
      //echo $tone . "\n";
      //if ($tone == "Colour") {
      //  echo "Found Colour";
      //  exit;
      //}
      if (trim($tone) != '') {
        if (strpos(("," . $ourlistc . ","), ("," . $tone . ",")) === false && strpos(("," . $anyextras . ","), (">" . $tone . "<")) === false) {
            $anyextras.='<option value="' . $tone . '">' . $tone . '</option>';
        }
      }
    }
  }
} 

echo "<!doctype html>
<html>
<head>
<title>Favourites - RJM Programming - November, 2020" . $titlebit . "</title>
<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.25, maximum-scale=8, user-scalable=yes' />
<meta charset='utf-8' />
<style>
 input { margin-left: 2%; width:95%; }
 textarea { margin-left: 2%; width:95%; }
 select { margin-left: 2%; }
 td:nth-child(3) a { text-decoration:none; }
</style>
<script type='text/javascript'>

var inurl='';
var lastinurl=' ';
var ourwo=null;
var alm='Sent for moderation to RJM Programming';
var arect=null;
var lastain=null;

var prevh='';
var selcon='';
var selconv='';
var zhr=null;
var zform=null;
var dou=false;
var mytype='" . $mytype . "'; 
var umytype='" . $umytype . "';
var alltogether=\"" . $ourlistc . str_replace('"','',$anyextras) . "\";  

 Date.prototype.toDateInputValue = (function() {  // thanks to https://stackoverflow.com/questions/6982692/how-to-set-input-type-dates-default-value-to-today
    var local = new Date(this);
    local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
    return local.toJSON().split('.')[0].replace('T',' '); //.slice(0,10);
 });

function showStuff(evt) {
  if (zhr != null) {
  if (zhr.readyState == 4) {
    if (zhr.status == 200) {
    alert(alm);
    }
  }
  }
 }
 
 function ol() { 
" . $proposedemail . " 
  var sv=new Date().toDateInputValue();
  prevh=document.getElementById('htimestamp').value;
  selcon=document.getElementById('favouritetype').outerHTML;
  selconv=document.getElementById('favouritetype').value;
  //alert(sv);
  document.getElementById('favouritetimestamp').value='' + sv;
  document.getElementById('htimestamp').value='' + sv + '.' + '" . str_replace("|","",$rval) . "';
  prevh=document.getElementById('htimestamp').value;
  document.getElementById('favouritename').focus();
  var asis=document.getElementsByTagName('a');
  for (var ia=0; ia<asis.length; ia++) {
    if (('' + asis[ia].href).toLowerCase().indexOf('http') == 0) {
      asis[ia].onmouseover = function(evt) { lastain=evt.target;  setTimeout(preajaxit,5000); }
      asis[ia].onmouseout = function(evt) { lastinurl=' '; lastain=null; }
      asis[ia].onclick = function(evt) { lastain=null; }
    }
  }
 }
 
 function ouralert() {
  alert(alm);
 }
 
 function fs() {
  if (!dou && prevh != '' && document.getElementById('htimestamp').value != prevh) {
    document.getElementById('htimestamp').value=prevh;
  }
  if (document.getElementById('favouritename').value.trim() == '') { 
    alm=('Not sent for moderation due to incomplete form below'); 
    setTimeout(ouralert,2000); 
    return false; 
  } else if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) {  
    alm=('Sent for moderation to RJM Programming'); 
    //setTimeout(ouralert,2000); 
    return true; 
  } 
  return true;
 }

 function tpi(iois) {
    if (iois.value.indexOf('-') != -1 && (document.getElementById('tlink').value == '10' || document.getElementById('tlink').value == '13')) {
      iois.value=iois.value.replace(/\-/g,'');
    }
 }

 function dosfe(selo) {
    if (selo.value != '') {
      document.getElementById('femail').innerHTML=selo.value;
      document.getElementById('oemail').innerHTML=' (required)';
      if (selo.value == 'Email') {
       document.getElementById('favouriteemail').type='email';
       document.getElementById('favouriteemail').required=true;
      } else if (selo.value == 'SMS') { 
       document.getElementById('favouriteemail').type='tel';
       document.getElementById('favouriteemail').required=true;
      }
    }
    selo.value='';
 }
 
 function tlinkc(selo) {
    if (selo.value == '') {
      document.getElementById('favouritevalue').type='text';
      document.getElementById('favouritevalue').required=false;
      document.getElementById('htimestamp').value=document.getElementById('htimestamp').value.trim() + '';
      prevh=prevh.trim() + '';
      document.getElementById('slink').innerHTML='';
      document.getElementById('favouritevalue').pattern='';
      document.getElementById('favouritevalue').placeholder='';
      document.getElementById('favouritevalue').minlength=0;
      document.getElementById('favouritevalue').maxlength=1000;
    } else if (selo.value.trim() == '') { // URL
      document.getElementById('favouritevalue').type='url';
      document.getElementById('favouritevalue').required=true;
      document.getElementById('htimestamp').value=document.getElementById('htimestamp').value.trim() + '';
      prevh=prevh.trim() + '';
      document.getElementById('slink').innerHTML=' (required)';
      document.getElementById('favouritevalue').pattern='[Hh][Tt][Tt][Pp][Ss]?:\/\/(?:(?:[a-zA-Z\u00a1-\uffff0-9]+-?)*[a-zA-Z\u00a1-\uffff0-9]+)(?:\.(?:[a-zA-Z\u00a1-\uffff0-9]+-?)*[a-zA-Z\u00a1-\uffff0-9]+)*(?:\.(?:[a-zA-Z\u00a1-\uffff]{2,}))(?::\d{2,5})?(?:\/[^\s]*)?';
      document.getElementById('favouritevalue').placeholder='';
      document.getElementById('favouritevalue').minlength=0;
      document.getElementById('favouritevalue').maxlength=1000;
    } else if (selo.value == '0') { // SMS
      document.getElementById('favouritevalue').type='tel';
      document.getElementById('htimestamp').value=document.getElementById('htimestamp').value.trim() + ' ';
      prevh=prevh.trim() + ' ';
      document.getElementById('favouritevalue').required=true;
      document.getElementById('slink').innerHTML=' (required)';
      document.getElementById('favouritevalue').pattern='';
      document.getElementById('favouritevalue').placeholder='';
      document.getElementById('favouritevalue').minlength=0;
      document.getElementById('favouritevalue').maxlength=1000;
    } else if (selo.value == '@') { // Email
      document.getElementById('favouritevalue').type='email';
      document.getElementById('favouritevalue').required=true;
      document.getElementById('htimestamp').value=document.getElementById('htimestamp').value.trim() + '';
      prevh=prevh.trim() + '';
      document.getElementById('slink').innerHTML=' (required)';
      document.getElementById('favouritevalue').pattern='';
      document.getElementById('favouritevalue').placeholder='';
      document.getElementById('favouritevalue').minlength=0;
      document.getElementById('favouritevalue').maxlength=1000;
    } else if (selo.value == '11') { // YouTube ID
      // Q_qA89dmGco
      document.getElementById('favouritevalue').type='text';
      document.getElementById('htimestamp').value=document.getElementById('htimestamp').value.trim() + '';
      prevh=prevh.trim() + '';
      document.getElementById('favouritevalue').required=true;
      document.getElementById('slink').innerHTML=' (required)';
      document.getElementById('favouritevalue').pattern='[a-zA-Z0-9-_]{11}$';
      if (document.getElementById('favouritevalue').value == '') {
        document.getElementById('favouritevalue').placeholder='Q_qA89dmGco';
      }
      document.getElementById('favouritevalue').minlength=11;
      document.getElementById('favouritevalue').maxlength=11;
    } else if (selo.value == '10') { // ISBN 10 digits
      // 0-297-81242-4
      document.getElementById('favouritevalue').type='text';  // 'number'
      document.getElementById('htimestamp').value=document.getElementById('htimestamp').value.trim() + '';
      prevh=prevh.trim() + '';
      document.getElementById('favouritevalue').required=true;
      document.getElementById('slink').innerHTML=' (required)';
      document.getElementById('favouritevalue').pattern='[0-9]{10}$';
      if (document.getElementById('favouritevalue').value == '') {
        document.getElementById('favouritevalue').placeholder='0-297-81242-4';
      }
      document.getElementById('favouritevalue').minlength=10;
      document.getElementById('favouritevalue').maxlength=13;
    } else if (selo.value == '13') { // ISBN 13 digits
      // 9-781760-527334
      document.getElementById('favouritevalue').type='text';  // 'number'
      document.getElementById('htimestamp').value=document.getElementById('htimestamp').value.trim() + '';
      prevh=prevh.trim() + '';
      document.getElementById('favouritevalue').required=true;
      document.getElementById('slink').innerHTML=' (required)';
      document.getElementById('favouritevalue').pattern='[0-9]{13}$';
      if (document.getElementById('favouritevalue').value == '') {
        document.getElementById('favouritevalue').placeholder='9-780297-812425';
      }
      document.getElementById('favouritevalue').minlength=13;
      document.getElementById('favouritevalue').maxlength=15;
    }
 }
 
 function newtype(odiv) {
    if ((odiv.innerText || odiv.contentWindow || odiv.contentDocument).trim() == '') {
      odiv.innerHTML='Type';
    } else {
      mytype=(odiv.innerText || odiv.contentWindow || odiv.contentDocument);
      umytype=mytype;
      if (umytype.substring(0,1) == umytype.substring(0,1).toLowerCase()) {
        umytype=umytype.replace(umytype.substring(0,1).toLowerCase(), umytype.substring(0,1).toUpperCase());
      }
      if (alltogether.indexOf(umytype) == -1) {
        document.getElementById('favouritetype').innerHTML+='<option value=\"' + umytype + '\">' + umytype + '</option>';
        document.getElementById('favouritetype').value=umytype;
        dou=true;
        selcon=document.getElementById('favouritetype').outerHTML;
        selconv=document.getElementById('favouritetype').value;
        prevh=document.getElementById('htimestamp').value;
        document.getElementById('htimestamp').value+=umytype;
      } 
      odiv.innerHTML='Type';
    }
 }
 
 function dnewtype(odiv) {
    if ((odiv.innerText || odiv.contentWindow || odiv.contentDocument).trim() == '') {
      odiv.innerHTML=selcon;
      document.getElementById('favouritetype').value=selconv;
    } else {
      mytype=(odiv.innerText || odiv.contentWindow || odiv.contentDocument);
      umytype=mytype;
      if (umytype.substring(0,1) == umytype.substring(0,1).toLowerCase()) {
        umytype=umytype.replace(umytype.substring(0,1).toLowerCase(), umytype.substring(0,1).toUpperCase());
      }
      if (alltogether.indexOf(umytype) == -1) {
        odiv.innerHTML=selcon;
        document.getElementById('favouritetype').innerHTML+='<option value=\"' + umytype + '\">' + umytype + '</option>';
        document.getElementById('favouritetype').value=umytype;
        dou=true;
        selcon=document.getElementById('favouritetype').outerHTML;
        selconv=document.getElementById('favouritetype').value;
        document.getElementById('htimestamp').value+=umytype;
      } else {
        selconv=umytype;
      }
      odiv.innerHTML=selcon;
      document.getElementById('favouritetype').value=selconv;
    }
 }
 
 function dotdtypek(event) {
    if (event.target.innerHTML.indexOf('<') != -1) {
      event.target.innerHTML='';
    }
 }
 
 function huhit(selval) {
   if (selval != '' && umytype != '' && selval.toLowerCase() != umytype.toLowerCase()) {
     dou=false;
   } else if (selval != '' && umytype != '' && selval.toLowerCase() == umytype.toLowerCase()) { 
     dou=true;
   }
 }

function maybeshowStuff(evt) {
  if (zhr != null) {
  if (zhr.readyState == 4) {
    if (zhr.status == 200) {
    alm=zhr.responseText;
    if (ourwo) {
      if (!ourwo.closed) {
        ourwo.close();
        ourwo=null;
      } else {
        ourwo=null;
      }
    }
    document.getElementById('ajaxd').innerHTML=alm;
    document.getElementById('ajaxd').style.top='' + arect.top + 'px';
    document.getElementById('ajaxd').style.left='' + eval(100 + eval('' + arect.left)) + 'px';
    document.getElementById('ajaxd').style.display='block';
    }
  }
  }
 }
 
 function postalm() {
  if (alm == '') {
    if (ourwo) {
      if (!ourwo.closed) {
        ourwo.close();
        ourwo=null;
      } else {
        ourwo=null;
      }
    }
    ourwo=window.open(inurl, '_blank', 'top=' + arect.top + ',left=' + eval(100 + eval('' + arect.left)) + ',width=600,height=600');
  }
 }
 
 function preajaxit() {
  if (lastain) {
    ajaxit(lastain);
    lastain=null;
  }
 }
 
 function ajaxit(ain) {
  alm='';
  inurl=ain.href;
  if (inurl != lastinurl) {
  if (document.getElementById('ajaxd').innerHTML != '') {
    document.getElementById('ajaxd').innerHTML='';
    document.getElementById('ajaxd').style.top='-2000' + 'px';
    document.getElementById('ajaxd').style.left='-2000' + 'px';
    document.getElementById('ajaxd').style.display='none;';
  }
  arect=ain.getBoundingClientRect();
  lastinurl=inurl;
  setTimeout(postalm, 2000); 
  zhr = new XMLHttpRequest(); 
  zhr.open('get', ain.href, true); 
  zhr.onreadystatechange = maybeshowStuff;
  zhr.send(null);
  }
 }

</script>
</head>
<body onload=ol();>
<h1>Favourites Poll</h1>
<h3>Moderated by <a href='mailto:rmetcalfe@rjmprogramming.com.au?Subject=Favourites'>RJM Programming</a> - November, 2020</h3>
<form onsubmit=\" return fs(); \" method=POST action=./favourites_midair.php>
<input id=htimestamp name=htimestamp type=hidden value=''></input>
<table border=1 style='width:100%;'>
<tr style='background-color:#f0f0f0;'><th colspan=6><input title='Click to have your entry below be moderated by RJM Programming' style='background-color: pink;font-weight:bold;' type=submit value='Favourite'></input></th></tr>
<tr style='background-color:#f7f7f7;'><th id='thfavouritetimestamp'>Datetime</th><th id='thfavouritetype'><div onblur='newtype(this);' title='Can moderate a new type you define via your keyboard here, as required' contenteditable=true id=thtype>Type</div></th><th title='Required' id='thfavouritename'>Title (by Creator) <font size=1>(required)</font></th><th id='thfavouritevalue' title='http: or https: URL or 11 character YouTube ID or 10 or 13 digit ISBN'><select title='Optional' id=tlink onchange=tlinkc(this);><option value=''></option><option value='11'>YouTube ID</option><option value='10'>ISBN 10 digits</option><option value='13'>ISBN 13 digits</option><option value=' '>URL</option><option value='@'>Email</option><option value='0'>SMS</option></select> Link <span id=slink style='font-size:10px;'></span></th><th id='thfavouriteemail' title='Email or SMS number'><span id=femail>Email</span> <select title='Optional' id=sfe onchange=dosfe(this); style='font-size:10px;'><option id=oemail value=''></option><option value='SMS'>SMS</option><option value='Email'>Email</option></select></th><th title='Optional' id='thfavouriteshare''>Comments</th></tr>
<tr><td id='tdfavouritetimestamp'><input id=favouritetimestamp name=favouritetimestamp type=datetime value=''></input></td><td id='tdfavouritetype'><div title='Use keyboard to enter your own new type' onblur=dnewtype(this); onkeydown=dotdtypek(event); contenteditable=true id=tdtype><select onchange='selconv=huhit(this.value);' id=favouritetype name=favouritetype><option value=Book>Book</option><option value=Movie>Movie</option><option value=Play>Play</option><option value=Song>Song</option><option value=Video>Video</option><option value=Audio>Audio</option>" . $anyextras . "</select></div></td><td id='tdfavouritename'><input id=favouritename name=favouritename type=text value='' required></input></td><td id='tdfavouritevalue'><input onblur=tpi(this); id=favouritevalue name=favouritevalue type=text value=''></input></td><td id='tdfavouriteemail'><input id=favouriteemail name=favouriteemail type=text value=''></input></td><td id='tdfavouriteshare''><textarea name=favouriteshare value=''></textarea></td></tr>
</table>
</form>
<a id=aemail style=display:none; href='mailto:rmetcalfe@rjmprogramming.com.au?subject='>Email</a><input type=text value='' style='position:absolute;top:-3000px;left:-3000px;'></input>
" . $sofar . "
<div id=ajaxd style='position:absolute;z-index:67;top:-2000px;left:-2000px;width:600px;height:600px;display:none;'></div>
</body>
</html>";




?>