<?php
// linear_trendline.php
// Google Chart Linear Trendline
// RJM Programming
// Updated in November, 2015
  if (file_exists("csv.php")) {
    include "csv.php";
  } else if (file_exists("../csv.php")) { 
    include "../csv.php";
  }
  if (file_exists("bigemail.php")) {
    include "bigemail.php";
  } else if (file_exists("../bigemail.php")) { 
    include "../bigemail.php";
  }
  function retval($inv) {
    if (strpos($inv, "E-") !== false) return "0";
    return $inv;
  }
?>
<!doctype html>
<html>
  <head> <script type='text/javascript' src='http://www.rjmprogramming.com.au/gchartgen.js'></script>
    <title>Linear Trendline - RJM Programming - http://www.rjmprogramming.com.au (Copyright &copy; 2013 rjmprogramming.com.au all rights reserved.)</title>
    <meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=4, user-scalable=yes' />
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
   var ready=false, x=-1, y=-1, elemLeft=0, elemTop=0, ir=-1, ic=-1, talkingpoint='', formula='', x1=0.0, y1=0.0, x2=0.0, y2=0.0, x3=0.0, y3=0.0;   
   var data, chart;
   var pardata = (location.search.split('data=')[1] ? location.search.split('data=')[1].split('&')[0] : '');
   var paramdata=decodeURIComponent(pardata);
   

function calcdist() { 
  var area=0.0, base=0.0, height=0.0, smallesth=-1.0;
         area=herons_formula_triangle_area(x3, y3, x1, y1, x2, y2);
         base=find_dist(x1, y1, x2, y2);
         if (eval(base) > 0.001) {
         height=eval((2.0 * area) / base);
         }
         smallesth=height;
  return smallesth;
}
   
   function fillindata() {
     var bed=document.getElementById('bdataval');
     if (bed == null) {
       document.getElementById('bitsatend').innerHTML+='<input type="hidden" id="bdataval" value="' + encodeURIComponent(paramdata) + '"></input>';
     }
   }
   
   function filldata() {
     var bed=document.getElementById('bedata');
     if (bed != null) {
       if (bed.value == '') {
         bed.value=encodeURIComponent(paramdata);
       }
     }
   }

function find_dist(xx1, yy1, xx2, yy2) {
  return Math.sqrt((xx1 - xx2) * (xx1 - xx2) + (yy1 - yy2) * (yy1 - yy2));
}

function herons_formula_triangle_area(xx1, yy1, xx2, yy2, xx3, yy3) {
  var a=Math.sqrt((xx1 - xx2) * (xx1 - xx2) + (yy1 - yy2) * (yy1 - yy2));
  var b=Math.sqrt((xx3 - xx2) * (xx3 - xx2) + (yy3 - yy2) * (yy3 - yy2));
  var c=Math.sqrt((xx3 - xx1) * (xx3 - xx1) + (yy3 - yy1) * (yy3 - yy1));
  var s=eval((a + b + c) / 2.0);
  return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
   
<?php
      $GETtp="";
      if (isset($_GET['tp'])) $GETtp = urldecode($_GET['tp']);
      if (isset($_POST['tp'])) $GETtp = urldecode($_POST['tp']);
      if ($GETtp != "") echo "\n talkingpoint=String.fromCharCode(10) + String.fromCharCode(10) + " . '"' . str_replace("`", '" + String.fromCharCode(10) + "', str_replace("\n", '" + String.fromCharCode(10) + "', $GETtp)) . '"' . " + String.fromCharCode(10) + String.fromCharCode(10); \n";

      $GETr = "";
      $GETc = "";
      if (isset($_GET['r'])) $GETr = urldecode($_GET['r']);
      if (isset($_POST['r'])) $GETr = urldecode($_POST['r']);
      if (isset($_GET['c'])) $GETc = urldecode($_GET['c']);
      if (isset($_POST['c'])) $GETc = urldecode($_POST['c']);
      if ($GETr != "") echo "\n ir=eval('" . $GETr . "'); \n";
      if ($GETc != "") echo "\n ic=eval('" . $GETc . "'); \n";

      $GETx = "";
      $GETy = "";
      if (isset($_GET['x'])) $GETx = urldecode($_GET['x']);
      if (isset($_POST['x'])) $GETx = urldecode($_POST['x']);
      if (isset($_GET['y'])) $GETy = urldecode($_GET['y']);
      if (isset($_POST['y'])) $GETy = urldecode($_POST['y']);
      if ($GETx != "" && $GETy != "") {
        echo "\n x=eval('" . $GETx . "'); \n";
        echo "\n y=eval('" . $GETy . "'); \n";
      }

      $GETtitle = "Age of sugar maples vs. trunk diameter, in inches";
      $GETtask = "Diameter";
      $GETdesc = "Age";
      $GETdata = ",[8, 37], [4, 19.5], [11, 52], [4, 22], [3, 16.5], [6.5, 32.8], [14, 72]";
      
      
      if (isset($_POST['data'])) {
       $GETdata = str_replace('~','"',urldecode(checkdatadata0('POST',$_POST['data'])));
       //echo "\n pardata = " . '"' . $_POST['data'] . '"; ' . " \n";
       echo "\n paramdata='" . $GETdata . "'; \n setTimeout(fillindata, 300); \n setTimeout(filldata, 3000); \n"; // = decodeURIComponent(pardata); " . " \n";
      } else if (isset($_GET['data'])) {
       $prevg=$_GET['data'];
       $GETdata = checkdatadata0('GET',$_GET['data']);
       if ($prevg != $GETdata) {
        echo "\n paramdata='" . $GETdata . "'; \n setTimeout(fillindata, 300); \n setTimeout(filldata, 3000); \n"; // = decodeURIComponent(pardata); " . " \n";
       }
      }
      if (isset($_POST['title'])) {
       $GETtitle = str_replace("~~", "'", str_replace('~', '"', str_replace("`", "'", urldecode($_POST['title']))));
       $GETtask = urldecode($_POST['task']);
       $GETdesc = str_replace("~~", "'", str_replace('~', '"', str_replace("`", "'", urldecode($_POST['desc']))));
      } else if (isset($_GET['title'])) {
       $GETtitle = str_replace("~~", "'", str_replace('~', '"', str_replace("`", "'", $_GET['title'])));
       $GETtask = $_GET['task'];
       $GETdesc = str_replace("~~", "'", str_replace('~', '"', str_replace("`", "'", $_GET['desc'])));
      } else if (isset($_GET['desc'])) {
       $GETtitle = $_GET['title'];
       $GETtask = $_GET['task'];
       $GETdesc = $_GET['desc'];
       $GETdata = $_GET['data'];
      }
      echo " var colnames= '" . $GETtask . "," . str_replace("'", "", $GETdesc) . "'; \n";
      
      $GEToptions = "";
      if (isset($_GET['options'])) $GEToptions = urldecode($_GET['options']);
      if (isset($_POST['options'])) $GEToptions = urldecode($_POST['options']);
      
      if (isset($_GET['exponential'])) $GEToptions .= "type:'exponential',";
      if (isset($_POST['exponential'])) $GEToptions .= "type:'exponential',";
      
      if (isset($_GET['polynomial'])) {
        if (substr(($_GET['polynomial'] . ' '), 0, 1) < '0' && substr(($_GET['polynomial'] . ' '), 0, 1) > '9') {
        $GEToptions .= "type:'polynomial',degree:2,";
        } else {
        $GEToptions .= "type:'polynomial',degree:" . str_replace(",","",urldecode($_GET['polynomial'])) . ",";
        }
      }
      if (isset($_POST['polynomial'])) {
        if (substr(($_POST['polynomial'] . ' '), 0, 1) < '0' && substr(($_POST['polynomial'] . ' '), 0, 1) > '9') {
        $GEToptions .= "type:'polynomial',degree:2,";
        } else {
        $GEToptions .= "type:'polynomial',degree:" . str_replace(",","",urldecode($_POST['polynomial'])) . ",";
        }
      }
     
      echo ' google.load("visualization", "1", {packages:["corechart"]}); ' . "\n";
      echo ' google.setOnLoadCallback(drawChart); ' . "\n";
      echo ' function drawChart() { ' . "\n";
      echo ' data = google.visualization.arrayToDataTable([ ' . "\n";
      echo " ['" . $GETtask . "','" . $GETdesc . "'] \n";
      echo str_replace("~,", "',", str_replace("[~", "['", str_replace(",]", ",0]", str_replace(",]", ",0]", str_replace(",]", ",0]", $GETdata)))));
      echo "        ]);\n";
      echo "        var options = { \n";
      echo "        title: '" . $GETtitle . "', \n";
      echo "        hAxis: {title:  '" . $GETtask . "'}, \n";
      echo "        vAxis: {title:  '" . $GETdesc . "'}, \n";
      //echo "        legend: 'none', \n";
      echo "        trendlines: { 0: { " . $GEToptions . " visibleInLegend: true } }  \n";
      echo "        }; \n";

      echo " chart = new google.visualization.ScatterChart(document.getElementById('chart_div')); \n";
      $GETonclick = "";
      if (isset($_GET['onclick'])) $GETonclick = urldecode($_GET['onclick']);
      if (isset($_POST['onclick'])) $GETonclick = urldecode($_POST['onclick']);
      if ($GETonclick != "") {
      echo "google.visualization.events.addListener(chart, 'ready', function() { ready=true; }); \n";
      echo "google.visualization.events.addListener(chart, 'select', myselectfunction); \n function myselectfunction() {
 if (ready) {
  var hgt=0.0,relist=['remail',''],ire,ansbits,avblurb=talkingpoint,ioffset=0,ans=null,doit=true,kl,lk, thisnum, num=0,message = '', dparts, colparts=colnames.split(','), colav=new Array(), predparts, postdparts, diffcolnames=colnames,diffvalues;
  var selection = chart.getSelection();
  for (var i = 0; i < selection.length; i++) {
    var item = selection[i];
    if ((item.row != null && item.column != null) || (eval(ir) >= 0 && eval(ic) >= 0)) {
      if (eval(ir) >= 0 && eval(ic) >= 0) {
  //alert(999);
        item.row=ir;
        item.column=ic;
  //alert(9999);
        ir=-1;
        ic=-1;
      } else {
        ir=item.row;
        ic=item.column;
        //document.title=ir + ',' + ic;
      }
      var str = data.getFormattedValue(item.row, item.column);
      //document.title=str;
      message=document.head.innerHTML.substring(ioffset);
      while (message.indexOf(', [') != -1) {
       message=message.replace(', [',',[');
      }
      dparts=message.split(',[');
        //document.title='';
        colav.push(0.0);
      for (kl=1; kl<colparts.length; kl++) {
        //document.title=1;
        colav.push(0.0);
        doit=true;
        num=1;
        for (lk=1; lk<dparts.length; lk++) {
         if (doit) {
        //document.title=3;
         thisnum=dparts[lk].replace(']',',').split(',');
         //if (kl == 1) document.title+=4 + ' .. '  + eval(thisnum[kl]) + ' ... ' + lk + ' ... ' + colav[kl] + ' ... ' + thisnum.length + ' ... ' + dparts[lk];
         colav[kl] = eval(eval(colav[kl] * eval(-1 + num)) + eval(thisnum[kl])) / eval(0.0 + num);
        //document.title=5;
         if (dparts[lk].indexOf(';') != -1) doit=false;
         if (doit) num++;
         }
        }
        //document.title=7;
        if (avblurb == '') avblurb+=String.fromCharCode(10) + String.fromCharCode(10);
        //document.title=8;
        avblurb+='Average ' + colparts[kl] + ' over ' + num + ' ' + colparts[0] + ' values is ' + colav[kl] + String.fromCharCode(10);
        //document.title=7;
      }
      if (avblurb != '') avblurb+=String.fromCharCode(10) + String.fromCharCode(10);
      if (eval(dparts.length) >= eval(1 + item.row)) {
        postdparts=dparts[eval(1 + item.row)].replace(']',',').split(',');
        if (eval(postdparts.length) >= eval(1 + item.column)) {
         if (eval(colparts.length) == 3) { 
          if (1 == 1) {
          //avblurb+='Show this as Area Chart=A' + String.fromCharCode(10);
          //avblurb+='Show this as Bar Chart=B' + String.fromCharCode(10);
          //avblurb+='Show this as Column Chart=C' + String.fromCharCode(10) + String.fromCharCode(10);
          if (eval(x1 + y1 + x2 + y2) == 0.0000) {
            later();
          }
          x3=eval(postdparts[0].replace(']',''));
          y3=eval(str);
                    
          x=eval(x3 - 1.0);
          x1=x;
          eval(formula);
          y1=y;
          
          x=eval(x3 + 1.0);
          x2=x;
          eval(formula);
          y2=y;
          
          //alert('x1=' + x1 + ' y1=' + y1 + 'x2=' + x2 + ' y2=' + y2 + 'x3=' + x3 + ' y3=' + y3);

          hgt=calcdist();
          avblurb+=String.fromCharCode(10) + '' + String.fromCharCode(10) + 'Offset from ' + formula + ' best fit=' + hgt + String.fromCharCode(10) + '' + String.fromCharCode(10);
          avblurb+='Comma Separated Values Spreadsheet Export=filename.csv' + String.fromCharCode(10) + '' + String.fromCharCode(10) + 'Email with or without Talking Point=fillin.email@address This is when the improvement started.  Robert' + String.fromCharCode(10) + '' + String.fromCharCode(10);
          avblurb+='Change data as required below (keeping it the same or being blank or choosing Cancel leaves the status quo)';
          ans=prompt('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + ' while ' + colparts[eval(3 - item.column)] + ' were ' + postdparts[eval(3 - item.column)].replace(']','') + ' the difference being ' + eval(eval(postdparts[1].replace(']','')) - eval(postdparts[2].replace(']',''))) + avblurb, paramdata);
          if (ans != null) {
            if (ans != '' && ans != paramdata) {
             if ((ans.toLowerCase() + '~').indexOf('.csv~') != -1 && ans.toLowerCase().indexOf('&') == -1) ans=paramdata + '&csv=' + ans;
             ansbits=ans.split(' ');
             if (ansbits[0].indexOf('@') != -1) {
              if (document.getElementById('altemail')) {
              document.getElementById('altemail').value=ansbits[0];
              document.getElementById('remail').href=document.getElementById('remail').href.replace('mt=','mt=' + ansbits[0] + '&junk=youshallneverfindthis').replace('get=','mt=' + ansbits[0] + '&junk=youshallneverfindthis');
              }
              if (document.getElementById('email')) relist[1]='email';
              for (ire=0; ire<2; ire++) {
              if (relist[ire] != '') {
              document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace('fillin.email@address', ansbits[0]);
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&r=') + item.row) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&r=') + item.row;
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&c=') + item.column) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&c=') + item.column;
              if (ansbits.length > 1) {
                ans=ans.replace(ansbits[0] + ' ','');
                while (ans.indexOf(' ') != -1) {
                  ans=ans.replace(' ','%20');
                }
                while (ans.indexOf('?') != -1) {
                  ans=ans.replace('?','%3f');
                }
                while (ans.indexOf('&') != -1) {
                  ans=ans.replace('&','%26');
                }
                if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) == -1 && document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) == -1) {
                  document.getElementById(relist[ire]).href+=encodeURIComponent('&tp=') + encodeURIComponent(ans);
                } else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('&tp='), encodeURIComponent('&tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
				} else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('?tp='), encodeURIComponent('?tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
                }
                while (document.getElementById(relist[ire]).href.indexOf(' ') != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(' ','%20');
                }
                //alert(document.getElementById(relist[ire]).href);
              }
              document.getElementById(relist[ire]).click();
              }
              }
             //} else if (ans.substring(0,1).toLowerCase() == 'a') {
             // location.href=iftoobig('',document.URL.replace('/LinearTrendline', '/AreaChart').replace('line_chart.php', 'area_chart.php'));
             //} else if (ans.substring(0,1).toLowerCase() == 'b') {
             // location.href=iftoobig('',document.URL.replace('/LinearTrendline', '/BarChart').replace('line_chart.php', 'bar_chart.php'));
             //} else if (ans.substring(0,1).toLowerCase() == 'c') {
             // location.href=iftoobig('',document.URL.replace('/LinearTrendline', '/ColumnChart').replace('line_chart.php', 'column_chart.php'));
             } else {
              location.href=iftoobig('',document.URL.replace(pardata, ourencodeURIComponent(ans)));
             }
            }
          }
          } else {
          alert('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + ' while ' + colparts[eval(3 - item.column)] + ' were ' + postdparts[eval(3 - item.column)].replace(']','') + ' the difference being ' + eval(eval(postdparts[1].replace(']','')) - eval(postdparts[2].replace(']',''))) + avblurb);
          if (eval(x1 + y1 + x2 + y2) == 0.0000) {
            later();
          }
          x3=eval(postdparts[0].replace(']',''));
          y3=eval(str);
                    
          x=eval(x3 - 1.0);
          x1=x;
          eval(formula);
          y1=y;
          
          x=eval(x3 + 1.0);
          x2=x;
          eval(formula);
          y2=y;
          
          //alert('x1=' + x1 + ' y1=' + y1 + 'x2=' + x2 + ' y2=' + y2 + 'x3=' + x3 + ' y3=' + y3);

          hgt=calcdist();
          }
         } else {
          predparts=dparts[eval(1 + item.row)].split(']');
          diffvalues=('~' + predparts[0] + '~').replace('~' + postdparts[0].replace(']','') + ',','~');
          if (eval(0 + item.column) == 1) {
            diffvalues=diffvalues.replace('~' + str + ',','').replace(/~/g,'');
          } else if (eval(colparts.length) == eval(1 + item.column)) {
            diffvalues=diffvalues.replace(',' + str + '~','').replace(/~/g,'');
          } else {
            diffvalues=diffvalues.replace(',' + str + ',',',').replace(/~/g,'');
          }
          diffcolnames=('~' + colnames + '~').replace('~' + colparts[0] + ',','~').replace('~' + colparts[eval(0 + item.column)] + ',','').replace(',' + colparts[eval(0 + item.column)] + '~','').replace(',' + colparts[eval(0 + item.column)] + ',',',').replace(/~/g,'');
          if (1 == 1) {
          //avblurb+='Show this as Area Chart=A' + String.fromCharCode(10);
          //avblurb+='Show this as Bar Chart=B' + String.fromCharCode(10);
          //avblurb+='Show this as Column Chart=C' + String.fromCharCode(10) + String.fromCharCode(10);
          if (eval(x1 + y1 + x2 + y2) == 0.0000) {
            later();
          }
          x3=eval(postdparts[0].replace(']',''));
          y3=eval(str);
                    
          x=eval(x3 - 1.0);
          x1=x;
          eval(formula);
          y1=y;
          
          x=eval(x3 + 1.0);
          x2=x;
          eval(formula);
          y2=y;
          
          //alert('x1=' + x1 + ' y1=' + y1 + 'x2=' + x2 + ' y2=' + y2 + 'x3=' + x3 + ' y3=' + y3);

          hgt=calcdist();
          avblurb+=String.fromCharCode(10) + '' + String.fromCharCode(10) + 'Offset from ' + formula + ' best fit=' + hgt + String.fromCharCode(10) + '' + String.fromCharCode(10);
          avblurb+='Comma Separated Values Spreadsheet Export=filename.csv' + String.fromCharCode(10) + '' + String.fromCharCode(10) + 'Email with or without Talking Point=fillin.email@address This is when the improvement started.  Robert' + String.fromCharCode(10) + '' + String.fromCharCode(10);
          avblurb+='Change data as required below (keeping it the same or being blank or choosing Cancel leaves the status quo)';
          //ans=prompt('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + ' while (' + diffcolnames + ') were (' + diffvalues + ').' + avblurb, paramdata);
          ans=prompt('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + '.' + avblurb, paramdata);
          if (ans != null) {
            if (ans != '' && ans != paramdata) {
             if ((ans.toLowerCase() + '~').indexOf('.csv~') != -1 && ans.toLowerCase().indexOf('&') == -1) ans=paramdata + '&csv=' + ans;
             ansbits=ans.split(' ');
             if (ansbits[0].indexOf('@') != -1) {
              if (document.getElementById('altemail')) {
              document.getElementById('altemail').value=ansbits[0];
              document.getElementById('remail').href=document.getElementById('remail').href.replace('mt=','mt=' + ansbits[0] + '&junk=youshallneverfindthis').replace('get=','mt=' + ansbits[0] + '&junk=youshallneverfindthis');
              }
              if (document.getElementById('email')) relist[1]='email';
              for (ire=0; ire<2; ire++) {
              if (relist[ire] != '') {
              document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace('fillin.email@address', ansbits[0]);
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&r=') + item.row) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&r=') + item.row;
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&c=') + item.column) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&c=') + item.column;
              if (ansbits.length > 1) {
                ans=ans.replace(ansbits[0] + ' ','');
                while (ans.indexOf(' ') != -1) {
                  ans=ans.replace(' ','%20');
                }
                while (ans.indexOf('?') != -1) {
                  ans=ans.replace('?','%3f');
                }
                while (ans.indexOf('&') != -1) {
                  ans=ans.replace('&','%26');
                }
                if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) == -1 && document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) == -1) {
                  document.getElementById(relist[ire]).href+=encodeURIComponent('&tp=') + encodeURIComponent(ans);
                } else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('&tp='), encodeURIComponent('&tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
				} else if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('?tp=')) != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(encodeURIComponent('?tp='), encodeURIComponent('?tp=') + encodeURIComponent(ans) + String.fromCharCode(96));
                }
                while (document.getElementById(relist[ire]).href.indexOf(' ') != -1) {
                  document.getElementById(relist[ire]).href=document.getElementById(relist[ire]).href.replace(' ','%20');
                }
                //alert(document.getElementById(relist[ire]).href);
              }
              document.getElementById(relist[ire]).click();
              }
              }
             //} else if (ans.substring(0,1).toLowerCase() == 'a') {
             // location.href=iftoobig('',document.URL.replace('/LinearTrendline', '/AreaChart').replace('line_chart.php', 'area_chart.php'));
             //} else if (ans.substring(0,1).toLowerCase() == 'b') {
             // location.href=iftoobig('',document.URL.replace('/LinearTrendline', '/BarChart').replace('line_chart.php', 'bar_chart.php'));
             //} else if (ans.substring(0,1).toLowerCase() == 'c') {
             // location.href=iftoobig('',document.URL.replace('/LinearTrendline', '/ColumnChart').replace('line_chart.php', 'column_chart.php'));
             } else {
              location.href=iftoobig('',document.URL.replace(pardata, ourencodeURIComponent(ans)));
             }
            }
          }
          } else {
          //alert('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + ' while (' + diffcolnames + ') were (' + diffvalues + ').' + avblurb);
          alert('In ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[item.column] + ' of ' + str + '.' + avblurb);
          if (eval(x1 + y1 + x2 + y2) == 0.0000) {
            later();
          }
          x3=eval(postdparts[0].replace(']',''));
          y3=eval(str);
                    
          x=eval(x3 - 1.0);
          x1=x;
          eval(formula);
          y1=y;
          
          x=eval(x3 + 1.0);
          x2=x;
          eval(formula);
          y2=y;

          hgt=calcdist();
          }
         }
        }
      }
      message += '{row:' + item.row + ',column:' + item.column + '} = ' + str;
    } else if (item.row != null) {
      var str = data.getFormattedValue(item.row, 0);
      message += '{row:' + item.row + ', column:none}; value (col 0) = ' + str;
    } else if (item.column != null) {
      var str = data.getFormattedValue(0, item.column);
      message += '{row:none, column:' + item.column + '}; value (row 0) = ' + str;
    }
  }
  if (message == '') {
    message = 'nothing';
  }
  if (1 == 2) console.log('You selected ' + message);
 }
 }     
      //);  
      ";
      }
      echo " chart.draw(data, options); \n";
      if ($GETr != "" && $GETc == "") echo " chart.setSelection([{'row': " . $GETr . ", 'column': null}]); setTimeout(myselectfunction,4100); \n";
      if ($GETr == "" && $GETc != "") echo " chart.setSelection([{'row': null, 'column': " . $GETc . "}]);  setTimeout(myselectfunction,4100); \n";
      if ($GETr != "" && $GETc != "") echo " chart.setSelection([{'row': " . $GETr . ", 'column': " . $GETc . "}]);  setTimeout(myselectfunction,4100); \n";
      echo " } \n";
      
      echo " </script> \n";
      echo " </head> \n";
      if (isset($_POST['data'])) {
      echo " <body onload='setTimeout(later, 2 * 1000);'> \n";
      } else {
      echo " <body onload='setTimeout(later, 2 * 1000);'> \n";
      }
      if (strpos($GEToptions, "polynomial") !== false) {
      echo " <h1>" . $GETtitle . " Polynomial Trendline</h1> \n";
      } else if (strpos($GEToptions, "exponential") !== false) {
      echo " <h1>" . $GETtitle . " Exponential Trendline</h1> \n";
      } else {
      echo " <h1>" . $GETtitle . " Linear Trendline</h1> \n";
      }
?>
    <div id="chart_div" style="width: 900px; height: 500px; background-color: yellow;"></div><div id="bitsatend" style="display:inline;"></div>
    <a title="Another Linear Trendline" href="./linear_trendline.php">Another linear trendline?</a>
    <?php 
      if (!isset($_GET['desc']) && !isset($_POST['desc'])) {
      
        echo '<' . 'script' . '>' . "\n " . " function later() { \n" . ' var datalineprefix = " "; ' . "\n";
        echo ' var datalinesuffix = ""; ' . "\n";
        echo ' var extra = ""; ' . "\n";
        echo ' var thisline = 1; ' . "\n";
        echo ' var totalleft = 100.0; ' . "\n";
        echo ' var title = prompt("Enter (Linear) Trendline Title (optionally suffix by ... &onclick=y for added onclick functionality, &polynomial=2 (degree 2), &polynomial=3 (degree 3 etcetera), &exponential=y)", "Age of sugar maples vs. trunk diameter, in inches"); ' . "\n";
        echo ' if (title != null) { ' . "\n";
        echo ' var task = prompt("Enter Horizontal Axis Title", "Diameter"); ' . "\n";
        echo ' var desc = prompt("Enter Vertical Axis Title", "Age"); ' . "\n";
        echo ' var datac = ""; ' . "\n";
        echo ' var sq = "' . "'" . '"; ' . "\n";
        echo ' datalineprefix = prompt("Enter " + task + " Value " + thisline, "0"); ' . "\n";
        echo ' while (datalineprefix.length != 0 && (desc.indexOf("ercent") == (0 - 1) || desc.indexOf("  ercent ") != (0 - 1) || (desc.indexOf("ercent") != (0 - 1) && totalleft > 0))) { ' . "\n";
        echo ' if (datalinesuffix.length != 0) datalineprefix = prompt("Enter " + task + " Value " + thisline + " (for no more hit Cancel button)", ""); ' . "\n";
        echo ' if (datalineprefix == null) { ' . "\n";
        echo '     datalineprefix = ""; ' . "\n";
        echo '     totalleft = 0; ' . "\n";
        echo ' } else if (datalineprefix.length != 0) { ' . "\n";
        echo '     extra = "";  ' . "\n";
        echo '     if (desc.indexOf("ercent") != (0 - 1) && desc.indexOf("  ercent ") == (0 - 1)) extra = " (for " + totalleft + " enter nothing or hit Cancel button)";  ' . "\n";
        echo '     datalinesuffix = prompt("Enter " + desc.replace("  ercent ", "") + " Value " + thisline + extra, "0"); ' . "\n";
        echo '     if (datalinesuffix == null && desc.indexOf("ercent") != (0 - 1) && desc.indexOf("  ercent ") == (0 - 1)) datalinesuffix = totalleft; ' . "\n";
        echo '     if (datalinesuffix == null) datalinesuffix = "0"; ' . "\n";
        echo '     if (datalinesuffix.length == 0) { ' . "\n";
        echo '       datalinesuffix = eval(totalleft); ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(totalleft) == eval(datalinesuffix) && desc.indexOf("ercent") != (0 - 1) && desc.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(datalinesuffix) > eval(totalleft) && desc.indexOf("ercent") != (0 - 1) && desc.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       datalinesuffix = eval(totalleft); ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(datalinesuffix) > eval(totalleft) && desc.indexOf("ercent") == (0 - 1) && desc.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       desc = desc + "  ercent "; ' . "\n";
        echo '     } else { ' . "\n";
        echo '       totalleft = eval(totalleft) - eval(datalinesuffix); ' . "\n";
        echo '     } ' . "\n";
        echo '     datac = datac + ", [" + datalineprefix + "," + datalinesuffix + "] "; ' . "\n";
        echo '     thisline++; ' . "\n";
        echo '   } ' . "\n";
        echo ' } ' . "\n";
        echo ' window.location = iftoobig("","./linear_trendline.php?title=" + title + "&task=" + task + "&desc=" + desc.replace("  ercent ", "") + "&data=" + datac); ' . "\n";
        echo "} \n" . "} \n";
        echo "\n" . '</' . 'script' . '>' . "\n";
      } else {
        echo '<' . 'script' . '>' . "\n " . " function later() { var formulae; \n";
        echo "
          var gs=document.getElementsByTagName('g'), posttexts, texts, dsbits, fact='', nn, mm=0, xx='*x',cfrom='', cto='', ix, fct='';
          for (var kk=0; kk<gs.length; kk++) {
          if (gs[kk].innerHTML.indexOf('>y = ') != -1) {
          texts=gs[kk].innerHTML.split('</text>');
          for (var ik=0; ik<texts.length; ik++) {
            if (texts[ik].indexOf('>') != -1) {
            posttexts=texts[ik].split('>');
            if (posttexts[1].indexOf('y = ') == 0) {
              formula=posttexts[1];
            } else {
              formula+=posttexts[1];
            }
            }
          }
          }
          }
          
          formula=formula.replace(/x/g, '*x').replace(' *x', ' 1.0*x').replace(' *x', ' 1.0*x').replace(' *x', ' 1.0*x');
          formula=formula.replace(/E+/g, '@#');
          formula=formula.replace(/E-/g, '!$');
          formula=formula.replace(/\+/g, ' + ');
          formula=formula.replace(/-/g, ' - ');
          formula=formula.replace(/@#/g, 'E+');
          formula=formula.replace(/!$/g, 'E-');

          if (formula.indexOf('e^') != -1) {
              dsbits = formula.split('e^');
              for (var kiik=1; kiik<dsbits.length; kiik++) {
                posttexts=dsbits[kiik].split(' ');
                texts=dsbits[eval(-1 + kiik)].split(' ');
                cfrom=texts[eval(-1 + texts.length)] + 'e^' + posttexts[0];
                cto='Math.pow((' + texts[eval(-1 + texts.length)] + '*2.7182818284590452353602874),' + posttexts[0] + ')';
                formula=formula.replace(cfrom,cto);
			  }              
              
          }
          
          if (formula.indexOf('E') != -1) {
              dsbits = formula.split('E');
              for (var jiik=1; jiik<dsbits.length; jiik++) {
                posttexts=dsbits[jiik].split('*');
                texts=dsbits[eval(-1 + jiik)].split(' ');
                cfrom=texts[eval(-1 + texts.length)] + 'E' + posttexts[0];
                cto=Number(cfrom);
                //alert('cfrom=' + cfrom + ' cto=' + cto);
                if (('' + cto).indexOf('NaN') != -1) {
                  cto=('' + cfrom).replace('E','e');
                }
                if (('' + cto).indexOf('e') != -1) {
                 posttexts=('' + cto).split('e');
                 fct='*'
                 if (posttexts[1].substring(0,1) == '-') {
                  fct+='0.1';
                  for (ix=-2; ix>=eval(posttexts[1]); ix--) {
                    fct=fct.replace('0.','0.0');
                  }
                 } else {
                  fct+='1';
                  for (ix=2; ix<=eval(posttexts[1].replace('+','')); ix++) {
                    fct+='0';
                  }
                 }
                 cto=('' + cto).replace('e' + posttexts[1],fct);
                //alert('cfrom=' + cfrom + ' new cto=' + cto);
                }
                formula=formula.replace(cfrom,cto);
                //alert('formula=' + formula);
			  }              
          }
          
          formula=formula.replace(/e/g, '*2.7182818284590452353602874*');
         
          if (formula.indexOf('^') != -1) {
           while (formula.indexOf('^') != -1) {
              dsbits = formula.split('^');
              for (var iik=eval(-1 + dsbits.length); iik>=eval(-1 + dsbits.length); iik--) {
                texts=dsbits[iik].split(' ');
                //alert(texts[0]);
                mm=eval(texts[0]);
                //alert(mm);
                xx='*x';
                for (nn=2; nn<mm; nn++) {
                  xx+='*x';
                }
                formula=formula.replace('^' + mm, xx);
                //alert('Formula=' + formula);
              }
            }
          }   
                //alert('FORMula=' + formula);
          
          x=0.0;
          x1=x;
          eval(formula);
          y1=y;
          
          x=100.0;
          x2=x;
          eval(formula);
          y2=y;

              x=-1;
              y=-1;
        ";
        echo "\n } \n";
        echo "\n</" . "script>\n";
      } 
    ?>
  </body>
</html>

