
<!--
function G3ns(data,UnitChange,CurrentDate)
  {
    //***********************************************************************
    //*
    //* Function G3ns calculates the gradient using coordinates entered directly
    //*          G3ns uses three points to determine the gradient by fitting a plane
    //*          to the heads
    //*
    //* Language:  JavaScript1.1
    //*
    //* Inputs: 
    //*  data                  string signal for type of processing values:
    //*                        noexample--perform calculation using user data
    //*                        example--paste example data and calculate
    //*                        example2--paste second example data set and calculate
    //*                        clear--clear all data and exit
    //*                        save--save data to cookie file
    //*                        restore--read data from cookie file
    //*  UnitChage             signal to change units
    //*                        if none no unit change
    //*                        if head change head unit
    //*                        if map change map unit
    //*                        if field change field unit
    //*  CurrentDate           boolean signal to paste current date in the date field
    //*
    //* Returns:
    //*  nothing
    //*
    //* Limitations: Only for use with gradient3.htm
    //*
    //* Author:
    //*  Dr. Jim Weaver
    //*  Hydrologist
    //*  Regulatory Support Branch
    //*  Ecosystems Research Division
    //*  National Exposure Research Laboratory
    //*  United States Environmental Protection Agency
    //*  960 College Station Road
    //*  Athens, Georgia 30605
    //*  weaver.jim@epa.gov
    //* 
    //*
    //* Reference:
    //*  Flanagan, D., 1997, JavaScript: The Definitive Guide, 2ed, O'Reilly and Associates
    //*
    //* Created: 12-16-1999 created from newgradient.htm
    //*          02-04-2001 cookies removed
    //*          02-10-2001 tenths added as map scale options
    //*          07-28-2001 created G3ns from G3
    //*          05-08-2002 added option for unconfined aquifer
    //*
    //* Required functions: clearAll, Input, ShortDate, save, restore, Num2String, determine
    //*
    //* Required scripts: input.js, digits.js, sdate.js, cookie.js, back.js, determine.js, clear.js
    //*
    //***********************************************************************
   var f=document.Gradient;
   var unithead = "";
   var unitdist0 = "";
   var unitdist1 = "";
   var aquifertype = "";
   var ucf1 = 1; 
   var ucf2 = 1;
   var ucf3 = 1;
   var ucfdist = 1;
   var ucfhead = 1;
   var SiteDate = "";
   var SiteName = "";
   var date = new Date();
   var Ent = new Array();
   var a = 0;
   var b = 0;
   var c = 0;
   var xx = new Array();
   var yy = new Array();
   var zz = new Array();
   var ww = new Array();

   var grad = 0;
   var gradstring = "";
   var degrees = 0;

   var x1 = 0;
   var y1 = 0;
   var x2 = 0;
   var y2 = 0;
   var h1 = 0;
   var h2 = 0;
  


    
//results
if (data=="noexample"  || data=="save")
   {
      //distances and heads
      xx[0] = Input(f.x1.value);
      yy[0] = Input(f.y1.value);
      xx[1] = Input(f.x2.value);
      yy[1] = Input(f.y2.value);
      xx[2] = Input(f.x3.value);
      yy[2] = Input(f.y3.value);
      zz[0] = Input(f.h1.value);
      zz[1] = Input(f.h2.value);
      zz[2] = Input(f.h3.value);
 
            

 
     
      SiteName = f.site.value;
      if (CurrentDate==true) {SiteDate=ShortDate(date); f.date.value = SiteDate;}
      else {SiteDate = f.date.value;}
    
        
     unithead = f.HeadUnit.options[f.HeadUnit.selectedIndex].value
     unitdist0 = f.DistUnit0.options[f.DistUnit0.selectedIndex].value    
     aquifertype = f.AquiferType.options[f.AquiferType.selectedIndex].value

     if (data=="save") 
       {
        //display the cookie message
        window.open('../../home/storage.html','storage','width=620,height=400,resizable');
        return;

         /*
         //store the data in an array for saving to a cookie
         Ent[0] = SiteDate;
         Ent[1] = SiteName;

         //create the id cookie
         save("id",Ent);
   
        //save the "new gradient" data
        Ent[0] = 1.;
        Ent[1] = f.DistUnit0.selectedIndex
        Ent[2] = xx[0];
        Ent[3] = yy[0];
        Ent[4] = xx[1];
        Ent[5] = yy[1];
        Ent[6] = 1.;
        Ent[7] = 0.;
        Ent[8] = f.HeadUnit.selectedIndex
        Ent[9] = zz[0];
        Ent[10] = zz[1];
        Ent[11] = fn;
        Ent[12] = f.InDen.selectedIndex;
        Ent[13] = fn1;
        Ent[14] = f.InDen1.selectedIndex;

        //create the "ng" cookie
        save("ng",Ent);

        //create the 'g3' cookie
        Ent[0] = xx[2];
        Ent[1] = yy[2];
        Ent[2] = zz[2];
        Ent[3] = fn2;
        Ent[4] = f.InDen2.selectedIndex;
        Ent[5] = fn3;
        Ent[6] = f.InDen3.selectedIndex;
        Ent[7] = fn4;
        Ent[8] = f.InDen4.selectedIndex;
        Ent[9] = fn5;
        Ent[10] = f.InDen5.selectedIndex;
        Ent[11] = fn6;
        Ent[12] = f.InDen6.selectedIndex;
        save ('g3',Ent);
        */
       }  

   }
if (data=="example" || data=="example2" || data=="example3" || data=="example4")
   {

     //example data set
     fn = 0;
     xx[0] = 100;
     yy[0] = 0;
     xx[1] = 0;
     yy[1] = 100;
     xx[2] = 100;
     yy[2] = 100;
     zz[0] = 20;
     zz[1] = 20;
     zz[2] = 19.75;

     unithead = "ft";
     f.HeadUnit.selectedIndex = 0;
     unitdist0 = "ft";
     unitdist1 = "ft";
     f.DistUnit0.selectedIndex = 0;
     aquifertype = "h";
     f.AquiferType.selectedIndex = 0;
    
       
     SiteName = "Map Gradient Ex 1";
     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");


      
      f.DistUnit0.selectedIndex = restore("ng","UnitIn");
      xx[0] = restore("ng","x1");
      yy[0] = restore("ng","y1");
      xx[1] = restore("ng","x2");
      yy[1] = restore("ng","y2");
      xx[2] = restore('g3','x3');
      yy[2] = restore('g3','y3');
  
      
      f.HeadUnit.selectedIndex = restore("ng","UnitHead");  
      zz[0] = restore("ng","h1");
      zz[1] = restore("ng","h2");
      zz[2] = restore('g3','h3');
 



      unithead = f.HeadUnit.options[f.HeadUnit.selectedIndex].value
      unitdist0 = f.DistUnit0.options[f.DistUnit0.selectedIndex].value    
     
      */  
     }

if ( data=="restore" || data=="example" || data=="example2" || data=="example3" || data=="example4")
  {
    f.date.value = SiteDate; 
    f.site.value = SiteName;  
    f.x1.value = xx[0];
    f.y1.value = yy[0];
    f.x2.value = xx[1];
    f.y2.value = yy[1];
    f.x3.value = xx[2];
    f.y3.value = yy[2];
    f.h1.value = zz[0];
    f.h2.value = zz[1];
    f.h3.value = zz[2];
  

  }

if (data=="clear") {clearAll(f); return;}



  
  

  //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";
      }
    }
 
  zz[0] = zz[0]*ucf1;
  zz[1] = zz[1]*ucf1;   
  zz[2] = zz[2]*ucf1; 

  f.h1.value = Num2String(zz[0],getDigits(zz[0],4),-10,10,-6,6);
  f.h2.value = Num2String(zz[1],getDigits(zz[1],4),-10,10,-6,6);
  f.h3.value = Num2String(zz[2],getDigits(zz[2],4),-10,10,-6,6);

 

 


  //Distance unit conversions
  ucf2 = 1;
  if (UnitChange=="map")
    {
     if (f.DistUnit0.options[f.DistUnit0.selectedIndex].value=="ft") 
     { 
      //ucf2=1/0.3048; 
      unitdist0="ft";
     }
     if (f.DistUnit0.options[f.DistUnit0.selectedIndex].value=="m") 
     {
      //ucf2=0.3048; 
      unitdist0="m";
     }
    }

  //Distance unit conversions
  xx[0] = xx[0]*ucf2;
  yy[0] = yy[0]*ucf2;
  xx[1] = xx[1]*ucf2;
  yy[1] = yy[1]*ucf2;
  xx[2] = xx[2]*ucf2;
  yy[2] = yy[2]*ucf2;
  
 
 
 



  //do all calculation in meters
  ucfdist = 1;
  if (unitdist0=="ft") {ucfdist=0.3048;}
  ucfhead = 1;
  if (unithead=="ft") {ucfhead=0.3048;} 

  //apply the scale factor to the distances


  //convert quantities before performing gradient calculation
  xx[0] = xx[0]*ucfdist;
  yy[0] = yy[0]*ucfdist;
  xx[1] = xx[1]*ucfdist;
  yy[1] = yy[1]*ucfdist;
  xx[2] = xx[2]*ucfdist;
  yy[2] = yy[2]*ucfdist;


  zz[0] = zz[0]*ucfhead;
  zz[1] = zz[1]*ucfhead;
  zz[2] = zz[2]*ucfhead;

  //use head squared for an unconfined aquifer
  if (aquifertype=="h2")
  {
    zz[0] = zz[0]*zz[0];
    zz[1] = zz[1]*zz[1];
    zz[2] = zz[2]*zz[2];
  }

 
  ww[0] = 1;
  ww[1] = 1;
  ww[2] = 1;

  //calculate the gradient
  //solve the 3 x 3 matrix for the plane passing through all the points
  //equation is ax + by + c = z
  var denominator = determinant(xx,yy,ww);
  if (denominator!=0)
   {
     //constants defining the plane
     var numerator = determinant(zz,yy,ww);
     a = numerator/denominator;
     numerator = determinant(xx,zz,ww);
     b = numerator/denominator;
     numerator = determinant(xx,yy,zz);
     c = numerator/denominator;
     //the gradient
     grad = -Math.sqrt(a*a + b*b);
     //the angle
     //the following combinations are opposite of what you think they should be 
     //because the flow is in the direction of minus the gradient
     if (a<0 && b<0) 
      {
       //solution in the first quadrant
       degrees = Math.atan(a/b)*180/Math.PI;
      }
     if (a<0 && b>0)
      {
       //solution in the second quadrant
       degrees = Math.atan(-b/a)*180/Math.PI + 90;
      }
     if (a>0 && b>0)
      {
       //solution in the third quadrant
       degrees = Math.atan(-a/-b)*180/Math.PI + 180;
      }
     if (a>0 && b<0)
      {
       //solution in the forth quadrant
       degrees = Math.atan(b/-a)*180/Math.PI + 270;
      }
   

   }
  else {f.grad.value=""; f.degrees.value=""; return;}
 


 
  gradstring = Num2String(Math.abs(grad),4,-10,10,-4,4); 
  f.grad.value = gradstring;
  f.degrees.value = Num2String(degrees,4,-10,10,-4,4);

  if (data == "save") 
   {
     /*
     //save the gradient result
     Ent[0] = gradstring;
     save ("gr",Ent);
     */
   }

}
//-->
<!-- end gradient3ns_1.js -->