Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StoneCypher/1f23bc899a98086f47af to your computer and use it in GitHub Desktop.
Save StoneCypher/1f23bc899a98086f47af to your computer and use it in GitHub Desktop.
function byId(id) {
return document.getElementById(id);
}
function byTag(tag) {
return document.getElementsByTagName(tag);
}
var ckShelf = byId("shelf"),
shelves = [ byId("shelf1"), byId("shelf2"), byId("shelf3") ],
ShelvingCfg = { enabler: ckShelf, items: shelves, total: byId("shelfCost") };
var ckDrawer = byId("drawer"),
drawers = [ byId("drawer1"), byId("drawer2"), byId("drawer3") ],
DrawerCfg = { enabler: ckDrawer, items: drawers, total: byId("drawerCost") };
var ckDesk = byId("desk"),
desks = [ byId("desk1"), byId("desk2"), byId("desk3") ],
DeskCfg = { enabler: ckDesk, items: desks, total: byId("deskCost") };
var Furniture = [ShelvingCfg, DrawerCfg, DeskCfg];
var tTotal = byId("taxCost");
var ckShip = byId("shiptax"),
shTotal = byId("shipCost");
var gTotal = byId("totalCost");
var totalInputs = [ShelvingCfg.total, DrawerCfg.total, DeskCfg.total, shTotal, tTotal, gTotal];
function clearAll() {
byTag("input").map(eachInput => eachInput.checked = false);
totalInputs.map(control => control.value = null);
}
function setDollarValue(Target, DollarValue) {
Target.value = ("$" + Number(DollarValue).toFixed(2));
}
function ckCheckedRow(Enabler, Controls, Target) {
if (Enabler.checked) {
var FirstOn = Controls.findIndex(function(C) { return C.checked; }),
DollarValue = (FirstOn !== -1)? Controls[FirstOn].value : 0;
setDollarValue(Target, DollarValue);
} else {
Target.value = null;
}
}
function ckChecked() {
Furniture.map( Cfg => ckCheckedRow(Cfg.enabler, Cfg.items, Cfg.total) );
if (ckShip.checked) { setDollarValue(shTotal, 10); }
else { shTotal.value = null; }
}
function sumInto(Target, Left, Right) {
Target.value = parseInt(Left.value) + parseInt(Right.value);
}
function addTax() {
tTotal.value = ("$" + (0).toFixed(2));
if (ckShelf.checked) { sumInto(tTotal, tTotal, ShelvingCfg.total); }
if (ckDrawer.checked) { sumInto(tTotal, tTotal, DrawerCfg.total); }
if (ckDesk.checked) { sumInto(tTotal, tTotal, DeskCfg.total); }
}
function grandTotal() {
gTotal.value = ("$" + (0).toFixed(2));
if (ckShelf.checked) { sumInto(gTotal, gTotal, ShelvingCfg.total); }
if (ckDrawer.checked) { sumInto(gTotal, gTotal, DrawerCfg.total); }
if (ckDesk.checked) { sumInto(gTotal, gTotal, DeskCfg.total); }
if (ckShip.checked) { sumInto(gTotal, gTotal, shTotal); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment