/* Javascript file for parachute accuracy landing */
/* (c) 2000 Chris Webb */
/* May 2002 changes to work with NS6 */

   airSpeed = 20
   brakes = 0
   sinkRate = 0
   yy = 400
   xx = 350
   total = 0
   round = 5
   stallCount = 0
   fastSinkCount = 0
   state = 0  /* 0 = landed, 1 = tow, 2 = flying, 3 = just landed */
   firstTime = true


/* StartTow: Starts the tow for next flight */

   function StartTow() {
   if (state == 0) {
      brakes = 10
      yy = 400
      xx = 600
      round = round +1
      state = 1
      if (round == 6) {
        document.myform.r1.value = ""
        document.myform.r2.value = ""
        document.myform.r3.value = ""
        document.myform.r4.value = ""
        document.myform.r5.value = ""
        document.myform.rt.value = ""
        total = 0
        round = 1
      }
    }
   }


/* ReleaseTow: Stops the tow and releases the tow line */

   function ReleaseTow() {
      if (state == 1) {
         brakes = 5
         sinkRate = 1
         state = 2
       }
   }


/* airspeed: calculates air speed as a function of brake position */

   function airspeed(brakeP) {
      airspd = ((100 - brakeP) *12/100)+7.8
      return airspd
   }


/* Sinkrate: calculates sinkrate as a function of brake position */
/* if brakes at max then allow time for stall to develop before setting fast sink rate */

   function sinkrate(brakeP,stallC,state) {
      if (state == 1) {
         offset = 3.5
      } else {
         offset = 0
      }
      if (brakeP < 60) {
         sink = 1.5 - (brakeP*1/60) - offset
      } else {
         sink = (brakeP*2/40) - 2.5 - offset
      }
      if ((brakeP == 100) && (stallC > 15)) sink = 15
      return sink
   }


/* Windspeed: generates windspeed for current parachute altitude */
/* decreasing Wind gradient with height plus small random gusts */

   function windSpeed(height,screenH) {
      wind = ((screenH-height) * 10 /250) + Math.random()*5
      wind = wind + Math.random()*4
      if (wind > 16) wind = 16
      return wind
   }

/* Brake position change functions for buttons on form */

   function brakeu() {
      brakes = brakes - 3
      if (brakes < 0) brakes =0
   }

   function brakefu() {
      brakes = brakes - 8
      if (brakes < 0) brakes =0
   }

   function braked() {
      brakes = brakes + 3
      if (brakes >100) brakes =100

   }

   function brakefd() {
      brakes = brakes + 8
      if (brakes >100) brakes =100

   }


/* Main function */
   function moveIt() {

      if (firstTime) {
         if (document.all) {
             /* IE   */
             maxHeight = document.body.clientHeight-15;
             maxWidth = document.body.clientWidth;
             document.all.note.style.visibility = "hidden";
             document.all.landpad.style.pixelTop = maxHeight+10;
             document.all.landpad.style.visibility = "visible";
             document.all.logo.style.pixelLeft = maxWidth- 338 - 20;
             document.all.logo.style.visibility = "visible";
             xx = maxWidth - 80;
             yy = maxHeight - 80;
             document.all.mover.style.pixelTop = yy;
             document.all.mover.style.pixelLeft = xx;
             document.all.mover.style.visibility = "visible";
          } else {
             /* Netscape   */
             maxHeight = window.innerHeight-15;
             maxWidth = window.innerWidth;

             if (document.getElementById) {
               /* NS6 */
               noteObj = document.getElementById("note").style
               landpadObj = document.getElementById("landpad").style
               logoObj = document.getElementById("logo").style
               moverObj = document.getElementById("mover").style
             } else {
               /* NS4 */
               noteObj = document.note
               landpadObj = document.landpad
               logoObj = document.logo
               moverObj = document.mover
             }
             
             noteObj.visibility = "hidden";
             landpadObj.top = maxHeight+10;
             landpadObj.visibility = "visible";
             logoObj.left = maxWidth - 338 - 20;
             logoObj.visibility = "visible";
             xx = maxWidth - 80;
             yy = maxHeight - 80;
             moverObj.top = yy;
             moverObj.left = xx-20;
             moverObj.visibility = "visible";
         }
         screenHeight = maxHeight
         firstTime = false
      }

      if (Math.abs(200 - xx) < 24) {
         groundH = maxHeight-85
      } else {
         groundH = maxHeight-80
      }

      /* if flying or being towed then */
      if ((state == 2) || (state == 1)) {
         /* calculate height of paracute*/
         yy=yy+sinkRate
         if (yy > maxHeight-80) yy = maxHeight-80
         if (yy < 0) yy = 0

         /* calculate windspeed at paracute height and airspeed */
         wind = windSpeed(yy,screenHeight)
         airSpeed = airspeed(brakes)

         if (brakes > 90) {
            stallCount = stallCount + 1
         } else {
            stallCount = 0
         }

         if (brakes > 85) {
            fastSinkCount = fastSinkCount + 1
         } else {
            fastSinkCount = 0
         }

         /* calculate current sink rate, ground speed and ground position */
         sinkRate = sinkrate(brakes,stallCount,state)
         groundSpeed = (airSpeed - wind)
         xx=xx-groundSpeed / 5
         if (xx < 1) xx = maxWidth 
         if (xx > maxWidth) xx = 0

         /* auto release if towed too far or wind speed too high */         
         if ((state == 1) && ((xx < 300) || (groundSpeed < -2))) {
            state = 2
            brakes = 5
         }

         /* update position of paracute */
         if (document.all) {
            /* IE   */
            document.all.mover.style.pixelTop = yy
            document.all.mover.style.pixelLeft = xx
         } else {
            /* Netscape   */
            moverObj.top = yy
            moverObj.left = xx
         }

         /* update dynamic data display */
         document.myform.wspeed.value = Math.round(wind)
         document.myform.aspeed.value = Math.round(airSpeed)
         document.myform.gspeed.value = Math.round(groundSpeed)
         document.myform.bposn.value = Math.round(brakes)
         document.myform.Tposn.value = Math.round(Math.abs((200 - xx)*10))/100

         /* if flying and touched down set state to just landed */
         if ((state==2) && (yy >= groundH)) {
            state = 3
         }
      } else {
         if (state == 3) {
         message = ""
         if (groundSpeed > 10) {
            message = message + "Owwwch!! Landing Far Too Fast!\n"
         }

         if ((sinkRate > 1.5) && (sinkRate <= 2.5) && (fastSinkCount > 10)) {
            message = message + "Heavy Landing - Beware of the sink rate when applying lots of brake!\n"
         }

         if (sinkRate > 2.5) {
            message = message + "You made a big hole in the ground - Know the Stall point of your glider!\n"
         }

         score = Math.round(Math.abs(200 - xx)*10)/100

         if (score == 0) {
            message = message + "Dead centre! Zero point Zero meters\n"
         }
         if ((score > 0) && (score <= 0.2)) {
            message = message + "Pad Score "+score+" m\n"
         }
         if ((score > 0.2) && (score <= 2.5)) {
            message = message + "On The Mat "+score+" m\n"
         }

         if (score > 20) {
            message = message + "Looks like you need a plenty of practice\n"
         }

         if (score > 2.5) {
            score = 2.5
            message = message + "Default score awarded "+score+" m\n"
         }

         alert(message)

         total = total + score
         if (round == 1) document.myform.r1.value = score
         if (round == 2) document.myform.r2.value = score
         if (round == 3) document.myform.r3.value = score
         if (round == 4) document.myform.r4.value = score
         if (round == 5) document.myform.r5.value = score
         document.myform.rt.value = Math.round(total*100)/100
         state = 0
         }
      }
      if (document.all) {
        /* IE */
        setTimeout("moveIt()",30)
      } else {
        if (document.getElementById) {
        /* NS6 */
          setTimeout("moveIt()",.1)
        } else {
        /* NS4 */
          setTimeout("moveIt()",10)
        }
      }
   }


/* end of script file */
