PDA

View Full Version : Javascript difficulties...


Ken Rogers
01-23-2006, 06:52 PM
Could someone have a look at this form and guess as to why the calculation doesn't work? Or could you redirect me to a good source of help?

http://www3.telus.net/sprecious/Faulty_form.htm

The above link is a stripped-down version of my troublesome order form. I've renamed all products to remain anonymous. This form is designed to calculate an order total including taxes, which depend on your Canadian province.

Taxes are properly calculated, but the condiment total and the grand total are wrong. All functions have previously worked. Here are the two I just added (which don't work). Can anyone see any immediately obvious syntax errors?







function calculate_total()
{
switch(document.myform.state.value)
{
case "Alberta":
var gst_perc=".07";
var pst_perc=".00";
var pack_quantity =
document.myform.west_pack.value +
document.myform.prarie_pack.value +
document.myform.cent_pack.value +
document.myform.east_pack.value +
document.myform.alta_pack.value;
document.myform.map_pack_subtotal.value = calculate_map_packs(pack_quantity);
document.myform.grand_subtotal.value =
document.myform.product_subtotal.value +
document.myform.map_pack_subtotal.value;
document.myform.gst.value = document.myform.grand_subtotal.value * gst_perc;
document.myform.pst.value = document.myform.grand_subtotal.value * pst_perc;
document.myform.grand_total.value =
document.myform.grand_subtotal.value +
document.myform.gst.value +
document.myform.pst.value +
document.myform.shipping.value;
setupFields()
break
-----the switch statement repeats the above code for all 13 provinces-----










[b]function calculate_map_packs(var1)[/b]
{
var x = 0;
switch(var1)
{
case 0:
x = 0;
break
case 1:
x = 0;
break
case 2:
x = 39.95;
break
case 3:
x = 79.90;
break
case 4:
x = 119.85;
break
case 5:
x = 159.8;
break
default:
}
return x
}
[/url]

davidj
01-24-2006, 11:10 AM
you have allot of repetative code in there thats not really needed

i would have a couple of hidden fields which you would use to catch [var gst_perc= and pst_perc=] variables. Use a function to set these hidden field values..

set them using an onChange from the state dropdown

function perc(){
a = document.form1.hiddenfld1;
b = document.form1.hiddenfld2;

c = document.form1.dropdown;

switch(c.value){
case "Alb": a.value = .07; b.value = .00; break;
case "Brit": a.value = .00; b.value = .00; break;
case "New": a.value = .15; b.value = .00; break;

}

document.myform.west_pack.value +
document.myform.prarie_pack.value +
document.myform.cent_pack.value +
document.myform.east_pack.value +
document.myform.alta_pack.value;

// etc etc etc
}