www.webdeveloper.com
Results 1 to 15 of 15

Thread: Order Form Won't Update

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Posts
    53

    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: Link

    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 @@@@@

  2. #2
    Join Date
    Feb 2008
    Posts
    1,666
    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.

  3. #3
    Join Date
    Dec 2007
    Posts
    53
    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?

  4. #4
    Join Date
    Feb 2008
    Posts
    1,666
    iCnt 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.

  5. #5
    Join Date
    Dec 2007
    Posts
    53
    But I have more than 3 products, I have 10.

  6. #6
    Join Date
    Dec 2007
    Posts
    53
    But I don't have 3 products I have 10.

  7. #7
    Join Date
    Feb 2008
    Posts
    1,666
    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.

  8. #8
    Join Date
    Dec 2007
    Posts
    53
    I did, it's in my first post.

  9. #9
    Join Date
    Feb 2008
    Posts
    1,666
    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;
    ...and each one like it after that. This also explains why iCnt is always a 3 -- because that is where your script fails and that represents the field which does not exist in your page: QTY_4_TxTrayKT_DS. You actually have these:

    PRICE_4_TxTrayHW_DS
    QTY_4_TxTrayKW_DS
    TOTAL_4_TxTrayKT_DS
    Last edited by MrNobody; 02-10-2009 at 09:54 PM.

  10. #10
    Join Date
    Dec 2007
    Posts
    53
    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?

  11. #11
    Join Date
    Feb 2008
    Posts
    1,666
    See updated post above.

  12. #12
    Join Date
    Dec 2007
    Posts
    53
    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.

  13. #13
    Join Date
    Feb 2008
    Posts
    1,666
    I used Firebug, too.
    1. Open Firebug
    2. Set the Firebug Option to Break on All Errors
    3. Refresh your page (to catch errors during page load)
    4. Select a quantity
    5. Firebug stops your script at the first error

  14. #14
    Join Date
    Dec 2007
    Posts
    53
    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?

  15. #15
    Join Date
    Feb 2008
    Posts
    1,666
    See the attached image for an explanation using your page as the example.
    Attached Images Attached Images

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles