//***********************************************************************
//*
//* Function  solubility from look tables and data
//*
//* Language:  JavaScript1.1
//*
//* Inputs:  chemical  = string containing chemical name
//*          temperature = temperature in C
//*
//* Returns:
//*          solubility = temperature dependent solubility
//*
//* Limitations:
//*		Method is based upon literature data that has contradictory values for some chemicals
//*         MTBE data from Fischer et al., 2004, Determination of Henry's Law constant for methyl tert-butyl ether (MTBE)...,
//*               Chemosphere, 54(2004) 689-694.
//*         Benzene data from Montgomery, 1996, Groundwater Chemicals, Desk Reference, CRC Press
//*                Stephens and Stphens data judged most accurate.
//*
//* 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:  November 27, 2003
//*
//* Required objects:
//*
//* Required functions:
//*
//* Required scripts:
//*
//***********************************************************************



//
//definition of the getChemical object begins here
//************************************************
function getChemical(c,temp)
{
  this.chemical = c;
  this.temperature = temp;
}


//data
getChemical.prototype.name = new Array;
getChemical.prototype.mw = new Array;
getChemical.prototype.t = new Array;
getChemical.prototype.c = new Array;
getChemical.prototype.tcEntries = new Array;


//benzene data
ii = 0;
getChemical.prototype.name[ii] = "benzene";
getChemical.prototype.mw[ii] = 78.1134;
getChemical.prototype.t[ii] = new Array;
getChemical.prototype.c[ii] = new Array;
getChemical.prototype.t[ii][0] = 0;
getChemical.prototype.t[ii][1] = 10;
getChemical.prototype.t[ii][2] = 20;
getChemical.prototype.t[ii][3] = 30;
getChemical.prototype.t[ii][4] = 40;
getChemical.prototype.c[ii][0] = 1530;
getChemical.prototype.c[ii][1] = 1630;
getChemical.prototype.c[ii][2] = 1750;
getChemical.prototype.c[ii][3] = 1900;
getChemical.prototype.c[ii][4] = 2060;
getChemical.prototype.tcEntries[ii] = 5;


//MTBE data
ii=ii+1;
getChemical.prototype.name[ii] = "mtbe";
getChemical.prototype.mw[ii] = 88.1492;
getChemical.prototype.t[ii] = new Array;
getChemical.prototype.c[ii] = new Array;
getChemical.prototype.t[ii][0] = 0;
getChemical.prototype.t[ii][1] = 10;
getChemical.prototype.t[ii][2] = 20;
getChemical.prototype.t[ii][3] = 30;
getChemical.prototype.t[ii][4] = 40;
getChemical.prototype.c[ii][0] = 73000;
getChemical.prototype.c[ii][1] = 50000;
getChemical.prototype.c[ii][2] = 32000;
getChemical.prototype.c[ii][3] = 21000;
getChemical.prototype.c[ii][4] = 16000;
getChemical.prototype.tcEntries[ii] = 5;

getChemical.prototype.entriesConstant = ii+1;

//Generate the solubilities
function getChemical_getSolubility()
{
  //
  for (i=0;i<this.entriesConstant;i=i+1)
  {
    if (this.chemical==this.name[i])
    {
      //estimated concentration in mg/l
      var result=-1;

      //look for interval containing the temperature
      for (var j=0;j<this.tcEntries[i]-1;j=j+1)
      {
        if (this.t[i][j]<=this.temperature && this.temperature<=this.t[i][j+1])
        {
          var sl = (this.c[i][j+1]-this.c[i][j])/(this.t[i][j]-this.t[i][j+1]);
          result = this.c[i][j] + (this.temperature-this.t[i][j])*sl;      
        }
      }
      return result;
     
    }
  }
}

//Add methods to the prototype
getChemical.prototype.solubility = getChemical_getSolubility;
