4 Replies Latest reply on May 18, 2013 6:47 PM by Neobus78

    Javascript - Working with Dates, Selection Boxes & Pro-Rated Amounts to Calculate Costs

    Neobus78

      Hi, I'm looking for some help. I'm new to Javascript and working off my limited knowledge. Below are three (3) items I need help figuring out.

       

      ---JavaScript 1: Today---

       

      I have a Document Level Javascript that gets the current date and automatically enters it into a field. Below is the example:

       

      var f = this.getField("Today");f.value = util.printd("mm/dd/yyyy", new Date());

       

      Problem: Each time I open the PDF, the date gets updated.

       

      Need: I would like for the date to only be updated if the field is blank (empty).

       

      ---JavaScript 2: Registration Fee---

       

      On this same form (application) I would like to calculate TWO costs. The first cost is mandatory registration cost, however the amount is prorated based on the month the application is filled out. Below is the pro-rated amounts based on the remaining months in the year:

       

      Months Remaining     Fee

       

                12                       $15.00

                11                       $13.75

                10                       $12.50

                9                         $11.25

                8                         $10.00

                7                         $8.75

                6                         $7.50

                5                         $6.25

                4                         $5.00

                3                         $3.75

                2                         $2.50

                1                         $1.25

       

      Note: The cost per month is $1.25. You are required to pay for the current month plus any remaining months.

       

      So let's assume that you fill the application out in May 15th (05/18/2013). There are eight (8) months remaining in the year [May - Dec], so the pro-rated cost is $10.00 (See chart above).

       

      For this calculation, I need to create a Document Level Javascript that would get the date from the field we used above called "Today" and calculate the "Registration Fee" based on the pro-rated amount. Then I would enter the cost into two fields (Dollars & Cents) as "10 00". (See below).

      Registration Fee Form Example.jpg

      Form Example: $ [Text Field - Combo 2 characters] . [Text Field - Combo 2 characters] ---> "$ 10 . 00"

       

      ---JavaScript 3: Subscription Fee---

       

      The next fee is an optional fee for a magazine subscription. On the form, the user will check a box (Named: "Subscription") if they want to subscribe to this magazine. This is similar to the example above in that the costs is based on a pro-rated amount. Below is the pro-rated amounts based on the remaining months in the year:

       

      Months Remaining               Fee

       

                12                       $12.00

                11                       $11.00

                10                       $10.00

                9                         $9.00

                8                         $8.00

                7                         $7.00

                6                         $6.00

                5                         $5.00

                4                         $4.00

                3                         $3.00

                2                         $2.00

                1                         $0.00

       

      Note: The cost per month is $1.00. Unlike the previous example, you are ONLY required to pay for the remaining months.

       

      So let's assume that you fill the application out in May 15th (05/18/2013). There are eight (7) months remaining in the year [Jun - Dec], so the pro-rated cost is $7.00 (See chart above).

       

      For this calculation, I need to create a Document Level Javascript: If the user checks a box (Named: "Subscription") requesting the subscription, then get the date from the field we used above called "Today" and calculate the "Subscription Fee" based on the pro-rated amount. I would enter the cost into two fields (Dollars & Cents) as "10 00". (Same as above).

       

      Thanks,

       

      Stephen

        • 1. Re: Javascript - Working with Dates, Selection Boxes & Pro-Rated Amounts to Calculate Costs
          try67 MVP & Adobe Community Professional

          1.

          var f = this.getField("Today");

          if (f.value=="") f.value = util.printd("mm/dd/yyyy", new Date());

           

          2.

          function calculateRegistrationFee() {

               var s = this.getField("Today").value;

               if (s!="") {

                    var d = util.scand("mm/dd/yyyy", s);

                    return (12-(d.getMonth())) * 1.25;

               }

               return "";

          }

           

          You'll still need to split the returned value into 2 parts in order to populate the different fields.

           

          3. Your description doesn't seem to make much sense...You write

          There are eight (7) months remaining in the year [Jun - Dec], so the pro-rated cost is $7.00 (See chart above).

          So which one is it, 7 or 8? And why is the fee for 1 month remaining zero? Shouldn't it be 1?

          • 2. Re: Javascript - Working with Dates, Selection Boxes & Pro-Rated Amounts to Calculate Costs
            Neobus78 Level 1

            3. Sorry that was a typo, what I meant to say is: There are seven (7) months remaining in the year [Jun - Dec], so the pro-rated cost is $7.00.

             

            The fee for 1 month remaining is zero. The reason for this is: that it takes about one month to start sending out magazines, thus we won't charge for the current month. Also, the subscribtion is yearly without automatic renewals. So if they sign up in Dec, they are not elgible for the magazine.

             

            Thanks,

             

            Stephen

             

            • 3. Re: Javascript - Working with Dates, Selection Boxes & Pro-Rated Amounts to Calculate Costs
              try67 MVP & Adobe Community Professional

              Something like this should do, then:

               

              function calculateSubscriptionFee() {

                   var s = this.getField("Today").value;

                   if (s!="") {

                        var d = util.scand("mm/dd/yyyy", s);

                        return (((12-d.getMonth())==1) ? 0 : 12-d.getMonth());

                   }

                   return "";

              }

              • 4. Re: Javascript - Working with Dates, Selection Boxes & Pro-Rated Amounts to Calculate Costs
                Neobus78 Level 1

                Two questions:

                 

                1. For "function calculateRegistrationFee", how do I format the return value into 2 parts in order to populate the different fields below. Let's use this example, see screen shot below:

                 

                Registration Fee Form Example 1.jpg

                 

                2. For "function calculateSubscriptionFee", how do I format the return value into 2 parts (like question 1) & adjust for check box variable. If the user checks a box (named: "Subscription") requesting the subscription, then calculate cost. Else cost is $0.00. See screen shot:

                 

                Subscription Fee Form Example 2.jpg

                 

                Thanks,

                 

                Stephen