Click to See Complete Forum and Search --> : function Total() not working


kjohn
07-15-2003, 01:41 PM
Can someone tell me why this function isn't working?
I worked fine earlier and then I changed the variable names and now it isn't working...

Look at the source file at http://www.surgimark.com/products/order.htm

Thank you!

cacalex
07-15-2003, 03:38 PM
Try to calculate the total, and THEN make a parseint of it...

function Total() {
var tot = 0;
document.order.output.value=
tot += (document.order.SMP001SS.value) * (document.order.Type1.value);
tot += (document.order.SMP001NS.value) * (document.order.Type2.value);
tot += (document.order.SMP203SS.value) * (document.order.Type3.value);
tot += (document.order.LD2007.value) * (document.order.Type4.value);
tot += (document.order.LD2010.value) * (document.order.Type5.value);

document.order.output.value = parseInt(tot);

}

cacalex
07-15-2003, 03:43 PM
Wait !

What is the error you are receiving ???

document.order.output ???

Where is it ???

I see <FORM METHOD="post" ACTION="http://www.surgimark.com/cgi-bin/FormMail.cgi" name="checkout"

So, shoud'nt you put document.checkout.output ???

Let me check your code more further...

kjohn
07-16-2003, 11:16 AM
Perfect, you are a genius :) I just changed the name of the form back to order.

I had cut and paste something there and it changed it.

Now... can you tell me how to make the currency in the total box into a rounded decimal place?

I need it to always round to two spots: $34.56
instead of $34.56333 etc.

thanks!!

Katie

cacalex
07-16-2003, 11:41 AM
Maybe something like substr(0,4);

It will work with a number like 1,2563636457575, and return 1,25

But with 15,25, you'll get 15,5

Let me check this a little more...
;)


Ho, and by the way, i'm not a freaking genius,

I'm just a simple man trying to make life easier for everyone
;)

kjohn
07-16-2003, 12:28 PM
Okay well simple man... :) can you figure this one out?

The "ship to same as bill to" checkbox on this worked a little while ago, and now all of a sudden it stopped working...

http://www.surgimark.com/products/checkout.htm

can you figure out why???

Any luck w/ figuring out the currency situation!?

Thanks so much simple genius man :P

Katie

cacalex
07-16-2003, 01:11 PM
First, thanks for the simple man ;)


<input type="text" name="Billto" size="45" maxlength="45">

this is the line that make your "ship to same as bill to" checkbox not working...

the error is BillTo.value is null because the name of the input is Billto... I don't know wich one you want to use in your script, but i strongly suggest that you rename it as in you script... (It's shorter...)

for the «currency situation» like you say, i have this wich can help you...
I'm still trying to make something more easy to handle, but give it a look :)

_______________________________________________
<HTML><HEAD>
<TITLE>JavaScript "Format Dollar" Example</TITLE>
</HEAD><BODY>

<SCRIPT LANGUAGE="JavaScript"><!--

function formatDollar (Val, DollarSign) {

Val=""+Val;


if (Val.indexOf (".", 0)!=-1)
{
Dollars = Val.substring(0, Val.indexOf (".", 0));
Cents = Val.substring(Val.indexOf (".", 0)+1, Val.indexOf (".", 0)+3);
if (Cents.length==0)
Cents="00";
if (Cents.length==1)
Cents=Cents+"0";
}
else
{ Dollars = Val;
Cents = "00";
}

OutString="";
len=Dollars.length;

if (len>=3)
{
while (len>0)
{
TempString=Dollars.substring(len-3, len)
if (TempString.length==3)
{
OutString=","+TempString+OutString
len=len-3;
}
else
{
OutString=TempString+OutString;
len=0;
}
}

if (OutString.substring(0, 1)==",")
Dollars=OutString.substring (1, OutString.length);
else
Dollars=OutString
}

if (DollarSign)
return ("$"+Dollars+"."+Cents);
else
return (Dollars+"."+Cents);
}//-->
</SCRIPT>

<P><CENTER>
<FORM NAME="test">
Enter number to convert to dollar amount:<BR>
<INPUT TYPE="text" NAME="input" VALUE=""><P>
<INPUT TYPE="button" onClick="this.form.output.value=formatDollar (this.form.input.value, true)" VALUE="Convert"><P>Converted string:<BR>
<INPUT TYPE="text" NAME="output" VALUE="">
</FORM>
</CENTER>
</BODY></HTML>
__________________________________________________

I know this script is bull**** and hard to figure out, but if take of it the interesting part, you might be able to do it ;)

kjohn
07-16-2003, 02:04 PM
DANG you are good!!!!! Thank you so much.

It would have taken me FOREVER to figure that stupid little capital was messing up the whole thing.


Ok well... I will try to figure out the other script you gave me...

I doubt I can understand it being really new to javascript, but I will give it a shot..

Katie

cacalex
07-16-2003, 02:16 PM
That's the spirit !!!! Give it a try !

DANG you are good!!!!!
Hey, i'm trying to remain modest ;)

kjohn
07-16-2003, 02:19 PM
umm..... well I tried.

I have no clue how to incorporate that into my page I've already got...

see when the user pushes "done" it has to call a function that will multiply the total.. THEN the total needs to be converted into dollar format.

http://www.surgimark.com/products/order.htm

Have ne ideas??

Katie

cacalex
07-16-2003, 03:01 PM
Here's a couples of changes, to put on your page...

The begginning of your script have been modified, but that's all...

Hope it will help you.

I tested it with IE6, so i don't know if it will work in another browser...

See ya'

:cool:

<script language="JavaScript">

function formatDollar (Val, DollarSign) {

Val=""+Val;


if (Val.indexOf (".", 0)!=-1)
{
Dollars = Val.substring(0, Val.indexOf (".", 0));
Cents = Val.substring(Val.indexOf (".", 0)+1, Val.indexOf (".", 0)+3);
if (Cents.length==0)
Cents="00";
if (Cents.length==1)
Cents=Cents+"0";
}
else
{ Dollars = Val;
Cents = "00";
}

OutString="";
len=Dollars.length;

if (len>=3)
{
while (len>0)
{
TempString=Dollars.substring(len-3, len)
if (TempString.length==3)
{
OutString=","+TempString+OutString
len=len-3;
}
else
{
OutString=TempString+OutString;
len=0;
}
}

if (OutString.substring(0, 1)==",")
Dollars=OutString.substring (1, OutString.length);
else
Dollars=OutString
}

if (DollarSign)
document.order.TotalCharge.value=("$"+Dollars+"."+Cents);
//return ("$"+Dollars+"."+Cents);
else
document.order.TotalCharge.value=(Dollars+"."+Cents);
//return (Dollars+"."+Cents);
}


function Total() {
var tot = 0;
document.order.TotalCharge.value=parseInt
tot += (document.order.SMP001SS.value) * (document.order.Type1.value);
tot += (document.order.SMP001NS.value) * (document.order.Type2.value);
tot += (document.order.SMP203SS.value) * (document.order.Type3.value);
tot += (document.order.LD2007.value) * (document.order.Type4.value);
tot += (document.order.LD2010.value) * (document.order.Type5.value);
document.order.TotalCharge.value = tot;
formatDollar (document.order.TotalCharge.value, false);
}

cacalex
07-16-2003, 03:38 PM
I don't know if you made it, Kathie,
But i'm leaving from the job now, so i post you the whole Script here, and i guess you'll only have to test it !!!

Anyway, gimme news of your progress, would you ???

_______________________

<script language="JavaScript">

function formatDollar (Val, DollarSign) {

Val=""+Val;


if (Val.indexOf (".", 0)!=-1)
{
Dollars = Val.substring(0, Val.indexOf (".", 0));
Cents = Val.substring(Val.indexOf (".", 0)+1, Val.indexOf (".", 0)+3);
if (Cents.length==0)
Cents="00";
if (Cents.length==1)
Cents=Cents+"0";
}
else
{ Dollars = Val;
Cents = "00";
}

OutString="";
len=Dollars.length;

if (len>=3)
{
while (len>0)
{
TempString=Dollars.substring(len-3, len)
if (TempString.length==3)
{
OutString=","+TempString+OutString
len=len-3;
}
else
{
OutString=TempString+OutString;
len=0;
}
}

if (OutString.substring(0, 1)==",")
Dollars=OutString.substring (1, OutString.length);
else
Dollars=OutString
}

if (DollarSign)
document.order.TotalCharge.value=("$"+Dollars+"."+Cents);
//return ("$"+Dollars+"."+Cents);
else
document.order.TotalCharge.value=(Dollars+"."+Cents);
//return (Dollars+"."+Cents);
}


function Total() {
var tot = 0;
var tot2
document.order.TotalCharge.value=parseInt
tot += (document.order.SMP001SS.value) * (document.order.Type1.value);
tot += (document.order.SMP001NS.value) * (document.order.Type2.value);
tot += (document.order.SMP203SS.value) * (document.order.Type3.value);
tot += (document.order.LD2007.value) * (document.order.Type4.value);
tot += (document.order.LD2010.value) * (document.order.Type5.value);
document.order.TotalCharge.value = tot;
formatDollar (document.order.TotalCharge.value, false);
}



function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->

function MM_callJS(jsStr) { //v2.0
return eval(jsStr)
}
//-->
</script>
_______________________________________________

I don't know if you write this whole page, but it is pretty complex...

I guess you are better than me with HTML ;)

kjohn
07-17-2003, 12:44 PM
Thank you so much!!!!!

That did work when I plugged it into my page.

Xcept it doesn't round correctly all the time. It just cuts off the rest of the numbers after 2 decimal places.

Oh well, I don't think a penny will matter too much...

Thanks again!!!

cacalex
07-17-2003, 01:10 PM
Hey !

At 2158.86 per cartons, a penny WILL matter...

Make this change :

if (DollarSign)
{
//round of the cents BEFORE write the total...
CentsRound=Math.round(Cents)
document.order.TotalCharge.value=("$"+Dollars+"."+CentsRound);
}
else
{
//round of the cents BEFORE write the total...
CentsRound=Math.round(Cents)
document.order.TotalCharge.value=(Dollars+"."+CentsRound);
}
}

And there you go !!!

See, javascript is simple, for simple person...
Just like me ;)

kjohn
07-17-2003, 01:32 PM
Hmmm...

Didn't work...

http://www.surgimark.com/products/order.htm

Try putting in 12 boxes of the first one, 15 of second, 18 of third, 4 of fourth, and 3 of fifth.

It should come out to 18601.83
and it's coming to 18,602.7

cacalex
07-17-2003, 01:40 PM
I'm checking this right now !

kjohn
07-17-2003, 01:46 PM
actually, don't bother.

Your first code you gave me was right on :)

but... now I am having problems w/ that other page that you first helped me with...

http://www.surgimark.com/products/checkout2.htm


nothing works now :(

cacalex
07-17-2003, 01:56 PM
I have make this test, with your page (online) and your page on my computer (for testing purpose)...

12-1=1195.92
15-2=12813.75
18-3=1793.88
4-4=1599.16
3-5=1998.95
==19401.66

which is the result of the page, and of my caculator... :)
seems ok to me !!!

I'll check you other page, now...

kjohn
07-17-2003, 03:02 PM
Great, thank you for doing that.

Yeah the first code you gave me was fine, I just tested it wrong. *duh*

Good luck with the next one....

its strange... I don't even change anything related to the ship to/bill to script and it stops working...?

cacalex
07-17-2003, 04:13 PM
Just came back from the office of a client...

Don't have checked you page yet, but i saw this:

Your page is now checkout2, and it was checkout last time i take a look at it...

You don't have change nothing ???

Anyway, i will check your page, and came back to you...