<!doctype html>
<html lang="en">
<head>
  <title>Valhalla Tools - Reverse Polish Calculator</title>
  <style>
  .table_converter {
    border: 1px ridge silver;
    border-collapse: seperate;
    border-radius: 10px;
    postion: center;
    background-color: #555;
    width: 320px;
  }
  .table_converter2 {
    border: 2px ridge silver;
    border-collapse: seperate;
    border-radius: 10px;
    postion: center;
    background-color: #2F3F4F;
    padding: 20px;
    text-align: center;
  }
  td.td_converter0, td.td_converter1, td.td_converter2, td.td_converter3, td.td_converter4{
    padding: 5px;
    background-color: #ccc;
    border: 2px ridge silver;
    border-radius: 10px;
    text-align: center;
  }
  td.td_converter0{
    background-color: #999;
  }
  td.td_converter2, td.td_converter3 td.td_converter4{
    padding:10px;
  }
  td.td_converter4 {
    background-color: firebrick;
  }
  </style>
</head>
<body>
<script>
/* EXPLANATION OF FUNCTIONALITY
* Higher text boxes contain more recent stack entries than lower text boxes.
* Each operator takes two arguments except for sqrt.
* If you try to push a value with improper syntax it will be tossed out.
* The ce/c button clears all fields and resets display0 to "0.0".
* If you try to use a binary operator such as + when there are fewer than 2 operands it will ignore it.
*/
stack = new Array();
currentValue = "";

function updateDisplay() {
   length = stack.length
   document.forms.form.Display0.value = "0.0";
   document.forms.form.Display1.value = ""
   document.forms.form.Display2.value = ""
   document.forms.form.Display3.value = ""
   document.forms.form.Display4.value = ""
   document.forms.form.Display5.value = ""
   if (length >= 1) document.forms.form.Display1.value = stack[length - 1]
   if (length >= 2) document.forms.form.Display2.value = stack[length - 2]
   if (length >= 3) document.forms.form.Display3.value = stack[length - 3]
   if (length >= 4) document.forms.form.Display4.value = stack[length - 4]
   if (length >= 5) document.forms.form.Display5.value = stack[length - 5]
}

function pushValue() {
   a = Number(currentValue);
   if (!isNaN(a)) {
	   stack.push(a);
	   updateDisplay();
   }
   currentValue = "";
   document.forms.form.Display0.value = "0.0"
}

function changeSign() {
   if (currentValue[0] == "-") currentValue = currentValue.substring(1);
   else currentValue = "-" + currentValue;
   document.forms.form.Display0.value = currentValue;
}

function operator(op) {
   if (op == "sqrt") {
	   right = stack.pop()
	   stack.push(Math.sqrt(right))
   } else if (stack.length > 1) {
	   right = stack.pop()
	   left = stack.pop()
	   switch (op) {
	   case "+":
		   stack.push(left + right);
		   break;
	   case "-":
		   stack.push(left - right);
		   break;
	   case "*":
		   stack.push(left * right);
		   break;
	   case "/":
		   stack.push(left / right);
		   break;
	   case "pow":
		   stack.push(Math.pow(left, right));
		   break;
	   default:
		   stack.push(right);
		   stack.push(left);
	   }
   } else {} //why did you call me?
   updateDisplay()
}
</script>

<form name="form">
  <div>
    <table class="table_converter">
      <tr>
        <td class="td_converter0">
          <input type="text" name="Display0" value="0.0" size="37">
        </td>
      </tr>
      <tr>
        <td class="td_converter1">
          <input type="text" name="Display1" size="37">
        </td>
      </tr>
      <tr>
        <td class="td_converter1">
          <input type="text" name="Display2" size="37">
        </td>
      </tr>
      <tr>
        <td class="td_converter1">
          <input type="text" name="Display3" size="37">
        </td>
      </tr>
      <tr>
        <td class="td_converter1">
          <input type="text" name="Display4" size="37">
        </td>
      </tr>
      <tr>
        <td class="td_converter1">
          <input type="text" name="Display5" size="37">
        </td>
      </tr>
    </table>

    <table class="table_converter">
      <tr>
        <td class="td_converter2">
          <input type="button" name="click" value=" . " onclick="currentValue+='.'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value="sqrt" onclick="operator('sqrt')">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value="pwr " onclick="operator('pow')">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value=" &divide; " onclick="operator('/')">
        </td>
      </tr>
      <tr>
        <td class="td_converter2">
          <input type="button" name="click" value=" 7 " onclick="currentValue+='7'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter2">
          <input type="button" name="click" value=" 8 " onclick="currentValue+='8'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter2">
          <input type="button" name="click" value=" 9 " onclick="currentValue+='9'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value=" &times; " onclick="operator('*')">
        </td>
      </tr>
      <tr>
        <td class="td_converter2">
          <input type="button" name="click" value=" 4 " onclick="currentValue+='4'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter2">
          <input type="button" name="click" value=" 5 " onclick="currentValue+='5'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter2">
          <input type="button" name="click" value=" 6 " onclick="currentValue+='6'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value=" - " onclick="operator('-')">
        </td>
      </tr>
      <tr>
        <td class="td_converter2">
          <input type="button" name="click" value=" 1 " onclick="currentValue+='1'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter2">
          <input type="button" name="click" value=" 2 " onclick="currentValue+='2'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter2">
          <input type="button" name="click" value=" 3 " onclick="currentValue+='3'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value=" + " onclick="operator('+')">
        </td>
      </tr>
      <tr>
        <td class="td_converter4">
          <input type="button" name="click" value="ce/c" onclick="currentValue=''; stack = new Array(); updateDisplay();">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value=" 0 " onclick="currentValue+='0'; document.forms.form.Display0.value=currentValue;">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value=" &plusmn; " onclick="changeSign();">
        </td>
        <td class="td_converter3">
          <input type="button" name="click" value="push" onclick="pushValue()">
        </td>
      </tr>
    </table>
  </div>
</form>
</body>
</html>