jscotter
Feb 17th 2009, 12:21 pm
I have this script set up on my page and it all works fine. It basically pulls info from a form and calculates costs. But now, I need a change and I can't get it to do what I need it to do.
There are two values that I need to set minimums for; form.height.value and form.linear.value. I have these as defualt values on the form. But, what I want the script to do is this: If the user changes the value to less than a specific number (8 and 50, respectively) the onChange (which calls this function from every input box) will automatically change the value back to the minimum, yet continue to re-calculate. I added an if statement in, only I am not doing something right. It will change the value back, but the rest of the calculations will not continue. I am sure it has to do with logic and how the script "flows". I am new to scripting and flounder my way through it. You can see the basic function at http://www.shayden.com/_WIFPCO/pricing.htm
however, I have changed the drop down in height to an entry box.
Anyway, as a user enters or changes any of the boxes, the price is re-clculated because the onChange calls the function calculateCost(form). However, when I add the if statement at the beginning to check the minimum and have it re-enter the minimum automatically, only the first one works and does not re-calculate. Can someone offer some help?
This is the code I tried adding:
var height = form.height.value;
if (height <= 50) { var height = 50}
form.height.value = height.toFixed(0);
var linear = form.linear.value;
if (linear <= 50) { var linear = 50}
form.linear.value = linear.toFixed(0);
--------------------------------------------------------
This is the code that works without the new necessary minimums.
function calculateCost(form) {
var basecost = form.height.value * form.linear.value * .9375;
form.basecost.value = basecost.toFixed(2);
var Ornamentcost = form.OrnamentLinear.value * .25;
form.OrnamentCost.value = Ornamentcost.toFixed(2);
var Rustcost = form.RustLinear.value * .25;
form.RustCost.value = Rustcost.toFixed(2);
var ExtraOnecost = form.ExtraOneLinear.value * .25;
form.ExtraOneCost.value = ExtraOnecost.toFixed(2);
var Extra2cost = form.Extra2Linear.value * .50;
form.Extra2Cost.value = Extra2cost.toFixed(2);
var Colorcost = form.ColorLinear.value * .25;
form.ColorCost.value = Colorcost.toFixed(2);
var Primercost = form.PrimerLinear.value * .25;
form.PrimerCost.value = Primercost.toFixed(2);
var Additionalcost = Ornamentcost + Rustcost + ExtraOnecost + Extra2cost + Colorcost + Primercost;
form.additionalcost.value = Additionalcost.toFixed(2);
var Subtotal = Additionalcost + basecost;
form.subtotal.value = Subtotal.toFixed(2);
var Tax = Subtotal * .0825;
form.tax.value = Tax.toFixed(2);
var Totalcost = Subtotal + Tax;
form.totalcost.value = Totalcost.toFixed(2);
}
There are two values that I need to set minimums for; form.height.value and form.linear.value. I have these as defualt values on the form. But, what I want the script to do is this: If the user changes the value to less than a specific number (8 and 50, respectively) the onChange (which calls this function from every input box) will automatically change the value back to the minimum, yet continue to re-calculate. I added an if statement in, only I am not doing something right. It will change the value back, but the rest of the calculations will not continue. I am sure it has to do with logic and how the script "flows". I am new to scripting and flounder my way through it. You can see the basic function at http://www.shayden.com/_WIFPCO/pricing.htm
however, I have changed the drop down in height to an entry box.
Anyway, as a user enters or changes any of the boxes, the price is re-clculated because the onChange calls the function calculateCost(form). However, when I add the if statement at the beginning to check the minimum and have it re-enter the minimum automatically, only the first one works and does not re-calculate. Can someone offer some help?
This is the code I tried adding:
var height = form.height.value;
if (height <= 50) { var height = 50}
form.height.value = height.toFixed(0);
var linear = form.linear.value;
if (linear <= 50) { var linear = 50}
form.linear.value = linear.toFixed(0);
--------------------------------------------------------
This is the code that works without the new necessary minimums.
function calculateCost(form) {
var basecost = form.height.value * form.linear.value * .9375;
form.basecost.value = basecost.toFixed(2);
var Ornamentcost = form.OrnamentLinear.value * .25;
form.OrnamentCost.value = Ornamentcost.toFixed(2);
var Rustcost = form.RustLinear.value * .25;
form.RustCost.value = Rustcost.toFixed(2);
var ExtraOnecost = form.ExtraOneLinear.value * .25;
form.ExtraOneCost.value = ExtraOnecost.toFixed(2);
var Extra2cost = form.Extra2Linear.value * .50;
form.Extra2Cost.value = Extra2cost.toFixed(2);
var Colorcost = form.ColorLinear.value * .25;
form.ColorCost.value = Colorcost.toFixed(2);
var Primercost = form.PrimerLinear.value * .25;
form.PrimerCost.value = Primercost.toFixed(2);
var Additionalcost = Ornamentcost + Rustcost + ExtraOnecost + Extra2cost + Colorcost + Primercost;
form.additionalcost.value = Additionalcost.toFixed(2);
var Subtotal = Additionalcost + basecost;
form.subtotal.value = Subtotal.toFixed(2);
var Tax = Subtotal * .0825;
form.tax.value = Tax.toFixed(2);
var Totalcost = Subtotal + Tax;
form.totalcost.value = Totalcost.toFixed(2);
}