6 Replies Latest reply on Mar 12, 2013 1:44 AM by try67

    Javascript Help Needed to do calculations with boolean logic

    AffordableParalegal

      I have a form here  https://workspaces.acrobat.com/?d=Cc013FJeVGtcPt*h1U*4Ww

       

      Sample2.JPG

      This is the tricky part of the form. So far, I've set it up so that there are three tiers of mileage options as follows:

       

      1.  STANDARD  If you wanted to charge all miles driven a flat fee you would enter a number here for cost/mile. It could be for all miles or miles less than first limit.

       

      2.  MILEAGE LIMIT 1  All miles driven over what ever you choose, up to but not exceeding the second limit, if there is any, at the set cost/mile.

       

      3.  MILEAGE LIMIT 2  All miles driven over what ever you choose, beyond this limit, if there is any set, at the set cost/mile.

       

      It would be nice to develop javascript that would allow me to do the following:

       

      For miles < first limit, the cost would be the standard fee times the number of miles driven up to the first limit or 1 to < [First Limit], but only if a number is inputted into the [First Limit]

       

      For miles in excess of the [First Limit] or  < [Upper Limit] >= [First Limit], the number of miles would be charged the amount set in cost per miles for the [First Limit]

       

      For the miles in excess of the [Upper Limit] , or >= [Upper Limit]   - the miles cost the number of miles driven times the amount set for cost/mile

       

      It would also be great to set a trap so no negative numbers or zero return an error but are reported as not allowable.

       

      ANy ideas would be welcome

        • 1. Re: Javascript Help Needed to do calculations with boolean logic
          AffordableParalegal Level 1

          9:15 PM EST

          I have a new form https://workspaces.acrobat.com/?d=CmBCPDROCLXR8FChn1KU6w which should be editable.

           

          There appears to be a bug causing the ACTION of the RESET button to default to MOUSE UP instead of allowing me to set it to MOUSE ENTER.  Ayone know why?

           

          Also, although this is not perfect, this is where I am for constructing the mileage logic --

           

          NAMES OF FIELDS

           

          StandardCost

          FirstCost

          SecondCost

          MileageOne

          MileageTwo

          CalcField1  =  [TotalMiles]-[MileageOne]   current calculations

          CalcField2   = [CalcField1]-[MileageTwo]  current calculations

          CalcField3       not used

          TotalStandard_Miles_Cost

          Total_1st_Mileage_Cost

          Total_2nd_Mileage_Cost

          TotalMiles

          Option1  -  this is the checkbox on the form

           

          LOGIC

          If Option1 = true && [MileageOne] = 0 then [TotalStandard_Miles_Cost] = [StandardCost]*[TotalMiles]

          else

          msg "If you want to use different costs for miles traveled, uncheck checkbox and enter mileage limit"

          else

          If option1 = False && [MileageOne] !Null && [MileageTwo] is Null then

          [TotalStandard_Miles_Cost] = [TotalMiles] - [CalcField1] * [StandardCost] + [FirstCost]* [MileageOne]

          else

          If option1 = False && [MileageOne] !Null && [MileageTwo] is !Null then

          [TotalStandard_Miles_Cost] = [TotalMiles] - [CalcField1] * [StandardCost] + [FirstCost]* [MileageOne] + [CalcField2] * [SecondCost]

           

          I am making some assumptions in so much as "&&" = boolean operator AND and the "!" is the NOT operator.

           

          Thanks to those that helped up to this point - I need to turn the above logic into javacript so any help would be wonderful. 

          • 2. Re: Javascript Help Needed to do calculations with boolean logic
            AffordableParalegal Level 1

            3:50 AM EST   OK, I've tried to make in roads on my own so I've tried this

             

            var nCheck_bx = [Check_bx]

            var nMil1 = [MileageOne]

            var nMil2 = [MileageTwo]

            var nTotal = [TotalMiles]

            var nCost1 = [FirstCost]

            var nCost2 = [SecondCost]

             

             

            if (nCheck_bx = no && [nMil1] = Null) then

            [TotalStandard_Miles_Cost] = [nTotal]*[nCost1]

            elseif (nCheck_bx = yes && [nMil1] > 0 && [nMil2] = Null) then

            [TotalStandard_Miles_Cost] = [nMil1]*[StandardCost]+[nMil2]-[nMil1]*[nCost1]

            elseif ( nCheck_bx = yes && [nMil1] > 0 && [nMil2] > 0 ) then

            [TotalStandard_Miles_Cost] = [nMil1]*[StandardCost]+[nMil2]-[nMil1]*[nCost1] + [CalcField3]*[nCost2]

            endif

             

            But got this as errors  --  am I getting closer? 

             

            Acrobat JavaScript Debugger Functions Version 10.0

            Acrobat EScript Built-in Functions Version 10.0

            Acrobat SOAP 10.0

             

            SyntaxError: missing ) after argument list

            1:Field:Calculate

            InvalidSetError: Set not possible, invalid or unknown.

            Event.value:1:Field Total_1st_Mileage_Cost:Calculate

             

            InvalidSetError: Set not possible, invalid or unknown.

            Event.value:1:Field Total_2nd_Mileage_Cost:Calculate

             

            SyntaxError: missing ] after element list

            1:Field:Calculate

            InvalidSetError: Set not possible, invalid or unknown.

            Event.value:1:Field CalcField3:Calculate

             

            SyntaxError: missing ; before statement

            14:Field:Calculate

            • 3. Re: Javascript Help Needed to do calculations with boolean logic
              AffordableParalegal Level 1

              Nothing new today. I was hoping someone would make a constructive comment here but -- guess I have to go back to javascript school.

              • 4. Re: Javascript Help Needed to do calculations with boolean logic
                try67 MVP & Adobe Community Professional

                What you're writing is not JavaScript. I suggest you look for some

                tutorials online on how to write correct JS code.

                 

                 

                On Tue, Mar 12, 2013 at 3:28 AM, AffordableParalegal <

                • 5. Re: Javascript Help Needed to do calculations with boolean logic
                  AffordableParalegal Level 1

                  I know.  You're exactly right.  Why else would I be up at 4 am doing java tutorials?  LOL

                   

                  Here is what I have so far -- not sure where this is going yet .  haven't run any of it.  but it is java - just have to get syntax correct. for instance, in Adobe the Calc fields do a calculation without java - will just calling that field with // Number(document.forms["add"].[CalcField1].value; // retrieve the number and assign it to the variable that I named?

                   

                  <script>

                   

                  function updateMileage()

                  form name = "summary"

                  input type="checkbox" name="nCheck_bx" checked;

                  var aCheck_bx = document.forms["add"].[Check_bx].value;

                  var nMil1 =  Number(document.forms["add"].[MileageOne].value;

                  var nMil2 =  Number(document.forms["add"].[MileageTwo].value;

                  var nTotal =  Number(document.forms["add"].[TotalMiles].value;

                  var nCost1 =  Number(document.forms["add"].[FirstCost].value;

                  var nCost2 =  Number(document.forms["add"].[SecondCost].value;

                  var nStCos = Number(document.forms["add"].[StandardCost].value;

                  var nTSMC =  Number(document.forms["add"].[TotalStandard_Miles_Cost].value;

                  var nCalc1 =  Number(document.forms["add"].[CalcField1].value;

                  var nCalc2 =  Number(document.forms["add"].[CalcField2].value;

                  var nCalc3 =  Number(document.forms["add"].[CalcField3].value;

                   

                  script type="text/javascript"

                  for document.summary.aCheck_bx.length {

                  if (document.test.Check_bx.checked==true && [nMil1] = Null){

                  [nTSMC] = [nTotal]*[nCost1]

                  }

                  else (aCheck_bx = "Yes" && [nMil1] > 0 && [nMil2] = Null) {

                  [nTSMC] = [nMil1]*[StandardCost]+[nMil2]-[nMil1]*[nCost1]

                  }

                  elseif ( nCheck_bx = yes && [nMil1] > 0 && [nMil2] > 0 ) {

                  [nTSMC] = [nMil1]*[StandardCost]+[nMil2]-[nMil1]*[nCost1] + [CalcField3]*[nCost2]

                  }

                  endif

                  <script>

                  • 6. Re: Javascript Help Needed to do calculations with boolean logic
                    try67 MVP & Adobe Community Professional
                    First of all, the language you need to use is JavaScript, not Java. The two are NOT the same.

                    And your code is not valid Java either, by the way...


                    Another thing you have to keep in mind is that you can't use JavaScript objects and methods that were created to use in browsers, as you're not writing code for a browser window, but for a PDF.
                    So this is never going to work:
                    document.forms["add"]

                     

                    In Acrobat you access a form field like this:

                    this.getField("FieldName")