<?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>Candlestick or Waterfall Chart - 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 src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
<?php 
      $candlestick = "Candlestick";
      $xgd = ""; 
      $gd = ""; 
      $ntt=false;
      $nondefs="legend:'none', width:600, height:400";
      $GETtitle = "Company Performance";
      $GETlabel = "Day";
      $GETvalue = "Low,Opening,Closing,High";
      $GETdata = "['Mon', 20, 28, 38, 45],['Tue', 31, 38, 55, 66],['Wed', 50, 55, 77, 80],['Thu', 77, 77, 66, 50],['Fri', 68, 66, 22, 15]";
      
      
      if (isset($_GET['value'])) {
       $GETtitle = $_GET['title'];
       //$GETlabel = $_GET['label'];
       //$GETvalue = $_GET['value'];
       $GETdata = $_GET['data'];
       $ourtitles=explode(";", $_GET['title']);
       if (sizeof($ourtitles) > 1 && strpos(strtolower($ourtitles[0]), "waterfall") !== false) {
        $candlestick = "Waterfall";
        $nondefs.=", bar: { groupWidth: '100%' }, candlestick: { fallingColor: { strokeWidth: 0, fill: '#a52714' }, risingColor: { strokeWidth: 0, fill: '#0f9d58' }   } ";
       }
       $GETtitle = $ourtitles[sizeof($ourtitles) - 1];
      } else if (isset($_POST['data'])) {
       $GETtitle = urldecode($_POST['title']);
       //$GETlabel = urldecode($_POST['label']);
       //$GETvalue = urldecode($_POST['value']);
       $GETdata = urldecode($_POST['data']);
       $ourtitles=explode(";", urldecode($_POST['title']));
       if (sizeof($ourtitles) > 1 && strpos(strtolower($ourtitles[0]), "waterfall") !== false) {
        $candlestick = "Waterfall";
        $nondefs=", legend: 'none', bar: { groupWidth: '100%' }, candlestick: { fallingColor: { strokeWidth: 0, fill: '#a52714' }, risingColor: { strokeWidth: 0, fill: '#0f9d58' }   } ";
       }
       $GETtitle = $ourtitles[sizeof($ourtitles) - 1];
      }
      
      
      $newextras=$nondefs;
      
      
      echo "   var refresh=0; prerefresh=0;   \n";
      echo " google.load('visualization', '1', {packages:['corechart']}); " . "\n";
      echo ' google.setOnLoadCallback(drawChart); ' . "\n";
      echo ' function drawChart() { ' . "\n";
      // A column for custom tooltip content
 if ((isset($_GET['data']) || isset($_POST['data'])) && (isset($_GET['tooltip']) || isset($_POST['tooltip']) || strpos($GETdata, "'") !== false || strpos($GETdata, '"') !== false)) {
      $ntt=true;
  //$newextras=$nondefs . ", focusTarget: 'category', tooltip: {isHtml: true} ";
  $newextras=$nondefs . ", tooltip: {isHtml: true}, showTip: true"; //, selectionMode: 'multiple', tooltip: {trigger: 'selection'}, aggregationTarget: 'none' ";
  echo "       var tooltip = [ \n";
  $bits=explode("~,", substr($GETdata,1));
  $delts="";
  for ($iy=1; $iy<sizeof($bits); $iy++) {
   $postbits=explode(",", $bits[$iy]);
   $xgd.=str_replace("%3C", "<", str_replace("%3E", ">", $postbits[0]));
   echo $delts . str_replace("%3C", "<", str_replace("%3E", ">", $postbits[0]));
   $delts=",";
  }
  echo "]; \n";
  $gd=$GETdata;
  for ($iy=1; $iy<sizeof($bits); $iy++) {
   $postbits=explode(",", $bits[$iy]);
   $gd=str_replace("," . $postbits[0] . "", "", $gd);
  }
  $GETdata=$gd;
  echo "     var data = new google.visualization.DataTable();  \n";
  echo "   data.addColumn('string', 'Title');  \n";
  //echo "   data.addColumn({'type': 'string', 'role': 'tooltip', 'p': {'html': true}});  \n";
  echo "   data.addColumn('number', 'n1');  \n";
  echo "   data.addColumn('number', 'n2');  \n";
  echo "   data.addColumn('number', 'n3');  \n";
  echo "   data.addColumn('number', 'n4');  \n";
  echo " data.addRows([ \n";
  //echo str_replace("~,", "',", str_replace("[~", "['", str_replace(",]", ",0]", str_replace(",]", ",0]", str_replace(",]", ",0]", substr($GETdata,1))))));
  echo str_replace("~", "'", substr($GETdata,1));
  echo "   ]); \n";
 } else {
       echo ' var data = google.visualization.arrayToDataTable([ ' . "\n";
      //echo " ['" . $GETlabel . "','" . str_replace(",", "','", str_replace("'", "", $GETvalue)) . "'] \n";
      echo str_replace("~,", "',", str_replace("[~", "['", str_replace(",]", ",0]", str_replace(",,", ",0,", str_replace(",]", ",0]", $GETdata)))));
      echo "        ], true);\n";
 }
      
      //echo "        var options = { \n";
      //echo "        title: '" . $GETtitle . "', \n";
      //echo "        hAxis: {title: '" . $GETlabel . "',  titleTextStyle: {color: 'red'}}" . " \n";
      //echo "       }; \n";


      echo " var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div')); \n";

 if ($ntt) {
   
      //echo "   var selection; \n";
      echo "   var sliceid = 0; /" . "/ thanks to https://gist.github.com/alexrainman/bb8d49357250df0859c0  \n";

      echo "   function checkhere() { prerefresh++; if (prerefresh == 20) { if (refresh == 0) { refresh=1; document.getElementById('ihide').value=1;  } } \n";
      echo "   refresh=document.getElementById('ihide').value; if (refresh != 0) { prerefresh=0; refresh++; document.getElementById('ihide').value=refresh; if (refresh == 10) { var huhss=document.URL.split('&xx='); if (huhss.length == 1) {  location.href=document.URL + '&xx=1'; } else { var two=eval(huhss[1]); two++; location.href=huhss[0] + '&xx=' + two;  }  }    } setTimeout(checkhere,1000); \n } \n";

      echo "   checkhere(); \n";
  
      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){ document.getElementById('ihide').value='1';  \n";
      //echo "   google.visualization.events.addListener(chart, 'select', eventHandler); \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', eventHandler); \n";
        echo "   google.visualization.events.addListener(chart, 'onmouseover', ceventHandler); \n";
      } else {
        echo "   google.visualization.events.addListener(chart, 'onmouseover', eventHandler); \n";
      }
}
      echo " chart.draw(data, {" . $newextras . "}); \n } \n";
      
      echo " </script> \n";
      echo " </head> \n";
      if (!isset($_GET['value'])) {
      echo " <body onload='setTimeout(later, 2 * 1000);' style='background-color: silver;'> \n";
      } else {
      echo " <body style='background-color: silver;'> \n";
      }
      echo " <h1>" . $GETtitle . " " . $candlestick . " Chart</h1> \n";

      echo '<div id="chart_div"></div>' . " \n";
      echo '<a title="Another ' . $candlestick . ' Chart" href="./candlestick_chart.php">Another ' . strtolower($candlestick) . ' chart?</a>' . " \n";
      if (!isset($_GET['value'])) {
      
        echo '<' . 'script' . '>' . "\n " . " function later() { \n" . ' var datalineprefix = " "; ' . "\n";
        echo ' var dlsa, dlp2="", exra = "", four; ' . "\n";
        echo ' var iof=0; ' . "\n";
        echo ' var datalinesuffix = ""; ' . "\n";
        echo ' var extra = "", subtitle=["' . $GETtitle . '"]; ' . "\n";
        echo ' var thisline = 1; ' . "\n";
        echo ' var ii = 0; ' . "\n";
        echo ' var thisdef = "0"; ' . "\n";
        echo ' var comma = ""; ' . "\n";
        echo ' var totalleft = 100.0; ' . "\n";
        echo ' var title = prompt("Enter Candlestick Chart Title (prefix waterfall (followed by ;) before title is an available option)", "' . $GETtitle . '"); ' . "\n";
        echo ' if (title != null) { subtitle=title.split(";");  ' . "\n";
        echo ' var label = prompt("Enter Candlestick Concept", "' . $GETlabel . '"); ' . "\n";
        echo ' var value = prompt("Enter Candlestick " + label + " Measure(s) (separated by ,)", "' . $GETvalue . '"); ' . "\n";
        echo ' if (value.indexOf(",") != (0 - 1)) { ' . "\n";
        echo '  for (ii=0; ii<value.length; ii++) { if (value.substring(ii,(ii+1)) == ",") thisdef = thisdef + ",0"; } ' . "\n";
        echo ' } ' . "\n";
        echo ' var datac = ""; ' . "\n";
        echo ' var sq = "' . "'" . '"; ' . "\n";
        //echo ' datalineprefix = prompt("Enter " + label + " " + thisline, ""); ' . "\n";
        echo " datalineprefix = prompt('Enter ' + label + ' ' + thisline +  ' (optionally append with ' + '\\n\\n' + ',\"A tooltip (regarding low: {n1} opening: {n2} closing: {n3} high: {n4}) of some sort like <a target=_blank href=' + \"'\" + 'https://www.google.com/#q=candlestick {day} low: {n1} opening: {n2} closing: {n3} high: {n4}' + \"'\" + '>{day}</a>\" ' + '\\n\\n' + ' or maybe perhaps ' + '\\n\\n' + ',\"A tooltip (regarding low: {n1} opening: {n2} closing: {n3} high: {n4}) of some sort like <a target=_blank href=' + \"'\" + 'https://en.wikipedia.org/wiki/{title}' + \"'\" + '>{day}</a>\"' + '\\n\\n' + ' as an example of what is possible with HTML included)', ''); " . "\n";
        echo ' if (datalineprefix != null) { iof=0; dlsa=datalineprefix.split(","); if (dlsa.length > 1 && (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(",")))) { while (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(","))) { datalineprefix=datalineprefix.replace(",", "%2C"); iof+=0;   }  }    datalineprefix=datalineprefix.replace(/#/g, "%23"); if (datalineprefix.indexOf("{") != -1 && datalineprefix.indexOf(",") != -1) { dlsa=datalineprefix.split(","); if (dlsa.length > 1) {  datalineprefix=datalineprefix.replace("{day}",encodeURIComponent(dlsa[0])).replace("{day}",encodeURIComponent(dlsa[0])).replace("{day}",encodeURIComponent(dlsa[0])).replace("{day}",encodeURIComponent(dlsa[0])).replace("{title}",encodeURIComponent(subtitle[eval(-1 + subtitle.length)])).replace("{title}",encodeURIComponent(subtitle[eval(-1 + subtitle.length)]));   } }   } ' . "\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 ' if (datalinesuffix.length != 0) { ' . "\n";
        //echo '    datalineprefix = prompt("Enter " + label + " " + thisline + " (for no more hit Cancel button)", ""); ' . "\n";
        echo "    datalineprefix = prompt('Enter ' + label + ' ' + thisline +  ' (for no more please hit Cancel button and optionally append with ' + '\\n\\n' + ',\"A tooltip (regarding low: {n1} opening: {n2} closing: {n3} high: {n4}) of some sort like <a target=_blank href=' + \"'\" + 'https://www.google.com/#q=candlestick {day} low: {n1} opening: {n2} closing: {n3} high: {n4}' + \"'\" + '>{day}</a>\" ' + '\\n\\n' + ' or maybe perhaps ' + '\\n\\n' + ',\"A tooltip (regarding low: {n1} opening: {n2} closing: {n3} high: {n4}) of some sort like <a target=_blank href=' + \"'\" + 'https://en.wikipedia.org/wiki/{title}' + \"'\" + '>{day}</a>\"' + '\\n\\n' + ' as an example of what is possible with HTML included)', ''); " . "\n";
        echo '    if (datalineprefix != null) {  iof=0; dlsa=datalineprefix.split(","); if (dlsa.length > 1 && (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(",")))) { while (eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(39))) > eval(datalineprefix.indexOf(",")) && eval(-1 + iof + datalineprefix.indexOf(String.fromCharCode(34))) > eval(datalineprefix.indexOf(","))) { datalineprefix=datalineprefix.replace(",", "%2C"); iof+=0;   }  }   datalineprefix=datalineprefix.replace(/#/g, "%23"); if (datalineprefix.indexOf("{") != -1 && datalineprefix.indexOf(",") != -1) { dlsa=datalineprefix.split(","); if (dlsa.length > 1) {  datalineprefix=datalineprefix.replace("{day}",encodeURIComponent(dlsa[0])).replace("{day}",encodeURIComponent(dlsa[0])).replace("{day}",encodeURIComponent(dlsa[0])).replace("{day}",encodeURIComponent(dlsa[0])).replace("{title}",encodeURIComponent(subtitle[eval(-1 + subtitle.length)])).replace("{title}",encodeURIComponent(subtitle[eval(-1 + subtitle.length)]));   } }   } ' . "\n";
        echo ' } ' . "\n";
        //echo ' } ' . "\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, thisdef); ' . "\n";
        echo '     if (datalinesuffix == null && value.indexOf("ercent") != (0 - 1) && value.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) && 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 '     if (datalinesuffix != null) { four=datalinesuffix.split(","); datalineprefix = datalineprefix.replace("{n1}", four[0]).replace("{n2}", four[1]).replace("{n3}", four[2]).replace("{n4}", four[3]).replace("{n1}", four[0]).replace("{n2}", four[1]).replace("{n3}", four[2]).replace("{n4}", four[3]).replace("{title}", encodeURIComponent(subtitle[eval(-1 + subtitle.length)])); } ' . "\n";
        //echo '     datac = datac + comma + " [~" + datalineprefix + "~," + datalinesuffix + "] "; ' . "\n";
        echo '     if (datalineprefix.indexOf("' . "'" . '") == -1) {  four=datalinesuffix.split(","); if (exra != "") { exra=String.fromCharCode(39) + " low: " + four[0] + " opening: " + four[1] + " closing: " + four[2] + " high: " + four[3] + String.fromCharCode(39) + ","; }  datac = datac + comma + " [~" + datalineprefix + "~," + exra + datalinesuffix + "] "; } ' . "\n";
        echo '     if (datalineprefix.indexOf("' . "'" . '") != -1) {  four=datalinesuffix.split(","); exra=String.fromCharCode(39) + " low: " + four[0] + " opening: " + four[1] + " closing: " + four[2] + " high: " + four[3] + String.fromCharCode(39) + ","; datac = datac + comma + " [~" + datalineprefix.replace(",", "~,") +  "," + datalinesuffix + "] "; } ' . "\n"; 
        echo '     comma = ","; ' . "\n";
        echo '     thisline++; ' . "\n";
        echo '   } ' . "\n";
        echo ' } ' . "\n";
        echo ' window.location = "./candlestick_chart.php?title=" + title + "&label=" + label + "&value=" + value.replace("  ercent ", "") + "&data=" + datac; ' . "\n";
        echo "} \n" . "} \n" . '</' . 'script' . '>' . "\n";
      } 
    ?>
  <input type="hidden" id="ihide" value="0"></input>
  </body>
</html>
