Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StoneCypher/4b35d26a11d6faf97a99 to your computer and use it in GitHub Desktop.
Save StoneCypher/4b35d26a11d6faf97a99 to your computer and use it in GitHub Desktop.
function byId(id) {
return document.getElementById(id);
}
function byTag(tag) {
return document.getElementsByTagName(tag);
}
var ShelvingCfg = { enabler : byId("shelf"),
items : [ byId("shelf1"), byId("shelf2"), byId("shelf3") ],
total : byId("shelfCost") };
var DrawerCfg = { enabler : byId("drawer"),
items : [ byId("drawer1"), byId("drawer2"), byId("drawer3") ],
total : byId("drawerCost") };
var DeskCfg = { enabler : byId("desk"),
items : [ byId("desk1"), byId("desk2"), byId("desk3") ],
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));
Furniture.map(
Cfg => if (Cfg.enabler.checked) { sumInto(tTotal, tTotal, Cfg.total); }
);
}
function grandTotal() {
gTotal.value = ("$" + (0).toFixed(2));
Furniture.map(
Cfg => if (Cfg.enabler.checked) { sumInto(gTotal, gTotal, Cfg.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