<!doctype html>
<html>
<head>
<title>Fairy Story Phrase Helper - RJM Programming - April, 2019</title>
<script type='text/javascript'>
var lastokn='';
var okn='';
var oko=null;
var okt=null;
var okd=null;
var empty=true;
var sword="datalist"; // "select";
var nooc=false;
var zero=1; //0;
var lsuff=0;

// Thanks to https://www.hitched.co.uk/chat/forums/thread/is-anyone-clued-up-on-fairytale-phrases-or-slogans-303336/ and http://www.surlalunefairytales.com/boardarchives/2007/sep2007/endingphrases.html and http://dm.net/~richard-parks
var fphrases="Long ago, when the wind spoke with a voice you could understand;There once was;Mukashi, Mukashi;Tale as old as time;a dream is a wish your heart makes;May all your wishes come true;true love's first kiss;they all lived happily ever after;und wenn sie nicht gestorben sind dann leben sie noch heute;and they lived happily ever after;When you wish upon a star;Miracles do come true;dreams come true;I can show you the world;You are my world;Happily ever after;believe in love;once upon a time";
var fphrasesa=fphrases.split(';');

function noc() {
if (sword == 'select' || 1 == 1) { oc(); }
}

function cleanslate() {
//okt.value+=okn;
okn='';
oko.value='';
empty=true;
okd.innerHTML='';
}


function oc() {
if (okn != '' && !nooc) {
okt=document.getElementById('thewholething');
if ((okt.value + '~').indexOf(okn + '~') == -1) {
for (var jj=eval(-1 + okn.length); jj>0; jj--) {
if ((okt.value + '~').indexOf(okn.substring(0,jj) + '~') != -1) {
okt.value+=okn.substring(jj).replace(/\ \ /g,',').replace(/\`/g,"'");
//alert(415);
if (sword == "datalist") {
okt.value+=' ';
okn='';
oko.value='';
empty=true;
okd.innerHTML='';
}
return;
}
}
}
}
nooc=false;
}

function obprecval(iois) {
//document.title=(iois.value + '~' + okn + '!');
if ((iois.value + '~').indexOf(iois.value + '~') == -1 && iois.value != '') {
okt=document.getElementById('thewholething');
okt.value+=iois.value.replace(/\ \ /g,',').replace(/\`/g,"'");
okn='';
oko.value='';
empty=true;
}
}

function precval(iois) {
//document.title=(iois.value + '~~' + okn + '!!');
if (iois.value.trim() == okn && okn != iois.value && okn != '' && sword == "datalist") {
okt=document.getElementById('thewholething');
okt.value+=okn.replace(/\ \ /g,',').replace(/\`/g,"'") + ' ';
okn='';
iois.value='';
empty=true;
//alert('here');
} else if (iois.value.trim().length < okn.trim().length) {
okt=document.getElementById('thewholething');
okt.value=okt.value.replace(okn, iois.value.replace(/\ \ /g,',').replace(/\`/g,"'"));
okn=iois.value;
if (okn.length == 0) {
empty=true;
} else {
empty=false;
noc();
}
} else if (okn.trim().length < iois.value.trim().length) {
okn=iois.value;
noc();
} else if (iois.value.trim().length == 0) {
okn=okn;
} else if (1 == 1 && iois.value.trim() == okn && okn == iois.value && okn.length >= 2 && sword == "datalist") {
okt=document.getElementById('thewholething');
if ((okt.value + '~').indexOf(okn + '~') == -1 && (okt.value + '~').indexOf(okn.substring(1) + '~') != -1) {
okt.value=(okt.value + '~').replace(okn.substring(1) + '~','') + ' ' + iois.value.replace(/\ \ /g,',').replace(/\`/g,"'") + '';
} else if (1 == 9 && (okt.value + '~').indexOf(okn + '~') == -1) {
okt.value+=' ' + iois.value + '';
}
//alert('here');
} else if (1 == 1 && iois.value.trim() == okn && okn == iois.value && okn.length == 1 && sword == "datalist") {
okt=document.getElementById('thewholething');
if ((okt.value + '~').indexOf(' ' + okn + '~') == -1 && okt.value.indexOf(' ') != -1) {
okt.value+=' ' + iois.value.replace(/\ \ /g,',').replace(/\`/g,"'") + '';
} else if (1 == 9 && (okt.value + '~').indexOf(okn + '~') == -1) {
okt.value+=' ' + iois.value.replace(/\ \ /g,',').replace(/\`/g,"'") + '';
}
//alert('here');
}
//else if ((okt.value + '~').indexOf(okn + '~') == -1) {
//okt.value+=okn;
//okn='';
//oko.value='';
//empty=true;
//}
if (iois.value == ' ' && sword == "datalist") { iois.value=''; okt+=' '; empty=true; }
}

function std(tv) {
okt=document.getElementById('thewholething');
okt.value+=tv.substring(zero).replace(/\ \ /g,',').replace(/\`/g,"'");
if (sword == "datalist") {
okn='';
oko.value='';
empty=true;
} else {
okn=' ';
oko.value=' ';
empty=true;
}
//if (document.getElementById('sideas' + eval(-1 + eval('' + lsuff)))) {
//document.getElementById('sideas' + eval(-1 + eval('' + lsuff))).innerHTML='';
//} else {
//document.getElementById('sideas' + lsuff).innerHTML='';
//}
okd.innerHTML='';
//alert(45);
}

function cval(eiois) {
var ideas=[], selbit='', semiempty=false, prefix='';
//alert(eiois.which);
//okn+=String.fromCharCode(eiois.which || eiois.keyCode);
noc();
if ((eiois.which || eiois.keyCode) == 8) {
okn='';
} else if ((eiois.which || eiois.keyCode) == 186 || (eiois.which || eiois.keyCode) == 900000016) {
okn=okn;
} else if ((eiois.which || eiois.keyCode) < 32) { // && (eiois.which || eiois.keyCode) != 32) { // || (eiois.which || eiois.keyCode) > 58) {
okn=okn;
} else {
//alert(eiois.which || eiois.keyCode);
okn+=String.fromCharCode(eiois.which || eiois.keyCode);
//document.title+='=' + okn + '+' + eiois.which + ' ' + eiois.keyCode + ' ... ';
}
if (okn.length > 0) {
if (okn.slice(-1).replace(',',' ').replace(';',' ').replace(':',' ').replace('!',' ').replace('"',' ').replace("'"," ").replace("("," ").replace("["," ").replace("{"," ").replace("<"," ").replace("@"," ") == ' ' && sword == 'datalist') {
cleanslate();
eiois.stopPropagation();
} else {
if (!semiempty && okn.length > 1) {
if (okn.slice(-2).substring(0,1).replace(',',' ').replace(';',' ').replace(':',' ').replace('!',' ').replace('"',' ').replace("'"," ").replace("("," ").replace("["," ").replace("{"," ").replace("<"," ").replace("@"," ") == ' ' && okn.slice(-1) != ' ') {
semiempty=true;
}
}
if (empty || semiempty) {
ideas=(' ;' + fphrases.toLowerCase() + '; ').split(';' + okn.slice(-1).toLowerCase());
} else {
if (okn.length == 2) {
if (okn.substring(0,1) == ' ') { okt.value+=' '; }
}
okt.value+=okn.slice(-1);
}
if (ideas.length >= 1) {
if (okn.length == 2) {
if (okn.substring(0,1) == ' ') { okt.value+=' '; }
}
okt.value+=okn.slice(-1);
}
}
} //else {
//document.title='' + (eiois.which || eiois.keyCode);
//}
if (ideas.length <= 1) {
empty=false;
okd.innerHTML="";
} else {
var there=false;
if (sword == 'datalist') {
selbit="<" + sword + " id=sideas" + lsuff + " onchange=' std(this.value); ' size=" + eval(-1 + ideas.length) + "></" + sword + ">";
} else {
selbit="<" + sword + " id=sideas" + lsuff + " onchange=' std(this.value); ' size=" + eval(0 + ideas.length) + "><option value=''>Optionally choose a stock fairy story phrase below</option></" + sword + ">";
}
for (var ij=1; ij<ideas.length; ij++) {
if (ideas[ij].split(';')[0] != '') {
if (sword == 'datalist') { prefix=okn.slice(-1); }
there=true;
selbit=selbit.replace("</" + sword + ">", "<option value='" + prefix + ideas[ij].split(';')[0].replace(/\,/g,' ').replace(/\'/g,"`") + "'>" + okn.slice(-1) + ideas[ij].split(';')[0] + "</option></" + sword + ">");
}
}
//alert(selbit);
if (there) { if (lsuff > 0 && sword == 'datalist') { oko.setAttribute("list","sideas" + lsuff); oko.datalist_id="sideas" + lsuff; } okd.innerHTML=selbit; document.getElementById("sideas" + lsuff).setAttribute('open',true); if (1 == 2) { document.getElementById('dover').innerHTML+=''; } else if (1 == 5) { document.title=(document.getElementById('dover').innerHTML); } lsuff++; }
}
}

</script>
</head>
<body style='background:linear-gradient(180deg, rgb(255,175,189), rgb(255,195,160));' onload=" oko=document.getElementById('tfs'); okt=document.getElementById('thewholething'); okd=document.getElementById('dsuggestions'); ">
<h1>Fairy Story Phrase Helper</h1>
<h3>RJM Programming - April, 2019</h3>

<div id=dover>
<input onchange=' nooc=true; std(this.value); ' list=sideas0 style='width:80%;' onclick=noc(); type=text placeholder='Enter fairy story snippet ...' onkeypress=cval(event); oninput=precval(this);; onblur=obprecval(this); value='' id=tfs></input><br><br>
<div id=dsuggestions></div><br><br>
</div>

<textarea placeholder=' ... that gets compiled into here.' id='thewholething' rows=20 cols=120></textarea>

</body>
</html>