1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
function full_reset(form) { cost_per_unit.innerHTML = "" charge_per_unit.innerHTML = "" } function addEvent(els, type, func) { if(!(els instanceof Array)) els = [els]; for(var i=0, n=els.length; i<n; ++i) { if(els[i].addEventListener) els[i].addEventListener(type, func, false); else els[i].attachEvent("on"+type, func); } } String.prototype.trim = function() { return this.replace(/^\s+/, "").replace(/\s+$/, ""); }; var quantity = document.getElementById("quantity"); var cost = document.getElementById("cost"); var charge = document.getElementById("charge"); var profit_amount = document.getElementById("profit_amount"); var profit_percent = document.getElementById("profit_percent"); var tax = document.getElementById("tax"); var cost_per_unit = document.getElementById("cost_per_unit"); var charge_per_unit = document.getElementById("charge_per_unit"); var subtotal = document.getElementById("subtotal"); var discount = document.getElementById("discount"); var discount_amount = document.getElementById("discount_amount"); var total = document.getElementById("total"); var dpp = document.getElementById("dpp"); var dpa = document.getElementById("dpa"); var dcc = document.getElementById("dcc"); var dt = document.getElementById("dt"); var dst = document.getElementById("dst"); addEvent( [cost, charge], "keyup", function(e) { var vcost = cost.value.trim(); var vcharge = charge.value.trim(); var ncost = vcost*1; var ncharge = vcharge*1; if(vcost=="" || vcharge=="" || isNaN(ncost) || isNaN(ncharge)) { profit_amount.value = profit_percent.value = ""; return; } prof = (ncharge - ncost).toFixed(2) profit_amount.value = prof; profit_percent.value = (prof/ncharge*100).toFixed(2); discount.value = "" dcc.innerHTML = ""; dt.innerHTML = ""; dst.innerHTML = ""; dpa.innerHTML = ""; dpp.innerHTML = ""; } ); addEvent( [cost, profit_percent], "keyup", function(e) { var vcost = cost.value.trim(); var vprofit_percent = profit_percent.value.trim(); var ncost = vcost*1; var nprofit_percent = vprofit_percent*1; if(vcost=="" || vprofit_percent=="" || isNaN(ncost) || isNaN(nprofit_percent)) { profit_amount.value = ""; return; } profit_amount.value = (ncost / nprofit_percent * 100).toFixed(2); charge.value = (ncost + (ncost / nprofit_percent *100)).toFixed(2); discount.value = ""; dcc.innerHTML = ""; dt.innerHTML = ""; dst.innerHTML = ""; dpa.innerHTML = ""; dpp.innerHTML = ""; } ); addEvent( [cost, profit_amount], "keyup", function(f) { var vcost = cost.value.trim(); var vprofit_amount = profit_amount.value.trim(); var ncost = vcost*1; var nprofit_amount = vprofit_amount*1; if(vcost=="" || vprofit_amount==""|| isNaN(ncost) || isNaN(nprofit_amount)) { profit_amount.value = ""; return; } profit_percent.value = (nprofit_amount / (ncost + nprofit_amount) * 100).toFixed(2); charge.value = (ncost + nprofit_amount).toFixed(2); } ); addEvent( [quantity, cost, charge, profit_amount, profit_percent], "keyup", function(e) { var vquantity = quantity.value.trim(); var nquantity = vquantity*1; var vcharge = charge.value.trim(); var ncharge = vcharge*1; var vcost = cost.value.trim(); var ncost = vcost*1; var vprofit_amount = profit_amount.value.trim(); var nprofit_amount = vprofit_amount*1; var vprofit_percent = profit_percent.value.trim(); var nprofit_percent = vprofit_percent*1; if(vquantity=="" || vcharge=="" || vcost=="" || vprofit_amount=="" || vprofit_percent=="" || isNaN(nquantity) || isNaN(ncharge) || isNaN(ncost) || isNaN(nprofit_amount) || isNaN(nprofit_percent)) { profit_amount.value = profit_percent.value = ""; return; } tax.value = (ncharge * 0.08375).toFixed(2); cost_per_unit.innerHTML = "$" + (ncost / nquantity).toFixed(2) + "/per unit"; charge_per_unit.innerHTML = "$" + (ncharge / nquantity).toFixed(2) + "/per unit"; subtotal.value = (ncharge + (ncharge * 0.08375)).toFixed(2); discount.value = "" dcc.innerHTML = ""; dt.innerHTML = ""; dst.innerHTML = ""; dpa.innerHTML = ""; dpp.innerHTML = ""; } ); addEvent( [discount], "keyup", function(e) { var vcost = cost.value.trim(); var ncost = vcost*1; var vdiscount = discount.value.trim(); var ndiscount = vdiscount*1/100; var vcharge = charge.value.trim(); var ncharge = vcharge*1; var vprofit_amount = profit_amount.value.trim(); var nprofit_amount = vprofit_amount*1; var vquantity = quantity.value.trim(); var nquantity = vquantity*1; var vsubtotal = subtotal.value.trim(); var nsubtotal = vsubtotal*1; off = (ncharge * ndiscount).toFixed(2) dcharge = (ncharge - off).toFixed(2) dtax = (dcharge * 0.08375).toFixed(2) dsub = (nsubtotal - off).toFixed(2) dprof = (dcharge-ncost).toFixed(2) dper = (dprof/ncharge*100).toFixed(2) // tax.value = (tax.value * ndiscount).toFixed(2); discount_amount.innerHTML = off; dcc.innerHTML = "<strong>$"+ dcharge + "</strong> ($"+ (dcharge / nquantity).toFixed(2) +"/per unit)"; dt.innerHTML = "<strong>$"+ dtax + "</strong>"; dst.innerHTML = "<strong>$"+ dsub + "</strong>"; dpa.innerHTML = "<strong>$"+ dprof + "</strong>"; dpp.innerHTML = "<strong>"+ dper + "%</strong>"; } );
Refactorings
No refactoring yet !
philippe.rathe.myopenid.com
December 21, 2007, December 21, 2007 03:27, permalink
First of all, you should use a JavaScript api like Prototype or jQuery to do DOM manipulation. Your code will become more clean. Take a look at jQuery.com
I am almost embarrassed to post this mess. It gets the job done, but it's quite hideous to look at.
I'm very curious to see how it could be done more elegantly.