
function compute(data,UnitChange,CurrentDate)
{
   var f=document.Gradient;
   var unitdist = "";
   var unithead = "";
   var ucf1 = 1; 
   var ucf2 = 1;

   var grad = 0;

   var x1 = 0;
   var y1 = 0;
   var dist = 0;
   var y2 = 0;
   var h1 = 0;
   var h2 = 0;
   var SiteName = "";
   var SiteDate = "";

   var Ent = new Array();
   var date = new Date();

//note:  only the distance between the points is entered;  (x1,y1) and (x2,y2) are used
//       in the gradient function to calculate the distance.
//       Therefore, the following are used  (x1=0,y1=0) and (x2=distance, y2=0)

    
//results
if (data=="noexample" || data=="save")
   {
      //distances and heads
      dist = Input(f.dist.value);
      h1 = Input(f.h1.value);
      h2 = Input(f.h2.value);
     
      SiteName = f.site.value;
      if (CurrentDate==true) {SiteDate=ShortDate(date); f.date.value = SiteDate;}
      else {SiteDate = f.date.value;}
    
      unitdist = f.DistUnit0.options[f.DistUnit0.selectedIndex].value;
      unithead = f.HeadUnit.options[f.HeadUnit.selectedIndex].value;

      if (data=="save")
       {
        //display the cookie message
        window.open('../../home/storage.html','storage','width=620,height=400,resizable');
        return;

        /*
        //save data for future use
       
        //store the data in an array for saving to cookie
        Ent[0] = SiteDate;
        Ent[1] = SiteName;

        //create the id cookie
        save("id",Ent);

        //save the date using the "new gradient" cookie
        //(items 0,1,6 are redundant and not used here, but allow the same cookie to be used)
        Ent[0] = 1;
        Ent[1] = 0;
        Ent[2] = 0;
        Ent[3] = 0;
        Ent[4] = dist;
        Ent[5] = 0;
        Ent[6] = 1;
        Ent[7] = f.DistUnit0.selectedIndex;
        Ent[8] = f.HeadUnit.selectedIndex;
        Ent[9] = h1;
        Ent[10] = h2;

        //create the "ng" cookie
        save("ng",Ent);
        */
     }  
   }
if (data=="example")
   {
     //example data set
     dist = 2000;
     h1 = 20;
     h2 = 19;
     unithead = "ft";
     unitdist = "ft";
     f.DistUnit0.selectedIndex = 0;
     f.HeadUnit.selectedIndex = 0;
     SiteName = "Example";
     SiteDate = ShortDate(date);    
    }

  if (data=="restore")
     {
      //display the cookie message
      window.open('../../home/storage.html','storage','width=620,height=400,resizable');
      return;

      /*
      //recover each value from the cookie
      SiteDate = restore("id","date");
      SiteName = restore("id","site");
      Unit1 = restore("ng","UnitOut");
      f.DistUnit0.selectedIndex = Unit1;
      dist = restore("ng","x2");
      Unit2 = restore("ng","UnitHead");  
      f.HeadUnit.selectedIndex = Unit2;
      h1 = restore("ng","h1");
      h2 = restore("ng","h2");
      */
     }

if (data=="example" || data=="restore")
  {
     f.site.value = SiteName;
     f.date.value = SiteDate;
     f.dist.value = dist;
     f.h1.value = h1;
     f.h2.value = h2;
     unitdist = f.DistUnit0.options[f.DistUnit0.selectedIndex].value;
     unithead = f.HeadUnit.options[f.HeadUnit.selectedIndex].value;
  }

if (data=="clear") {clearAll(f); return;}


    

  //length unit conversions;
  ucf1 = 1;
  if (UnitChange=='distance')
    {
     if (f.DistUnit0.options[f.DistUnit0.selectedIndex].value=="ft") {ucf1=1/0.3048; unitdist="ft";}
     if (f.DistUnit0.options[f.DistUnit0.selectedIndex].value=="m") {ucf1=0.3048; unitdist="m";}
    }
 
  dist = dist*ucf1;
  f.dist.value = Num2String(dist,4,-10,10,-6,6);
 
  //Head unit conversions;
  ucf1 = 1;
  if (UnitChange=='head')
    {
     if (f.HeadUnit.options[f.HeadUnit.selectedIndex].value=="ft") {ucf1=1/0.3048; unithead="ft";}
     if (f.HeadUnit.options[f.HeadUnit.selectedIndex].value=="m") {ucf1=0.3048; unithead="m";}
    }
 
  h1 = h1*ucf1;
  h2 = h2*ucf1;     
  f.h1.value = Num2String(h1,4,-10,10,-6,6);
  f.h2.value = Num2String(h2,4,-10,10,-6,6);


  ucf2 = 1;
  //additional unit conversion factor needed if the two units are not the same
  if (unithead=="ft" && unitdist=="m") {ucf2=0.3048;}
  if (unithead=="m" && unitdist=="ft") {ucf2=1/0.3048;}

  //calculate the gradient
  grad = Gradient (x1,y1,dist,y2,h1,h2);      
  grad = grad*ucf2;
  
  var GradValue = Num2String(grad,3,-15,15,-6,6);

  f.grad.value = GradValue;

  if (data=="save")
   {
    /*
    //save the calculated gradient to the gradient virtual cookie
    Ent[0] = GradValue;
    save("gr",Ent);
    */
   }
}
<!-- end gradient_1.js -->