/*  
 * Lighter Than Air Lift Calculator 
 * $Source: /Users/syzdek/Sites/calculators/RCS/ltalc.js,v $
 * $Revision: 1.1 $
 * $Date: 2007/01/10 14:53:01 $
 * $Author: syzdek $
 */
/*
 * This script determines the volume of a gas needed to lift
 * a particular mass within earth's atmosphere. This script uses
 * the following formula's
 *
 * Ideal Gas Law:
 *   PV = nRT
 *
 * Kelvin to Fahrenheit Conversion:
 *    F = 1.8(K - 273.15) + 32
 *
 */
/*
 * Copyright (c) 2005 David Syzdek <syzdek@mosquitonet.com>
 * All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modifi-
 * cation, are permitted provided that the following conditions are met:
 *
 *   o  Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *
 *   o  Redistributions in binary form must reproduce the above copyright no-
 *      tice, this list of conditions and the following disclaimer in the do-
 *      cumentation and/or other materials provided with the distribution.
 *
 *   o  The names of the contributors may not be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LI-
 * ABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUEN-
 * TIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEV-
 * ER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABI-
 * LITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* Set Global Var Defaults */

   var LTALC_outside_temperature = 75;
   var LTALC_outside_pressure = 1;
   var LTALC_inside_temperature = 75;
   var LTALC_inside_pressure = 14.695;
   var LTALC_volume = 1000;

/* Set known constants */
   var LTALC_atomicmass_nitrogen2 = 28.01348;
   var LTALC_atomicmass_oxygen2 = 31.9988;
   var LTALC_atomicmass_argon = 39.948;
   var LTALC_atomicmass_co2 = 44.0095;
   var LTALC_atomicmass_hydrogen2 = 2.01588;
   var LTALC_atomicmass_helium = 4.002602;

   var LTALC_aircomp_nitrogen = 0.78084;
   var LTALC_aircomp_oxygen = 0.20947;
   var LTALC_aircomp_argon = 0.00934;
   var LTALC_aircomp_co2 = 0.00002;

   var LTALC_const_ggc = 0.08205;		/* General Gas Constant = 0.08205 atm dm^3 K^(-1) mol^(-1) */
   var LTALC_const_lb2g = 453.59237;		/* 1 lb = 453.59237 grams */
   var LTALC_const_ft2dm_cubic = 28.31684387;	/* 1'^3 = 28.31684387dm^3 */
   var LTALC_const_psi2atm = 0.068;		/* 1 psi = 0.068 atm */

/* Declare Global Vars */
   var LTALC_weight_of_atmosphere;
   var LTALC_weight_of_hydrogen;
   var LTALC_weight_of_helium;
   var LTALC_weight_of_hot_air;
   var LTALC_lift_of_hydrogen;
   var LTALC_lift_of_helium;
   var LTALC_lift_of_hot_air;

/* 
 * This script assumes that the user is attempting to 
 * determine the lift and will calculate the new lift
 * if any changes are made.  The exception is, if a new
 * lift is given, the script will determine the volume 
 * required to provide the given lift.
 */

    /*******************************/
   /*                             */
  /* Functions to determine lift */
 /*                             */
/*******************************/

/* calculate the lift */
   function ltalc_find_lift() {
   
      /* declare local vars */ 
         var Moles;
         var Volume;
   
      /* Determine volume in dm^3 */
         Volume = (LTALC_const_ft2dm_cubic*LTALC_volume);

      /* Determine number of moles of atmosphere displaced */
         Moles = (LTALC_outside_pressure*Volume)/(LTALC_const_ggc*(((LTALC_outside_temperature-32)/1.8)+273.15));

      /* Determined mass of atmosphere displaced */
         LTALC_weight_of_atmosphere = 0;
         LTALC_weight_of_atmosphere = (LTALC_weight_of_atmosphere+(((LTALC_aircomp_nitrogen*Moles)*LTALC_atomicmass_nitrogen2)/LTALC_const_lb2g));
         LTALC_weight_of_atmosphere = (LTALC_weight_of_atmosphere+(((LTALC_aircomp_oxygen*Moles)*LTALC_atomicmass_oxygen2)/LTALC_const_lb2g));
         LTALC_weight_of_atmosphere = (LTALC_weight_of_atmosphere+(((LTALC_aircomp_argon*Moles)*LTALC_atomicmass_argon)/LTALC_const_lb2g));
         LTALC_weight_of_atmosphere = (LTALC_weight_of_atmosphere+(((LTALC_aircomp_co2*Moles)*LTALC_atomicmass_co2)/LTALC_const_lb2g)); 

      /* Determine number of moles of gas in envelope */
         Moles = (((LTALC_inside_pressure*LTALC_const_psi2atm)+LTALC_outside_pressure)*Volume)/(LTALC_const_ggc*(((LTALC_inside_temperature-32)/1.8)+273.15));

      /* Determined mass of gas in envelope */
         LTALC_weight_of_hydrogen = ((Moles * LTALC_atomicmass_hydrogen2)/LTALC_const_lb2g);
         LTALC_weight_of_helium = ((Moles * LTALC_atomicmass_helium)/LTALC_const_lb2g);
         LTALC_weight_of_hot_air = 0;
         LTALC_weight_of_hot_air = (LTALC_weight_of_hot_air+(((LTALC_aircomp_nitrogen*Moles)*LTALC_atomicmass_nitrogen2)/LTALC_const_lb2g));
         LTALC_weight_of_hot_air = (LTALC_weight_of_hot_air+(((LTALC_aircomp_oxygen*Moles)*LTALC_atomicmass_oxygen2)/LTALC_const_lb2g));
         LTALC_weight_of_hot_air = (LTALC_weight_of_hot_air+(((LTALC_aircomp_argon*Moles)*LTALC_atomicmass_argon)/LTALC_const_lb2g));
         LTALC_weight_of_hot_air = (LTALC_weight_of_hot_air+(((LTALC_aircomp_co2*Moles)*LTALC_atomicmass_co2)/LTALC_const_lb2g));

      /* Determine lift of gas */
         LTALC_lift_of_hydrogen = LTALC_weight_of_atmosphere - LTALC_weight_of_hydrogen;
         LTALC_lift_of_helium = LTALC_weight_of_atmosphere - LTALC_weight_of_helium;
         LTALC_lift_of_hot_air = LTALC_weight_of_atmosphere - LTALC_weight_of_hot_air;

      /* exits function */
         return(0);

   };


/* calculate the volume required to lift with hydrogen */
   function ltalc_volume_from_hydrogen() {

      /* Declare local vars */
         var Moles;

      /* Calculates Volume */
         Moles = (LTALC_outside_pressure*LTALC_aircomp_nitrogen*LTALC_atomicmass_nitrogen2*LTALC_const_lb2g);
         alert("Is this the problem");
         Moles = ( ((LTALC_outside_pressure*LTALC_aircomp_nitrogen)*(LTALC_atomicmass_nitrogen2*LTALC_const_lb2g)) / (LTALC_const_gcc * (((LTALC_outside_temperature-32)/1.8)+273.15) ) );
alert("Is this the problem");

         //Moles = Moles - (((LTALC_inside_pressure*LTALC_const_psi2atm)+LTALC_outside_pressure)*LTALC_atomicmass_hydrogen2*LTALC_const_lb2g)/(LTALC_const_gcc*(((LTALC_inside_temperature-32)/1.8)+273.15));
         //LTALC_volume = (LTALC_lift_of_hydrogen*LTALC_const_lb2g)/Moles;
 
      /* Exits function */
         return(0);

   };

/* Sets values on form */
   function ltalc_set_values() {
 
      /* Displays lift */
         document.liftcalc.elift.value = (Math.round(LTALC_lift_of_helium*100)/100);
         document.liftcalc.ylift.value = (Math.round(LTALC_lift_of_hydrogen*100)/100);
         document.liftcalc.hlift.value = (Math.round(LTALC_lift_of_hot_air*100)/100);

      /* Displays Volume */
         document.liftcalc.ivolume.value = (Math.round(LTALC_volume*100)/100);
         document.getElementById('ovolume').innerHTML = (Math.round(LTALC_volume*100)/100);

      /* Displays Temperature */
         document.liftcalc.itemperature.value = (Math.round(LTALC_inside_temperature*10)/10);
         document.liftcalc.otemperature.value = (Math.round(LTALC_outside_temperature*10)/10);

      /* Displays pressure */
         document.liftcalc.ipressure.value = (Math.round(LTALC_inside_pressure*1000)/1000);
         document.liftcalc.opressure.value = (Math.round(LTALC_outside_pressure*1000)/1000);

      /* Displays Weight of Envelope */
         document.getElementById('yweigth').innerHTML = (Math.round(LTALC_weight_of_hydrogen*100)/100);
         document.getElementById('eweigth').innerHTML = (Math.round(LTALC_weight_of_helium*100)/100);
         document.getElementById('hweigth').innerHTML = (Math.round(LTALC_weight_of_hot_air*100)/100);
         
      /* Displays weight of air displaced */
         document.getElementById('yair').innerHTML = (Math.round(LTALC_weight_of_atmosphere*100)/100);
         document.getElementById('eair').innerHTML = (Math.round(LTALC_weight_of_atmosphere*100)/100);
         document.getElementById('hair').innerHTML = (Math.round(LTALC_weight_of_atmosphere*100)/100);
     
      /* exits function */
         rturn(0);

   };

/* Check data */
   function ltalc_check_data() {
      
      /* Check Volume */
         if ( isNaN(document.liftcalc.ivolume.value) ) { 
            alert("You must enter a number value");
            return(1);
         };

      /* Check Temperature */
         if ( isNaN(document.liftcalc.itemperature.value) ) {
            alert("You must enter a number value");
            return(1);
         };
         if ( isNaN(document.liftcalc.otemperature.value) ) {
            alert("You must enter a number value");
            return(1);
         };

      /* Check Pressure */
         if ( isNaN(document.liftcalc.ipressure.value) ) {
            alert("You must enter a number value");
            return(1);
         };
         if ( isNaN(document.liftcalc.opressure.value) ) {
            alert("You must enter a number value");
            return(1);
         };

      /* Check lift */
         if ( isNaN(document.liftcalc.ylift.value) ) {
            alert("You must enter a number value");
            return(1);
         };
         if ( isNaN(document.liftcalc.elift.value) ) {
            alert("You must enter a number value");
            return(1);
         };

      /* Save Volume */
         LTALC_volume = document.liftcalc.ivolume.value;

      /* Save Temperature */
         LTALC_outside_temperature = document.liftcalc.otemperature.value;
         LTALC_inside_temperature = document.liftcalc.itemperature.value;

      /* Save Pressure */
         LTALC_outside_pressure = document.liftcalc.opressure.value;
         LTALC_inside_pressure = document.liftcalc.ipressure.value;

      /* Save lift */
         LTALC_weight_of_hydrogen = document.liftcalc.ylift.value; 
         LTALC_weight_of_helium = document.liftcalc.elift.value;

   };

/* Verifies that the data is correct before recalculating */
   function ltalc_recalculate_lift() {

      ltalc_check_data(); 
      ltalc_find_lift();
      ltalc_set_values();
   };

/* Verifies that the data is correct before recalculating volume from hydrogen */
   function ltalc_recalculate_volume_hydrogen() {

      ltalc_check_data();
      ltalc_volume_from_hydrogen();
alert("Got Here");
      ltalc_find_lift();
      ltalc_set_values();
   };

/* Displays form */
   function ltalc_display_form() {
      /* Prints Form */
         document.write("<form name=liftcalc>\n");
         document.write("   <table>\n");
         document.write("      <tr>\n");
         document.write("         <td colspan=2 align=center>\n");
         document.write("            <b>Inside Envelope</b>\n");
         document.write("         </td>\n");
         document.write("         <td> &nbsp; &nbsp; </td>\n");
         document.write("         <td colspan=2 align=center>\n");
         document.write("            <b>Outside Envelope</b>\n");
         document.write("         </td>\n");
         document.write("      </tr>\n");
         document.write("      <tr>\n");
         document.write("         <td align=right>\n");
         document.write("            Volume (feet):  <br>\n");
         document.write("            Temperature (F): <br>\n");
         document.write("            Pressure (PSI): <br>\n");
         document.write("         </td>\n");
         document.write("         <td>\n");
         document.write("            <input type=text onChange=ltalc_recalculate_lift(); name=ivolume size=8 value=0><br>\n");
         document.write("            <input type=text onChange=ltalc_recalculate_lift(); name=itemperature size=8 value=0><br>\n");
         document.write("            <input type=text onChange=ltalc_recalculate_lift(); name=ipressure size=8 value=0><br>\n");
         document.write("         </td>\n");
         document.write("         <td> &nbsp; &nbsp; </td>\n");
         document.write("         <td align=right>\n");
         document.write("            Volume (feet):  <br>\n");
         document.write("            Temperature (F): <br>\n");
         document.write("            Pressure (atm): <br>\n");
         document.write("         </td>\n");
         document.write("         <td>\n");
         document.write("            <font id=ovolume>0</font><br>\n");
         document.write("            <input type=text onChange=ltalc_recalculate_lift(); name=otemperature size=8 value=0><br>\n");
         document.write("            <input type=text onChange=ltalc_recalculate_lift(); name=opressure size=8 value=0><br>\n");
         document.write("         </td>\n");
         document.write("      </tr>\n");
         document.write("      <tr>\n");
         document.write("         <td colspan=5> &nbsp; <br> &nbsp; </td>\n");
         document.write("      </tr>\n");
         document.write("      <tr>\n");
         document.write("         <td colspan=2 align=center>\n");
         document.write("            <b>Hydrogen</b>\n");
         document.write("         </td>\n");
         document.write("         <td> &nbsp; </td>\n");
         document.write("         <td colspan=2 align=center>\n");
         document.write("            <b>Helium</b>\n");
         document.write("         </td>\n");
         document.write("      </tr>\n");
         document.write("      <tr>\n");
         document.write("         <td align=right>\n");
         document.write("            Weight of Air (lb):<br>\n");
         document.write("            Weight of Envelope (lb):<br>\n");
         document.write("            Amount of lift (lb):<br>\n");
         document.write("         </td>\n");
         document.write("         <td>\n");
         document.write("            <font id=yair>0</font><br>\n");
         document.write("            <font id=yweigth>0</font><br>\n");
         document.write("            <input type=text onchange=ltalc_recalculate_volume_hydrogen(); name=ylift size=8 value=0><br>\n");
         document.write("         </td>\n");
         document.write("         <td> &nbsp; </td>\n");
         document.write("         <td align=right>\n");
         document.write("            Weight of Air (lb):<br>\n");
         document.write("            Weight of Envelope: (lb): <br>\n");
         document.write("            Amount of lift (lb):<br>\n");
         document.write("         </td>\n");
         document.write("         <td>\n");
         document.write("            <font id=eair>0</font><br>\n");
         document.write("            <font id=eweigth>0</font><br>\n");
         document.write("            <input type=text onChange=ltalc_elift(); name=elift size=8 value=0><br>\n");
         document.write("         </td>\n");
         document.write("      </tr>\n");
         document.write("      <tr>\n");
         document.write("         <td colspan=5> &nbsp; <br> &nbsp; </td>\n");
         document.write("      </tr>\n");
         document.write("      <tr>\n");
         document.write("         <td colspan=2 align=center>\n");
         document.write("            <b>Hot Air</b>\n");
         document.write("         </td>\n");
         document.write("         <td colspan=2> &nbsp; </td>\n");
         //document.write("         <td colspan=2 align=center>\n");
         //document.write("            <b>Helium</b>\n");
         //document.write("         </td>\n");
         document.write("      </tr>\n");
         document.write("      <tr>\n");
         document.write("         <td align=right>\n");
         document.write("            Weight of Air (lb):<br>\n");
         document.write("            Weight of Envelope (lb):<br>\n");
         document.write("            Amount of lift (lb):<br>\n");
         document.write("         </td>\n");
         document.write("         <td>\n");
         document.write("            <font id=hair>0</font><br>\n");
         document.write("            <font id=hweigth>0</font><br>\n");
         document.write("            <input type=text onchange=ltalc_recalculate_volume_hydrogen(); name=hlift size=8 value=0><br>\n");
         document.write("         </td>\n");
         document.write("         <td colspan=3> &nbsp; </td>\n");
         //document.write("         <td align=right>\n");
         //document.write("            Weight of Air (lb):<br>\n");
         //document.write("            Weight of Envelope: (lb): <br>\n");
         //document.write("            Amount of lift (lb):<br>\n");
         //document.write("         </td>\n");
         //document.write("         <td>\n");
         //document.write("            <font id=hair>0</font><br>\n");
         //document.write("            <font id=hweigth>0</font><br>\n");
         //document.write("            <input type=text onChange=ltalc_elift(); name=elift size=8 value=0><br>\n");
         //document.write("         </td>\n");
         document.write("      </tr>\n");
         document.write("   </table>\n");
         document.write("</form>\n");
   };

/* LTALC init function */
   function ltalc_init() {

      /* Draw form */
         ltalc_display_form();

      /* Set defaults */
         ltalc_find_lift();
         ltalc_set_values(); 
         ltalc_check_data();
         ltalc_find_lift();
         ltalc_set_values(); 
   };


