<?php
  if (file_exists("csv.php")) {
    include "csv.php";
  } else if (file_exists("../csv.php")) { 
    include "../csv.php";
  }

  $data0 = "";
  if (isset($_GET['data0'])) {
      if ($_GET['data0'] != "" && strpos($_GET['data0'], "[") !== false && strpos($_GET['data0'], "[") < strpos($_GET['data0'], ",")) {
   $data0 = "&data0=," . $_GET['data0'];
      } else {
   $data0 = "&data0=" . $_GET['data0'];
      }
  }
  if (isset($_POST['data0'])) {
      if ($_POST['data0'] != "" && strpos($_POST['data0'], "[") !== false && strpos($_POST['data0'], "[") < strpos($_POST['data0'], ",")) {
   $data0 = "&data0=," . $_POST['data0'];
      } else {
   $data0 = "&data0=" . $_POST['data0'];
      }
  }
  
  


  function retval($inv) {
    if (strpos($inv, "E-") !== false) return "0";
    return $inv;
  }
?>

<html>
  <head> <script type="text/javascript" src="http://www.rjmprogramming.com.au/gchartgen.js"></script>
    <title>Pie Chart (Differences) - RJM Programming - http://www.rjmprogramming.com.au (Copyright &copy; 2013 rjmprogramming.com.au all rights reserved.)</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=4, user-scalable=yes" />
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
   
   var operc=-1, ready=true, x=-1, y=-1, elemLeft=0, elemTop=0, ir=-1, ic=-1, talkingpoint='', targ=null, targ0=null, datasetis='data';   

function PieClick() { // thanks to http://stackoverflow.com/questions/17728880/how-to-add-different-click-events-on-each-pie-of-a-pie-chart-created-by-highchar
    var sliceClicked = this.name;
    

    // do something
}
<?php 
      echo "   var textras='', pieChartBefore, pieChartAfter, pieChartDiff, data, chart, data0, pardata = (location.search.split('data=')[1] ? location.search.split('data=')[1].split('&')[0] : ''), paramdata=decodeURIComponent(pardata), pardata0 = (location.search.split('data0=')[1] ? location.search.split('data0=')[1].split('&')[0] : ''), paramdata0=decodeURIComponent(pardata0); \n";
      echo "   var sliceid = 0; /" . "/ thanks to https://gist.github.com/alexrainman/bb8d49357250df0859c0  \n";

      if (isset($_GET['onclickhuh'])) echo "\n textras='&onclick=" . urldecode($_GET['onclickhuh']) . "'; \n";
      if (isset($_POST['onclickhuh'])) echo "\n textras='&onclick=" . urldecode($_POST['onclickhuh']) . "'; \n";
 
      $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";
      }

 
      $GETonclick = "";
      $GETyonclick = "";
      if (isset($_GET['onclick'])) $GETonclick = urldecode($_GET['onclick']);
      if (isset($_POST['onclick'])) $GETonclick = urldecode($_POST['onclick']);
      if (isset($_GET['onclick'])) $GETyonclick = '&onclick=' + urldecode($_GET['onclick']);
      if (isset($_POST['onclick'])) $GETyonclick = '&onclick=' + urldecode($_POST['onclick']);
      
      echo "var yonclick='" . $GETyonclick . "'; \n";
      
     
      
      $xgd = ""; 
      $gd = ""; 
      $ntt=false;
      $nondefs=", is3D: true ";
      $GETtitle = "My Daily Activities";
      $GETtask = "Task";
      $GETdesc = "Hours per Day";
      if (isset($_GET['deschuh'])) $GETdesc = urldecode($_GET['deschuh']);
      if (isset($_POST['deschuh'])) $GETdesc = urldecode($_POST['deschuh']);
      $GETdata = ",['Work',11],['Eat',2],['Commute',2],['Watch TV', 2],['Sleep',7]";
      



       if (isset($_GET['country'])) {
       $GETtask = str_replace("\\'", "'", $_GET['country']);
       } else if (isset($_POST['country'])) {
       $GETtask = str_replace("\\'", "'", $_POST['country']);
       }
       if (isset($_GET['popularity'])) {
       $GETdesc = str_replace("\\'", "'", $_GET['popularity']);
       } else if (isset($_POST['popularity'])) {
       $GETdesc = str_replace("\\'", "'", $_POST['popularity']);
       }



      if (isset($_GET['desc']) || isset($_POST['desc'])) {
      if (isset($_GET['desc'])) {
       $GETtitle = $_GET['title'];
       $GETtask = $_GET['task'];
       $GETdesc = $_GET['desc'];
       $GETdata = $_GET['data'];
      } else if (isset($_POST['desc'])) {
       $GETtitle = $_POST['title'];
       $GETtask = $_POST['task'];
       $GETdesc = $_POST['desc'];
       $GETdata = urldecode($_POST['data']);
      }

       if (isset($_GET['country'])) {
       $GETtask = str_replace("\\'", "'", $_GET['country']);
       } else if (isset($_POST['country'])) {
       $GETtask = str_replace("\\'", "'", $_POST['country']);
       }
       if (isset($_GET['popularity'])) {
       $GETdesc = str_replace("\\'", "'", $_GET['popularity']);
       } else if (isset($_POST['popularity'])) {
       $GETdesc = str_replace("\\'", "'", $_POST['popularity']);
       }


      
      //if ($GETdata != "" && strpos($GETdata, "[") !== false && strpos($GETdata, "[") < strpos($GETdata, ",")) {
      // $GETdata="," . $GETdata;
      //}

      $newextras=$nondefs;
      echo " var colnames= '" . $GETtask . "," . str_replace("'", "", $GETdesc) . "'; \n";
      

      
      if ($GETdata != "" && strpos($GETdata, "[") !== false && strpos($GETdata, "[") < strpos($GETdata, ",")) {
       $GETdata="," . $GETdata;
      }

      if ($GETonclick != "") {
      echo " function myselectfunctioncca() {
 if (ready) {
  var relist=['remail',''],ire,ansbits,ioffset=0,ans=null,doit=true,avblurb=talkingpoint,kl,lk, thisnum, num=0,message = '', dparts, colparts=colnames.split(','), colav=new Array(), predparts, postdparts, diffcolnames=colnames,diffvalues;
  var selection = pieChartAfter.getSelection();
  for (var i = 0; i < 1; i++) { //selection.length; i++) {
    //var item = selection[i];
    if ((eval(ir) >= 0 && eval(ic) < 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(999);
        ir=ir;
        //ic=ic;
  //alert(9999);
        //ir=-1;
        //ic=-1;
      } else {
        //ir=ir;
        //ic=ic;
        //document.title=ir + ',' + ic;
      }
      var str = data.getFormattedValue(ir, 0);
      //document.title=str;
      message=document.head.innerHTML.substring(ioffset);
      while (message.indexOf(', [') != -1) {
       message=message.replace(', [',',[');
      }
      dparts=message.split(',[');
      postdparts=dparts[eval(1 + ir)].replace(']',',').split(',');
      var prevals=message.split('[' + String.fromCharCode(39) + str + String.fromCharCode(39) + ',');
      if (prevals.length > 1) {
        var postvals=prevals[1].replace(']',',').split(',');
        avblurb+=String.fromCharCode(10) + '' + String.fromCharCode(10) + '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('Regarding ' + colparts[0] + ' ' + str + ' you clicked ' + postvals[0].replace(']','') + ' (' + operc + '%) ' + 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 (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';

    
             if (ansbits[0].indexOf('@') != -1) {
               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=') + ir) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&cca=y&r=') + ir;
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&c=') + ic) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&cca=y&c=') + ic;
              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 {
              location.href=document.URL.replace(pardata, ourencodeURIComponent(ans));
             }
            }
          }
        ir=-1;
      }
      
      message += '{row:' + ir + ',column:' + ic + '} = ' + str;
    } else if (ir != null) {
      var str = data.getFormattedValue(ir, 0);
      message += '{row:' + ir + ', column:none}; value (col 0) = ' + str;
      alert(message);
    } else if (ic != null) {
      var str = data.getFormattedValue(0, ic);
      message += '{row:none, column:' + ic + '}; value (row 0) = ' + str;
    }
  }
  if (message == '') {
    message = 'nothing';
  }
  //document.title='2 ' + message;
  if (1 == 2) console.log('You selected ' + message);
 }
 }     
     // );  
      ";
      

      echo " function myselectfunctionccb() {
 if (ready) {
  var relist=['remail',''],ire,ansbits,ioffset=document.head.innerHTML.indexOf(']);'),ans=null,doit=true,avblurb=talkingpoint,kl,lk, thisnum, num=0,message = '', dparts, colparts=colnames.split(','), colav=new Array(), predparts, postdparts, diffcolnames=colnames,diffvalues;
  var selection = pieChartBefore.getSelection();
  for (var i = 0; i < 1; i++) { //selection.length; i++) {
    //var item = selection[i];
    if ((eval(ir) >= 0 && eval(ic) < 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(999);
        ir=ir;
        //ic=ic;
  //alert(9999);
        //ir=-1;
        //ic=-1;
      } else {
        ir=ir;
        //ic=ic;
        //document.title=ir + ',' + ic;
      }
      //alert('ir=' + ir);
      var str = data0.getFormattedValue(ir, 0);
      //alert(str);
      //document.title=str;
      message=document.head.innerHTML.substring(ioffset);
      while (message.indexOf(', [') != -1) {
       message=message.replace(', [',',[');
      }
      dparts=message.split(',[');
      postdparts=dparts[eval(1 + ir)].replace(']',',').split(',');
      var prevals=message.split('[' + String.fromCharCode(39) + str + String.fromCharCode(39) + ',');
      if (prevals.length > 1) {
        var postvals=prevals[1].replace(']',',').split(',');
        avblurb+=String.fromCharCode(10) + '' + String.fromCharCode(10) + '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('Regarding ' + colparts[0] + ' ' + str + ' you clicked ' + postvals[0].replace(']','') + ' (' + operc + '%) ' + avblurb, paramdata0);
          if (ans != null) {
            if (ans != '' && ans != paramdata0) {
             if ((ans.toLowerCase() + '~').indexOf('.csv~') != -1 && ans.toLowerCase().indexOf('&') == -1) ans=paramdata0 + '&csv0=' + ans;
             ansbits=ans.split(' ');
                  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';

    
             if (ansbits[0].indexOf('@') != -1) {
               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=') + ir) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&ccb=y&r=') + ir;
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&c=') + ic) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&ccb=y&c=') + ic;
              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 {
              location.href=document.URL.replace(pardata0, ourencodeURIComponent(ans));
             }
            }
          }
        ir=-1;
      }

      message += '{row:' + ir + ',column:' + ic + '} = ' + str;
    } else if (ir >= 0) {
      var str = data.getFormattedValue(ir, 0);
      message += '{row:' + ir + ', column:none}; value (col 0) = ' + str;
      alert(message);
    } else if (ic >= 0) {
      var str = data.getFormattedValue(0, ic);
      message += '{row:none, column:' + ic + '}; value (row 0) = ' + str;
    }
  }
  if (message == '') {
    message = 'nothing';
  }
  //document.title='1 ' + message;
  if (1 == 2) console.log('You selected ' + message);
 }
 }     
     // );  
      ";
      

      echo " function myselectfunction() {
 if (ready) {
  var relist=['remail',''],ire,ansbits,ioffset=0,ans=null,doit=true,avblurb=talkingpoint,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 < 1; i++) { //selection.length; i++) {
    //var item = selection[i];
    if ((eval(ir) >= 0 && eval(ic) < 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(999);
        ir=ir;
        //ic=ic;
  //alert(9999);
        //ir=-1;
        //ic=-1;
      } else {
        //ir=ir;
        //ic=ic;
        //document.title=ir + ',' + ic;
      }
      var str = data.getFormattedValue(ir, 0);
      //document.title=str;
      message=document.head.innerHTML.substring(ioffset);
      while (message.indexOf(', [') != -1) {
       message=message.replace(', [',',[');
      }
      dparts=message.split(',[');
      postdparts=dparts[eval(1 + ir)].replace(']',',').split(',');
      var prevals=message.split('[' + String.fromCharCode(39) + str + String.fromCharCode(39) + ',');
      if (prevals.length > 1) {
        var postvals=prevals[1].replace(']',',').split(',');
        avblurb+=String.fromCharCode(10) + '' + String.fromCharCode(10) + '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('Regarding ' + colparts[0] + ' ' + str + ' you clicked ' + postvals[0].replace(']','') + ' (' + operc + '%) ' + 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 (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';

    
             if (ansbits[0].indexOf('@') != -1) {
               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=') + ir) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&cca=y&r=') + ir;
              if (document.getElementById(relist[ire]).href.indexOf(encodeURIComponent('&c=') + ic) == -1) document.getElementById(relist[ire]).href+=encodeURIComponent('&cca=y&c=') + ic;
              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 {
              location.href=document.URL.replace(pardata, ourencodeURIComponent(ans));
             }
            }
          }
        ir=-1;
      }
      
      message += '{row:' + ir + ',column:' + ic + '} = ' + str;
    } else if (ir != null) {
      var str = data.getFormattedValue(ir, 0);
      message += '{row:' + ir + ', column:none}; value (col 0) = ' + str;
      alert(message);
    } else if (ic != null) {
      var str = data.getFormattedValue(0, ic);
      message += '{row:none, column:' + ic + '}; value (row 0) = ' + str;
    }
  }
  if (message == '') {
    message = 'nothing';
  }
  //document.title='2 ' + message;
  if (1 == 2) console.log('You selected ' + message);
 }
 }     
     // );  
      ";
}      


      
       if (isset($_GET['data0']) || isset($_POST['data0'])) { 
      echo " google.load('visualization', '1.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 ' data0 = google.visualization.arrayToDataTable([ ' . "\n";
      echo " ['" . $GETtask . "','" . $GETdesc . "'] \n";
      if (isset($_POST['data0'])) {
      echo str_replace("~,", "',", str_replace("[~", "['", str_replace(",]", ",0]", str_replace(",]", ",0]", str_replace(",]", ",0]", urldecode($_POST['data0']))))));
      } else {
      echo str_replace("~,", "',", str_replace("[~", "['", str_replace(",]", ",0]", str_replace(",]", ",0]", str_replace(",]", ",0]", $_GET['data0'])))));
      }
      echo "        ]);\n";

      
      echo " pieChartBefore = new google.visualization.PieChart(document.getElementById('piechart_before')); \n";
      echo " pieChartAfter = new google.visualization.PieChart(document.getElementById('piechart_after')); \n";
      echo " pieChartDiff = new google.visualization.PieChart(document.getElementById('piechart_diff')); \n";

      //echo " var options = { pieSliceText: 'none' }; \n";

http://www.rjmprogramming.com.au/PHP/PieChart/pie_chart_diff.php?title=Five%20Most%20Populous%20Countries&onclick=y&width=556&height=347&label=Country&desc=Population%20on%202015,Population%20in%202000&data=,[~China~,1401586609]+,[~India~,1282390303]+,[~United+States+of+America~,322583006]+,[~Indonesia~,252812245]+,[~Brazil~,202033670]&data0=,[~China~,1280428583],[~India~,1042261758],[~United+States+of+America~,284594395],[~Indonesia~,208938698],[~Brazil~,174504898]

      if (strpos(urldecode($GETdesc), ",") !== false) {
        $others=explode(",", urldecode($GETdesc));
      echo "        var optionszero = { \n";
      echo "        title: '" . $others[0] . "'" . $newextras . " \n";
      echo "        }; \n";
      echo "        var optionsone = { \n";
      echo "        title: '" . $others[1] . "'" . $newextras . " \n";
      echo "        }; \n";
        
      } 
      echo "        var options = { \n";
      echo "        title: '" . $GETtitle . "'" . $newextras . " \n";
      echo "        }; \n";
 }  // huh?
   
      //echo "   var selection; \n";

 if ($ntt) {
  
      echo "   function eventHandler(e){ \n";
      echo "     chart.setSelection([e]); \n";
      echo "     try { \n";
      echo "       selection = chart.getSelection(); \n";
      echo "       sliceid = selection[0].row;  \n";
      echo "     } \n";
      echo "     catch(err) { \n";
      echo "       ; \n";
      echo "     } \n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(0)").css("font-weight", "bold"); ' . "\n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(1)").html(tooltip[sliceid]).css("font-family", "Arial"); ' . "\n";
      echo "   } \n";
  
      echo "   function ceventHandler(e){ \n";
      echo "     chart.setSelection([e]); \n";
      echo "     try { \n";
      echo "       selection = chart.getSelection(); \n";
      echo "       sliceid = selection[0].row;  \n";
      echo "     } \n";
      echo "     catch(err) { \n";
      echo "       ; \n";
      echo "     } \n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(0)").css("font-weight", "bold"); ' . "\n";
      echo '    $(".google-visualization-tooltip-item-list li:eq(1)").html(tooltip[sliceid]).css("font-family", "Verdana"); ' . "\n";
      echo "   } \n";
  
  
      if (strpos($xgd, "<") !== false) {
       // echo "   google.visualization.events.addListener(chart, 'select', ceventHandler); \n";
        echo "   google.visualization.events.addListener(chart, 'onmouseover', ceventHandler); \n";
      } else {
        echo "   google.visualization.events.addListener(chart, 'onmouseover', eventHandler); \n";
      }
}

      if ($GETonclick != "") {
      //echo "google.visualization.events.addListener(pieChartBefore, 'onmouseover', myselectfunctionccb); \n";
      //echo "google.visualization.events.addListener(pieChartBefore, 'select', myselectfunctionccb); \n";
      }


      if (strpos(urldecode($GETdesc), ",") !== false) {
      echo " pieChartBefore.draw(data0, optionszero); \n";
      } else {
      echo " pieChartBefore.draw(data0, options); \n";
      }
      if ($GETr != "" && $GETc == "" && (isset($_GET['ccb']) || isset($_POST['ccb']))) echo " pieChartBefore.setSelection([{'row': " . $GETr . ", 'column': null}]); setTimeout(myselectfunctionccb,4100); \n";
      if ($GETr == "" && $GETc != "" && (isset($_GET['ccb']) || isset($_POST['ccb']))) echo " pieChartBefore.setSelection([{'row': null, 'column': " . $GETc . "}]);  setTimeout(myselectfunctionccb,4100); \n";
      if ($GETr != "" && $GETc != "" && (isset($_GET['ccb']) || isset($_POST['ccb']))) echo " pieChartBefore.setSelection([{'row': " . $GETr . ", 'column': " . $GETc . "}]);  setTimeout(myselectfunctionccb,4100); \n";



      if ($GETonclick != "") {
      echo "google.visualization.events.addListener(pieChartAfter, 'ready', function() { ready=true; }); \n";
      //echo "google.visualization.events.addListener(pieChartAfter, 'onmouseover', myselectfunctioncca); \n";
      //echo "google.visualization.events.addListener(pieChartAfter, 'select', myselectfunctioncca); \n";
      }

      if (strpos(urldecode($GETdesc), ",") !== false) {
      echo " pieChartAfter.draw(data, optionsone); \n";
      } else {
      echo " pieChartAfter.draw(data, options); \n";
      }
      if ($GETr != "" && $GETc == "" && (isset($_GET['cca']) || isset($_POST['cca']))) echo " pieChartAfter.setSelection([{'row': " . $GETr . ", 'column': null}]); setTimeout(myselectfunctioncca,4100); \n";
      if ($GETr == "" && $GETc != "" && (isset($_GET['cca']) || isset($_POST['cca']))) echo " pieChartAfter.setSelection([{'row': null, 'column': " . $GETc . "}]);  setTimeout(myselectfunctioncca,4100); \n";
      if ($GETr != "" && $GETc != "" && (isset($_GET['cca']) || isset($_POST['cca']))) echo " pieChartAfter.setSelection([{'row': " . $GETr . ", 'column': " . $GETc . "}]);  setTimeout(myselectfunctioncca,4100); \n";

      echo " var diffData = pieChartDiff.computeDiff(data0, data); \n";
      echo " pieChartDiff.draw(diffData, options);  \n } \n";
      
      // Thanks to http://www.quirksmode.org/js/events_properties.html
      echo "\n function ocdata0(e) { 
      var prevtarg0=targ0;
	if (!e) var e = window.event;
	if (e.target) targ0 = e.target;
	else if (e.srcElement) targ0 = e.srcElement;
	if (targ0.nodeType == 3) targ0 = targ.parentNode;
		if (1 == 4 && targ0.innerHTML.length > 0) { alert(targ0.innerHTML); }
		if (targ0 == null && prevtarg0 != null) {
		 targ0=prevtarg0;
		} else if (targ0 != null) {
		 if (targ0.innerHTML.length > 0 && targ0.innerHTML.indexOf('%') != -1) {
		  datasetis='data0';
		  prevtarg0=targ0;
		 } else if (prevtarg0 != null) {
		  targ0=prevtarg0;
		 }
		}
		 }  \n";
		 
       echo "\n function ocdata(e) { 
      var prevtarg=targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode;
		if (1 == 4 && targ.innerHTML.length > 0) { alert(targ.innerHTML); }
		if (targ == null && prevtarg != null) {
		 targ=prevtarg;
		} else if (targ != null) {
		 if (targ.innerHTML.length > 0 && targ.innerHTML.indexOf('%') != -1) {
		  datasetis='data';
		  prevtarg=targ;
		 } else if (prevtarg != null) {
		  targ=prevtarg;
		 }
		}
		 }  \n";
		 
     echo "\n function postoc(e) {  var selection, item; \n
		 	if (datasetis == 'data') { ocdata(e);	if (targ) { if (targ.innerHTML.length > 0) { ir=wio(data, targ.innerHTML.replace('%',''));  myselectfunctioncca(); } } } else {  ocdata0(e);	if (targ0) { if (targ0.innerHTML.length > 0) { ir=wio(data0, targ0.innerHTML.replace('%',''));  myselectfunctionccb(); } }   }
	 }  \n";
		 
	 echo "\n function wio(ad, perc) { \n
	  operc=perc;
	  var retval=0, ex, totx=0.0, numx=0, thisnumx=0.0, ij=0, iok=1, ioffset=0, xdps, ada=new Array();
	  var dhi=document.head.innerHTML.replace(/, \[/g, ',[').replace(/ = /g, '=');
	  ioffset=dhi.indexOf(datasetis + '=google.visualization.arrayToDataTable(');
	  var dps=dhi.substring(ioffset).split(',[');
	  while (iok == 1) {
	   try {
	    xdps=dps[eval(1 + ij)].replace(']',',').split(',');
	    //alert(dps[eval(1 + ij)].replace(']',',') + ' ... ' + xdps.length);
	    //alert('thisnumx=' + datasetis + '.getFormattedValue(ij, 0)');
	    //eval('thisnumx=' + datasetis + '.getFormattedValue(ij, 0)');
	    thisnumx=eval(xdps[1]);
	    ada.push(thisnumx);
	    totx+=eval(thisnumx);
	    //alert('thisnumx=' + thisnumx);
	    ij++;
	    numx++;
	    if (xdps[2].indexOf(']);') != -1) iok=0;
	    if (eval(ij) >= dps.length) iok=0; 
	   } catch (ex) {
	    //alert('Error:' + ex.message);
	    iok=0;
	   }
	  }
	  //alert('numx=' + numx + ' and totx=' + totx);
	  for (ij=0; ij<numx; ij++) {
	    if (Math.abs(eval(ada[ij] * 100.0 / totx) - eval(perc)) <= 0.1) {
	      return ij;
	    }
	  }
	  return retval;
	 } \n";
	 

          
      echo " </script> \n";
      echo " </head> \n";
      echo " <body onmousedown='postoc(event);'> \n";
      echo " <h1>" . $GETtitle . " Pie Chart</h1> \n";
 
      echo " <span onmouseover='ocdata0(event);' id='piechart_before' style='width: 450px; height: 250px; display: inline-block'></span>\n";
      echo " <span onmouseover='ocdata(event);' id='piechart_after' style='width: 450px; height: 250px; display: inline-block'></span>\n";
      echo " <span id='piechart_diff' style='width: 450px; height: 250px; display: inline-block'></span>\n";
       } else {
      echo ' data0 = "";' . "\n";
      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 "        }; \n";

      echo " chart = new google.visualization.PieChart(document.getElementById('chart_div')); \n";
      if ($GETonclick != "") {
      echo "google.visualization.events.addListener(chart, 'ready', function() { ready=true; }); \n";
      echo "google.visualization.events.addListener(chart, 'select', myselectfunction); \n";
      }
      echo " chart.draw(data, options); \n";
      if ($GETr != "" && $GETc == "" && (!isset($_GET['cca']) && !isset($_POST['cca']))) echo " chart.setSelection([{'row': " . $GETr . ", 'column': null}]); setTimeout(myselectfunction,4100); \n";
      if ($GETr == "" && $GETc != "" && (!isset($_GET['cca']) && !isset($_POST['cca']))) echo " chart.setSelection([{'row': null, 'column': " . $GETc . "}]);  setTimeout(myselectfunction,4100); \n";
      if ($GETr != "" && $GETc != "" && (!isset($_GET['cca']) && !isset($_POST['cca']))) echo " chart.setSelection([{'row': " . $GETr . ", 'column': " . $GETc . "}]);  setTimeout(myselectfunction,4100); \n";
      echo "  } \n";
       
      echo " </script> \n";
      echo " </head> \n";
      if ($GETonclick != "") {
      echo " <body style='background-color: silver;'> \n";
      } else {
      echo " <body onload='setTimeout(later, 2 * 1000);' style='background-color: silver;'> \n";
      }
      echo " <h1>" . $GETtitle . " Pie Chart</h1> \n";
      echo '<div id="chart_div" style="width: 900px; height: 500px; background-color: yellow;"></div><div id="bitsatend" style="display:inline;"></div>' . "\n";
       }
?>
    <a title="Another Pie Chart to show differences for" href="./pie_chart_diff.php">Another pie chart to show differences for?</a>
    <?php 
       if ((!isset($_GET['desc']) && !isset($_POST['desc']) && !isset($_GET['popularity']) && !isset($_POST['popularity'])) || $data0 == "") { // || (!isset($_GET['data0']) && !isset($_POST['data0']))) {
       
         if (isset($_GET['data'])) {
      if ($_GET['data'] != "" && strpos($_GET['data'], "[") !== false && strpos($_GET['data'], "[") < strpos($_GET['data'], ",")) {
   $data0 = "&data0=," . $_GET['data'];
      } else {
   $data0 = "&data0=" . $_GET['data'];
      }
  }
      
        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";
        if (isset($_GET['data'])) {
          echo " var title = '" . $GETtitle . "'; " . "\n";
        } else {
          echo ' var title = prompt("Enter Pie Chart Title (prefixes 2d or donut (followed by ;) before title are available options, and suffix by &onclick=y for added onclick functionality)", "My Daily Activities"); ' . "\n";
        }
        echo ' if (title != null) { ' . "\n";
         if (isset($_GET['data'])) {
          echo " var task = '" . $GETtask . "'; " . "\n";
          echo " var desc = '" . $GETdesc . "'; " . "\n";
          } else {
        echo ' var task = prompt("Enter Activity Type", "' . $GETtask . '"); ' . "\n";
        echo ' var desc = prompt("Enter " + task + " Measure", "Percentage"); ' . "\n";
          }
        echo ' var datac = ""; ' . "\n";
        echo ' var sq = "' . "'" . '"; ' . "\n";
        echo ' datalineprefix = prompt("Enter " + task + " Type " + thisline, ""); ' . "\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 + " Type " + 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 ", "") + " " + 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 ' if (document.URL.indexOf("&") == -1) window.location = "./pie_chart_diff.php?title=" + title.replace("onclick=","onclickhuh=") + "&task=" + task + "&deschuh=" + desc.replace("  ercent ", "") + "&data=" + datac; ' . "\n";
        echo ' if (document.URL.indexOf("&") != -1) window.location = "./pie_chart_diff.php?title=" + title + "&task=" + task + textras + "&desc=" + desc.replace("  ercent ", "") + "' . $data0 . '&data=" + datac; ' . "\n";
        echo "} \n" . "} \n" . '</' . 'script' . '>' . "\n";
      } 
    ?>
  </body>
</html>
