<?php
  function retval($inv) {
    if (strpos($inv, "E-") !== false) return "0";
    return $inv;
  }

$eto="";
$formreply="";
$ivd = "https://" . $_SERVER['SERVER_NAME'] . ":" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "" . str_replace("index.php", "", str_replace("parabola_lgraph.php", "", $_SERVER['REQUEST_URI']));
if (strpos($ivd, "email=") !== false) {
  $eto=str_replace("+"," ",urldecode($_GET['email']));
  $formreply="<style> .plus:after { content: '\\\\2b'; } </style><form action=" . explode("?", $ivd)[0] . " method=GET><input type=hidden name=itsurl value='" . $ivd . "'></input><input type=hidden name=vse2 value='" . str_replace("+","",urldecode($_GET['e2'])) . "'></input><input type=hidden name=vse1 value='" . str_replace("+","",urldecode($_GET['e1'])) . "'></input><input type=hidden name=vse0 value='" . str_replace("+","",urldecode($_GET['e0'])) . "'></input>Parabola (below) Formula Quiz: <b>y</b> = <input style='display:inline-block;text-align:right;width:50px;' type=text name=e2 value='0'></input><b>x</b><b><sup>2</sup></b> <span class=plus></span> (<input style='display:inline-block;text-align:right;width:50px;' type=text name=e1 value='0'></input>)<b>x</b> <span class=plus></span> (<input style='display:inline-block;text-align:right;width:50px;' type=text name=e0 value='0'></input>)    <input style='display:inline-block;' type=submit value=Check></input></form>  <a target=_blank style='display:inline-block;' title=Hint href='" . $ivd . "'>Hint</a><br>";
} else if (isset($_GET['vse2'])) {
  if ($_GET['vse2'] == $_GET['e2'] && $_GET['vse1'] == $_GET['e1'] && $_GET['vse0'] == $_GET['e0']) {
    echo "<!doctype html><html><head><title>Parabola Quiz - RJM Programming - April, 2020</title></head><body><h1>Parabola Quiz</h1><h3>RJM Programming - April, 2020</h3><p>Your answers are correct, as you can see below</p><br><br><iframe src='" . str_replace("+"," ",urldecode($_GET['itsurl'])) . "' style='width:100%;height:900px;'></iframe></body></html>";
  } else {
    echo "<!doctype html><html><head><title>Parabola Quiz - RJM Programming - April, 2020</title></head><body><h1>Parabola Quiz</h1><h3>RJM Programming - April, 2020</h3><p>Sorry, but your answer <b>y = " . str_replace("+","",urldecode($_GET['e2'])) . "x<sup>2</sup> + (" . str_replace("+","",urldecode($_GET['e1'])) . ")x + (" . str_replace("+","",urldecode($_GET['e0']))  . ")</b> is not all correct, as you can see below</p><br><br><iframe src='" . str_replace("+"," ",urldecode($_GET['itsurl'])) . "' style='width:100%;height:900px;'></iframe></body></html>";
  }
  exit;
}

?>
<!doctype html>
<html>
  <head> <script type="text/javascript" src="//www.rjmprogramming.com.au/gchartgen.js"></script>
<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=4, user-scalable=yes" />
    <title>Parabola Line Graph - RJM Programming - http://www.rjmprogramming.com.au (Copyright &copy; 2013 rjmprogramming.com.au all rights reserved.)</title>
    <script type="text/javascript" src="//www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);

<?php
 echo "\n var emailstuff=''; \n";
 echo "\n var formreply=\"" . str_replace("\n","",$formreply) . "\"; \n"; 
?>

function backin() {
  if (myxhr.readyState == 4) {
    if (myxhr.status == 200) {
      if (myxhr.responseText) {
        var dbits = myxhr.responseText.split('\"data\":');
        if (dbits.length > 1) {
         // replace all '_' with '/' and all '-' with '+' thanks to https://stackoverflow.com/questions/757675/website-screenshots
         //document.getElementById('duc').innerHTML='<img id=\"imgduc\" onload=\"beforeftit(this);\" title=\"Click to make this image, thanks to Google Page Insights, the content above\" onclick=\"ftit(this);\" src=\"data:image/jpeg;base64,' + dbits[1].split('\"')[1].split('\"')[0].replace(/\_/g,'/').replace(/\-/g,'+') + '\"></img>';
         document.getElementById('formtable').innerHTML=formreply + '<img style="width:100%;" id="imgduc" onload="beforeftit(this);" title="Click to make this image, thanks to Google Page Insights, the content above" onclick="ftit(this);" src="data:image/jpeg;base64,' + dbits[1].split('"')[1].split('"')[0].replace(/\_/g,'/').replace(/\-/g,'+') + '"></img>'; 
         //document.getElementById('formtable').rows='10';
         //document.getElementById('duc').style.display='inline-block';
         //alert(document.getElementById('iformit').outerHTML);
         document.getElementById('pbquiz').click();
         //window.open('data:image/jpeg;base64,' + dbits[1].split('\"')[1].split('\"')[0].replace(/\_/g,'/').replace(/\-/g,'+'),'_blank','top=200,left=200,width=320,height=180');
         //document.getElementById('formtable').innerHTML=encodeURIComponent('<body><style> body: { background: URL(data:image' + document.getElementById('duc').outerHTML.split('data:image')[1].split(String.fromCharCode(34))[0].replace(/\ /g,'+') + '); } </style></body>');
        }
      }
    }
  }
 }

 function beforeftit(ithis) {
   ithis.style.width=('' + ithis.width).replace('px','') + 'px';
   ithis.style.height=('' + ithis.height).replace('px','') + 'px';
 }


 function ajaxit(urlin) {
  if (urlin.length > 0) {
      aurl=urlin;
      if (window.XMLHttpRequest) {
        myxhr = new window.XMLHttpRequest;
    }
    else {
  try {
    myxhr = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (othermicrosoft) {
    try {
      myxhr = new ActiveXObject('Microsoft.XMLHTTP');
    } catch (failed) {
      myxhr = false;
    }
  }
  }
  var xurl = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=' + encodeURIComponent(urlin) + '&screenshot=true';
  if (myxhr) {
    myxhr.onreadystatechange = backin;
    myxhr.open('GET', xurl, true);
    myxhr.send(null);
  }
  }
 }

<?php 
if (strpos($ivd, "email=") !== false) {
 echo "\n ajaxit('" . $ivd . "'); \n";
}
?>

      function drawChart() {
        
        var dataTable = new google.visualization.DataTable();
        dataTable.addColumn('number', 'X');
        dataTable.addColumn('number', 'Y');
        dataTable.addRows([
<?php 
// https://developers.google.com/speed/pagespeed/insights/?url=https%3A%2F%2Fwww.rjmprogramming.com.au%2FPHP%2FParabolaLineGraph%2Fparabola_lgraph.php%3Fminval%3D1400%26maxval%3D1400%26e2%3D2%26e1%3D%2B1%26e0%3D%2B5&tab=desktop
// http://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=https%3A%2F%2Fwww.rjmprogramming.com.au%2FPHP%2FParabolaLineGraph%2Fparabola_lgraph.php%3Fminval%3D1400%26maxval%3D1400%26e2%3D2%26e1%3D%2B1%26e0%3D%2B5&tab=desktop
//echo "here";
$minval = "-2";
$maxval = "1.5";
$se2 = "2";
$se1 = "-1";
$se0 = "-6";
$e2 = "2";
$e1 = "-1";
$e0 = "-6";
if (isset($_GET['minval'])) {
  $minval = $_GET['minval'];
  $maxval = $_GET['maxval'];
  if ($minval == $maxval && $maxval == 1400) {
  $minval = -10.0;
  $maxval = 10.0;
  }
  $e1 = str_replace("+","",$_GET['e1']);
  $e0 = str_replace("+","",$_GET['e0']);
  $se1 = str_replace("++","+",str_replace("+-","-",("+".$_GET['e1'])));
  $se0 = str_replace("++","+",str_replace("+-","-",("+".$_GET['e0'])));
  if (strpos($_GET['e2'],"x") !== false) {
  $e2 = str_replace("~","+",urldecode(str_replace('%2B','|',$_GET['e2'])));
  $e2 = $_GET['e2'];
  $se2 = str_replace("~","+",str_replace("pow(x,","(x^",urldecode(str_replace('%2B','~',$_GET['e2']))));
  $se2 = str_replace("Math.pow(x,","(x^",(($_GET['e2'])));
  } else {
  $e2 = str_replace("+","",$_GET['e2']);
  $se2 = $_GET['e2'];
  }
}
$off = 1.0;
$c = "";
if (strpos($e2,"x") !== false) {
for ($x=($minval-$off); $x<($maxval+$off); $x+=0.1) {
  $prex = str_replace("x", "(" . $x . ")", $e2);
  $y= "eval(" . str_replace("^","",$prex) . ")";
  $c = "[".retval($x).",".retval($y)."]\n";
  echo "[".retval($x).",".retval($y)."],\n";
}
if ($minval != $maxval) {
 $x = ($maxval+$off);
 $prex = str_replace("x", "(" . $x . ")", $e2);
 $y = "eval(" . str_replace("^","",$prex) . ")";
 echo "[".retval($x).",".retval($y)."]\n";
} else {
  echo $c;
}
echo "        ]);\n";
} else {
for ($x=($minval-$off); $x<($maxval+$off); $x+=0.1) {
  $y = ((($e2) * ($x) * ($x)) + (($e1) * ($x)) + ($e0));
  $c = "[".retval($x).",".retval($y)."]\n";
  echo "[".retval($x).",".retval($y)."],\n";
}
if ($minval != $maxval) {
 $x = ($maxval+$off);
 $y = ((($e2) * (($maxval)+$off) * (($maxval)+$off)) + (($e1) * (($maxval)+$off)) + ($e0));
 echo "[".retval($x).",".retval($y)."]\n";
} else {
  echo $c;
}
echo "        ]);\n";
}
echo "        var dataView = new google.visualization.DataView(dataTable);\n";
echo "        var emailurl=location.search.split('email=')[1] ? decodeURIComponent(location.search.split('email=')[1].split('&')[0]) : ''; \n";
echo "        var options = {\n";
echo "          backgroundColor: 'lightgray',\n";
if ($se2 == "0") {
if (isset($_GET['letter'])) {
echo "          title: 'Line y = ".str_replace("+","",$se1).$_GET['letter']." ".$se0."'\n";
} else {
echo "          title: 'Line y = ".str_replace("+","",$se1)."x ".$se0."'\n";
}
} else if (strpos($e2,"x") !== false) {
echo "          title: 'Equation y = ".$se2."'\n";
} else {
echo "          title: 'Parabola y = ".$se2."x² ".$se1."x ".$se0."'\n";
}
echo "        };\n";

?>
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(dataTable, options);
      }
      
      function ask() {
<?php
 echo "\n  document.getElementById('eto').value='" . $eto . "'; \n";
?>
       if (document.URL.indexOf("&") == (0 - 1)) {
        var minval=-600.0;
        var maxval=-600.0;
        var x=minval, y, goes=0;
        var nonsqval='0',val='0',sqval = prompt("Enter ??? of Parabola graph (append &email=[toEmailAddress]) for share with somebody in the form of a quiz to them) ... y = ???x^2 + ??x + ? ... or alternatively define any other complete equation (for y) mentioning x (by name) where ^ is 'the power of' and * is 'multiply by' and + is 'plus' and - is 'minus' and % is 'modulus of'", "0");
        if (sqval == null) { sqval=''; }
        if (sqval.indexOf('&email=') != -1) {
          emailstuff='&email=' + sqval.split('&email=')[1];
          sqval=sqval.replace(emailstuff,'');
        }
        if (sqval.indexOf('x') == -1) {
          nonsqval = prompt("Enter ?? of Parabola graph ... y = " + sqval + "x^2 + ??x + ?", "0");
          val = prompt("Enter ? of Parabola graph ... y = " + sqval + "x^2" + nonsqval + "x + ?", "0");  
        y = eval(( (sqval) * (minval) * (minval)) + ( (nonsqval) * (minval) ) + (val));
        if (y < 0) {
         while (y < 0 && goes < 2000) {
          minval = eval(minval + 1.0);
          y = eval(( (sqval) * (minval) * (minval)) + ( (nonsqval) * (minval) ) + (val));
          goes = eval(goes + 1);
         }
         if (goes < 2000) {
          goes = 0;
          maxval = minval;
          y = eval(( (sqval) * (maxval) * (maxval)) + ( (nonsqval) * (maxval) ) + (val));
          while (y > 0 && goes < 2000) {
           maxval = eval(maxval + 1.0);
           y = eval(( (sqval) * (maxval) * (maxval)) + ( (nonsqval) * (maxval) ) + (val));
           goes = eval(goes + 1);
          }
         } else {
          minval = -10.0;
          maxval = 10.0;
         }  
        } else {
         while (y > 0 && goes < 2000) {
          minval = eval(minval + 1.0);
          y = eval(( (sqval) * (minval) * (minval)) + ( (nonsqval) * (minval) ) + (val));
          goes = eval(goes + 1);
         }
         if (goes < 2000) {
          goes = 0;
          maxval = minval;
          y = eval(( (sqval) * (maxval) * (maxval)) + ( (nonsqval) * (maxval) ) + (val));
          while (y < 0 && goes < 2000) {
           maxval = eval(maxval + 1.0);
           y = eval(( (sqval) * (maxval) * (maxval)) + ( (nonsqval) * (maxval) ) + (val));
           goes = eval(goes + 1);
          }
         } else {
          maxval = minval;
         }   
        }
        if (nonsqval.indexOf("+") == (0 - 1)) {
        if (nonsqval.indexOf("-") == (0 - 1)) {
         nonsqval = "+" + nonsqval;
        }
        }
        if (val.indexOf("+") == (0 - 1)) {
        if (val.indexOf("-") == (0 - 1)) {
         val = "+" + val;
        }
        }

        } else if (sqval.indexOf('^') != -1) {  // all defined equation
           //sqval=sqval.replace(/\^/g,' ** ');
           minval=-100;
           maxval=100;
           minval = prompt("Enter minimum x for " + sqval + " equation usage?", "" + minval);
           if (minval == null) minval="" + maxval;
           maxval = prompt("Enter maximum x for " + sqval + " equation usage?", "" + maxval);
           if (maxval == null) maxval="" + maxval;
           var eb=')',iuy,juy,vprefix='', wbits, vbits=sqval.replace(/0x/g,'0 * x').replace(/1x/g,'1 * x').replace(/2x/g,'2 * x').replace(/3x/g,'3 * x').replace(/4x/g,'4 * x').replace(/5x/g,'5 * x').replace(/6x/g,'6 * x').replace(/7x/g,'7 * x').replace(/8x/g,'8 * x').replace(/9x/g,'9 * x').replace(/0\(/g,'0 * (').replace(/1\(/g,'1 * (').replace(/2\(/g,'2 * (').replace(/3\(/g,'3 * (').replace(/4\(/g,'4 * (').replace(/5\(/g,'5 * (').replace(/6\(/g,'6 * (').replace(/7\(/g,'7 * (').replace(/8\(/g,'8 * (').replace(/9\(/g,'9 * (').replace(/\//g,' /').replace(/\-/g,' -').replace(/\+/g,' +').replace(/ ^ /g,'^').replace(/^ /g,'^').replace(/ ^/g,'^').replace(/\) -/g,')-').split('x^');
           var sofar=vbits[0];
           for (iuy=1; iuy<vbits.length; iuy++) {
             eb=')';
             wbits=vbits[iuy].trim().split(' ');
             //alert(wbits[0]);
             if (wbits[0].indexOf('-') == -1 && wbits[0].indexOf('.') == -1) {
             sofar+='(x';
             for (juy=1; juy<eval(wbits[0]); juy++) {
             sofar+='*x';
             }
             sofar+=')';
             eb='';
             vbits[iuy]=vbits[iuy].replace(wbits[0],'');
             } else {
             if (wbits[0].indexOf('-') != -1) {
              vprefix=" (1.0 / ";
              vbits[iuy]=vbits[iuy].trim().replace('-','');
             }
             //alert(vbits[iuy]);
             sofar+=vprefix + 'Math.pow(x,';
             if (wbits[0].indexOf('-') != -1) vprefix=')';
             }
             if (vbits[iuy].trim().indexOf(' ') != -1) {
                sofar+=vbits[iuy].trim().replace(' ',eb + vprefix + ' ');
             } else {
                sofar+=vbits[iuy] + eb + vprefix;
             }
             vprefix="";
           }
           //alert(sqval + ' ... ' + sofar);
           sqval=encodeURIComponent(sofar.replace('^',''));
        } else { // all defined equation
           minval=-100;
           maxval=100;
           minval = prompt("Enter minimum x for " + sqval + " equation usage?", "" + minval);
           if (minval == null) minval="" + maxval;
           maxval = prompt("Enter maximum x for " + sqval + " equation usage?", "" + maxval);
           if (maxval == null) maxval="" + maxval;
           sqval=encodeURIComponent(sqval);
        }

        if (nonsqval.replace("0","").length > 0 || sqval.replace("0","").length > 0 || val.replace("0","").length > 0) {
        window.location = "./parabola_lgraph.php?minval=" + minval + "&onclick=y&maxval=" + maxval + emailstuff + "&e2=" + sqval + "&e1=" + nonsqval + "&e0=" + val;  
        }
        }
      }
    </script>
  </head>
  <body onload="ask();">
<?php
if ($se2 == "0") {
   echo "\n <h1>Line Graph</h1> \n";
} else if (strpos($_GET['e2'],"x") !== false) {
   echo "\n <h1>Equation</h1> \n";
} else {
   echo "\n <h1>Parabola Line Graph</h1> \n";
}
?>
    <div id="chart_div" style="width: 900px; height: 500px; background-color: yellow;"></div>
    <a title="Another Line Graph" href="./parabola_lgraph.php">Another parabola graph?</a>
<div id=duc style=display:none;></div><form target=iemailit id=iformit style=display:none; action=//www.rjmprogramming.com.au/HTMLCSS/emailhtml.php method=POST>
<input type=hidden name=inline value=''></input><input id=eto type=hidden name=toe value=''></input><input type=hidden name=subj value='Parabola Quiz'></input>
<textarea id=formtable style=display:none; name=cont></textarea><input style=display:none; type=submit value=Quiz id=pbquiz></input></form>
<iframe name=iemailit style=display:none; src=></iframe>
</body>
</html>
