Created
          January 23, 2016 11:01 
        
      - 
      
 - 
        
Save StoneCypher/a98ff2913a98726ff8a9 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function byId(id) { | |
| return document.getElementById(id); | |
| } | |
| function byTag(tag) { | |
| return document.getElementsByTagName(tag); | |
| } | |
| var shelves = [ byId("shelf1"), byId("shelf2"), byId("shelf3") ], | |
| ShelvingCfg = { enabler: byId("shelf"), items: shelves, total: byId("shelfCost") }; | |
| var drawers = [ byId("drawer1"), byId("drawer2"), byId("drawer3") ], | |
| DrawerCfg = { enabler: byId("drawer"), items: drawers, total: byId("drawerCost") }; | |
| var desks = [ byId("desk1"), byId("desk2"), byId("desk3") ], | |
| DeskCfg = { enabler: byId("desk"), 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 (ShelvingCfg.enabler.checked) { sumInto(tTotal, tTotal, ShelvingCfg.total); } | |
| if (DrawerCfg.enabler.checked) { sumInto(tTotal, tTotal, DrawerCfg.total); } | |
| if (DeskCfg.enabler.checked) { sumInto(tTotal, tTotal, DeskCfg.total); } | |
| } | |
| function grandTotal() { | |
| gTotal.value = ("$" + (0).toFixed(2)); | |
| if (ShelvingCfg.enabler.checked) { sumInto(gTotal, gTotal, ShelvingCfg.total); } | |
| if (DrawerCfg.enabler.checked) { sumInto(gTotal, gTotal, DrawerCfg.total); } | |
| if (DeskCfg.enabler.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