...

View Full Version : Help adding function to a script



mestes
12-10-2004, 06:05 PM
I need help writing a function is javascript

I have an order form that right now when some puts in the number of books they want to order it calculates the total by basically

taking the number of books orders x 15.99 base price + 3.00 shipping cost + 5% sales tax if you state u select is Massaschusetts = total cost.

This is all displayed on order form page as you fill it out and it will change the total cost right away if you change the number ordered.

I need to add a function that will do this

Check the the field where the number of books ordered is entered by the user and if that amount is only one book the shipping cost is $3.00 but if the number order is more than 1 book the cost of shipping is determined adding $1.50 to the shipping base cost for every book over one that is ordered example


Number Ordered greater than 1 the shipping cost is $3.00+($1.50 x Books Ordered - 1 book ordered to subtract the 1st book that had a shipping charge of 3.00

so simple formula

5 Books are going to be ordered

3.00+(1.50x5-1)

3.00+(1.50x4)

3.00+6.00

9.00 total shipping cost for 5 books is 9.00

I need to add a function just like the settax function in the code below to make the form make this second calculation along with the alreay working total cost calculation.

I tried to write it bases off the settax function is the code below but I do not know the correct syntax for the function?

Code of both the javascript that I want to ad this to and the form fields that the function uses to display the final answer to the calculations.

Can anyone help me?



Javascript Code


function settax(Info){

if(Info.state.value == " "){ return false; }

if(Info.state.value == "MA"){

Info.tax.value = 5;

}else{

Info.tax.value = 0;
}

recalculate(Info);

return false;

}

function recalculate(Info){

var pprice = Info.qty.value*Info.price.value;
var price = pprice.toFixed(2);

var pshiphand = Info.qty.value*Info.shiphand.value;
var shiphand = pshiphand.toFixed(2);

var ptax = Info.price.value*(.01*Info.tax.value);
var tax = Info.qty.value*ptax.toFixed(2);

var total = parseFloat(tax)+parseFloat(shiphand)+parseFloat(price);

Info.total.value = total.toFixed(2);

return false;
}



onLoad = settax(document.buynow);





Form Fields

input type="text" name="qty" size=4 value="1" onBlur="recalculate(this.form);">
input type="text" name="price" value="15.99" style="border-style: none; background: inherit" readonly="yes" size=6>
input name="shiphand" type="text" style="border-style: none; background: inherit" value="3.00" size=3 maxlength="3" readonly="yes"
input name="tax" type="text" style="border-style: none; background: inherit" value="0" size=1 maxlength="1" width="10"

kansel
12-10-2004, 06:46 PM
Here's a rather simplified recalculate function.
function recalculate(Info){
var qty = parseInt(Info.qty.value);
if(isNaN(qty)) qty = 0;
var price = parseFloat(Info.price.value);
var shiphand = parseFloat(Info.shiphand.value);
var tax = parseFloat(Info.tax.value) * .01;

price *= qty;
shiphand = (qty > 1)?(shiphand + ((qty - 1) * shiphand * 0.5)):(qty == 1)?(shiphand):(0);
tax *= price;

Info.total.value = (price + tax + shiphand).toFixed(2);
}The first part gets the values from the form, converts them to usable numbers.

The middle part does the calculation. The shiphand line in the middle does the complete shiphand calculation taking into account whether qty is 1 or greater than 1. I made an assumption about the value 1.50 for additional books, that it would be the standard ship rate cut in half for each additional book. If this is not the case, simply replace the part * shiphand * 0.5 with * 1.5 or another value or variable.

The last line sets the total.

added color clarification

mestes
12-11-2004, 01:42 AM
That works great but on the form it will automatically adjust the total price when you change the quantity how can I get it to change the shipping total too?

The input fields for the form are in the original post.

mestes
12-11-2004, 02:03 AM
I added this to the code to make the shipping total automatically change depending on the number ordered



Info.shiphand.value = (3.00 + ((1.50 + Info.qty.value - 1) * Info.qty.value * 1.5)).toFixed(2);


I think I have that wrong because it works good for 2 books but if I change it to 3 the shipping cost is 5.39 and it should be 6.00

mestes
12-11-2004, 02:24 AM
I got it working here is the complete function


function recalculate(Info){
var qty = parseInt(Info.qty.value);
if(isNaN(qty)) qty = 0;
var price = parseFloat(Info.price.value);
var shiphand = parseFloat(Info.shiphand.value);
var tax = parseFloat(Info.tax.value) * .01;

price *= qty;
shiphand = (qty > 1)?(shiphand + ((qty - 1) * 1.5)):(qty == 1)?(shiphand):(0);
tax *= price;

Info.shiphand.value = (3.00 + ((Info.qty.value - 1) * 1.50)).toFixed(2);

Info.total.value = (price + tax + shiphand).toFixed(2);
}

Thanks for the Help

kansel
12-11-2004, 07:17 AM
If you want to display the updated ship value, you should calculate based on fixed values. You can do this with a hidden form field or hardcode the 3.00 rate to a variable. Otherwise if the function is run again (for instance if quantity is updated), the function will try to use the new value instead of 3.00.

mestes
12-11-2004, 07:24 AM
Yes I noticed that after some testing to I hard coded the shiphand variable to be 3.00 and then made the qty and shiphand form fields have no value.

Updated code



function recalculate(Info){
var qty = parseInt(Info.qty.value);
if(isNaN(qty)) qty = 0;
var price = parseFloat(Info.price.value);
var shiphand = parseFloat(Info.shiphand.value);
var shiphand = 3.00;
var tax = parseFloat(Info.tax.value) * .01;

price *= qty;
shiphand = (qty > 1)?(shiphand + ((qty - 1) * 1.5)):(qty == 1)?(shiphand):(0);
tax *= price;

Info.shiphand.value = (3.00 + ((Info.qty.value - 1) * 1.50)).toFixed(2);

Info.total.value = (price + tax + shiphand).toFixed(2);
}

kansel
12-12-2004, 01:30 AM
This one's going to be a bit easier to maintain if the shipping values ever changed. Variables at top of function control the shipping for one book and the additional shipping value for more books. (set to 3.00 and 1.50 right now)
function recalculate(Info){
var shipone = 3.00;
var shipadd = 1.50;

var qty = parseInt(Info.qty.value);
if(isNaN(qty)) qty = 0;
var price = parseFloat(Info.price.value);
var tax = parseFloat(Info.tax.value);
if(isNaN(tax)) tax = 0; else tax *= 0.01;

price *= qty;
var shiphand = (qty > 1)?(shipone + ((qty - 1) * shipadd)):(qty == 1)?(shipone):(0);
tax *= price;

Info.shiphand.value = shiphand.toFixed(2);
Info.total.value = (price + tax + shiphand).toFixed(2);
}There's no reason to recalculate the shipping when putting into the shiphand field for display. Just use the toFixed() method on the shiphand variable.

I also added an error check to the code that imports the tax.

changes are in blue



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum