function checkBrowser()
 {
  if (navigator.appName != "Microsoft Internet Explorer" && parseFloat(navigator.appVersion) < 5)
    {
	 alert("The Javascript on this web page requires MS Internet Explorer or Netscape 6 to " +
	        "work properly.  You will be returned to the WebScience Home Page. You appear to " +
			"be using " + navigator.appName + " " + parseFloat(navigator.appVersion));
	 window.location  = "http://webscience.nmsu.edu";
	}
 }

var densityWaterSI = 1.0;         // Mg/m3
var densityWaterSI2 = 1000.0;     // kg/m3
var densityWaterMetric = 1.0;     // g/cm3
var densityWaterEnglish = 62.43;  // lb/ft3

function setBulkDensity()
 {
   document.P.bdsi.value = Math.round(document.P.bdsi.value * 100) / 100;
   if (document.P.bdsi.value > 1.9)
     {
	   alert("Soil Bulk Density is rarely greater than 1.9 Mg/m3");
	   document.getElementById("myTable").rows[2].cells[0].style.background = "red";
	 }
    else if (document.P.bdsi.value < 1.0)
     {
	   alert("Soil Bulk Density is rarely less than 1.0 Mg/m3");
	   document.getElementById("myTable").rows[2].cells[0].style.background = "red";
	 }
   document.P.bdsi2.value = document.P.bdsi.value * densityWaterSI2;
   document.P.bdm.value = document.P.bdsi.value;
   document.P.bde.value = Math.round(document.P.bdsi.value * densityWaterEnglish * 100)/ 100;
   calcPorosityFromBdPd();
   calcVolWaterFromPorosityRelSaturation();
   calcAirFilledPorosityFromVolWaterPorosity();
   calcMassWaterFromVolWaterBulkDensity();
   calcMixedWaterFromMassWaterBulkDensity();
 }

function setParticleDensity()
 {
   document.P.pdsi.value = Math.round(document.P.pdsi.value * 100) / 100;
   if (document.P.pdsi.value > 2.8)
     {
	   alert("Soil Particle Density is rarely greater than 2.8 Mg/m3");
	   document.getElementById("myTable").rows[2].cells[1].style.background = "red";
	 }
    else if (document.P.pdsi.value < 2.6)
     {
	   alert("Soil Particle Density is rarely less than 2.6 Mg/m3");
	   document.getElementById("myTable").rows[2].cells[1].style.background = "red";
	 }
   document.P.pdsi2.value = document.P.pdsi.value * densityWaterSI2;
   document.P.pdm.value = document.P.pdsi.value;
   document.P.pde.value = Math.round(document.P.pdsi.value * densityWaterEnglish * 100) / 100;
   calcPorosityFromBdPd();
   calcVolWaterFromPorosityRelSaturation();
   calcAirFilledPorosityFromVolWaterPorosity();
   calcMassWaterFromVolWaterBulkDensity();
   calcMixedWaterFromMassWaterBulkDensity();
 }

function setPorosity()
 {
   document.P.psi.value = Math.round(document.P.psi.value * 1000) / 1000;
   if (document.P.psi.value > 1.0)
	 {
	   alert("Porosity cannot be greater than 1.0 m3/m3 ... reseting table");
	   resetTable();
	 }
    else
	  {
	    if (document.P.psi.value > 0.65)
	      {
	        alert("Porosity is rarely greater than 0.65 m3/m3");
	        document.getElementById("myTable").rows[2].cells[2].style.background = "red";
	      }
        document.P.pm.value = document.P.psi.value;
        document.P.pe.value = document.P.psi.value;
        calcBdFromPorosityPd();
        calcVolWaterFromPorosityRelSaturation();
        calcAirFilledPorosityFromVolWaterPorosity();
        calcMassWaterFromVolWaterBulkDensity();
        calcMixedWaterFromMassWaterBulkDensity();
	  }
 }

function setVolWaterContent()
 {
   document.P.vwsi.value = Math.round(document.P.vwsi.value * 1000) / 1000;
   if (document.P.vwsi.value > 1.0)
	 {
	   alert("Volumetric Water Content cannot be greater than 1.0 m3/m3 ... reseting table.");
	   resetTable();
	 }
    else
	  {
	    if (document.P.vwsi.value > document.P.psi.value)
	      {
	        alert("Volumetric Water Content cannot be greater than Porosity.");
	        document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	      }
         else
          if (document.P.vwsi.value > 0.65)
	        {
	          alert("Volumetric Water Content is rarely greater than 0.65 m3/m3.");
	          document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	        }
        document.P.vwm.value = document.P.vwsi.value;
        document.P.vwe.value = document.P.vwsi.value;
        calcMassWaterFromVolWaterBulkDensity();
        calcRelSaturationFromVolWaterPorosity();
        calcMixedWaterFromMassWaterBulkDensity();
        calcAirFilledPorosityFromVolWaterPorosity();
		calcRelAirFilledPorosityFromPorosityAirFilledPorosity();
	  }
 }

function setMassWaterContent()
 {
   document.P.mwsi.value = Math.round(document.P.mwsi.value * 1000) / 1000;
   if (document.P.mwsi.value > 1.0)
     {
	   alert("Mass Water Content is rarely greater than 1.0 Mg/Mg." +
	         "This can only happen when the bulk density is less than 1.0 Mg/m3.");
	   document.getElementById("myTable").rows[8].cells[1].style.background = "red";
	 }
    else if (document.P.mwsi.value > 0.60)
	  {
	   alert("Mass Water Content is rarely greater than 0.60 Mg/Mg." +
	         "This implies the bulk density is very low (i.e., close to 1.0 Mg/m3).");
	   document.getElementById("myTable").rows[8].cells[1].style.background = "red";
	  }
   document.P.mwsi2.value = document.P.mwsi.value;
   document.P.mwm.value = document.P.mwsi.value;
   document.P.mwe.value = document.P.mwsi.value;
   calcVolWaterFromMassWaterBulkDensity();
   calcAirFilledPorosityFromVolWaterPorosity();
   calcRelAirFilledPorosityFromPorosityAirFilledPorosity();
   calcRelSaturationFromVolWaterPorosity();
   calcMixedWaterFromMassWaterBulkDensity();
 }

function setRelSaturation()
 {
   if (document.P.rssi.value > 1.0)
     {
	   alert("Relative Saturation cannot be greater than 1.0 Mg/Mg ..." +
	         "reseting table.");
	   resetTable();
	 }
	else
	  {
        document.P.rsm.value = document.P.rssi.value;
        document.P.rse.value = document.P.rssi.value;
        calcVolWaterFromPorosityRelSaturation();
        calcAirFilledPorosityFromVolWaterPorosity();
        calcRelAirFilledPorosityFromPorosityAirFilledPorosity();
		calcMassWaterFromVolWaterBulkDensity();
        calcMixedWaterFromMassWaterBulkDensity();
	  }
 }

function setMixedWaterContent()
 {
   if (document.P.mxwsi.value > 1.0)
	 {
	   alert("Mixed Unit Water Content cannot be greater than 1.0 m3/m3 ... reseting table.");
	   resetTable();
	 }
    else
	  {
	    if (document.P.mxwsi.value > document.P.psi.value)
	      {
	        alert("Mixed Unit Water Content cannot be greater than Porosity.");
	        document.getElementById("myTable").rows[14].cells[1].style.background = "red";
	      }
	     else if (document.P.mxwsi.value > 0.65)
	       {
	         alert("Mixed Unit Water Content is rarely greater than 0.65 m3/m3.");
	         document.getElementById("myTable").rows[14].cells[1].style.background = "red";
	       }
	    document.P.mxwsi2.value = document.P.mxwsi.value * densityWaterSI2;
        document.P.mxwm.value = document.P.mxwsi.value;
        document.P.mxwe.value = Math.round(document.P.mxwsi.value * densityWaterEnglish * 100) / 100;
        calcVolWaterFromMixedWater();
        calcAirFilledPorosityFromVolWaterPorosity();
        calcRelAirFilledPorosityFromPorosityAirFilledPorosity();
		calcMassWaterFromVolWaterBulkDensity();
        calcRelSaturationFromVolWaterPorosity(); 
	  }
 }

function setAirFilledPorosity()
 {
   if (document.P.afpsi.value > 1.0)
	 {
	   alert("Air-Filled Porosity cannot be greater than 1.0 m3/m3 ... reseting table.");
	   resetTable();
	 }
    else
	  {
	    if (document.P.afpsi.value > document.P.psi.value)
	      {
	        alert("Air-Filled Porosity cannot be greater than Porosity.");
	        document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	      }
	     else if (document.P.afpsi.value > 0.65)
	       {
	         alert("Air-Filled Porosity is rarely greater than 0.65 m3/m3.");
	         document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	       }
        document.P.afpm.value = document.P.afpsi.value;
        document.P.afpe.value = document.P.afpsi.value;
        calcVolWaterFromPorosityAirFilledPorosity();
        calcRelSaturationFromVolWaterPorosity();
        calcMassWaterFromVolWaterBulkDensity();
        calcMixedWaterFromMassWaterBulkDensity();
        calcRelAirFilledPorosityFromPorosityAirFilledPorosity();  
	  }
 }

function setRelAirFilledPorosity()
 {
   if (document.P.rafpsi.value > 1.0)
     {
	   alert("Relative Air-Filled Porosity cannot be greater than 1.0 Mg/Mg ..." +
	         "reseting table.");
	   resetTable();
	 }
    else
	  {
        document.P.rafpm.value = document.P.rafpsi.value;
        document.P.rafpe.value = document.P.rafpsi.value;
        calcAirFilledPorosityFromPorosityRelAirFilledPorosity()
        calcRelSaturationFromRelAirFilledPorosity();
        calcVolWaterFromPorosityRelSaturation();
        calcMassWaterFromVolWaterBulkDensity();
        calcMixedWaterFromMassWaterBulkDensity();
      }
 }

function calcVolWaterFromMassWaterBulkDensity()
 {
   document.P.vwsi.value = Math.round(document.P.mwsi.value * document.P.bdsi.value / densityWaterSI * 1000) / 1000;
   if (document.P.vwsi.value > 1.0)
	 {
	   alert("Volumetric Water Content cannot be greater than 1.0 m3/m3 ..." +
	         "some other value in the table is too high");
	   document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	 }
    else if (document.P.vwsi.value > 0.65)
	   {
	     alert("Volumetric Water Content is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	   }
      else if (document.P.vwsi.value < 0.0)
	    {
	      alert("Volumetric Water Content cannot be less than 0.0 m3/m3.");
	      document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	    }
   document.P.vwm.value = document.P.vwsi.value;
   document.P.vwe.value = document.P.vwsi.value;
 }

function calcVolWaterFromPorosityRelSaturation()
 {
   document.P.vwsi.value = Math.round(document.P.psi.value * document.P.rssi.value * 1000) / 1000;
   if (document.P.vwsi.value > 1.0)
	 {
	   alert("Volumetric Water Content cannot be greater than 1.0 m3/m3 ..." +
	         "some other value in the table is too high");
	   document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	 }
    else if (document.P.vwsi.value > 0.65)
	   {
	     alert("Volumetric Water Content is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	   }
      else if (document.P.vwsi.value < 0.0)
	    {
	      alert("Volumetric Water Content cannot be less than 0.0 m3/m3.");
	      document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	    }
   document.P.vwm.value = document.P.vwsi.value;
   document.P.vwe.value = document.P.vwsi.value;
 }

function calcVolWaterFromPorosityAirFilledPorosity()
 {
   document.P.vwsi.value = Math.round((document.P.psi.value - document.P.afpsi.value) * 1000) / 1000;
   if (document.P.vwsi.value > 1.0)
	 {
	   alert("Volumetric Water Content cannot be greater than 1.0 m3/m3 ..." +
	         "some other value in the table is too high");
	   document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	 }
    else if (document.P.vwsi.value > 0.65)
	   {
	     alert("Volumetric Water Content is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	   }
      else if (document.P.vwsi.value < 0.0)
	    {
	      alert("Volumetric Water Content cannot be less than 0.0 m3/m3.");
	      document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	    }
   document.P.vwm.value = document.P.vwsi.value;
   document.P.vwe.value = document.P.vwsi.value;
 }

function calcMassWaterFromVolWaterBulkDensity()
 {
   document.P.mwsi.value = Math.round(document.P.vwsi.value * densityWaterSI / document.P.bdsi.value * 1000) / 1000 ;
   if (document.P.mwsi.value < 0.0)
     {
	   alert("Mass Water Content cannot be less than 0.0 Mg/Mg");
	   document.getElementById("myTable").rows[8].cells[1].style.background = "red";
	 }
   document.P.mwsi2.value = document.P.mwsi.value;
   document.P.mwm.value = document.P.mwsi.value;
   document.P.mwe.value = document.P.mwsi.value;
 }

function calcRelSaturationFromVolWaterPorosity()
 {
   document.P.rssi.value = Math.round(document.P.vwsi.value / document.P.psi.value * 1000) / 1000;
   if (document.P.rssi.value > 1.0)
     {
	   alert("Relative Saturation cannot be greater than 1.0.");
	   document.getElementById("myTable").rows[8].cells[3].style.background = "red";
	 }
    else if (document.P.rssi.value < 0.0)
      {
	    alert("Relative Saturation cannot be less than 0.0.");
	    document.getElementById("myTable").rows[8].cells[3].style.background = "red";
	  }
   document.P.rsm.value = document.P.rssi.value;
   document.P.rse.value = document.P.rssi.value;
 }

function calcRelSaturationFromRelAirFilledPorosity()
 {
   document.P.rssi.value = Math.round((1.0 - document.P.rafpsi.value) * 1000) / 1000;
   if (document.P.rssi.value > 1.0)
     {
	   alert("Relative Saturation cannot be greater than 1.0.");
	   document.getElementById("myTable").rows[8].cells[3].style.background = "red";
	 }
    else if (document.P.rssi.value < 0.0)
     {
	   alert("Relative Saturation cannot be less than 0.0.");
	   document.getElementById("myTable").rows[8].cells[3].style.background = "red";
	 }
   document.P.rsm.value = document.P.rssi.value;
   document.P.rse.value = document.P.rssi.value;
 }
function calcPorosityFromBdPd()
 {
   document.P.psi.value = Math.round((1 - document.P.bdsi.value / document.P.pdsi.value) * 1000) / 1000;
   if (document.P.psi.value > 1.0)
	 {
	   alert("Porosity cannot be greater than 1.0 m3/m3 ... reseting table.");
	   resetTable();
	 }
    else if (document.P.psi.value > 0.65)
	   {
	     alert("Porosity is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[2].cells[2].style.background = "red";
	   }
      else if (document.P.psi.value < 0.0)
	     {
	       alert("Porosity cannot be less than 0.0 m3/m3.");
	       document.getElementById("myTable").rows[2].cells[2].style.background = "red";
	     }
   document.P.pm.value = document.P.psi.value;
   document.P.pe.value = document.P.psi.value;
 }

function calcBdFromPorosityPd()
 {
   document.P.bdsi.value = Math.round((1 - document.P.psi.value) * document.P.pdsi.value * 100) / 100;
   if (document.P.bdsi.value > 1.9)
     {
	   alert("Soil Bulk Density is rarely greater than 1.9 Mg/m3.");
	   document.getElementById("myTable").rows[2].cells[0].style.background = "red";
	 }
    else if (document.P.bdsi.value < 1.0)
      {
	    alert("Soil Bulk Density is rarely less than 1.0 Mg/m3.");
	    document.getElementById("myTable").rows[2].cells[0].style.background = "red";
	  }
     else if (document.P.bdsi.value < 0.0)
       {
	     alert("Soil Bulk Density cannot be less than 0.0 Mg/m3.");
	     document.getElementById("myTable").rows[2].cells[0].style.background = "red";
	   }
   document.P.bdsi2.value = document.P.bdsi.value * densityWaterSI2;
   document.P.bdm.value = document.P.bdsi.value;
   document.P.bde.value = Math.round(document.P.bdsi.value * densityWaterEnglish * 100)/ 100;
 }

function calcMixedWaterFromMassWaterBulkDensity()
 {
   document.P.mxwsi.value = Math.round(document.P.mwsi.value * document.P.bdsi.value * 1000) / 1000;
   if (document.P.mxwsi.value > 1.0)
	 {
	   alert("Mixed Unit Water Content cannot be greater than 1.0 m3/m3.");
	     document.getElementById("myTable").rows[14].cells[1].style.background = "red";
	 }
    else if (document.P.mxwsi.value > 0.65)
	   {
	     alert("Mixed Unit Water Content is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[14].cells[1].style.background = "red";
	   }
    else if (document.P.mxwsi.value < 0.0)
	   {
	     alert("Mixed Unit Water Content cannot be less than 0.0 m3/m3.");
	     document.getElementById("myTable").rows[14].cells[1].style.background = "red";
	   }
   document.P.mxwsi2.value = document.P.mxwsi.value * densityWaterSI2;
   document.P.mxwm.value = document.P.mxwsi.value;
   document.P.mxwe.value = Math.round(document.P.mxwsi.value * densityWaterEnglish * 100) / 100;
 }

function calcVolWaterFromMixedWater()
 {
   document.P.vwsi.value = Math.round(document.P.mxwsi.value / densityWaterSI * 1000) / 1000;
   if (document.P.vwsi.value > 1.0)
	 {
	   alert("Volumetric Water Content cannot be greater than 1.0 m3/m3 ..." +
	         "some other value in the table is not reasonable.");
	   document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	 }
    else if (document.P.vwsi.value > 0.65)
	   {
	     alert("Volumetric Water Content is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	   }
      else if (document.P.vwsi.value < 0.0)
	     {
	       alert("Volumetric Water Content cannot be less than 0.0 m3/m3.");
	       document.getElementById("myTable").rows[8].cells[2].style.background = "red";
	     }
   document.P.vwm.value = document.P.vwsi.value;
   document.P.vwe.value = document.P.vwsi.value; 
 }

function calcRelAirFilledPorosityFromPorosityAirFilledPorosity()
 {
   document.P.rafpsi.value = Math.round(document.P.afpsi.value / document.P.psi.value * 1000) / 1000;
   if (document.P.rafpsi.value > 1.0)
     {
	   alert("Relative Air-Filled Porosity cannot be greater than 1.0 Mg/Mg.");
	   document.getElementById("myTable").rows[14].cells[3].style.background = "red";
	 }
    else if (document.P.rafpsi.value < 0.0)
     {
	   alert("Relative Air-Filled Porosity cannot be less than 0.0 Mg/Mg.");
	   document.getElementById("myTable").rows[14].cells[3].style.background = "red";
	 }
   document.P.rafpm.value = document.P.rafpsi.value;
   document.P.rafpe.value = document.P.rafpsi.value;
 }

function calcAirFilledPorosityFromVolWaterPorosity()
 {
   document.P.afpsi.value = Math.round((document.P.psi.value - document.P.vwsi.value) * 1000) / 1000;
   if (document.P.afpsi.value > 1.0)
	 {
	   alert("Air-Filled Porosity cannot be greater than 1.0 m3/m3 ... reseting table.");
	   document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	 }
    else if (document.P.afpsi.value > 0.65)
	   {
	     alert("Air-Filled Porosity is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	   }
      else if (document.P.afpsi.value < 0.0)
	     {
	       alert("Air-Filled Porosity cannot be less than 0.0 m3/m3.");
	       document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	     }
   document.P.afpm.value = document.P.afpsi.value;
   document.P.afpe.value = document.P.afpsi.value;
 }

function calcAirFilledPorosityFromPorosityRelAirFilledPorosity()
 {
   document.P.afpsi.value = Math.round(document.P.psi.value * document.P.rafpsi.value * 1000) / 1000;
   if (document.P.afpsi.value > 1.0)
	 {
	   alert("Air-Filled Porosity cannot be greater than 1.0 m3/m3 ... reseting table.");
	   document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	 }
    else if (document.P.afpsi.value > 0.65)
	   {
	     alert("Air-Filled Porosity is rarely greater than 0.65 m3/m3.");
	     document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	   }
      else if (document.P.afpsi.value < 0.0)
	     {
	       alert("Air-Filled Porosity cannot be less than 0.0 m3/m3.");
	       document.getElementById("myTable").rows[14].cells[2].style.background = "red";
	     }
   document.P.afpm.value = document.P.afpsi.value;
   document.P.afpe.value = document.P.afpsi.value;
 }

function resetTable()
 {
//   alert("resetting table values");
   window.document.P.reset();
   for(var i=0;i<document.getElementById("myTable").rows.length;i++)
    {
//	 alert("rows.length = " + document.getElementById("myTable").rows.length);
     for(var j=0;j<document.getElementById("myTable").rows[i].cells.length;j++)
	  {
//	   alert("row[" + i + "].cells[" + j + "].length = " + document.getElementById('myTable').rows[i].cells.length);
	   document.getElementById("myTable").rows[i].cells[j].style.background="none";
	  }
	 }
 }

function changeColor(column)
 {
//  alert("Executing changeColor");
  window.document.getElementById("myTable").rows[1].cells[column].style.background="#ff0000";
 }

function validate(inputValue)
 {
  if ((Number(inputValue) < 0) || isNaN(inputValue))
    {
	 alert("Input Values must be positive numbers ... resetting table");
	 resetTable();
	 return false;
	}
   else
     return true;
 }


