View Full Version : Payment calculator script
swanharbor
09-03-2002, 09:41 PM
I'm trying to write a script for a custom quilting web site that will calculate a total payment, based on selection of a quilt size from a drop down box, selection of a few optional features using checkboxes for two options and a text box for the the other, and choice of shipping method using radio buttons. The script should return a total payment amount to a text box I've specified.
My trouble is that I don't know much about javascript. I've looked at quite a few tutorials, and I've come up with something that I believe is close to working -- I'm no longer getting a Javascript error in the browser's status bar, but when I click the button to calculate the payment the result is NaN. I suspect the problem is coming from improper coding to handle the values from the drop down box and the radio buttons. Can anyone verify that for me and tell me how it should properly be coded? Or if I'm completely wrong about what I'm trying to do, please give me some direction about what I should try instead? (And please be simple in your explanation -- I'm left feeling terribly unconfident about my knowledge of all of this, at this point!)
You can find the script at the following URL:
http://www.creativequilts.com/pricecalculator2.htm
Thank you for your time!
beetle
09-04-2002, 02:56 AM
function calcTotal(f) {
var a = parseFloat(f.quilt.options[f.quilt.selectedIndex].value);
var b = parseFloat(f.embroidery.value) * 10;
var c = (f.sleeve.checked) ? parseFloat(f.sleeve.value) : 0;
var d = (f.label.checked) ? parseFloat(f.label.value) : 0;
for (var i=0; i<f.shipping.length; i++)
if (f.shipping[i].checked) var e = parseFloat(f.shipping[i].value);
var total = a+b+c+d+e;
f.txtPmtTotal.value = total+".00";
}
<input type="Button" NAME="b1" VALUE="Calculate Payment" onClick="calcTotal(this.form);">
swanharbor
09-04-2002, 03:24 AM
That is EXACTLY what I needed! Thank you so much!
chrismiceli
09-04-2002, 03:30 AM
when you NaN (not a number) you are trying to calculate a number with a literal string (text). That can't be done, only text and text or numbers and more numbers. Hope that helps a bit.
swanharbor
09-04-2002, 03:56 AM
Yeah, I knew NaN meant that kind of error, but I knew that everything I was passing to the script was a number, or was supposed to be. I assume it was that the coding was screwed up and I wasn't passing the numbers as I intended to. Anyway, thanks for the information!
beetle
09-04-2002, 04:05 AM
swanharbor,
although I extensively optimized your code, the NaN error was generated by your calculations for A and E. Had you fixed those, and left the rest the same, the result would effectively be the same.
swanharbor
09-04-2002, 04:17 AM
It's nice to know specifically what the trouble was, but I sure appreciate the total revision you did. :)
While I have you, I've realized it would be nice to have this calculator add 9.25% sales tax for Tennessee residents. What I'd like to do is add a checkbox after the shipping, and have Tennessee residents check it to add the sales tax. If it were a static number, like all of the other prices, I think I could figure out how to add it to the script myself. But I'm not sure how to alter the script to have it add the percentage of the total (excluding shipping). If you wouldn't mind helping me once more, is this something hard to do? Thank you!
swanharbor
09-04-2002, 05:21 AM
It's nice to know specifically what the trouble was, but I sure appreciate the total revision you did. :)
While I have you, I've realized it would be nice to have this calculator add 9.25% sales tax for Tennessee residents. What I'd like to do is add a checkbox after the shipping, and have Tennessee residents check it to add the sales tax. If it were a static number, like all of the other prices, I think I could figure out how to add it to the script myself. But I'm not sure how to alter the script to have it add the percentage of the total (excluding shipping). If you wouldn't mind helping me once more, is this something hard to do? Thank you!
beetle
09-04-2002, 09:31 PM
No, that's not hard at all. This *should* do it.
function calcTotal(f) {
var tax = 0.0;
var a = parseFloat(f.quilt.options[f.quilt.selectedIndex].value);
var b = parseFloat(f.embroidery.value) * 10;
var c = (f.sleeve.checked) ? parseFloat(f.sleeve.value) : 0;
var d = (f.label.checked) ? parseFloat(f.label.value) : 0;
for (var i=0; i<f.shipping.length; i++)
if (f.shipping[i].checked) var e = parseFloat(f.shipping[i].value);
var total = a+b+c+d+e+"";
if (f.tennessee.checked) tax = 0.0925;
var taxTotal = Math.round(total * tax).toString();
if (taxTotal.length == 1) taxTotal += 0;
var output = total + "." + taxTotal;
f.txtPmtTotal.value=output;
}
swanharbor
09-04-2002, 11:48 PM
Thanks beetle -- I really do appreciate your help! I made one modification -- I needed the script to calculate the total of the quilt and options without the shipping charge, so I added a var subtotal. Here's the script after I changed it:
function calcTotal(f) {
var tax = 0.0;
var a = parseFloat(f.quilt.options[f.quilt.selectedIndex].value);
var b = parseFloat(f.embroidery.value) * 10;
var c = (f.sleeve.checked) ? parseFloat(f.sleeve.value) : 0;
var d = (f.label.checked) ? parseFloat(f.label.value) : 0;
for (var i=0; i<f.shipping.length; i++)
if (f.shipping[i].checked) var e = parseFloat(f.shipping[i].value);
var subtotal = a+b+c+d+"";
var total = a+b+c+d+e+"";
if (f.tennessee.checked) tax = 0.0925;
var taxTotal = Math.round(subtotal * tax).toString();
if (taxTotal.length == 1) taxTotal += 0;
var output = total + "." + taxTotal;
f.txtPmtTotal.value=output;
}
I'm still having one problem, and I'm not sure how to solve it. If I choose a taxable item and select the checkbox for sales tax, it isn't adding the right amount of tax. I've been experimenting using the first product in the dropdown box, which costs $125.00. If I select that product, none of the other options, leave the $15.00 shipping option that's selected by default, and select the sales tax checkbox, it returns a total payment of $140.12. Looks to me like it's leaving the shipping charge out of the tax calculation like I want it to, but instead of adding $11.56 for tax (9.25% of $125.00), it's adding 12 cents (.0925%). I tried changing the rate:
if (f.tennessee.checked) tax = 9.25
which I didn't think was right, but it seemed like it was worth a try -- this returned a total payment of $140.1156. This looks like it just took the $140 (product + shipping) and kept writing the tax amount ($11.56) right behind it. I think this has something to do with the tax total being converted to a string, but I'm not sure why it only caused a problem after I changed the rate. What can I change to make the tax calculate at 9.25% rather than .0925%?
Also, one more quick question. (Sorry, I know this is getting long!) In the lines defining the variables, does f (e.g. (f.embroidery.value), (f.sleeve.checked), etc.) designate the text box that the final output will be sent to, or is it an abbreviation for form? This site offers several different types of quilts, with different options available for each. I want to be able to modify this script for use with each quilt type. Suppose a different quilt type has 2 more options, for example, am I correct to assume that I could insert more variable letters (f, g) and adjust the definition of the variables accordingly -- (h.embroidery.value), (h.sleeve.checked), etc. I don't want to just have this script handed to me, I want to try to understand it so I can work with it. (This is showing me that I have some learning to do!)
Thank you very much for your time!
swanharbor
09-06-2002, 03:11 AM
I've kept working on this, but not making much if any headway. I've read something in a tutorial that when a calculation strings numbers together instead of adding them, that the script may be recognizing them as text rather than numbers. I think the total variable is recognized as a number all the way through, but I know the taxTotal variable is converted to a string toward the end, so I tried sticking in this line:
taxTotal = Number(taxTotal);
right before this one:
var output = total + "." + taxTotal;
but it didn't help. I've tried a lot of things, but as I say, none of it seems to be making a difference. Also, on the tax rate, I changed the line that read:
if (f.tennessee.checked) tax = 0.0925;
to
if (f.tennessee.checked) tax = 0.0925 * 100;
I don't know if that's really the way to do it (it just doesn't seem right to me), but at least now the output is 140.1156 instead of 140.12 -- it seems to be achieving the desired effect, if it would just add the 11.56 to the 140.00 instead of stringing it onto the end.
I really need to figure this out -- beetle, or anybody else, do you have some ideas about it? Thanks in advance!
beetle
09-06-2002, 04:17 AM
Apologies, swanharbor. In my last post I used a pretty cheap method for calculating tax. Here's a currency conversion function I had laying around. I'll see if you can implement it ;)function currency(anynum)
{
//-- Returns passed number as string in $xxx,xxx.xx format.
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}
//--- Adds comma in thousands place.
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}
//-- Adds comma in millions place.
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval = dStr + pStr
//-- Put numbers in parentheses if negative.
if (anynum<0) {retval="("+retval+")"}
return "$"+retval
}
swanharbor
09-06-2002, 01:13 PM
Wow, this looks challenging to say the least!:) OK, I'll give it my best shot. I'll post back either that I was successful or that I fell on my face and need more help -- thanks!
swanharbor
09-07-2002, 02:18 AM
OK beetle, I'm crying "uncle"! :o I can see what the currency function is trying to do, I think, but when I try to integrate it into the script I'm not pulling it off -- no matter what I try (and I've tried a lot of different things!!!) I keep getting script errors. What do I do -- how to I put the two together and make it all work? Thanks in advance -- I do appreciate your help!
swanharbor
09-10-2002, 01:39 AM
Hey beetle, are you out there? :) I've kept trying to make this work this weekend, but I'm just spinning my wheels. If you know the magic solution that will make this work, I'd sure appreciate any help you can give me!
beetle
09-11-2002, 02:20 PM
This *should* do itfunction calcTotal(f) {
var tax = 0.0;
var a = parseFloat(f.quilt.options[f.quilt.selectedIndex].value);
var b = parseFloat(f.embroidery.value) * 10;
var c = (f.sleeve.checked) ? parseFloat(f.sleeve.value) : 0;
var d = (f.label.checked) ? parseFloat(f.label.value) : 0;
for (var i=0; i<f.shipping.length; i++)
if (f.shipping[i].checked) var e = parseFloat(f.shipping[i].value);
var subtotal = a+b+c+d+"";
var total = a+b+c+d+e+"";
if (f.tennessee.checked) tax = 0.0925;
var taxTotal = Math.round(subtotal * tax)
var total = currency(subtotal + taxTotal);
f.txtPmtTotal.value=output;
}
swanharbor
09-11-2002, 07:48 PM
Hey beetle, thanks!!! I still had to do some tweaking to it, but I finally figured it out and now it's doing exactly what I want it to. Here's the whole script as I modified it (in case you'd like to see what I did to it, and in the event somebody else may want to use it):
<SCRIPT language = JavaScript>
function currency(anynum)
{
//-- Returns passed number as string in $xxx,xxx.xx format.
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}
//--- Adds comma in thousands place.
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}
//-- Adds comma in millions place.
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval = dStr + pStr
//-- Put numbers in parentheses if negative.
if (anynum<0) {retval="("+retval+")"}
return "$"+retval
}
function calcTotal(f) {
var tax = 0.0;
var a = parseFloat(f.quilt.options[f.quilt.selectedIndex].value);
var b = parseFloat(f.embroidery.value) * 10;
var c = (f.sleeve.checked) ? parseFloat(f.sleeve.value) : 0;
var d = (f.label.checked) ? parseFloat(f.label.value) : 0;
for (var i=0; i<f.shipping.length; i++)
if (f.shipping[i].checked) var e = parseFloat(f.shipping[i].value);
var subtotal = a+b+c+d;
var total = a+b+c+d+e;
if (f.tennessee.checked) tax = 0.0925;
var taxCalc = subtotal * tax;
var taxTotal = Math.round(taxCalc * 100)/100;
var output = currency(total + taxTotal);
f.txtPmtTotal.value=output;
}
</SCRIPT>
If you'd like to see it working, it's at this URL for now:
http://www.creativequilts.com/pricecalculator2.htm
Thank you so much for your help! :D
slater
03-29-2004, 02:54 PM
Hey,
Is there any way of using this script but getting the price to change automatically wehen the user changes an option. Like this one:
https://www.demonite.com/demon-system.asp?systemvar=%0%
Cheers,
Slater
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.