<?php
  // bubble_chart.php
  // Google Bubble Chart supervisor
  // RJM Programming
  // January, 2016
  // 
  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;
  }
?>
<html>
  <head> <script type="text/javascript" src="//www.rjmprogramming.com.au/gchartgen.js?c=gd"></script>
    <title>Bubble Chart - RJM Programming - //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, maximum-scale=1" />
    <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='';   
    
   var xpnum=1; // done here at supervisor
   var allowed=true; // done here at supervisor
   var nojwinextra=""; // done here at supervisor
   var pretitle=location.search.split('val1=')[1] ? decodeURIComponent(decodeURIComponent(location.search.split('val1=')[1].split('&')[0])) : '';
   var prelabel=location.search.split('val2=')[1] ? decodeURIComponent(decodeURIComponent(location.search.split('val2=')[1].split('&')[0])) : '';
   var prevalue=location.search.split('val3=')[1] ? decodeURIComponent(decodeURIComponent(location.search.split('val3=')[1].split('&')[0])) : '';
   var prenextv=location.search.split('val4=')[1] ? decodeURIComponent(decodeURIComponent(location.search.split('val4=')[1].split('&')[0])) : '';

   document.title+=' pretitle=' + pretitle;
   document.title+=' prelabel=' + prelabel;
   document.title+=' prevalue=' + prevalue;
   document.title+=' prenextv=' + prenextv;
<?php
  if (!isset($_POST['data'])) {
  echo "
   if (document.URL.indexOf('nojwin') == -1 && navigator.userAgent.match(/iPhone|iPad/i)) {
     if (document.URL.indexOf('?') == -1) {
       location.href=document.URL.split('#')[0] + '?nojwin=Q';
     } else {
       location.href=document.URL.split('#')[0] + '&nojwin=Q';
     }
   }
   ";
  }
?>
   if (document.URL.indexOf('nojwin') != -1 || navigator.userAgent.match(/iPhone|iPad/i)) {
     nojwinextra="&nojwinyes=Y";
   }

<?php
 echo "
 
     function selectevent(ans) {
            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 {
              location.href=document.URL.replace(pardata, ourencodeURIComponent(ans));
             }
            }
            }
            
      function selecteventtwo(ans) {
            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 {
              location.href=document.URL.replace(pardata, ourencodeURIComponent(ans));
             }
            }      
      }            
  ";
?>
<?php 
  echo "var data, chart, pardata = (location.search.split('data=')[1] ? location.search.split('data=')[1].split('&')[0] : ''), paramdata=decodeURIComponent(pardata); \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";
      }

      $GETtitle = "Correlation between life expectancy, fertility rate and population of some world countries (2010)";
      $GETlabel = "'ID'";
      $GETvalue = "Life Expectancy,Fertility Rate,'Region',Population";
      $GETdata = ",['CAN',80.66,1.67,'North America',33739900],['DEU',79.84,1.36,'Europe',81902307],['DNK',78.6,1.84,'Europe',5523095],['EGY',72.73,2.78,'Middle East',79716203],['GBR',80.05,2,'Europe',61801570],['IRN',72.49,1.7,'Middle East',73137148],['IRQ',68.09,4.77,'Middle East',31090763],['ISR',81.55,2.96,'Middle East',7485600],['RUS',68.6,1.54,'Europe',141850000],['USA',78.09,2.05,'North America',307007000]";
      $mg=$GETdata;
      
       $aaq="";
      
            
      if (isset($_POST['data'])) {
       $aaq="";
       if (isset($_GET['title'])) {
       $GETtitle = str_replace("\\'", "'", $_GET['title']);
       $aaq.=".1";
       } else {
       $GETtitle = str_replace("\\'", "'", $_POST['title']);
       $aaq.="-1";
       }
       if (isset($_GET['label'])) {
       $GETlabel = str_replace("\\'", "'", $_GET['label']);
       $aaq.=".2";
       } else {
       $GETlabel = str_replace("\\'", "'", $_POST['label']);
       $aaq.="-2";
       }
       if (isset($_GET['value'])) {
       $GETvalue = str_replace("\\'", "'", $_GET['value']);
       $aaq.=".3";
       } else {
       $GETvalue = str_replace("\\'", "'", $_POST['value']);
       $aaq.="-3";
       }
       $GETdata = str_replace("\\'", "'", $_POST['data']);
       $aaq.=$GETdata;
       echo $aaq;
       exit;
      }
      if (isset($_GET['data'])) {
       $aaq.="-0";
       $GETtitle = str_replace("\\'", "'", $_GET['title']);
       $GETlabel = str_replace("\\'", "'", $_GET['label']);
       $GETvalue = str_replace("\\'", "'", $_GET['value']);
       $GETdata = str_replace("\\'", "'", $_GET['data']);
      } else if ($mg == $GETdata && strpos($_SERVER['QUERY_STRING'],"title=") !== false) {
       $aaq.="^0";
       $bbits = explode("data=", file_get_contents('php://input'));
       if (sizeof($bbits) > 1) {
       $aaq.="^~0";
         //echo "jmhgjh";
         //exit;
         $GETdata = str_replace("\\'", "'", urldecode($bbits[1]));
         $GETtitle = str_replace("\\'", "'", urldecode(explode('&',explode("title=", $_SERVER['QUERY_STRING'])[1])[0]));
         $GETlabel = str_replace("\\'", "'", urldecode(explode('&',explode("label=", $_SERVER['QUERY_STRING'])[1])[0]));
         $GETvalue = str_replace("\\'", "'", urldecode(explode('&',explode("value=", $_SERVER['QUERY_STRING'])[1])[0]));
       }
      } else if ($mg == $GETdata) {
       $aaq.="^1";
       $bbits = explode("data=", file_get_contents('php://input'));
       if (sizeof($bbits) > 1) {
       $aaq.="^~1";
         echo "aaajmhgjh";
         exit;
       }
      } else {
       $aaq.="^~9";
      }
      
      //file_put_contents("awa.html", $aaq);
      
      $hdgs = split(",", $GETvalue);
      $GETval = "";
      if (isset($hdgs[0])) $GETval = "'" . str_replace("'", "", $hdgs[0]) . "'";
      if (isset($hdgs[1])) $GETval .= ",'" . str_replace("'", "", $hdgs[1]) . "'";
      if (isset($hdgs[2])) $GETval .= ",'" . str_replace("'", "", $hdgs[2]) . "'";
      if (isset($hdgs[3])) $GETval .= ",'" . str_replace("'", "", $hdgs[3]) . "'";
      
      echo " var colnames= '" . str_replace("'", "", $GETlabel) . "," . str_replace("'", "", $GETvalue) . "'; \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 " [" . $GETlabel . "," . $GETval . "] \n";
      echo str_replace("''" . "''", "''", str_replace("~", "'", $GETdata));
      echo "        ]);\n";
      
      echo "        var options = { \n";
      if (isset($hdgs[3])) {
      echo "        title: '" . $GETtitle . "', \n";
      echo "        hAxis: {title: '" . $hdgs[0] . "'}," . " \n";
      echo "        vAxis: {title: '" . $hdgs[1] . "'}," . " \n";
      echo "        bubble: {textStyle: {fontSize: 11}} \n";
      } else {
      echo "        colorAxis: {colors: ['yellow', 'red']} \n";
      }
      echo "       }; \n";


      echo " chart = new google.visualization.BubbleChart(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() {
 //alert(1);
 if (ready) {
 //alert(2);
  var 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, str;
  selection = chart.getSelection();
  sliceid = selection[0].row;

  for (var i = 0; i < selection.length; i++) {
 //alert(22);
    var item = selection[i];
 //alert(222 + ' ' + ir + ' ' + ic + ':' + item.row); // + ';' + item.column);
    if (item.row != null) ir = item.row;
  //alert('ir=' + ir + ' ic=' + ic);
    if ((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 {
 //alert(12);
        ir=item.row;
        //ic=item.column;
        //document.title=ir + ',' + ic;
      }
      //document.title=str;
      message=document.head.innerHTML.substring(ioffset);
      while (message.indexOf(', [') != -1) {
       message=message.replace(', [',',[');
      }
      dparts=message.split(',[');
 //alert(212 + ' dparts.length=' + dparts.length + ' vs message=' + message);
        //document.title='';
        colav.push(0.0);
      for (kl=1; kl<colparts.length; kl++) {
      //alert('kl=' + kl);
        //document.title=1;
        colav.push(0.0);
        doit=true;
        num=1;
        for (lk=1; lk<dparts.length; lk++) {
      //alert('lk=' + 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];
      //alert('thisnum[kl]=' + thisnum[kl] + ' colav[kl]=' + colav[kl]);
         colav[kl] = 0; //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;
      }
      //alert(312);
      if (avblurb != '') avblurb+=String.fromCharCode(10) + String.fromCharCode(10);
      //alert('dparts.length=' + dparts.length + ' vs 2');
      if (eval(dparts.length) >= eval(1 + item.row)) {
      //alert('dparts[eval(1 + item.row)]++=' + dparts[eval(1 + item.row)].replace(']',',') + ' vs split by ,');
        postdparts=dparts[eval(1 + item.row)].replace(']',',').split(',');
        if (eval(postdparts.length) >= eval(1 + 0)) { //item.column)) {
         //alert('colparts.length=' + colparts.length + ' postdparts.length=' + postdparts.length);
         if (eval(colparts.length) == 5) { //2) { 
          if (1 == 1) {
          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('For ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked [' + colparts[1] + ',' + colparts[2] + ',' + colparts[3] + ',' + colparts[4] + '] of [' + postdparts[1].replace(']','') + ',' + postdparts[2].replace(']','') + ',' + postdparts[3].replace(']','') + ',' + postdparts[4].replace(']','') + '] ' + avblurb, paramdata);
          if (ans != null) {
			selectevent(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);
          }
         }
        }
      }
    } else if ((item.row != null && item.column != null) || (eval(ir) >= 0 && eval(ic) >= 0)) {
      if (eval(ir) >= 0 && eval(ic) >= 0) {
  //alert(7999);
        item.row=ir;
        item.column=ic;
  //alert(79999);
        ir=-1;
        ic=-1;
      } else {
        ir=item.row;
        ic=item.column;
        //document.title=ir + ',' + ic;
      }
      str = data.getFormattedValue(item.row, item.column);
      message += '{row:' + item.row + ',column:' + item.column + '} = ' + str;
    } else if ((item.row != null) || (eval(ir) >= 0 && eval(ic) < 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(8999);
        item.row=ir;
        //item.column=ic;
  //alert(89999);
        ir=-1;
        //ic=-1;
      } else {
        ir=item.row;
        //ic=item.column;
        //document.title=ir + ',' + ic;
      }
      str = data.getFormattedValue(item.row, 0);
      //document.title=str;
      //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) == 2) { 
          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);
          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(' For ' + colparts[0] + ' ' + postdparts[0].replace(']','') + ' you clicked ' + colparts[1] + ' of ' + postdparts[1].replace(']','') + avblurb, paramdata);
          if (ans != null) {
			selecteventtwo(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);
          }
         }
        }
      }

    } else if ((item.column != null) || (eval(ir) < 0 && eval(ic) >= 0)) {
      if (eval(ir) >= 0 && eval(ic) < 0) {
  //alert(3999);
        //item.row=ir;
        item.column=ic;
  //alert(39999);
        //ir=-1;
        ic=-1;
      } else {
        //ir=item.row;
        ic=item.column;
        //document.title=ir + ',' + ic;
      }
      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);
 }
 ir=-1;
 ic=-1;    
 } 
      //);  
      ";
      }      
      
      
      
      
      echo " chart.draw(data, options); \n  \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";
      //echo " <body onload='setTimeout(later, 2 * 1000);' style='background-color: silver;'> \n";
      if (!isset($_GET['label']) && !isset($_POST['label'])) {
      echo " <body onload='setTimeout(later, 2 * 1000);' style='background-color: silver;'> \n";
      } else {
      echo " <body> \n";
      }
      echo " <h1>" . $GETtitle . " Bubble Chart</h1> \n";

      echo '<div id="chart_div" style="width: 900px; height: 500px;"></div><div id="bitsatend" style="display:inline;"></div>' . " \n";
      echo '<a id=another title="Another Bubble Chart" href="./bubble_chart.php">Another bubble chart?</a>' . " \n";
      if (!isset($_GET['value']) && !isset($_POST['value'])) {
      
        echo '<' . 'script' . '>' . "\n " . " function later() { \n" . ' var datalineprefix = " "; ' . "\n";
        echo ' var datalinesuffix = ""; ' . "\n";
        echo ' var extra = ""; ' . "\n";
        echo ' var thisline = 1; ' . "\n";
        echo ' var ii = 0; ' . "\n";
        echo ' var thisdef = "0"; ' . "\n";
        echo ' var totalleft = 100.0; ' . "\n";


        echo ' var title = prompt("Enter Bubble Chart Title (suffix by &onclick=y for added onclick functionality)", "' . $GETtitle . '"); ' . "\n";
        echo ' if (pretitle != "") title=pretitle;  ' . "\n";
        echo ' if (title != null) { ' . "\n";
        echo ' var label = prompt("Enter Bubble ID (you can hit Cancel for no Bubble ID)", "' . $GETlabel . '"); ' . "\n";
        echo ' if (prelabel != "") label=prelabel;  ' . "\n";
        echo ' if (label == null) label = ""; ' . "\n";
        echo ' if (label == "null") label = ""; ' . "\n";
        echo ' var xlabel = "' . "'" . '" + label.replace("' . "'" . '","").replace("' . "'" . '","") + "' . "'" . '"; ' . "\n";
        echo ' var value = prompt("Enter Bubble " + xlabel + " Criteria (separated by ,) ... The first two dimensions are visualized as coordinates, the (optional) 3rd as colour and the 4th as size. ", "' . $GETvalue . '"); ' . "\n";
        echo ' if (prevalue != "") value=prevalue;  ' . "\n";

        echo ' if (value.indexOf(",") != (0 - 1)) { ' . "\n";
        echo '  for (ii=0; ii < value.length; ii++) { if (value.substring(ii,(ii+1)) == ",") { if (value.substring((ii+1),(ii+2)) == "' . "'" . '") {  thisdef = thisdef + ",' . "'0'" . '"; } else { thisdef = thisdef + ",0"; } } } ' . "\n";
        echo ' } ' . "\n";


        echo ' var datac = ""; ' . "\n";
        echo ' var sparedp = "' . "'" . '"; ' . "\n";
        echo ' var sq = "' . "'" . '"; ' . "\n";
        echo ' datalineprefix = "' . "''" . '";' . "\n";
        echo ' if (("" + label).length != 0 || (prelabel != "" && prevalue != "" && prenextv == "")) { datalineprefix = prompt("Enter " + xlabel + " " + thisline, ""); }  if (datalineprefix == null) { datalineprefix=""; } else if (datalineprefix == "null") { datalineprefix=""; }  sparedp=datalineprefix;  ' . "\n";

        echo ' while (("" + datalineprefix).length != 0 && (value.indexOf("ercent") == (0 - 1) || value.indexOf("  ercent ") != (0 - 1) || (value.indexOf("ercent") != (0 - 1) && totalleft > 0))) { ' . "\n";
        echo ' datalineprefix = sparedp;' . "\n";
        echo ' sparedp = "' . "'" . '"; ' . "\n";
        echo ' if (("" + datalinesuffix).length != 0 && ("" + label).length != 0) datalineprefix = prompt("Enter " + xlabel + " " + thisline + " (for no more hit Cancel button)", "");  if (1 == 23) { document.title="b:" + datalineprefix; }  ' . "\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 (value.indexOf("ercent") != (0 - 1) && value.indexOf("  ercent ") == (0 - 1)) extra = " (for " + totalleft + " enter nothing or hit Cancel button)";  ' . "\n";
        echo '     datalinesuffix = prompt("Enter " + value.replace("  ercent ", "") + " " + thisline + extra +  " (for no more hit Cancel button)", thisdef); ' . "\n";
        echo '     if (datalinesuffix == null && value.indexOf("ercent") != (0 - 1) && value.indexOf("  ercent ") == (0 - 1)) datalinesuffix = totalleft; ' . "\n";
        echo '     if (datalinesuffix == null) break; ' . "\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) && value.indexOf("ercent") != (0 - 1) && value.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(datalinesuffix) > eval(totalleft) && value.indexOf("ercent") != (0 - 1) && value.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       datalinesuffix = eval(totalleft); ' . "\n";
        echo '       totalleft = 0; ' . "\n";
        echo '     } else if (eval(datalinesuffix) > eval(totalleft) && value.indexOf("ercent") == (0 - 1) && value.indexOf("  ercent ") == (0 - 1)) { ' . "\n";
        echo '       value = value + "  ercent "; ' . "\n";
        echo '     } else { ' . "\n";
        echo '       totalleft = eval(totalleft) - eval(datalinesuffix); ' . "\n";
        echo '     } ' . "\n";
        echo '     datac = datac + ", [~" + datalineprefix + "~," + datalinesuffix + "] "; document.title=datac; ' . "\n";
        echo '     thisline++; ' . "\n";
        echo '   } ' . "\n";
        echo ' } ' . "\n";
        echo ' if (allowed) window.location = "./bubble_chart.php?title=" + title + "&label=" + label + nojwinextra + "&value=" + value.replace("  ercent ", "") + "&data=" + datac; ' . "\n";
        echo "} \n" . "} \n" . '</' . 'script' . '>' . "\n";
      } 
    ?>
<script type='text/javascript'>
if (document.URL.indexOf('nojwin') != -1 || navigator.userAgent.match(/iPhone|iPad/i)) {
  //document.getElementById('pch1').innerHTML+='.';
  var xansis='x', hiddenbits='';
  xpnum=1;
  if (document.URL.indexOf('?') != -1) {
    var pairing, argsare=document.URL.split('#')[0].split('?')[1].split('&');
    for (var iargsare=0; iargsare<argsare.length; iargsare++) {
      pairing=argsare[iargsare].split('=');
      if (('*' + pairing[0]).indexOf('*val') == -1 && ('*' + pairing[0]).indexOf('*prompt') == -1 && ('*' + pairing[0]).indexOf('*OK') == -1 && ('*' + pairing[0]).indexOf('*Cancel') == -1) {
       hiddenbits+="<input type=hidden name=" + pairing[0] + " value='" + pairing[1] + "'></input>";
      }
    }
  }
  while (xansis != '') {
   xansis=location.search.split('val' + xpnum + '=')[1] ? decodeURIComponent(decodeURIComponent(location.search.split('val' + xpnum + '=')[1].split('&')[0]).replace(/\+/g,' ')) : '';
   if (document.URL.indexOf('val' + xpnum + '=') != -1) {
     hiddenbits+="<input type=hidden name=val" + xpnum + " value=\"" + encodeURIComponent(xansis) + "\"></input>";
     xpnum++;
     xansis='x';
   } else {
     xansis='';
   }
  }
  if (document.URL.indexOf('nojwinyes=') != -1) { 
  document.write("<form onsubmit=\" if (document.getElementById('val" + xpnum + "').value.replace('null','') != '') { if (decodeURIComponent(document.getElementById('prompt').value).indexOf(' For ') == 0) {  selecteventtwo(document.getElementById('val" + xpnum + "').value);  } else { selectevent(document.getElementById('val" + xpnum + "').value); } } else if (document.URL.indexOf('&data=') != -1) { location.href=document.URL; } else { this.style.display='none'; } xpnum++; return false; \" enctype='text/plain' style='opacity:0.9;display:none;position:absolute;top:70;left:2%;z-index:56;width:48%;background-color:rgba(255,165,0,0.8);border:2px solid red;' id=safariform method=GET action=" + document.URL.split('#')[0].split('?')[0] + " readonly>" + hiddenbits + "<textarea style='width:96%;background-color:#eeeeee;' rows=5 id='prompt' readonly='readonly'></textarea><br><input type=hidden value='' name=val" + xpnum + " id=hval" + xpnum + "></input><input onkeypress=\"checkEnter(event);\" onblur=\" document.getElementById('h' + this.id).value=encodeURIComponent(this.value); \" style='width:50%;background-color:yellow;' type=text value='' id=val" + xpnum + "></input>&nbsp;&nbsp;<input style='float:right;width:25%;' type=submit value=OK name=OK id=OK></input>&nbsp;<input style='display:none;' type=submit value=Cancel name=Cancel id=Cancel></input>&nbsp;<input type=button value=Cancel id=bCancel onclick=\" document.getElementById('val" + xpnum + "').value='null'; document.getElementById('hval" + xpnum + "').value=encodeURIComponent('null'); this.style.display='none';  document.getElementById('Cancel').style.display='block'; document.getElementById('Cancel').click(); \"></input>&nbsp;</input></form>"); 
  //document.getElementById('pch1').innerHTML+='....';
  } else {
  document.write("<form enctype='text/plain' style='opacity:0.9;display:none;position:absolute;top:70;left:2%;z-index:56;width:48%;background-color:rgba(255,165,0,0.8);border:2px solid red;' id=safariform method=GET action=" + document.URL.split('#')[0].split('?')[0] + " readonly>" + hiddenbits + "<textarea style='width:96%;background-color:#eeeeee;' rows=2 id='prompt' readonly='readonly'></textarea><br><input type=hidden value='' name=val" + xpnum + " id=hval" + xpnum + "></input><input onkeypress=\"checkEnter(event);\" onblur=\" document.getElementById('h' + this.id).value=encodeURIComponent(this.value); \" style='width:50%;background-color:yellow;' type=text value='' id=val" + xpnum + "></input>&nbsp;&nbsp;<input style='float:right;width:25%;' type=submit value=OK name=OK id=OK></input>&nbsp;<input style='display:none;' type=submit value=Cancel name=Cancel id=Cancel></input>&nbsp;<input type=button value=Cancel id=bCancel onclick=\" document.getElementById('val" + xpnum + "').value='null'; document.getElementById('hval" + xpnum + "').value=encodeURIComponent('null'); this.style.display='none';  document.getElementById('Cancel').style.display='block'; document.getElementById('Cancel').click(); \"></input>&nbsp;</input></form>"); 
  //document.getElementById('pch1').innerHTML+='..';
  }
  if (document.getElementById('another')) {
    if (document.getElementById('another').href.indexOf('nojwin') == -1) {
     document.getElementById('another').href+='?nojwin=Y';
    }
  }
}
</script>
</body>
</html>
