<?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>Chart Wrapper - 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">
<?php 
      $GETcharttype = "ColumnChart";
      $GETtitle = "Countries";
      //$GETtask = "Task";
      //$GETdesc = "Hours per Day";
      $GETdata = "['', 'Germany', 'USA', 'Brazil', 'Canada', 'France', 'RU'],['', 700, 300, 400, 500, 600, 800]";
      if (isset($_GET['data'])) {
       $GETcharttype = $_GET['charttype'];
       $GETtitle = $_GET['title'];
       //$GETtask = $_GET['task'];
       //$GETdesc = $_GET['desc'];
       $GETdata = $_GET['data'];
      }



      
      echo ' google.load("visualization", "1"); ' . "\n";
      echo ' google.setOnLoadCallback(drawChart); ' . "\n";
      echo ' function drawChart() { ' . "\n";
      
      echo " var data = new google.visualization.ChartWrapper({ 'chartType': '"  . $GETcharttype . "', 'dataTable': [  " . "\n";
      //echo " ['" . $GETtask . "','" . $GETdesc . "'] \n";
      echo str_replace("~,", "',", str_replace("~", "'", str_replace(",]", ",0]", str_replace(",]", ",0]", str_replace(",]", ",0]", $GETdata)))));
      echo "        ],"; //);\n";
      echo "        'options': { \n";
      if ($GETcharttype == "PieChart") echo "'showRowNumber' : true," . "\n";
      echo "        'title': '" . $GETtitle . "'}, \n";
      echo "        'containerId': 'chart_div' }); \n";
      echo " data.draw(); \n } \n";
      
      echo " </script> \n";
      echo " </head> \n";
      echo " <body onload='setTimeout(later, 2 * 1000);'> \n";
      echo " <h1>" . $GETtitle . " Chart Wrapper</h1> \n";
?>
    <div id="chart_div" style="width: 900px; height: 500px; background-color: yellow;"></div>
    <a title="Another Chart Wrapper" href="./chart_wrapper.php">Another Chart Wrapper?</a>
    <?php 
      if (!isset($_GET['desc'])) {
      
        echo '<' . 'script' . '>' . "\n " . " function later() { \n" . ' var datalineprefix = " "; ' . "\n";
        echo ' var datalinesuffix = ""; ' . "\n";
        echo ' var extra = ""; ' . "\n";
        echo ' var charttype = "ColumnChart"; ' . "\n";
        echo ' var thisline = 1; ' . "\n";
        echo ' var totalleft = 100.0; ' . "\n";
        echo ' var title = prompt("Enter Chart Wrapper Title", "Countries"); ' . "\n";
        echo ' if (title != null) { ' . "\n";
        echo ' charttype = prompt("Enter Chart Type (please type 1 for ColumnChart, 2 for BarChart)", charttype); ' . "\n";
        echo ' charttype = charttype.replace("1", "ColumnChart").replace("2", "BarChart"); ' . "\n";
        echo ' var task = title; /' . '/ prompt("Enter Activity Type", "Task"); ' . "\n";
        echo ' var desc = "value";  /' . '/ prompt("Enter " + task + " Measure", "Percentage"); ' . "\n";
        echo ' var datac = ""; ' . "\n";
        echo ' var datactwo = ""; ' . "\n";
        echo ' var sq = "' . "'" . '"; ' . "\n";
        echo " var delim = \"['',\"; " . "\n";
        echo ' datalineprefix = prompt("Enter " + task + " Value " + 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 + " 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 ", "") + " " + 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 + delim + "~" + datalineprefix + "~"; ' . "\n";
        echo '     datactwo = datactwo + delim + datalinesuffix; ' . "\n";
        echo '     delim = ","; ' . "\n";
        echo '     thisline++; ' . "\n";
        echo '   } ' . "\n";
        echo ' } ' . "\n";
        echo ' window.location = "./chart_wrapper.php?title=" + title + "&charttype=" + charttype + "&data=" + datac + "]," + datactwo + "]"; ' . "\n";
        echo "} \n" . "} \n" . '</' . 'script' . '>' . "\n";
      } 
    ?>
  </body>
</html>
