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.