20 Replies Latest reply on Sep 2, 2015 4:00 AM by newsusy3

    PDF Javascript Help

    newsusy3

      I have a registration pdf form that I need help with when it comes to Javascript.

       

      There are 3 types of registrants each with different pricing: Member $100, Nonmember $150 and Student $60

      I need a discount of 10% applied if the total registrants is 4 or more.

       

      I had it working but it only applied to each type of registrant, so only worked if 4 or more was input for Member, and only applied if 4 or more was input for Nonmember when I realized that the discount needs to be applied should there be a combination of types of registrants that total 4 or more.

       

      I put this in at document level:

       

      function calcPriceWithDiscount(pricemember, qtymember) {

           var price = +this.getField(pricemember).value;

           var qty = +this.getField(qtymember).value;

           var discount = 0;

           if (qty>3) discount = 10;

           event.value = (price*qty) * ((100-discount)/100);

      }

       

      Can anyone help me with this??

       

      Much appreciated.

        • 1. Re: PDF Javascript Help
          gkaiseril MVP & Adobe Community Professional

          A function is a defined action with or without parameters. It does nothing until explicitly called.

           

          calcPriceWithDiscount("MemberPriceFieldName" "MemberQuantityFieldName");

          • 2. Re: PDF Javascript Help
            newsusy3 Level 1

            Yes, sorry - I had that as well for each individual registrant category and everything is working fine.

             

            calcPriceWithDiscount("pricemember", "qtymember");

             

            etc.

             

            But I need the discount applied when the total number of ALL category registrants is 4 or more.

            That's where I need help.

            • 3. Re: PDF Javascript Help
              newsusy3 Level 1

              Maybe it would be easier to understand what I'm trying to do with this chart:

               

              chart.jpg

              • 4. Re: PDF Javascript Help
                gkaiseril MVP & Adobe Community Professional

                Some users make the choice not to program the field names into their functions or parameters for functions.

                 

                So you might try using the values of the fields for the parameters and then returning the computed value and not setting a field or event value. This would allow one to use a variable to gather the discount for each group and sum them. The one can do what ever is needed to be done using the variable's value.

                 

                function calcPriceWithDiscount(nPrice, nQty) {
                     var nDiscount = 0;
                  if (nQty > 3) nDiscount = 10;
                  return nDiscount * nPrice;
                }


                // custom calculation for subtotal;
                var nPriceMemeber = this.getField("pricemember").value;
                var nQtyMember = this.getField("qtymember").value;
                var nPriceNonMemeber = this.getField("pricenonmember").value;
                var nQtyNonMember = this.getField("qtynonmember").value;
                var nPriceStudent = this.getField("pricestuden").value;
                var nQtyStudent = this.getField("qtystuden").value;

                event.value = calcPriceWithDiscount(nPriceMemeber, nQtyMemeber) +calcPriceWithDiscount(nPriceNonMemeber, nQtyNonMemeber) + calcPriceWithDiscount(nStudent, nQtyStudent);
                // end custom calculation for subtotal;

                 


                • 5. Re: PDF Javascript Help
                  newsusy3 Level 1

                  Thanks so much for your time but it's not working and being that I really don't understand Javascript it's probably my error.

                  The subtotal field doesn't even show up.

                  Could you please take a look at the document, and possibly help me out?

                   

                  http://www.rtso.ca/wp-content/uploads/2015/08/Inspire-2015-Registration-Form-Aug31-working .pdf

                   

                  I input this in the document javascript:

                  function calcPriceWithDiscount(nPrice, nQty) {

                       var nDiscount = 0;

                    if (nQty > 3) nDiscount = 10;

                    return nDiscount * nPrice;

                  }

                   

                  And input this in the "subtotal" custom caculation script:

                   

                  // custom calculation for subtotal;

                  var nPriceMember = this.getField("PriceMember").value;

                  var nQtyMember = this.getField("QtyMember").value;

                  var nPriceNonMember = this.getField("PriceNonMember").value;

                  var nQtyNonMember = this.getField("QtyNonMember").value;

                  var nPriceStudent = this.getField("PriceStudent").value;

                  var nQtyStudent = this.getField("QtyStudent").value;

                   

                  event.value = calcPriceWithDiscount(nPriceMember, nQtyMember) +calcPriceWithDiscount(nPriceNonMember, nQtyNonMember) + calcPriceWithDiscount(nStudent, nQtyStudent);

                  // end custom calculation for subtotal;

                   

                   

                  Thanks in advance.

                  • 6. Re: PDF Javascript Help
                    Bernd Alheit Adobe Community Professional & MVP

                    Did you see the errors in the Debugger?

                     

                    In the calculations you use field names like "pricenonmember", but the field name is e.g. "PriceNonMember".

                     

                    Acrobat Javascript is case sensitive.

                    • 7. Re: PDF Javascript Help
                      newsusy3 Level 1

                      Yes - I noticed that yesterday. I gather I'm still inputting something incorrectly as it still isn't working.

                       

                      Here's the latest version:

                      http://www.rtso.ca/wp-content/uploads/2015/09/Inspire-2015-Registration-Form-Aug31-secondt ry.pdf

                      • 8. Re: PDF Javascript Help
                        Bernd Alheit Adobe Community Professional & MVP

                        I get following error:

                         

                        8:AcroForm:Subtotal:Calculate

                        ReferenceError: nStudent is not defined

                        • 9. Re: PDF Javascript Help
                          newsusy3 Level 1

                          Thanks - I've corrected that and now the field shows up but still isn't calculating. Any ideas?

                           

                          http://www.rtso.ca/wp-content/uploads/2015/09/Inspire-2015-Registration-Form-Aug31-secondt ry.pdf

                          • 10. Re: PDF Javascript Help
                            Bernd Alheit Adobe Community Professional & MVP

                            The calculating is working now:

                            Bild1.jpg

                            • 11. Re: PDF Javascript Help
                              newsusy3 Level 1

                              The subtotal field isn't calculating correctly:

                              4 x $150 = $600

                              The subtotal field shouldn't be $1,500, plus there are 4 registrants, so the javascript that was input for the subtotal field should actually apply a 10% discount.

                              So... it should read $540.

                               

                              I'm gathering either the document level javascript isn't working, or the subtotal javascript isn't working.

                               

                              Document level:

                               

                              function calcPriceWithDiscount(nPrice, nQty) {

                                   var nDiscount = 0;

                                if (nQty > 3) nDiscount = 10;

                                return nDiscount * nPrice;

                              }

                               

                              Subtotal Custom Calculation script:

                               

                              // custom calculation for subtotal;

                              var nPriceMember = this.getField("PriceMember").value;

                              var nQtyMember = this.getField("QtyMember").value;

                              var nPriceNonMember = this.getField("PriceNonMember").value;

                              var nQtyNonMember = this.getField("QtyNonMember").value;

                              var nPriceStudent = this.getField("PriceStudent").value;

                              var nQtyStudent = this.getField("QtyStudent").value;

                              event.value = calcPriceWithDiscount(nPriceMember, nQtyMember) +calcPriceWithDiscount(nPriceNonMember, nQtyNonMember) + calcPriceWithDiscount(nPriceStudent, nQtyStudent);

                              // end custom calculation for subtotal;

                              • 12. Re: PDF Javascript Help
                                Bernd Alheit Adobe Community Professional & MVP

                                Your function returns the value of 10 * 150, this gives 1500. This is correct.

                                 

                                Where did you calculate a discount?

                                • 13. Re: PDF Javascript Help
                                  newsusy3 Level 1

                                  Hmmmm - not sure. I just took the code from what was suggested above in the thread.

                                   

                                  I thought the discount calculation is what was put in the document level but not sure.

                                  I'm gathering the "10" should be ".10"??

                                  Or how do you input a percentage in javascript?

                                  • 14. Re: PDF Javascript Help
                                    Bernd Alheit Adobe Community Professional & MVP

                                    0.10 are 10%

                                    • 15. Re: PDF Javascript Help
                                      newsusy3 Level 1

                                      Thank you so much.

                                       

                                      I see now that 10% is only applying to the $150 and not the sum of $600. So it's showing up as $15 and not $60 when inputting 4 registrants.

                                      I was hoping to see the proper subtotal with the discount applied if 4 or more registered, and the proper subtotal should 3 or less register - not just the amount that was discounted.

                                      Maybe I have to add some hidden fields.

                                       

                                      Also, nothing is working when under 4 registrants are input. Ugh - I can't win as I muddle my way through this!

                                       

                                      I guess the javascript, although working, is completely wrong for what I'm trying to do.

                                       

                                      http://www.rtso.ca/wp-content/uploads/2015/09/Inspire-2015-Registration-Form-Aug31-secondt ry.pdf

                                      • 16. Re: PDF Javascript Help
                                        Bernd Alheit Adobe Community Professional & MVP

                                        $15 is the discount for one person.

                                        • 17. Re: PDF Javascript Help
                                          newsusy3 Level 1

                                          Yes it is. So it's not automatically applying the discount for 4 people, when 4 have been input, which is the whole point of this exercise.

                                           

                                          I need to show the sum to be paid, whether it's 2 registrants or 4, but apply a 10% discount to the subtotal, if the number of registrants is 4 and above.

                                          • 18. Re: PDF Javascript Help
                                            gkaiseril MVP & Adobe Community Professional

                                            "Transparency" is a very popular buzz word. Your approach does not clearly show the discounted price or the amount being charged at the discounted price.

                                             

                                            You might want to consider using a custom JavaScript for each total fees for each membership price. So one could do something like

                                             

                                            var nPriceMember = this.getField("PriceMember").value;

                                            var nQtyMember = this.getField("QtyMember").value;

                                            var nTotal = nPriceMember * nQtyMember;

                                            event.value = nTotal - calcPriceWithDiscount(nTotal, nQtyMember);

                                             

                                            This would show the actual price being charged for each type of member. Then just add up the total amount for each membership class.

                                            • 19. Re: PDF Javascript Help
                                              Bernd Alheit Adobe Community Professional & MVP

                                              Why didn't you use the function calcPriceWithDiscount from your first posting?

                                              • 20. Re: PDF Javascript Help
                                                newsusy3 Level 1

                                                Thanks all. My son knows javascript and he came over and figured it out within minutes.

                                                He put in 2 hidden fields, for Total and Quantity, and input the below javascipt into the subtotal custom calculation.

                                                He says it may not be pretty but it works!

                                                 

                                                var nTotal = this.getField("HiddenTotal").value;

                                                var nQty = this.getField("HiddenQty").value;

                                                var nDiscount = 0;

                                                if (nQty > 3){

                                                nDiscount = 0.9 * nTotal;

                                                }else{

                                                nDiscount = nTotal;

                                                }

                                                event.value = nDiscount;

                                                 

                                                I'm sure it's a lot easier when you are sitting in front of the form.