<?php
// calculator.php - RJM Programming - April, 2018
// Calculator via middleman Tcl

$ahkxoff=-40;
$ahkyoff=-32;

$ahkmappings=["%,90,260",
"*-1*,84,314",
"C,248,260",
"7,88,370",
"4,91,429",
"1,91,481",
"+/-,91,538",
"*2*,169,314",
"8,169,370",
"5,169,429",
"2,169,481",
"0,169,538",
"*0.5*,248,314",
"9,248,370",
"6,248,429",
"3,248,481",
".,248,538",
"/,326,314",
"*,326,370",
"-,326,429",
"+,326,481",
"=,326,538"
];

$ahkprefix="IfWinExist Calculator
  WinActivate
else
  Run calculator://
  WinActivate calculator

// 75 / 8 =

CoordMode, Mouse, Window
Click 208,228
Sleep, 2500
";

if (isset($_GET['read']) && isset($_GET['via'])) {
  echo "<html><head><title>Calculator via TCL - RJM Programming - April, 2018</title>
  
  <script type='text/javascript'>
  var numprefix='';
  var lastpwr='';
  var lastsot='';
  
  var ahkthere=false;
  var ahkcthere=false;
  
  var ahkxoff=-40;
  var ahkyoff=-32;

  var ahkmappings=['%,90,260',
'*-1*,84,314',
'C,248,260',
'7,88,370',
'4,91,429',
'1,91,481',
'+/-,91,538',
'*2*,169,314',
'8,169,370',
'5,169,429',
'2,169,481',
'0,169,538',
'*0.5*,248,314',
'9,248,370',
'6,248,429',
'3,248,481',
'.,248,538',
'/,326,314',
'*,326,370',
'-,326,429',
'+,326,481',
'=,326,538'
];

  var wop=null;
  var ahkprefix='IfWinExist Calculator' + String.fromCharCode(10) + '  WinActivate' + String.fromCharCode(10) + 'else' + String.fromCharCode(10) + '  Run calculator://' + String.fromCharCode(10) + '  WinActivate calculator' + String.fromCharCode(10) + '' + String.fromCharCode(10) + '// 75 / 8 =' + String.fromCharCode(10) + '' + String.fromCharCode(10) + 'CoordMode, Mouse, Window' + String.fromCharCode(10) + 'Click 208,228' + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
  
  function isNumeric(n) { // thanks to https://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers
    return !isNaN(parseFloat(n)) && isFinite(n);
  }  
  function ourprompt(ourp, ourd) {
    var oura='*', noura='*', oklistfirst='(-0123456789.';
    while (!isNumeric(noura.replace('e',''))) {
      oura=prompt(ourp, ourd);
      if (oura == null) {
        noura='0';
        oura='';
      } else if (oura.trim() == '') {
        noura='0';
        oura='';
      } else {
        noura=oura;
        if (!isNumeric(noura.replace('e',''))) {
          var csuff='';
          try {
            if (oklistfirst.indexOf(oura.trim().toLowerCase().replace('math.pow(','0').replace('pow(','0').substring(0,1)) != -1) {
            csuff=eval('' + oura);
            alert(oura + ' = ' + csuff);
            }
          } catch (eee) { }
        }
      }
    }
    return oura;
  }
  function lval() {
   document.getElementById(lastsot).value='9';
   rest(document.getElementById(lastsot));
  }
  function rest(sot) {
   var pwr='';
   var was=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML;
   if (sot.id == 'fns') {
     if (sot.value == '9') {
       //pwr=prompt('Enter number (the way to achieve ** is via ... Math.pow([number], ... here and add an extra ) on the other side, or 3 ** 2 would be ... Math.pow(3,2))','1.0');
       pwr=ourprompt('Enter number','1.0');
       if (pwr == null) pwr='';
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('</select>','</select>' + numprefix.replace('Math.pow','') + pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ',''));
       document.getElementById('fi').value=document.getElementById('fi').value.replace('</select>','</select>' + numprefix + pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ',''));
       //document.getElementById('fi').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('</select>')[1];
       numprefix='';
     } else if (sot.value == '**') {
       numprefix='Math.pow(';
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('</select>','</select>**');
       document.getElementById('sn').innerHTML=')' + document.getElementById('sn').innerHTML;
       document.getElementById('si').value=')' + document.getElementById('si').value;
       document.getElementById('osi').innerHTML=': Second Bit';
       document.getElementById('fi').value=document.getElementById('fi').value.replace('</select>','</select>,');
       //document.getElementById('fi').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('</select>')[1];
       lastsot=sot.id;
       setTimeout(lval,400);
     } else {
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('</select>','</select>' + sot.value);
       document.getElementById('fi').value=document.getElementById('fi').value.replace('</select>','</select>' + sot.value);
       //document.getElementById('fi').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('</select>')[1];
     }
     document.getElementById('ofi').innerHTML='First Bit :';
   } else if (sot.id == 'sns') {
     if (sot.value == '9') {
       //pwr=prompt('Enter number (the way to achieve ** is via ... Math.pow([number],[number] ... eg. 3 ** 2 would be ... Math.pow(3,2))','1.0');
       pwr=ourprompt('Enter number','1.0');
       if (pwr == null) pwr='';
       if (document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.indexOf(',)') != -1 || document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.indexOf('**)') != -1) {
        document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('**)', '**' + pwr + ')').replace(',)', ',' + pwr + ')');
        document.getElementById('si').value=document.getElementById('si').value.replace(',)', ',' + pwr + ')');
        //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       } else {
        lastpwr=pwr;
        document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('<select',pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ','') + '<select');
        document.getElementById('si').value=document.getElementById('si').value.replace('<select',pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ','') + '<select');
        //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       }
     } else if (sot.value == '**') {
       if (lastpwr == '') {
       //lastpwr=prompt('Enter number (the way to achieve ** is via ... Math.pow([number],[number] ... eg. 3 ** 2 would be ... Math.pow(3,2))','1.0');
       lastpwr=ourprompt('Enter number','1.0');
       if (lastpwr == null) lastpwr='';
       //document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('<select','Math.pow(' + lastpwr + ',)' + '<select');
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('<select','(' + lastpwr + '**)' + '<select');
       document.getElementById('si').value=document.getElementById('si').value.replace('<select','Math.pow(' + lastpwr + ',)' + '<select');
       //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       } else {
       //document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace(lastpwr,'Math.pow(' + lastpwr + ',)');
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace(lastpwr,'(' + lastpwr + '**)');
       document.getElementById('si').value=document.getElementById('si').value.replace(lastpwr,'Math.pow(' + lastpwr + ',)');
       //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       }
       lastpwr='';
       lastsot=sot.id;
       setTimeout(lval,400);
     } else {
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('<select',sot.value + '<select');
       document.getElementById('si').value=document.getElementById('si').value.replace('<select',sot.value + '<select');
       //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
     }
     document.getElementById('osi').innerHTML=': Second Bit';
   }
  }
  
  function numberWithCommas(x) {  // thanks to https://www.codegrepper.com/code-examples/javascript/add+commas+to+large+numbers+js#:~:text=%E2%80%9Cadd%20commas%20to%20large%20numbers%20js%E2%80%9D%20Code%20Answer's&text=given%20an%20integer%20n%2C%20add,return%20it%20in%20string%20format.&text=given%20an%20integer%20n%2C%20add%20a%20dot%20(%22.,return%20it%20in%20string%20format.
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  }  
  
  function conl() {
    document.getElementById('oper').value=location.search.split('clientoper=')[1] ? decodeURIComponent(location.search.split('clientoper=')[1].split('&')[0]) : document.getElementById('oper').value;
    document.getElementById('firstnum').value=location.search.split('clientfirstnum=')[1] ? numberWithCommas(decodeURIComponent(location.search.split('clientfirstnum=')[1].split('&')[0])) : document.getElementById('firstnum').value;
    document.getElementById('secnum').value=location.search.split('clientsecnum=')[1] ? numberWithCommas(decodeURIComponent(location.search.split('clientsecnum=')[1].split('&')[0])) : document.getElementById('secnum').value;
    var zahkthere=location.search.split('ahks=')[1] ? decodeURIComponent(location.search.split('ahks=')[1].split('&')[0]) : '';
    if (zahkthere == 'y') {
       ahkthere=true;
       document.getElementById('ahks').value='y';
   } else if (zahkthere == 'n') {
      ahkthere=false;
      document.getElementById('ahks').value='n';
    }
    if (window.localStorage) {
      if (('' + window.localStorage.getItem('ahkthere')).replace(/^null$/g,'').toLowerCase == 'y') {
      document.getElementById('ahks').value='y';
      ahkthere=true;
      } else if (!ahkthere && document.getElementById('ahks').value == '' && window.self === window.top && !navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
    wop=window.open('HTTP://localhost/send_over.php?ahkthere=y','_blank','left=' + eval(-100 + eval('' + screen.width)) + ',top=100,width=100,height=100' );
    setTimeout(wopcheck, 5000);
      }
    } else if (!ahkthere && document.getElementById('ahks').value == '' && window.self === window.top && !navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
    wop=window.open('HTTP://localhost/send_over.php?ahkthere=y','_blank','left=' + eval(-100 + eval('' + screen.width)) + ',top=100,width=100,height=100' );
    setTimeout(wopcheck, 5000);
    }
  }
  
  function nocommas() {
    var ij=0, ji=0;
    document.getElementById('firstnum').value=document.getElementById('firstnum').value.replace(/\,/g,'');
    document.getElementById('secnum').value=document.getElementById('secnum').value.replace(/\,/g,'');
    var matchwith=document.getElementById('oper').value;
    var operx=0, opery=0;
    if (matchwith == '**' && document.getElementById('secnum').value == '2') { 
      matchwith='*2*'; 
    } else if (matchwith == '**' && document.getElementById('secnum').value == '0.5') { 
      matchwith='*0.5*'; 
    } else if (matchwith == '**' && document.getElementById('secnum').value == '-1') { 
      matchwith='*-1*'; 
    }
    for (ij=0; ij<ahkmappings.length; ij++) {
      if (('' + ahkmappings[ij]).trim().indexOf(matchwith + ',') == 0) {
        operx=eval(eval('' + ahkxoff) + eval('' + ahkmappings[ij].split(matchwith + ',')[1].split(',')[0]));
        opery=eval(eval('' + ahkyoff) + eval('' + ahkmappings[ij].split(matchwith + ',')[1].split(',')[1]));
        matchwith+=' ';        
      }
    }
    if (matchwith .indexOf(' ') != -1) {
    for (ij=0; ij<eval('' + document.getElementById('firstnum').value.length); ij++) {
      //alert('*' + ('' + ahkmappings[ij]).trim().split(',')[0] + '*');
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (('' + ahkmappings[ji]).trim().indexOf(document.getElementById('firstnum').value.substring(ij).substring(0,1) + ',') == 0) {
      //alert(document.getElementById('firstnum').value.substring(ij).substring(0,1) + ',');
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    }
    ahkprefix+='Click ' + operx + ',' + opery + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
    if (matchwith.length <= 3) {
    for (ij=0; ij<document.getElementById('secnum').value.length; ij++) {
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (('' + ahkmappings[ji]).trim().indexOf(document.getElementById('secnum').value.substring(ij).substring(0,1) + ',') == 0) {
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    }
    }
    for (ij=0; ij<eval('' + document.getElementById('secnum').value.length); ij++) {
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (ahkmappings[ij].indexOf('=,') == 0) {
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    }
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (ahkmappings[ji].indexOf('=,') == 0) {
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    ahkprefix+=String.fromCharCode(10) + 'return';
    if (ahkthere && window.self === window.top) {
      if (wop) {
        wop.close();
      }
      if (!navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
      window.open('HTTP://localhost/send_over.php?initf=thiscalculator.ahk&initc=' + encodeURIComponent(ahkprefix),'_blank'); //,'left=' + eval(-100 + eval('' + screen.width)) + ',top=100,width=100,height=100' );
      //var xx=prompt('' + ('HTTP://localhost/send_over.php?initf=thiscalculator.ahk&initc=' + encodeURIComponent(ahkprefix)).length, ahkprefix);
      }
    }
    }
    return true;
  }

  
  function oldnocommas() {
    document.getElementById('firstnum').value=document.getElementById('firstnum').value.replace(/\,/g,'');
    document.getElementById('secnum').value=document.getElementById('secnum').value.replace(/\,/g,'');
    return true;
  }
   
  
 function wopcheck() {
  if (wop.closed) {
    wop=null;
    ahkthere=true; //window.close();
    document.getElementById('ahks').value='y';
    if (window.localStorage) {
      if (('' + window.localStorage.getItem('ahkthere')).replace(/^null$/g,'') != '') {
        window.localStorage.removeItem('ahkthere');
      }
      window.localStorage.setItem('ahkthere', 'y');
    }
  } else {
    wop.close();
    wop=null;
    document.getElementById('ahks').value='n';
  }
 }
 </script>
  
  </head>";
  if (isset($_GET['fnum']) && isset($_GET['moper']) && isset($_GET['snum'])) {
  if (substr("(" . str_replace(" ","+",urldecode($_GET['fi'])),-1) != "(") {
  if (urldecode($_GET['moper']) == '**') {
  echo "<body style='background-color:orange;' onload=\" conl(); document.getElementById('pis').innerHTML+='' + eval(" .  str_replace(" ","+",urldecode($_GET['fi'])) . "Math.pow(" . urldecode($_GET['fnum']) . "," . urldecode($_GET['snum']) . ")" . str_replace(" ","+",urldecode($_GET['si'])) . "); \"><br><div style='margin-left:25%;width:50%;text-align:center;background-color:white;padding:8px 8px 8px 8px;' id='pis'>";
  } else {
  if (substr(str_replace(" ","+",urldecode($_GET['si']) . " "),0,1) == ")") {
  echo "<body style='background-color:orange;' onload=\" conl(); document.getElementById('pis').innerHTML+='' + eval(" .  str_replace(" ","+",urldecode($_GET['fi'])) . urldecode($_GET['fnum']) . ')' . str_replace(" ","+",urldecode($_GET['moper'])) . '(' . urldecode($_GET['snum']) . str_replace(" ","+",urldecode($_GET['si'])) . "); \"><br><div style='margin-left:25%;width:50%;text-align:center;background-color:white;padding:8px 8px 8px 8px;' id='pis'>";
  } else {
  //echo " " . str_replace(" ","+",urldecode($_GET['fi'])) . urldecode($_GET['fnum']) . str_replace(" ","+",urldecode($_GET['moper'])) . urldecode($_GET['snum']) . str_replace(" ","+",urldecode($_GET['si'])) . " ";
  echo "<body style='background-color:orange;' onload=\" conl(); document.getElementById('pis').innerHTML+='' + eval(" .  str_replace(" ","+",urldecode($_GET['fi'])) . urldecode($_GET['fnum']) . str_replace(" ","+",urldecode($_GET['moper'])) . urldecode($_GET['snum']) . str_replace(" ","+",urldecode($_GET['si'])) . "); \"><br><div style='margin-left:25%;width:50%;text-align:center;background-color:white;padding:8px 8px 8px 8px;' id='pis'>";
  }
  }
  echo str_replace("Math.pow", "", str_replace(",", " ** ",   str_replace(" ","+",urldecode($_GET['fi'])) . file_get_contents(dirname(__FILE__) . "/calculator.txt") . str_replace(" ","+",urldecode($_GET['si'])))) . " = ";

  } else if (substr(str_replace(" ","+",urldecode($_GET['si']) . ")"),0,1) != ")") {
  echo "<body style='background-color:orange;' onload=\" conl(); document.getElementById('pis').innerHTML+='' + eval(" .  str_replace(" ","+",urldecode($_GET['fi'])) . urldecode($_GET['fnum']) . '' . str_replace(" ","+",urldecode($_GET['moper'])) . '' . urldecode($_GET['snum']) . str_replace(" ","+",urldecode($_GET['si'])) . "); \"><br><div style='margin-left:25%;width:50%;text-align:center;background-color:white;padding:8px 8px 8px 8px;' id='pis'>";
  echo str_replace("Math.pow", "", str_replace(",", " ** ",   str_replace(" ","+",urldecode($_GET['fi'])) . file_get_contents(dirname(__FILE__) . "/calculator.txt") . str_replace(" ","+",urldecode($_GET['si'])))) . " = ";

  } else {
  echo "<body style='background-color:orange;' onload=\" conl(); document.getElementById('pis').innerHTML+='' + eval(" .  str_replace(" ","+",urldecode($_GET['fi'])) . urldecode($_GET['read']) . str_replace(" ","+",urldecode($_GET['si'])) . "); \"><br><div style='margin-left:25%;width:50%;text-align:center;background-color:white;padding:8px 8px 8px 8px;' id='pis'>";
  echo str_replace("Math.pow", "", str_replace(",", " ** ",   str_replace(" ","+",urldecode($_GET['fi'])) . file_get_contents(dirname(__FILE__) . "/calculator.txt") . str_replace(" ","+",urldecode($_GET['si'])))) . " = ";
  }
  } else {
  echo "<body style='background-color:orange;' onload=\" conl(); document.getElementById('pis').innerHTML+='' + eval(" .  str_replace(" ","+",urldecode($_GET['fi'])) . urldecode($_GET['read']) . str_replace(" ","+",urldecode($_GET['si'])) . "); \"><br><div style='margin-left:25%;width:50%;text-align:center;background-color:white;padding:8px 8px 8px 8px;' id='pis'>";
  echo str_replace("Math.pow", "", str_replace(",", " ** ",   str_replace(" ","+",urldecode($_GET['fi'])) . file_get_contents(dirname(__FILE__) . "/calculator.txt") . str_replace(" ","+",urldecode($_GET['si'])))) . " = ";
  }
  echo "</div><br><br><h1 align='center'>Calculator via TCL</h1><br><br><div  align='center'><form onsubmit='return nocommas();' align='center' method='GET' action='./calculator.php'><input type=hidden name=fi id=fi value='<select></select>'></input><input type=hidden name=si id=si value='<select></select>'></input><span id=fn><select onchange='rest(this);' id=fns><option id=ofi value=''>First Number:</option>

<option value='9'>Number</option>                                                                                      
<option value='('>(</option>                                                                                      
<option value=')'>)</option> 
<option value='+'>+</option>                                                                                      
<option value='-'>-</option> 
<option value='*'>*</option>                                                                                      
<option value='/'>/</option>                                                                               
<option value='**'>**</option> 
<option value='%'>%</option> 
<option value='~'>~</option> 
<option value='!'>!</option> 
<option value='<<'><<</option> 
<option value='>>'>>></option> 
<option value='&'>&</option>                                                                                      
<option value='^'>^</option>                                                                                      
<option value='||'>||</option>                                                                                      
<option value='&&'>&&</option>   
<option value='<'><</option> 
<option value='>'>></option>    
<option value='<='><=</option>   
<option value='>='>>=</option>
  
  </select></span> <input type='text' id='firstnum' name='firstnum' value='1.0'></input>
  
  
  <select id=oper name=oper>
<option value='+'>+</option>                                                                                      
<option value='-'>-</option> 
<option value='*'>*</option>                                                                                      
<option value='/'>/</option>                                                                               
<option value='**'>**</option> 
<option value='%'>%</option> 
<option value='~'>~</option> 
<option value='!'>!</option> 
<option value='<<'><<</option> 
<option value='>>'>>></option> 
<option value='&'>&</option>                                                                                      
<option value='^'>^</option>                                                                                      
<option value='||'>||</option>                                                                                      
<option value='&&'>&&</option>   
<option value='<'><</option> 
<option value='>'>></option>    
<option value='<='><=</option>   
<option value='>='>>=</option></select><input type='text' id='secnum' name='secnum' value='1.0'></input> <span id=sn><select onchange='rest(this);' id=sns><option id=osi value=''>:Second Number</option>

<option value='9'>Number</option>                                                                                      
<option value='('>(</option>                                                                                      
<option value=')'>)</option> 
<option value='+'>+</option>                                                                                      
<option value='-'>-</option> 
<option value='*'>*</option>                                                                                      
<option value='/'>/</option>                                                                               
<option value='**'>**</option> 
<option value='%'>%</option> 
<option value='~'>~</option> 
<option value='!'>!</option> 
<option value='<<'><<</option> 
<option value='>>'>>></option> 
<option value='&'>&</option>                                                                                      
<option value='^'>^</option>                                                                                      
<option value='||'>||</option>                                                                                      
<option value='&&'>&&</option>   
<option value='<'><</option> 
<option value='>'>></option>    
<option value='<='><=</option>   
<option value='>='>>=</option>

</select></span><input type=hidden name=ahks id=ahks value=''></input><input style='background-color:yellow;' type='submit' name='submit' value='Calculate' id='submit'></input></form></div></body></html>";
} else if (isset($_GET['firstnum']) && isset($_GET['oper']) && isset($_GET['secnum'])) {
  file_put_contents(dirname(__FILE__) . "/calculator.txt", urldecode($_GET['firstnum']) . "\n" .  str_replace(" ","",str_replace("  ","+",(" " . urldecode($_GET['oper']) . " "))) . "\n" .  urldecode($_GET['secnum']) . "\n");
  exec("tclsh " . dirname(__FILE__) . "/calculator.tcl < " . dirname(__FILE__) . "/calculator.txt | tail -1 > " . dirname(__FILE__) . "/calculator.out");
  header("Location: ./calculator.php?read=" . urlencode(file_get_contents(dirname(__FILE__) . "/calculator.out")) . "&via=" . urlencode(file_get_contents(dirname(__FILE__) . "/calculator.txt")) . "&fi=" . urlencode(str_replace('<select></select>','',urldecode($_GET['fi']))) . "&si=" . urlencode(str_replace('<select></select>','',urldecode($_GET['si']))) . '&fnum=' . urlencode($_GET['firstnum']) . '&moper=' . urlencode($_GET['oper']) . '&snum=' . urlencode($_GET['secnum']) . '&ahks=' . urlencode($_GET['ahks']));
  exit;
} else {
  echo "<html><head><title>Calculator via TCL - RJM Programming - October, 2014</title>
  
  
  <script type='text/javascript'>
  
  var ahkthere=false;
  var ahkcthere=false;
  
  var ahkxoff=-40;
  var ahkyoff=-32;

  var ahkmappings=['%,90,260',
'*-1*,84,314',
'C,248,260',
'7,88,370',
'4,91,429',
'1,91,481',
'+/-,91,538',
'*2*,169,314',
'8,169,370',
'5,169,429',
'2,169,481',
'0,169,538',
'*0.5*,248,314',
'9,248,370',
'6,248,429',
'3,248,481',
'.,248,538',
'/,326,314',
'*,326,370',
'-,326,429',
'+,326,481',
'=,326,538'
];

  var wop=null;
  var ahkprefix='IfWinExist Calculator' + String.fromCharCode(10) + '  WinActivate' + String.fromCharCode(10) + 'else' + String.fromCharCode(10) + '  Run calculator://' + String.fromCharCode(10) + '  WinActivate calculator' + String.fromCharCode(10) + '' + String.fromCharCode(10) + '// 75 / 8 =' + String.fromCharCode(10) + '' + String.fromCharCode(10) + 'CoordMode, Mouse, Window' + String.fromCharCode(10) + 'Click 208,228' + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
  
  var numprefix='';
  var lastpwr='';
  var lastsot='';
  function isNumeric(n) { // thanks to https://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers
    return !isNaN(parseFloat(n)) && isFinite(n);
  }  
  function ourprompt(ourp, ourd) {
    var oura='*', noura='*', oklistfirst='(-0123456789.';
    while (!isNumeric(noura.replace('e',''))) {
      oura=prompt(ourp, ourd);
      if (oura == null) {
        noura='0';
        oura='';
      } else if (oura.trim() == '') {
        noura='0';
        oura='';
      } else {
        noura=oura;
        if (!isNumeric(noura.replace('e',''))) {
          var csuff='';
          try {
            if (oklistfirst.indexOf(oura.trim().toLowerCase().replace('math.pow(','0').replace('pow(','0').substring(0,1)) != -1) {
            csuff=eval('' + oura);
            alert(oura + ' = ' + csuff);
            }
          } catch (eee) { }
        }
      }
    }
    return oura;
  }
  function lval() {
   document.getElementById(lastsot).value='9';
   rest(document.getElementById(lastsot));
  }
  function rest(sot) {
   var pwr='';
   var was=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML;
   if (sot.id == 'fns') {
     if (sot.value == '9') {
       //pwr=prompt('Enter number (the way to achieve ** is via ... Math.pow([number], ... here and add an extra ) on the other side, or 3 ** 2 would be ... Math.pow(3,2))','1.0');
       pwr=ourprompt('Enter number','1.0');
       if (pwr == null) pwr='';
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('</select>','</select>' + numprefix.replace('Math.pow','') + pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ',''));
       document.getElementById('fi').value=document.getElementById('fi').value.replace('</select>','</select>' + numprefix + pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ',''));
       //document.getElementById('fi').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('</select>')[1];
       numprefix='';
     } else if (sot.value == '**') {
       numprefix='Math.pow(';
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('</select>','</select>**');
       document.getElementById('sn').innerHTML=')' + document.getElementById('sn').innerHTML;
       document.getElementById('si').value=')' + document.getElementById('si').value;
       document.getElementById('osi').innerHTML=': Second Bit';
       document.getElementById('fi').value=document.getElementById('fi').value.replace('</select>','</select>,');
       //document.getElementById('fi').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('</select>')[1];
       lastsot=sot.id;
       setTimeout(lval,400);
     } else {
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('</select>','</select>' + sot.value);
       document.getElementById('fi').value=document.getElementById('fi').value.replace('</select>','</select>' + sot.value);
       //document.getElementById('fi').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('</select>')[1];
     }
     document.getElementById('ofi').innerHTML='First Bit :';
   } else if (sot.id == 'sns') {
     if (sot.value == '9') {
       pwr=ourprompt('Enter number','1.0');
       if (pwr == null) pwr='';
       if (document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.indexOf(',)') != -1 || document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.indexOf('**)') != -1) {
        document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('**)', '**' + pwr + ')').replace(',)', ',' + pwr + ')');
        document.getElementById('si').value=document.getElementById('si').value.replace(',)', ',' + pwr + ')');
        //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       } else {
        lastpwr=pwr;
        document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('<select',pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ','') + '<select');
        document.getElementById('si').value=document.getElementById('si').value.replace('<select',pwr.replace(' ','').replace(' ','').replace(' ','').replace(' ','').replace(' ','') + '<select');
        //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       }
     } else if (sot.value == '**') {
       if (lastpwr == '') {
       lastpwr=ourprompt('Enter number','1.0');
       if (lastpwr == null) lastpwr='';
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('<select','Math.pow(' + lastpwr + ',)' + '<select');
       document.getElementById('si').value=document.getElementById('si').value.replace('<select','Math.pow(' + lastpwr + ',)' + '<select');
       //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       } else {
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace(lastpwr,'Math.pow(' + lastpwr + ',)');
       document.getElementById('si').value=document.getElementById('si').value.replace(lastpwr,'Math.pow(' + lastpwr + ',)');
       //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
       }
       lastpwr='';
       lastsot=sot.id;
       setTimeout(lval,400);
     } else {
       document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.replace('<select',sot.value + '<select');
       document.getElementById('si').value=document.getElementById('si').value.replace('<select',sot.value + '<select');
       //document.getElementById('si').value=document.getElementById(sot.id.substring(0,eval(-1 + sot.id.length))).innerHTML.split('<select')[0];
     }
     document.getElementById('osi').innerHTML=': Second Bit';
   }
  }
  
  function numberWithCommas(x) {  // thanks to https://www.codegrepper.com/code-examples/javascript/add+commas+to+large+numbers+js#:~:text=%E2%80%9Cadd%20commas%20to%20large%20numbers%20js%E2%80%9D%20Code%20Answer's&text=given%20an%20integer%20n%2C%20add,return%20it%20in%20string%20format.&text=given%20an%20integer%20n%2C%20add%20a%20dot%20(%22.,return%20it%20in%20string%20format.
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  }  
  
  function conl() {
    document.getElementById('oper').value=location.search.split('clientoper=')[1] ? decodeURIComponent(location.search.split('clientoper=')[1].split('&')[0]) : document.getElementById('oper').value;
    document.getElementById('firstnum').value=location.search.split('clientfirstnum=')[1] ? numberWithCommas(decodeURIComponent(location.search.split('clientfirstnum=')[1].split('&')[0])) : document.getElementById('firstnum').value;
    document.getElementById('secnum').value=location.search.split('clientsecnum=')[1] ? numberWithCommas(decodeURIComponent(location.search.split('clientsecnum=')[1].split('&')[0])) : document.getElementById('secnum').value;
    var zahkthere=location.search.split('ahks=')[1] ? decodeURIComponent(location.search.split('ahks=')[1].split('&')[0]) : '';
    if (zahkthere == 'y') {
       ahkthere=true;
       document.getElementById('ahks').value='y';
   } else if (zahkthere == 'n') {
      ahkthere=false;
      document.getElementById('ahks').value='n';
    }
    if (window.localStorage) {
      if (('' + window.localStorage.getItem('ahkthere')).replace(/^null$/g,'').toLowerCase == 'y') {
      document.getElementById('ahks').value='y';
      ahkthere=true;
      } else if (!ahkthere && document.getElementById('ahks').value == '' && window.self === window.top && !navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
    wop=window.open('HTTP://localhost/send_over.php?ahkthere=y','_blank','left=' + eval(-100 + eval('' + screen.width)) + ',top=100,width=100,height=100' );
    setTimeout(wopcheck, 5000);
      }
    } else if (!ahkthere && document.getElementById('ahks').value == '' && window.self === window.top && !!navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
    wop=window.open('HTTP://localhost/send_over.php?ahkthere=y','_blank','left=' + eval(-100 + eval('' + screen.width)) + ',top=100,width=100,height=100' );
    setTimeout(wopcheck, 5000);
    }
  }
  
  function nocommas() {
    var ij=0, ji=0;
    document.getElementById('firstnum').value=document.getElementById('firstnum').value.replace(/\,/g,'');
    document.getElementById('secnum').value=document.getElementById('secnum').value.replace(/\,/g,'');
    var matchwith=document.getElementById('oper').value;
    var operx=0, opery=0;
    if (matchwith == '**' && document.getElementById('secnum').value == '2') { 
      matchwith='*2*'; 
    } else if (matchwith == '**' && document.getElementById('secnum').value == '0.5') { 
      matchwith='*0.5*'; 
    } else if (matchwith == '**' && document.getElementById('secnum').value == '-1') { 
      matchwith='*-1*'; 
    }
    for (ij=0; ij<ahkmappings.length; ij++) {
      if (('' + ahkmappings[ij]).trim().indexOf(matchwith + ',') == 0) {
        operx=eval(eval('' + ahkxoff) + eval('' + ahkmappings[ij].split(matchwith + ',')[1].split(',')[0]));
        opery=eval(eval('' + ahkyoff) + eval('' + ahkmappings[ij].split(matchwith + ',')[1].split(',')[1]));
        matchwith+=' ';        
      }
    }
    if (matchwith .indexOf(' ') != -1) {
    for (ij=0; ij<eval('' + document.getElementById('firstnum').value.length); ij++) {
      //alert('*' + ('' + ahkmappings[ij]).trim().split(',')[0] + '*');
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (('' + ahkmappings[ji]).trim().indexOf(document.getElementById('firstnum').value.substring(ij).substring(0,1) + ',') == 0) {
      //alert(document.getElementById('firstnum').value.substring(ij).substring(0,1) + ',');
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    }
    ahkprefix+='Click ' + operx + ',' + opery + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
    if (matchwith.length <= 3) {
    for (ij=0; ij<document.getElementById('secnum').value.length; ij++) {
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (('' + ahkmappings[ji]).trim().indexOf(document.getElementById('secnum').value.substring(ij).substring(0,1) + ',') == 0) {
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    }
    }
    for (ij=0; ij<eval('' + document.getElementById('secnum').value.length); ij++) {
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (ahkmappings[ij].indexOf('=,') == 0) {
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    }
    for (ji=0; ji<ahkmappings.length; ji++) {
      if (ahkmappings[ji].indexOf('=,') == 0) {
        ahkprefix+=String.fromCharCode(10) + 'Click ' + eval(eval('' + ahkxoff) + eval('' + ahkmappings[ji].split(',')[1])) + ',' + eval(eval('' + ahkyoff) + eval('' + ahkmappings[ji].split(',')[2])) + String.fromCharCode(10) + 'Sleep, 2500' + String.fromCharCode(10);
      }
    }
    ahkprefix+=String.fromCharCode(10) + 'return';
    if (ahkthere && window.self === window.top) {
      if (wop) {
        wop.close();
      }
      if (!navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
      window.open('HTTP://localhost/send_over.php?initf=thiscalculator.ahk&initc=' + encodeURIComponent(ahkprefix),'_blank'); //,'left=' + eval(-100 + eval('' + screen.width)) + ',top=100,width=100,height=100' );
      //var xx=prompt('' + ('HTTP://localhost/send_over.php?initf=thiscalculator.ahk&initc=' + encodeURIComponent(ahkprefix)).length, ahkprefix);
      }
    }
    }
    return true;
  }
    
 function wopcheck() {
  if (wop.closed) {
    wop=null;
    ahkthere=true; //window.close();
    document.getElementById('ahks').value='y';
    if (window.localStorage) {
      if (('' + window.localStorage.getItem('ahkthere')).replace(/^null$/g,'') != '') {
        window.localStorage.removeItem('ahkthere');
      }
      window.localStorage.setItem('ahkthere', 'y');
    }
  } else {
    wop.close();
    wop=null;
    document.getElementById('ahks').value='n';
  }
 }
  </script>
  
  
  </head><body onload=conl(); style='background-color:orange;'><p align='center'>";
  echo "</p><br><br><h1 align='center'>Calculator via TCL</h1><br><br><div  align='center'><form onsubmit='return nocommas();' align='center' method='GET' action='./calculator.php'><input type=hidden name=fi id=fi value='<select></select>'></input><input type=hidden name=si id=si value='<select></select>'></input><span id=fn><select onchange='rest(this);' id=fns><option id=ofi value=''>First Number:</option>

<option value='9'>Number</option>                                                                                      
<option value='('>(</option>                                                                                      
<option value=')'>)</option> 
<option value='+'>+</option>                                                                                      
<option value='-'>-</option> 
<option value='*'>*</option>                                                                                      
<option value='/'>/</option>                                                                               
<option value='**'>**</option> 
<option value='%'>%</option> 
<option value='~'>~</option> 
<option value='!'>!</option> 
<option value='<<'><<</option> 
<option value='>>'>>></option> 
<option value='&'>&</option>                                                                                      
<option value='^'>^</option>                                                                                      
<option value='||'>||</option>                                                                                      
<option value='&&'>&&</option>   
<option value='<'><</option> 
<option value='>'>></option>    
<option value='<='><=</option>   
<option value='>='>>=</option>
  
  </select></span> <input type='text' id='firstnum' name='firstnum' value='1.0'></input>
  
  
  <select id=oper name=oper>
<option value='+'>+</option>                                                                                      
<option value='-'>-</option> 
<option value='*'>*</option>                                                                                      
<option value='/'>/</option>                                                                               
<option value='**'>**</option> 
<option value='%'>%</option> 
<option value='~'>~</option> 
<option value='!'>!</option> 
<option value='<<'><<</option> 
<option value='>>'>>></option> 
<option value='&'>&</option>                                                                                      
<option value='^'>^</option>                                                                                      
<option value='||'>||</option>                                                                                      
<option value='&&'>&&</option>   
<option value='<'><</option> 
<option value='>'>></option>    
<option value='<='><=</option>   
<option value='>='>>=</option></select><input type='text' id='secnum' name='secnum' value='1.0'></input> <span id=sn><select onchange='rest(this);' id=sns><option id=osi value=''>:Second Number</option>

<option value='9'>Number</option>                                                                                      
<option value='('>(</option>                                                                                      
<option value=')'>)</option> 
<option value='+'>+</option>                                                                                      
<option value='-'>-</option> 
<option value='*'>*</option>                                                                                      
<option value='/'>/</option>                                                                               
<option value='**'>**</option> 
<option value='%'>%</option> 
<option value='~'>~</option> 
<option value='!'>!</option> 
<option value='<<'><<</option> 
<option value='>>'>>></option> 
<option value='&'>&</option>                                                                                      
<option value='^'>^</option>                                                                                      
<option value='||'>||</option>                                                                                      
<option value='&&'>&&</option>   
<option value='<'><</option> 
<option value='>'>></option>    
<option value='<='><=</option>   
<option value='>='>>=</option>

</select></span><input type=hidden name=ahks id=ahks value=''></input><input style='background-color:yellow;' type='submit' name='submit' value='Calculate' id='submit'></input></form></div></body></html>";
}
?>