
<!--
function MassBalance(data,ChangeUnit,CurrentDate)
{
  var f=document.MassDistribution;
   
  var cw = 0;
  var co = 0;
  var ca = 0;
  var cs = 0;
  var mf = 0;
  var mwoil = 0;
  var mw = 0;

  var kh = 0;
  var kd = 0;
  var ko = 0;

  var sol = 0;
  var rsol = 0;

  var foc = 0;
  var koc = 0;
  var por = 0;
  var bd = 2.65;

  var sw = 0;
  var sa = 0;
  var so = 0;

  var mtotal = 0;
  var mwater = 0;
  var mnapl = 0;
  var mair = 0;
  var msoil = 0;

  var note = "";
  mchemical = new Array(4);
  var fuel = "";
  var chemical = "";

  var unitdist = "";
  var ucf1 = 1;
  var smear = 0;
  var radius = 0;
  var initialMass = 0;
  var initialVolume = 0;
  var bulkPartition = 0;

  var vt = 0;
  var ucfvol = 1;
  var ucfmass = 1;
  var ucfgal = 1;

  var SiteName = "";
  var SiteDate = "";
  var date0 = new Date();
  var Ent = new Array();


  if (data=="noexample" || data == "save")
    {
       //site name and date
      SiteName = f.site.value;
      if (CurrentDate=='1') {SiteDate=ShortDate(date0); f.date.value=SiteDate;}
      else {SiteDate = f.date.value;}

      //get the fraction organic carbon
      foc = Input(f.foc.value)

      //get the porosity
      por = Input(f.poro.value)

      //get the water saturation
      sw = Input(f.sw.value)

      //get the NAPL saturation
      so = Input(f.so.value)

      unitdist = f.unit3.options[f.unit3.selectedIndex].value

      //get the smear zone thickness
      smear = Input(f.Smear.value)

      //get the lens radius
      radius = Input(f.Radius.value)

      if (data == "save") 
       {

         //display the cookie message
         window.open('../../home/storage.html','storage','width=620,height=400,resizable');
         return;



         /*
         //set and save the cookie values
         Ent[0] = SiteDate;
         Ent[1] = SiteName;
         //save the id virtual cookie
         save('id',Ent);

         //retardation virtual cookie
         Ent[0] = foc;
         Ent[1] = koc;
         save('rd',Ent);

         //chemical virtual cookie
         Ent[0] = f.chemical.selectedIndex;
         save('ch',Ent);

         //store the fuel name
         Ent[0] = f.fuel.selectedIndex;
         save ('fu',Ent);

         //mass virtual cookie
         Ent[0] = sw;
         Ent[1] = so;
         Ent[2] = smear;
         Ent[3] = radius;
         Ent[4] = 2.*radius;
         save('ms',Ent);

         //Darcy virtual cookie
         Ent[0] = -1000;
         Ent[1] = -1000;
         Ent[2] = f.unit3.selectedIndex;
         Ent[3] = -1000;
         save('dc',Ent);
         */
        }
    }
  if (data=='example') 
    {
      //example data set
      SiteName = "Mass Balance Example";
      SiteDate = ShortDate(date0);
      f.fuel.selectedIndex = 1;
      f.chemical.selectedIndex = 1;

      foc = 0.0001;
      por = 0.25
      bd = 2.65;
      sw = 0.25;
      so = 0.30;
      radius = 10;
      smear = 2;
      unitdist = "ft";
      f.unit3.selectedIndex = 0;
    }
  if (data=="restore")
   {

    //display the cookie message
    window.open('../../home/storage.html','storage','width=620,height=400,resizable');
    return;



    /*
    //restore the cookie values
    SiteDate = restore('id','date');
    SiteName = restore('id','site');

    f.chemical.selectedIndex = restore('ch','chemical');
    f.fuel.selectedIndex = restore('fu','fuel');
   
    foc = restore('rd','foc');
    koc = restore('rd','koc');
    por = restore('po','porosity');

    sw = restore('ms','WaterSaturation');
    so = restore('ms','NAPLSaturation');
    smear = restore('ms','SmearZone');
    radius = restore('ms','LensRadius');

    f.unit3.selectedIndex = restore('dc','UnitDistance');
    */
   }

   if (data=="example" || data=="restore")
   {
     //set the form entries
     f.date.value = SiteDate;
     f.site.value = SiteName;
     
     f.foc.value = foc;
     f.poro.value = por;
     f.dens.value = bd;
     f.sw.value = sw;
     f.so.value = so;
     f.Radius.value = radius;
     f.Smear.value = smear;
   }

  chemical = f.chemical.options[f.chemical.selectedIndex].value;
  unitdist = f.unit3.options[f.unit3.selectedIndex].value;


   if (data=='clear') 
    {

      f.site.value = "";
      f.date.value = "";
      f.fuel.selectedIndex = -1;
      f.chemical.selectedIndex = -1;

      f.foc.value = "";
      f.poro.value = "";
      f.dens.value = "";
      f.sw.value = "";
      f.so.value = "";
      f.Radius.value = "";
      f.Smear.value = "";
      unitdist = "ft";
      f.unit3.selectedIndex = 0;
   
      f.TotalMass.value = "";
      f.volume.value = "";
      f.WaterOut.value = "";
      f.NAPLOut.value = "";
      f.AirOut.value = "";
      f.SolidOut.value = "";

      f.outsol.value= "";
      f.AvgMw.value = "";
      f.note.value = ""; 
      f.massfraction.value = "";
      f.esoutput.value = "";
      f.revisesol.value = "";
     return;
    }
 
    f.fuel.selectedIndex = CheckSelection (f.fuel); 
    fuel = f.fuel.options[f.fuel.selectedIndex].value;
    chemical = f.chemical.options[f.chemical.selectedIndex].value;


 
         f.WaterOut.value = "*";
         f.NAPLOut.value = "*";
         f.AirOut.value = "*";
         f.SolidOut.value = "*";

         f.CWaterOut.value = "*";
         f.CNAPLOut.value = "*";
         f.CAirOut.value = "*";
         f.CSolidOut.value = "*";

  

         if (fuel=="other")
           {
             //let the user enter a mass fraction instead of using the internal data
             mf = parseFloat(f.massfraction.value)
             mf = mf/100
             if (isNaN(mf)) {mf=0; f.massfraction.value="";}
             note = "User specified mass fraction"
             //let the user enter a average molecular weight
             mwoil = parseFloat(f.AvgMw.value);
             if (isNaN(mwoil)) {mwoil=105; f.AvgMw=105;}
           }
         else
           {  
             //get the value from the selected fuel
             mf = composition(fuel,chemical);
             mwoil = fuelmw(fuel);
           }


         //get the solubility for the selected chemical
         //solubilities are expressed in mg/l
         sol = solubility(chemical);

         //allow for revised solubility
         rsol = sol;
         rsol = parseFloat(f.revisesol.value)
         if (rsol!=sol)
           {
             if (isNaN(rsol)) {rsol=sol; f.revisesol.value = sol;}
             else {sol = rsol; f.outsol.value = rsol;}
           }




         //get the molecular weight for the selected chemical
         mw = mole(chemical);
 
         //prevent effective solubility from being larger than
         //than the pure compound solubilities
         mwoil = mf*mw + (1-mf)*mwoil;
   
         if (sol>0)
           {
           //NAPL/water partition coeffcient
           ko = (0.75/sol)*1000*1000*mwoil/mw;
           }
           
         //get the koc
         koc = getKOC(chemical);
         //get the henry's law
         kh = getKH(chemical);

         kd = foc*koc;

         //note on the fuel type
         note = fuelnote(fuel);
      
         //calculate the bulk density
         bd = bd*(1-por);

         //calculate the masses in mg/l
         sa = 1 - sw - so;

         //calculate the concentration in water from the
         //1) distribution coefficients
         //2) initial mass in oil
         //3) assumed density of 0.75 g/ml
         //3-20-2003
        
         //convert g/ml density to mg/l
         var ucfDensity = 1000*1000;

         var ucfLength = 1.;
         if (unitdist=="ft") {ucfLength = 0.3048*0.3048*0.3048;} 

         
         initialVolume = radius*radius*smear*ucfLength*Math.PI*so*por;
         initialMass = initialVolume*mf*(mwoil/mw)*0.75*ucfDensity;
         bulkPartition = (sw + ko*so + kh*sa + kd*bd/por);
         cw = initialMass/bulkPartition/initialVolume;

         //cw = mf*(mwoil/mw)*sol; 

  
         mwater = cw*sw*por;
         mnapl = cw*ko*so*por;
         mair = cw*kh*sa*por;
         msoil = cw*kd*bd;

         if (so>0) co = cw*ko;
         if (sa>0) ca = cw*kh;
         if (kd*bd/por>0) cs = cw*kd;

         if (mwater<0) {mwater=0;}
         if (mnapl<0) {mnapl=0;}
         if (mair<0) {mair=0;}
         if (msoil<0) {msoil=0;}
     

         mtotal = mwater + mnapl + mair + msoil;

         if (mtotal>0)
           {
            f.WaterOut.value = Num2String(100*mwater/mtotal,4,-15,15,-6,6);
            f.NAPLOut.value = Num2String(100*mnapl/mtotal,4,-15,15,-6,6);
            f.AirOut.value = Num2String(100*mair/mtotal,4,-15,15,-6,6);
            f.SolidOut.value = Num2String(100*msoil/mtotal,4,-15,15,-6,6);
            f.CWaterOut.value = Num2String(cw,4,-15,15,-6,6);
            f.CNAPLOut.value = Num2String(co,4,-15,15,-6,6);
            f.CAirOut.value = Num2String(ca,4,-15,15,-6,6);
            f.CSolidOut.value = Num2String(cs,4,-15,15,-6,6);
           }
         //set the output text boxes
         f.outsol.value=Num2String(sol,2,-15,15,-6,6);
         f.AvgMw.value = Num2String(mwoil,2,-15,15,-6,6);
         f.note.value = note

         if (mf>=0) {f.massfraction.value = mf*100;}
         else {f.massfraction.value = "";}

         if (mf<0) {f.esoutput.value = "data not available";}
         else {f.esoutput.value = Num2String(cw,4,-15,15,-6,6);}

         
     
 f.revisesol.value = "";   

//length unit conversions;
ucf1 = 1;
if (ChangeUnit=='distance')
    {
     if (f.unit3.options[f.unit3.selectedIndex].value=="ft") {ucf1=1/0.3048; unitdist="ft";}
     if (f.unit3.options[f.unit3.selectedIndex].value=="m") {ucf1=0.3048; unitdist="m";}
    }
 
smear = smear*ucf1;
f.Smear.value = Num2String(smear,4,-15,15,-6,6) + " " + unitdist;
radius = radius*ucf1;
f.Radius.value = Num2String(radius,4,-15,15,-6,6) + " " + unitdist;
 
 //lens volume
 vt = Math.PI*radius*radius*smear;
 
 //print out the volume in gallons
 f.volume.value = Num2String(vt,3,-15,15,-6,6);

 if (unitdist=="ft") {ucfgal=7.48}
 if (unitdist=="m") {ucfgal=7.48/0.3048/0.3048/0.3048}
 f.volume.value = Num2String(vt*ucfgal*por*so,3,-15,15,-6,6);

 //convert volume to cubic meters
 ucfvol = 1;
 if (unitdist=="ft") {ucfvol=0.3048*0.3048*0.3048;}

 //convert mass (mtotal) from mg/l to kg/m3 
 //unit conversion factor = 1000/1000/1000
 ucfmass = 0.001;

 //total mass
 mass = vt*ucfvol*mtotal*ucfmass;
 f.TotalMass.value = Num2String(mass,3,-15,15,-6,6);
 



  
}
//-->
<!-- end mass_1.js -->