/    Sign up×
Community /Pin to ProfileBookmark

Order Form Won’t Update

I took over this website a little while back. The order form will only update the first three products, and that’s only if I comment out the getTaxRate function. It doesn’t update the Final Total either. I’ve made some attempt at getting it to work but ended up pulling my hair out. I’m not too familiar with javascript, but it seems like the code was written poorly. I would appreciate any help.

Here is the form: [URL=”http://rcamodels.com/rcatest/ordernow.html”]Link[/URL]

Here is the Javascript:

[CODE]
// @@@@@ SALES ORDER SCRIPTS Start @@@@@
/*
Return Codes
————————-
0 – Success
1 – Error: Check sum failed
10 – Error: Too Few digits
11 – Error: Invalid Prefix
12 – Error: Incorrect Number of digits
13 – Error: Unsupported card type
*/
function validateCCard( nCardNum, strCardType)
{
// returns 0 if valid, positive number if invalid.
nTotal = 1*0;
nOff = 1*0;

nNumber = “”;

// Remove any non-numeric values from this number.
for( i = 0; i < nCardNum.length; i++ )
if ( nCardNum.charAt(i) >= “0” && nCardNum.charAt(i) <= “9” )
nNumber = nNumber + nCardNum.charAt(i);

nFirstNum = “” + nNumber.charAt(0);
nSecondNum = “” + nNumber.charAt(1);
nThirdNum = “” + nNumber.charAt(2);
nFirstTwoDigits = nFirstNum + nSecondNum;
nFirstFourDigits = nFirstTwoDigits + nThirdNum + nNumber.charAt(3);

if ( strCardType == “MasterCard” )
{
if ( nFirstNum != “5” || nSecondNum < “1” || nSecondNum > “5” )
return 11; // invalid prefix

if(nNumber.length != 16)
return 12; // Incorrect number of digits
}
else if(strCardType == “Visa”)
{
if(nFirstNum != “4”)
return 11; // invalid prefix

if(nNumber.length != 13 && nNumber.length != 16)
return 12; // Incorrect number of digits
}
else if(strCardType == “American Express”)
{
if(nFirstNum != “3” || (nSecondNum != “4” && nSecondNum != “7”))
return 11; // invalid Prefix

if(nNumber.length != 15)
return 12; // Incorrect number of digits
}
else if(strCardType == “Discover”)
{
if( nFirstFourDigits != “6011” )
return 11; // invalid prefix.

if( nNumber.length != 16 )
return 12; // Incorrect number of digits
}
else if(strCardType == “Diner”s Club”)
{
if ( nFirstTwoDigits != “36” && nFirstTwoDigits != “38” &&
(nFirstTwoDigits != “30” || (nThirdNum < “0” || nThirdNum > “5”)))
{
return 11; // invalid prefix.
}
if ( nNumber.length != 14 )
return 12; // Incorrect number of digits
}
else
return 13; // Card type not supported

// No card is less than 13 characters
if ( nNumber.length < 13 )
return 10;

// now check the credit card suffix and length vs. the type

// do the checksum
for ( loc = nNumber.length – 2; loc >= 0; loc -= 2 )
{
nTotal += 1 * nNumber.charAt(loc +1);
nOff = nNumber.charAt(loc) * 2;
if ( nOff > 9 )
nTotal += 1;
nTotal += (nOff % 10);
}

if( nNumber.length % 2 > 0 )
nTotal += 1 * nNumber.charAt(0);

if ( ( nTotal % 10 ) != 0 )
return 1; // Checksum failure

return 0; // Okey Dokey!
}
function formatCurrency(nAmount)
{
//– Returns passed number as string in $xxx,xxx.xx format.
nAmount=eval(nAmount)
workNum=Math.abs((Math.round(nAmount*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 (nAmount<0) {retval=”(“+retval+”)”}
return “$”+retval
}

function getTaxRate()
{
// Need to get the correct tax rate based upon what State.
// Get the state the user is shipping to.
/* nStateIDX = document.formSalesOrder.elements[“BillingState”].selectedIndex;
strState = document.formSalesOrder.elements[“BillingState”].options[nStateIDX].value;
nTaxRate = 0.00;
if ( strState == “MI – Michigan” )
{
// If the selected state is ‘MI – Michigan’ then return the correct tax
nTaxRate = 6.000000;
}
return nTaxRate/100;*/
}
function getShippingRate()
{
// Shipping is a fixed percentage rate
var nRate = 0.000000/100;
return nRate;
}

function updateTotal()
{
var dSubtotal = 0.00;
var dShipping = 0.00;
var dTax = 0.00;
var dTotal = 0.00;

dTaxRate = getTaxRate();

var itemQuantity = [“QTY_1_TxTrayLW_DS”,”QTY_2_TxTrayThumb_DS”,”QTY_3_TxTrayHW_DS”,”QTY_4_TxTrayKT_DS”,”QTY_5_TxTrayLW”,”QTY_6_TxTrayThumb” , “QTY_7_TxTrayHW” , “QTY_8_TxTrayKT” , “QTY_9_DS_Retrofit” , “QTY_10_DS”];
var itemTotal = [“TOTAL_1_TxTrayLW_DS”,”TOTAL_2_TxTrayThumb_DS”,”TOTAL_3_TxTrayHW_DS”,”TOTAL_4_TxTrayKT_DS”,”TOTAL_5_TxTrayLW”, “TOTAL_6_TxTrayThumb” , “TOTAL_7_TxTrayHW” , “TOTAL_8_TxTrayKT” , “TOTAL_9_DS_Retrofit” , “TOTAL_10_DS”];
var itemPrice = [“PRICE_1_TxTrayLW_DS”,”PRICE_2_TxTrayThumb_DS”,”PRICE_3_TxTrayHW_DS”,”PRICE_4_TxTrayKT_DS”,”PRICE_5_TxTrayLW”, “PRICE_6_TxTrayThumb” , “PRICE_7_TxTrayHW” , “PRICE_8_TxTrayKT” , “PRICE_9_DS_Retrofit” , “PRICE_10_DS”];

for(var iCnt=0;iCnt<itemQuantity.length;iCnt++){

// Get the quantity for item ‘Product 3’
nSelIDX = 0;

nSelIDX = document.formSalesOrder.elements[itemQuantity[iCnt]].selectedIndex;
nQty = Number(document.formSalesOrder.elements[itemQuantity[iCnt]].options[nSelIDX].value);
// Update the line total for this item.
// Product subtotal = Quantity * Price
dProdSub = 0.00;
dProdSub = nQty * document.formSalesOrder.elements[itemPrice[iCnt]].value;
document.formSalesOrder.elements[itemTotal[iCnt]].value = formatCurrency(dProdSub);

// Form subtotal
dSubtotal += dProdSub;
}

// Update the TOTAL for the form.
if ( nQty != 0 )
{
dTax += dProdSub * dTaxRate;
dShipping = (getShippingRate()*dSubtotal);
}

dTotal = dSubtotal + dTax + dShipping;
document.formSalesOrder.elements[“editSubtotal”].value=formatCurrency(dSubtotal);
// document.formSalesOrder.elements[“editSH”].value=formatCurrency(dShipping);
document.formSalesOrder.elements[“editTax”].value=formatCurrency(dTax);
document.formSalesOrder.elements[“editTotal”].value=formatCurrency(dTotal);
}

// @@@@@ SALES ORDER SCRIPTS End @@@@@

[/CODE]

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyFeb 11.2009 — Best thing to do is to step through it with a script debugger. Firefox has a free one called Firebug. If you have any MS application development tools, then you probably have the MS Script Debugger for IE, too.
Copy linkTweet thisAlerts:
@neoseeker191authorFeb 11.2009 — Alright I ran the debugger and the iCnt value was always 3 no matter which product I chose. I found this odd, shouldn't it change? And why is it always 3?
Copy linkTweet thisAlerts:
@MrNobodyFeb 11.2009 — [B]iCnt[/B] is not an important variable -- it is only a loop counter. Since you have three products, the loop counter will always be 3 after the loop is complete.
Copy linkTweet thisAlerts:
@neoseeker191authorFeb 11.2009 — But I have more than 3 products, I have 10.
Copy linkTweet thisAlerts:
@neoseeker191authorFeb 11.2009 — But I don't have 3 products I have 10.
Copy linkTweet thisAlerts:
@MrNobodyFeb 11.2009 — OK, then you need to step through it with the debugger and find out why the loop doesn't complete for all 10 of your products. If you want to post a live link to the page on your servers, then I can check it out in my browser with my debugger, too.
Copy linkTweet thisAlerts:
@neoseeker191authorFeb 11.2009 — I did, it's in my first post.
Copy linkTweet thisAlerts:
@MrNobodyFeb 11.2009 — Well, you haven't been checking for your error messages in your browser. An error is generated every time you try to pick a quantity. This also means you didn't do a very good job of stepping through your code with the debugger -- because the debugger would have puked on this statement:
[CODE]nSelIDX = document.formSalesOrder.elements[itemQuantity[iCnt]].selectedIndex;[/CODE]
...and each one like it after that. This also explains why [B]iCnt[/B] is always a 3 -- because that is where your script fails and that represents the field which does not exist in your page: [B]QTY_4_TxTrayKT_DS[/B]. You actually have these:

PRICE_4_TxTrayHW_DS

QTY_4_TxTrayKW_DS

TOTAL_4_TxTrayKT_DS
Copy linkTweet thisAlerts:
@neoseeker191authorFeb 11.2009 — Well I've never used a browser debugger before, I'm not sure how to step through. How do you step through and why does it error out?
Copy linkTweet thisAlerts:
@MrNobodyFeb 11.2009 — See updated post above.
Copy linkTweet thisAlerts:
@neoseeker191authorFeb 11.2009 — What a dumb mistake on my part, simple spelling errors. It works now thank you for your help. By the way how did you step through the javascript, I have firebug with Firefox.
Copy linkTweet thisAlerts:
@MrNobodyFeb 11.2009 — I used Firebug, too.
[LIST=1]
  • [*]Open Firebug

  • [*]Set the Firebug Option to Break on All Errors

  • [*]Refresh your page (to catch errors during page load)

  • [*]Select a quantity

  • [*]Firebug stops your script at the first error
  • [/LIST]
    Copy linkTweet thisAlerts:
    @neoseeker191authorFeb 11.2009 — Okay I think I did that same thing when I was debugging, except I couldnt tell that it stopped on that statement you told me. How did you know that it was the error and that it was looking for a value that didn't exist?
    Copy linkTweet thisAlerts:
    @MrNobodyFeb 11.2009 — See the attached image for an explanation using your page as the example.
    ×

    Success!

    Help @neoseeker191 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...