Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var $ = function (id) {
- return document.getElementById(id);
- }
- var calculateTax = function () {
- var subtotal = parseFloat($("subtotal").value);
- var tax_rate = parseFloat($("tax_rate").value);
- var isValid = true;
- if (isNaN(subtotal)) {
- alert("Subtotal must be numeric");
- }
- else if (isNaN(tax_rate)) {
- alert("Tax rate must be numeric");
- }
- else if (subtotal <= 0) {
- alert("Subtotal must not be negative");
- }
- else if (tax_rate <= 0) {
- alert("Tax rate must not be negative");
- }
- else {
- var sales_tax = calculateTax(subtotal, tax_rate);
- $("sales_tax").value = sales_tax.toFixed(1);
- var total = subtotal + sales_tax;
- $("total").value = total.toFixed(1);
- }
- }
- var clear = function () {
- $("subtotal").value = "";
- $("tax_rate").value = "";
- $("sales_tax").value = "";
- $("total").value = "";
- }
- window.onload = function () {
- $("calculate").onclick = calculateTax;
- $("clear").onclick = clear;
- $("subtotal").focus();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement