Advertisement
ThermiteKitten

Still broken

Oct 1st, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var $ = function (id) {
  2.     return document.getElementById(id);
  3. }
  4. var calculateTax = function () {
  5.     var subtotal = parseFloat($("subtotal").value);
  6.     var tax_rate = parseFloat($("tax_rate").value);
  7.     var isValid = true;
  8.  
  9.     if (isNaN(subtotal)) {
  10.         alert("Subtotal must be numeric");
  11.     }
  12.     else if (isNaN(tax_rate)) {
  13.         alert("Tax rate must be numeric");
  14.     }
  15.     else if (subtotal <= 0) {
  16.         alert("Subtotal must not be negative");
  17.     }
  18.     else if (tax_rate <= 0) {
  19.         alert("Tax rate must not be negative");
  20.     }
  21.     else {
  22.         var sales_tax = calculateTax(subtotal, tax_rate);
  23.         $("sales_tax").value = sales_tax.toFixed(1);
  24.         var total = subtotal + sales_tax;
  25.         $("total").value = total.toFixed(1);
  26.     }
  27. }
  28. var clear = function () {
  29.     $("subtotal").value = "";
  30.     $("tax_rate").value = "";
  31.     $("sales_tax").value = "";
  32.     $("total").value = "";
  33. }
  34. window.onload = function () {
  35.     $("calculate").onclick = calculateTax;
  36.     $("clear").onclick = clear;
  37.     $("subtotal").focus();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement