<?php

function expandDecimals($val) {
    $parts = explode('.',$val);
    if (count($parts) == 2) {
	if (strlen($parts[1]) > 0) {
	    if (strlen($parts[1]) == 1) {
                $part = $parts[0] . "." . $parts[1] . "0";
            } else {
                $part = $parts[0] . "." . $parts[1];
            }
        } 
    } else {
         $part = $parts[0] . ".00";
    }
    return $part;
}

require_once $_SERVER['DOCUMENT_ROOT'] . "/Base.php";
require_once $_SERVER['DOCUMENT_ROOT'] . $PREFIX_DIR . "/start.php";
?>

</head>
<body>
<?php 
include_once $DOCROOT . "$PREFIX_DIR/header_start.php";
include_once $DOCROOT . "$PREFIX_DIR/menu/userMenu.php";
include_once $DOCROOT . "$PREFIX_DIR/header_finish.php";
?>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

GLOBAL $currency;
if (!empty($_REQUEST)) {
  $currency = $_REQUEST['currency'];
}
if (empty($currency)) $currency = "CAD";

$ch = curl_init("https://blockchain.info/ticker");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$jsonArray = json_decode($data,true);
$keys = array_keys($jsonArray);
?>

</head>
<body>
  <div class="container">
    <style>
      table, thead {
        border: 1px solid black;
        border-collapse: collapse;
      }

      thead, th {
        text-align: center;
      }
    </style>
    <br />
    <h4>Data acquired on page load or conversion currency change from <a href="https://www.blockchain.com/explorer">blockchain.info</a>.</h4>
    <h6>The code for this page is <a href="BTCPriceCode.php">here</a>.</h6>
    <br />
    <table class="table table-bordered table-striped table-hover">
    <thead>
      <tr>
        <th colspan="2">Conversions</th>
        <th colspan="2">
            <label for="currency">Currency:</label>
            <script>
              function doReload() {
                window.location = "<?php echo $_SERVER['PHP_SELF']; ?>?currency=" + document.getElementById('currency').value;
	      }
            </script>
            <select id="currency" onchange="doReload();">
            <?php
               print("<option value='" . $currency. "'>" . $currency . "</option>\n");
               for($i = 0; $i < count($keys);$i++) {
                 if ($keys[$i] != $currency) {
                   print("<option value='" . $keys[$i] . "'>" . $keys[$i] . "</option>\n");
                 }
               }
            ?>
            </select>
	</th>
      </tr>
      <tr>
        <th>Amount</th>
        <th>Currency</th>
        <th colspan="4">Result</th>
      </tr>
    </thead>
    <tbody>
<?php
$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=50");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$fifty = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=200");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$twoHundred = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=3000");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$threeGrand = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=1");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$one = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=10");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$ten = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=100");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$oneHundred = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=1000");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$oneThousand = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=10000");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$tenThousand = $data;

$ch = curl_init("https://blockchain.info/tobtc?currency=$currency&value=100000");
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$hundredThousand = $data;
?>

      <tr><td align="right">$50</td><td><?php echo $currency; ?></td><td align="right"><?php echo $fifty;?></td></tr>
      <tr><td align="right">$200</td><td><?php echo $currency; ?></td><td align="right"><?php echo $twoHundred;?></td></tr>
      <tr><td align="right">$3000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $threeGrand;?></td></tr>
      <tr><td align="right">$1</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one;?></td></tr>
      <tr><td align="right">$10</td><td><?php echo $currency; ?></td><td align="right"><?php echo $ten;?></td></tr>
      <tr><td align="right">$100</td><td><?php echo $currency; ?></td><td align="right"><?php echo $oneHundred;?></td></tr>
      <tr><td align="right">$1,000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $oneThousand;?></td></tr>
      <tr><td align="right">$10,000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $tenThousand;?></td></tr>
      <tr><td align="right">$100,000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $hundredThousand;?></td></tr>

  </tbody>
  </table>
  <br />

  <table class="table table-bordered table-striped table-hover">
  <thead>
  <tr style="border: 1px solid black;"><th colspan="5"><strong>1 BTC</strong></th></tr>
  <tr>
     <th>Currency</th>
     <th>15 M</th>
     <th>Last</th>
     <th>Buy</th>
     <th>Sell</th>
  </tr>
  </thead>
  <tbody>
<?php
	for($i = 0; $i < count($keys);$i++) {
		$array = $jsonArray[$keys[$i]];
		print("<tr>\n");
		print("  <td>" . $keys[$i] . "</td>\n");
		print("  <td align='right'>" . expandDecimals($array["15m"])  . "</td>\n");
		print("  <td align='right'>" . expandDecimals($array["last"]) . "</td>\n");
		print("  <td align='right'>" . expandDecimals($array["buy"])  . "</td>\n");
		print("  <td align='right'>" . expandDecimals($array["sell"]) . "</td>\n");
		print("</tr>\n");
	}
?>
  </tbody>
  </table>
<br />
</div>
</body>
</html>