/* main.js 1.0.0  24 Oct 21
Copyright 2021 Del Castle */
const cS="rgb(202,222,190)";const cT="black";const kP="pk_test_51JmX36FJZ3VHcyJ0dhw8THMTkexXo1JANCJZshwK5pVK9nFjPx1BB7lkpXnZH1AMCO6llR6uKMWhHh57pC0B7pqY00T1IY4LOh";const $=id=>document.getElementById(id);function fPick(oB,iS){function fRset(tN){$("b"+tN).style.backgroundColor=cS;$("b"+tN).style.color=cT;$("d"+tN).style.display="none";}
fRset("Menu");fRset("Ordr");fRset("Locn");fRset("Bout");oB.style.backgroundColor=cT;oB.style.color=cS;$(iS).style.display="block";}
function fMenu(tF){const aM=oMenu.replace(/\r?\n$/m,"").split(/\r?\n/).map(item=>item.trim());var xF,nL,nW=0,tS="; ",tI="; ",tO="; ",tN,aL;var hM="<img src='img/find.png'><input type='text' id='qFind' maxlength='20' value='"+tF+"' onkeyup='fMenu(this.value.toLowerCase())'><br>";for(nL=1;nL<aM.length;nL++){aL=aM[nL].split(";");if((aL[0]||aL[1])&&(aL[2].length>nW)){nW=aL[2].length;}}
for(nL=1;nL<aM.length;nL++){xF=(tF&&aM[nL].toLowerCase().includes(tF));aL=aM[nL].split(";");if(aL[0]&&!aL[1]&&!aL[2]){tS="<hr class='sect'><span class='mitm'>"+aL[0]+"</span><br>";tI="; ";tO="; ";if(!tF||xF){if(xF){tS=" "+tS;}
hM+=tS;tS+=" ";}}else if(!aL[0]&&aL[1]&&!aL[2]){tI="<hr class='item'><span class='mitm'>"+aL[1]+"</span><br>";tO="; ";if(!tF||xF||(tS[0]==" ")){if(xF){if(tS[tS.length-1]!=" "){hM+=tS;tS+=" ";}
tI=" "+tI;}
hM+=tI;tI+=" ";}}else if((aL[0]||aL[1])&&aL[2]){if(aL[0]){tO="&nbsp;".repeat(nW-aL[2].length)+aL[2]+"&nbsp;&nbsp;"+aL[0]+"<br>";}else{tO="&nbsp;".repeat(nW-aL[2].length)+aL[2]+"&nbsp;&nbsp;"+aL[1]+"<br>";}
if(!tF||xF||(tS[0]==" ")||(tI[0]==" ")){if(xF){if(tS[tS.length-1]!=" "){hM+=tS;tS+=" ";}
if(tI[tI.length-1]!=" "){hM+=tI;tI+=" ";}
tO=" "+tO;}
hM+=tO;tO+=" ";}}else if(!aL[0]&&!aL[1]&&aL[2]){tN="<sup>"+aL[2]+"</sup><br>";if(!tF||xF||(tS[0]==" ")||(tI[0]==" ")||(tO[0]==" ")){if(xF){if(tS[tS.length-1]!=" "){hM+=tS;tS+=" ";if((tI[0]==";")&&(tO[0]==";")){tS=" "+tS;}}
if(tI[tI.length-1]!=" "){hM+=tI;tI+=" ";if(tO[0]==";"){tI=" "+tI;}}
if(tO[tO.length-1]!=" "){hM+=tO;tO+=" ";}}
hM+=tN;}else if((tI[0]==";")&&(tO[0]==";")){tS+=tN;}else if(tO[0]==";"){tI+=tN;}}}
hM+="<hr class='sect'>";$("sMenu").innerHTML=hM;$("qFind").focus();$("qFind").setSelectionRange(20,20);}
function fUnit(){const aU=$("dPkup").innerText.match(/^\u200C.+/gm);const aX=$("dPkup").innerText.match(/^\+ .+/gm);var tU="PICK UP"+aU.join("").replace(/\u200C/g,";");if(aX){tU+=";"+aX.join("");}
return encodeURIComponent(tU);}
function fPay(){const aB=$("dBill").innerText.replace(/\r?\n$/m,"").split(/\r?\n/);const aP=$("dBill").textContent.match(/~[\w\d]+\+/g);var stripe=Stripe(kP);var oF,aL,aI=[];for(var nL=0;nL<aB.length;nL++){aL=aB[nL].split("\t");aL[0]=aP[nL].replace(/(~|\+)/g,"");oF=aI.find((oI)=>{return oI.price==aL[0];});if(oF){oF.quantity+=parseInt(aL[4]);}else{aI.push({price:aL[0],quantity:parseInt(aL[4])});}}
stripe.redirectToCheckout({lineItems:aI,mode:"payment",successUrl:"https://"+location.hostname+"/?pay=good",cancelUrl:"https://"+location.hostname+"/?item="+fUnit()}).then(function(result){if(result.error){console.error(result.error.message);}});}
function fUpdt(oR,vS,xO){var nQ=parseInt(oR.cells[4].innerText);if(nQ<99){const nC=parseFloat(oR.cells[2].innerText);nQ+=vS;oR.cells[4].innerText=nQ;$("dTotl").innerText=(parseFloat($("dTotl").innerText)+(nC*vS)).toFixed(2);if(!nQ){var oT=oR.parentNode;if(!oR.rowIndex){while(oT.rows.length>1){fUpdt(oT.rows[1],-1,false);}
oT.parentNode.parentNode.parentNode.removeChild(oT.parentNode.parentNode);if(!$("dBill").childElementCount){$("dCart").style.display="none";}}else{oT.deleteRow(oR.rowIndex);if(xO&&(oT.rows.length==1)){var oD=document.getElementsByClassName("ditm");for(var nL=0;nL<oD.length;nL++){if(oD[nL].innerText==oT.rows[0].cells[0].innerText){fUpdt(oD[nL].parentNode,1,true);fUpdt(oT.rows[0],-1,true);return;}}
oT.rows[0].cells[0].className="ditm";oT.rows[0].cells[5].onclick=function(){fUpdt(this.parentNode,1,true);};oT.rows[0].cells[5].innerHTML="<img src='img/plus.png'>";}}}}}
function fAdd(){const aU=$("dPkup").innerText.match(/^\u200C.+/gm);const tU=aU.join("").replace(/\u200C/g,": ").slice(2);const aP=$("dPkup").textContent.match(/~[\w\d]+\+/g);var nL,nC=0.0,tC,hU="<table>";var aL=tU.split("\u00A0\u00A0$");var aA=$("dPkup").innerText.match(/^\+ .+/gm);if(aA){aA.unshift(tU);tC="style='padding-right:5px'";}else{var oD=document.getElementsByClassName("ditm");for(nL=0;nL<oD.length;nL++){if(oD[nL].innerText==aL[0]){fUpdt(oD[nL].parentNode,1,true);return;}}
aA=[tU];tC="class='ditm'";}
for(nL=0;nL<aA.length;nL++){aL=aA[nL].split("\u00A0\u00A0$");hU+="<tr><td "+tC+" onclick='fPkup(\"PICK UP"+aU.join("").replace(/\u200C/g,";")+"\");fPick($(\"bOrdr\"),\"dOrdr\")'>"+aL[0]+"</td><td>$</td>";hU+="<td>"+parseFloat(aL[1]).toFixed(2)+"</td><td onclick='fUpdt(this.parentNode,-1,true)'><img src='img/minus.png'></td><td>1</td>";if((nL==0)&&(aA.length>1)){hU+="<td></td><td style='display:none'>"+aP[nL]+"</td></tr>";}else{hU+="<td onclick='fUpdt(this.parentNode,1,true)'><img src='img/plus.png'></td><td style='display:none'>"+aP[nL]+"</td></tr>";}
nC+=parseFloat(aL[1]);}
hU+="</table>";var dU=document.createElement("div");dU.className="citm";dU.innerHTML=hU;$("dBill").appendChild(dU);$("dTotl").innerText=(parseFloat($("dTotl").innerText)+nC).toFixed(2);$("dCart").style.display="block";}
function fPkup(tU){const aM=oMenu.replace(/\r?\n$/m,"").split(/\r?\n/);const aU=tU.split(";");var xS=false,xI=false,xO=false,xN=false,xX=false,xB=false,nL,vS=0,vI=0,aL;var hP="<span class='ohdr' onclick='fPkup(\""+aU[0]+"\")'>"+aU[0]+"</span>";if(aU.length>1){for(nL=1;nL<aM.length;nL++){aL=aM[nL].split(";");if(aL[0]&&!aL[1]&&!aL[2]){if(xS||xI){break;}
vS=1;if(aU.includes(aL[0])){xS=true;}}else if(!aL[0]&&aL[1]&&!aL[2]&&(aL[1]!="Extras")&&(!vS||xS)){if(xI){break;}
vI=1;if(aU.includes(aL[1])){xI=true;}}else if(!aL[0]&&aL[1]&&aL[2]&&(!vS||xS)&&(!vI||xI)){if(aU.includes(aL[1]+"\u00A0\u00A0"+aL[2])){xO=true;break;}}}}
for(nL=1;nL<aM.length;nL++){aL=aM[nL].split(";");if(xN){if(!aL[0]&&!aL[1]&&aL[2]){hP+="<br><sup>"+aL[2]+"</sup></span>";}else{hP+="</span>";}
xN=false;}
if(aL[0]&&!aL[1]&&!aL[2]){vS=1;vI=0;xX=false;if(!xS){hP+="<br><span class='oitm' onclick='fPkup(\""+aU[0]+";"+aL[0]+"\")'>"+aL[0];xN=true;}else if(aU.includes(aL[0])){hP+="<br><span class='opik' onclick='fPkup(\""+aU[0]+";"+aL[0]+"\")'>&zwnj;"+aL[0];xN=true;vS=2;}}else if(!aL[0]&&aL[1]&&!aL[2]&&(aL[1]!="Extras")){vI=1;if(!xI){if(!vS){hP+="<br><span class='oitm' onclick='fPkup(\""+aU[0]+";"+aL[1]+"\")'>"+aL[1];xN=true;}else if(vS==2){hP+="<br><span class='oitm' onclick='fPkup(\""+aU[0]+";"+aU[1]+";"+aL[1]+"\")'>"+aL[1];xN=true;}}else if(aU.includes(aL[1])){if(!vS){hP+="<br><span class='opik' onclick='fPkup(\""+aU[0]+";"+aL[1]+"\")'>&zwnj;"+aL[1];xN=true;vI=2;}else if(vS==2){hP+="<br><span class='opik' onclick='fPkup(\""+aU[0]+";"+aU[1]+";"+aL[1]+"\")'>&zwnj;"+aL[1];xN=true;vI=2;}}}else if(!aL[0]&&aL[1]&&aL[2]){if(!xO){if(!vS&&!vI){hP+="<br><span class='oitm' onclick='fPkup(\""+aU[0]+";"+aL[1]+"\u00A0\u00A0"+aL[2]+"\")'>"+aL[1]+"&nbsp;&nbsp;"+aL[2];xN=true;}else if((vS==2)&&(vI==2)){hP+="<br><span class='oitm' onclick='fPkup(\""+aU[0]+";"+aU[1]+";"+aU[2]+";"+aL[1]+"\u00A0\u00A0"+aL[2]+"\")'>"+aL[1]+"&nbsp;&nbsp;"+aL[2];xN=true;}else if((vS!=1)&&(vI!=1)){hP+="<br><span class='oitm' onclick='fPkup(\""+aU[0]+";"+aU[1]+";"+aL[1]+"\u00A0\u00A0"+aL[2]+"\")'>"+aL[1]+"&nbsp;&nbsp;"+aL[2];xN=true;}}else if(aU.includes(aL[1]+"\u00A0\u00A0"+aL[2])&&(vS!=1)&&(vI!=1)){hP+="<br><span style='display:none'>~"+aL[3]+"+</span><span class='opik'>&zwnj;"+aL[1]+"&nbsp;&nbsp;"+aL[2];xN=true;xX=true;xB=true;}}else if(aL[0]&&!aL[1]&&aL[2]&&xX){if(aU[aU.length-1].includes("+ "+aL[0]+"\u00A0\u00A0"+aL[2])){hP+="<br><span style='display:none'>~"+aL[3]+"</span><span class='opik' onclick='if(this.className==\"oitm\"){this.className=\"opik\";this.innerText=\"+\"+this.innerText.slice(1);}else{this.className=\"oitm\";this.innerText=\"&nbsp;\"+this.innerText.slice(1);}'>+ "+aL[0]+"&nbsp;&nbsp;"+aL[2];}else{hP+="<br><span style='display:none'>~"+aL[3]+"</span><span class='oitm' onclick='if(this.className==\"oitm\"){this.className=\"opik\";this.innerText=\"+\"+this.innerText.slice(1);}else{this.className=\"oitm\";this.innerText=\"&nbsp;\"+this.innerText.slice(1);}'>&nbsp; "+aL[0]+"&nbsp;&nbsp;"+aL[2];}
xN=true;}}
if(xN){hP+="</span>";}
if(xB){hP+="<br><button type=button class='pkup' onclick='fAdd()'>ADD TO CART</button><img src='img/bmrk.png' title='Bookmark Item' onclick='$(\"dBmtx\").innerText=\"https://\"+location.hostname+\"/?item=\"+fUnit();$(\"dBmrk\").style.display=\"block\"'><div id='dBmrk' style='display:none'><span class='text'>Copy to bookmark address/URL:</span><br><div id='dBmtx' class='intx' contenteditable></div></div>";}
$("dPkup").innerHTML=hP;}
function fPara(){const oP=new URLSearchParams(location.search);if(oP.has("pay")&&(oP.get("pay")=="good")){$("sBanr").innerHTML="THANKS FOR YOUR PAYMENT<br>CONFIRMATION EMAIL SENT";$("dBanr").style.display="block";$("dRfsh").style.display="block";}else{if(oP.has("item")){fPkup(oP.get("item"));fPick($("bOrdr"),"dOrdr");}
$("dNav").style.display="block";}}
