6 Replies Latest reply on Mar 12, 2013 3:36 AM by Test Screen Name

    Javascript help

    AffordableParalegal Level 1

      What am I forgetting?  Will these calculations actually work? Link to form - https://workspaces.acrobat.com/?d=CmBCPDROCLXR8FChn1KU6w

      The idea is to place this code into a single cost field so that it will calculate costs depending on whether a checkbox is checked or not (returns "yes" or "no")

       

      Any ideas?  Also, for some unknown reason the RESET button keeps defaulting to the Mouse Up action - will not let me set it to Mouse Enter.  Why?

       

      var nCheck_bx = [Check_bx]

      var nMil1 = [MileageOne]

      var nMil2 = [MileageTwo]

      var nTotal = [TotalMiles]

      var nCost1 = [FirstCost]

      var nCost2 = [SecondCost]

      var nTSMC = [TotalStandard_Miles_Cost]

       

       

      if (nCheck_bx = "No" && [nMil1] = Null) {

      [nTSMC] = [nTotal]*[nCost1]

      }

      else (nCheck_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

        • 1. Re: Javascript help
          George_Johnson MVP & Adobe Community Professional

          There are a lot of things wrong with the code, and the document you linked to hasn't been published/shared properly so I can't examine it to help understand what it is you're trying to do. Is this a form you created in Acrobat or LiveCycle Designer?

          1 person found this helpful
          • 2. Re: Javascript help
            AffordableParalegal Level 1

            I'm sorry George, I thought that one was accessible. I am such a newbie with this - only second or third day here. LOTS of things wrong ..OMG . . then I am definitely in need of help -- or a psychologist LOL.  I will try to link to another document. I just save it as a pdf right? No special save technique?

             

            https://workspaces.acrobat.com/?d=i5IK6t5gCPGEz8OpS*i-vw

             

            OK - maybe this will work.

            • 3. Re: Javascript help
              AffordableParalegal Level 1

              I found this example which is about as close as I can come to the kind of syntax I may need.  I am going to assume I don't need html tags but just the <script> before and at the end? for starters. Correct? Next I see there is the use of eval function and Calc (can't find a syntax for this)  Do I have to convert the fields/objects to numbers? So I guess I can't just multiply one field by another? Ihave to call the number using Number() function?

               

              <html>

              <head>

              <title></title>

              <script type="text/javascript">

              function Calc(){

              //Validate here to see if the fields have a value

              //if yes, Validate here to see if the fields have a numeric value

              //if yes do the stuff below

              //repeat step for each box (each box must have a numeric value)


              VField1=eval(document.calcform.field1.value)

              VField2=eval(document.calcform.field2.value)

              VField3=eval(document.calcform.field3.value)

              document.calcform.field4output.value=VField1+VFiel d2+VField3

              }

              </script>

              </head>

              <body>

              <form name="calcform">

              <input type=text name="field1" size="12"onchange="javascript:Calc();"><br>

              <input type=text name="field2" size="12"onchange="javascript:Calc();"><br>

              <input type=text name="field3" size="12"onchange="javascript:Calc();"><br>

              <input type=text name="field4output" size="12" readonly><br>

              <input type=button value="Add" onclick="javascript:Calc();" >

              </form>

              </body>

              </html>

               

              Message was edited by: AffordableParalegal

              • 4. Re: Javascript help
                Bernd Alheit Adobe Community Professional & MVP

                Don't mix browser Javascript and Acrobat Javascript. You may read the documentation for Acrobat Javascript.

                • 5. Re: Javascript help
                  Test Screen Name Most Valuable Participant

                  You really can't just take an HTML script, delete a couple of things, and get a PDF script. There is nothing in this that could be salvaged. You need to move to the point where you understand each and every line in the script, what it does and why it is there (even if you couldn't write that line from scratch). 

                  • 6. Re: Javascript help
                    Test Screen Name Most Valuable Participant

                    But let's look at your questions, they are interesting.

                     

                    Next I see there is the use of eval function

                     

                    The eval function is standard JavaScript, but is really for special purposes. Avoid it until you hit a wall that can only be solved that way.

                     

                    and Calc (can't find a syntax for this) 

                     

                    Now, Calc is a function used in this script. Notice at the top

                    function Calc(){

                    ... some lines which won't work in Acrobat...

                    }

                     

                    That defines a function called Calc, which can now be used to do work. You would write just Calc() to do that. (This example calls Calc() but not in a way valid in Acrobat). A function is just like writing out the stuff in the function every time it is used ("called") but is there to save time and avoid the mistakes which come from retyping. It's a really useful thing for beginners to learn early.

                     

                    Here's an example of a function that calculates the average of three numbers

                     

                    function AverageOf3(x,y,z)

                    {

                         var sum = x+y+z ;

                         var average = sum / 3 ;

                         return average;

                    }

                     

                    Suppose you wanted the average of fields called f1, f2, and zoom in Acrobat. You could write

                     

                    var f1=this.getField("f1") ;

                    var f2=this.getField("f2") ;

                    var zoom=this.getField("zoom") ;

                    var theaverage = AverageOf3(f1,f2,zoom) ;