/*
 *
 * Sundance Flying Club
 * Weight and Balance Calculation Tool
 * 9/9/2005
 * version 0.2.1
 *
 * Copyright (c) 2005 David G. Jacobowitz
 * For questions and/or support, please contact:
 *  davej@alumni.virginia.edu
 * Reworked to handle more complex envelopes
 *  Evan Williams June 2007
 *
 */



/* New functions to handle envelope
 * We set up new associative array with std array in each element.
 *  Each element in the std array is an x,y coord pair of objects.
 * We can then ask whether the weight and arm pair is inside or outside this envelope.
*/

		var Coord = function (x,y){
			this.x = x;
			this.y = y;
		}

function MIN(x,y){return (x < y?x:y);}
function MAX(x,y){return (x > y?x:y);}

function printCoord(param,name){
	alert('<p>Coords for '+name+':'+param.x+','+param.y+'</p>');
}

function inPolygon(point,poly,vert)
{
  var counter = 0;
  var i;
  var xinters = 0.0;
  INSIDE = 1;
  OUTSIDE = 0;
  p1 = poly[0];
  for (i=1;i<=vert;i++) {
    p2 = poly[i % vert];
    if (point.y > MIN(p1.y,p2.y)) {
      if (point.y <= MAX(p1.y,p2.y)) {
        if (point.x <= MAX(p1.x,p2.x)) {
          if (p1.y != p2.y) {
            xinters = (point.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
            if (p1.x == p2.x || point.x <= xinters)
              counter++;
          }
        }
      }
    }
    p1 = p2;
  }
  if (counter % 2 == 0)
    return(OUTSIDE);
  else
    return(INSIDE);
}




function wb_calc (aircraft) {

/*Aircraft type*/
 var acTp  = new Array();
 acTp["choose"] = "Choose an aircraft from this list";
 acTp["N8256E"] = "PA-28-161 Piper Warrior II";
 acTp["N67307"] = "1978 Cessna 152";
 acTp["N94565"] = "1982 Cessna 152";
 acTp["N8171V"] = "PA-28-161 1980 Piper Warrior II";
 acTp["N15782"] = "PA-28-180 Piper Cherokee 180";
 acTp["N2865M"] = "PA-28-181 Piper Archer";
 acTp["N737EC"] = "1977 Cessna 172N";
 acTp["N738GE"] = "Cessna 172N";
 acTp["N739ZL"] = "1978 Cessna 172N";
 acTp["N738LF"] = "1977 Cessna 172N";
 acTp["N8370U"] = "1965 Cessna 172F";
 acTp["N6190R"] = "1980 Cessna 172RG \"Cutlass\"";
 acTp["N3518F"] = "1966 Cessna 182J";
 acTp["N25B"]   = "1978 BE-76 Beech Dutchess";
 acTp["N4531F"] = "Piper Arrow II";
 acTp["N7201N"] = "1968 Beech Bonanza V35A";

/* Deal with 2 seat airplanes */

 var twoPlace = new Array();
  twoPlace["N94565"] = 0;
  twoPlace["N67307"] = 0;
 
/*Notes*/

 var acNot = new Array();
 acNot["choose"] = "";
 acNot["N8256E"] = "dual nav/com, dme, adf";
 acNot["N67307"] = "nav/com";
 acNot["N94565"] = "nav/com";
 acNot["N8171V"] = "garmin 430 gps";
 acNot["N15782"] = "dual nav/com, dme, loran, autopilot, dvor";
 acNot["N2865M"] = "PFD, garmin 430W gps/com, digital nav/com, loran, adf, ";
 acNot["N737EC"] = "dual nav/com, adf, kln89b vfr gps, autopilot";
 acNot["N738GE"] = "dual nav/com, dme, adf";
 acNot["N739ZL"] = "dual nav/com, dme, adf";
 acNot["N738LF"] = "nav/com, garmin gns340 gps/com, dme, kns80 rho/theta rnav";
 acNot["N8370U"] = "dual nav/com, dme";
 acNot["N6190R"] = "nav/com, dme, adf";
 acNot["N3518F"] = "dual nav/com. dme, adf, northstar vfr gps, autopilot, moving map, bose headset built-in";
 acNot["N25B"]   = "garmin 430 gps, digital nav/com, autopilot. \nNOTE - max. zero fuel weight is 3500 pounds";
 acNot["N7201N"] = "PFD, Garmin 430W gps/nav/com, digital nav/com, gpss, autopilot, flight director";
 acNot["N4531F"]  = "The forward CG limit of the Arrow has two \"diagonal\" sections. These are modeled, but check the POH to make sure you\'re ok";

/*Basic Empty Weight*/

 var acBasEWt = new Array();
 acBasEWt["choose"] = 0;
 acBasEWt["N8256E"] = 1561;
 acBasEWt["N67307"] = 1152.2;
 acBasEWt["N94565"] = 1171.4;
 acBasEWt["N8171V"] = 1441.7;
 acBasEWt["N15782"] = 1484.05;
 acBasEWt["N2865M"] = 1598.3;
 acBasEWt["N737EC"] = 1508;
 acBasEWt["N738GE"] = 1496.4;
 acBasEWt["N739ZL"] = 1486;
 acBasEWt["N738LF"] = 1500.5;
 acBasEWt["N8370U"] = 1413.1;
 acBasEWt["N6190R"] = 1640.7;
 acBasEWt["N3518F"] = 1738.3;
 acBasEWt["N25B"]   = 2605;
 acBasEWt["N7201N"] = 2229;
 acBasEWt["N4531F"] = 1660;

/*Aircraft Empty Arms*/

 var acEArm = new Array();
 acEArm["choose"] = 0;
 acEArm["N8256E"] = 87.74;
 acEArm["N67307"] = 29.9;
 acEArm["N94565"] = 29.7;
 acEArm["N8171V"] = 86.04;
 acEArm["N15782"] = 86.2;
 acEArm["N2865M"] = 87.46;
 acEArm["N737EC"] = 38.54;
 acEArm["N738GE"] = 39.94; 
 acEArm["N739ZL"] = 38.88; 
 acEArm["N738LF"] = 40.02; 
 acEArm["N8370U"] = 36.62; 
 acEArm["N6190R"] = 37.74; 
 acEArm["N3518F"] = 35.53; 
 acEArm["N25B"]   = 110.13;
 acEArm["N7201N"] = 79.17;
 acEArm["N4531F"] = 84.7985;

/*Front Pax Arm*/

 var acFPaxArm = new Array();
 acFPaxArm["choose"] = 0;
 acFPaxArm["N8256E"] = 80.5;
 acFPaxArm["N67307"] = 39;
 acFPaxArm["N94565"] = 39;
 acFPaxArm["N8171V"] = 80.5;
 acFPaxArm["N15782"] = 80.5;
 acFPaxArm["N2865M"] = 80.5;
 acFPaxArm["N737EC"] = 37;
 acFPaxArm["N738GE"] = 37;
 acFPaxArm["N739ZL"] = 37;
 acFPaxArm["N738LF"] = 37;
 acFPaxArm["N8370U"] = 36;
 acFPaxArm["N6190R"] = 37;
 acFPaxArm["N3518F"] = 37;
 acFPaxArm["N25B"]   = 108;
 acFPaxArm["N7201N"] = 87;
 acFPaxArm["N4531F"] = 80.5;
 
/*Rear Pax Arm*/

 var acRPaxArm = new Array();
 acRPaxArm["choose"] = 0;
 acRPaxArm["N8256E"] = 118.1;
 acRPaxArm["N67307"] = 0;
 acRPaxArm["N94565"] = 0;
 acRPaxArm["N8171V"] = 118.1;
 acRPaxArm["N15782"] = 118.1;
 acRPaxArm["N2865M"] = 118.1;
 acRPaxArm["N737EC"] = 73;
 acRPaxArm["N738GE"] = 73;
 acRPaxArm["N739ZL"] = 73;
 acRPaxArm["N738LF"] = 73;
 acRPaxArm["N8370U"] = 70;
 acRPaxArm["N6190R"] = 73;
 acRPaxArm["N3518F"] = 74;
 acRPaxArm["N25B"]   = 144;
 acRPaxArm["N7201N"] = 128;
 acRPaxArm["N4531F"] = 118.1;

/*Baggage Arm*/

 var acBagArm = new Array();
 acBagArm["choose"] = 0;
 acBagArm["N8256E"] = 142.8;
 acBagArm["N67307"] = 63.8;
 acBagArm["N94565"] = 63.8;
 acBagArm["N8171V"] = 142.8;
 acBagArm["N15782"] = 142.8;
 acBagArm["N2865M"] = 142.8;
 acBagArm["N737EC"] = 95;
 acBagArm["N738GE"] = 95;
 acBagArm["N739ZL"] = 95;
 acBagArm["N738LF"] = 95;
 acBagArm["N8370U"] = 95;
 acBagArm["N6190R"] = 95;
 acBagArm["N3518F"] = 122;
 acBagArm["N25B"]   = 167;
 acBagArm["N7201N"] = 150;
 acBagArm["N4531F"] = 142.8;

/*Aft Baggage Arm*/

 var acRBagArm = new Array();
 acRBagArm["choose"] = 0;
 acRBagArm["N8256E"] = 0;
 acRBagArm["N67307"] = 84;
 acRBagArm["N94565"] = 84;
 acRBagArm["N8171V"] = 0;
 acRBagArm["N15782"] = 0;
 acRBagArm["N2865M"] = 0;
 acRBagArm["N737EC"] = 123;
 acRBagArm["N738GE"] = 123;
 acRBagArm["N739ZL"] = 123; 
 acRBagArm["N738LF"] = 123; 
 acRBagArm["N8370U"] = 0; 
 acRBagArm["N6190R"] = 0; 
 acRBagArm["N3518F"] = 0; 
 acRBagArm["N25B"]   = 0; 
 acRBagArm["N7201N"] = 0; 
 acRBagArm["N4531F"] = 0;

/*Fuel Arm*/

 var acFuelArm = new Array();
 acFuelArm["choose"] = 0;
 acFuelArm["N8256E"] = 95;
 acFuelArm["N67307"] = 48;
 acFuelArm["N94565"] = 48;
 acFuelArm["N8171V"] = 95;
 acFuelArm["N15782"] = 95;
 acFuelArm["N2865M"] = 95;
 acFuelArm["N737EC"] = 48;
 acFuelArm["N738GE"] = 48;
 acFuelArm["N739ZL"] = 48;
 acFuelArm["N738LF"] = 48;
 acFuelArm["N8370U"] = 48;
 acFuelArm["N6190R"] = 48;
 acFuelArm["N3518F"] = 48;
 acFuelArm["N25B"]   = 117;
 acFuelArm["N7201N"] = 33300/444;
 acFuelArm["N4531F"] = 95;

/*Fuel Capacity (gal)*/

 var acFuelCap = new Array();
 acFuelCap["choose"] = 999999999;
 acFuelCap["N8256E"] = 48;
 acFuelCap["N94565"] = 24.5;
 acFuelCap["N67307"] = 24.5;
 acFuelCap["N15782"] = 50;
 acFuelCap["N15782"] = 48;
 acFuelCap["N737EC"] = 40;
 acFuelCap["N738GE"] = 40;
 acFuelCap["N739ZL"] = 40;
 acFuelCap["N738LF"] = 40;
 acFuelCap["N8370U"] = 40;
 acFuelCap["N6190R"] = 40;
 acFuelCap["N3518F"] = 80;
 acFuelCap["N25B"]   = 100;
 acFuelCap["N7201N"] = 74;
 acFuelCap["N4531F"] = 48;

/*Max Gross Weight*/

 var acMaxWts = new Array();
 acMaxWts["choose"] = 999999999;
 acMaxWts["N8256E"] = 2325;
 acMaxWts["N67307"] = 1670;
 acMaxWts["N94565"] = 1670;
 acMaxWts["N8171V"] = 2525;
 acMaxWts["N15782"] = 2450;
 acMaxWts["N2865M"] = 2550;
 acMaxWts["N737EC"] = 2300;
 acMaxWts["N738GE"] = 2300;
 acMaxWts["N739ZL"] = 2300;
 acMaxWts["N738LF"] = 2300;
 acMaxWts["N8370U"] = 2300;
 acMaxWts["N6190R"] = 2650;
 acMaxWts["N3518F"] = 2800;
 acMaxWts["N25B"]   = 3900;
 acMaxWts["N7201N"] = 3400;
 acMaxWts["N4531F"] = 2650;
 
 /* utility maximum weight; if the AC is not utility 
  * certified then put zero.
  */
 var acUtMaxWt = new Array();
 acUtMaxWt["choose"] = 0;
 acUtMaxWt["N8256E"] = 2050;
 acUtMaxWt["N67307"] = 0;
 acUtMaxWt["N94565"] = 0;
 acUtMaxWt["N8171V"] = 2050;
 acUtMaxWt["N15782"] = 0;
 acUtMaxWt["N2865M"] = 2130;
 acUtMaxWt["N737EC"] = 2000;
 acUtMaxWt["N738GE"] = 2000;
 acUtMaxWt["N739ZL"] = 2000;
 acUtMaxWt["N738LF"] = 2000;
 acUtMaxWt["N8370U"] = 2000;
 acUtMaxWt["N6190R"] = 0;
 acUtMaxWt["N3518F"] = 0;
 acUtMaxWt["N25B"]   = 0;
 acUtMaxWt["N7201N"] = 3400;
 acUtMaxWt["N4531F"] = 0;

 var acUtCgMax = new Array();
 acUtCgMax["choose"] = 0;
 acUtCgMax["N8256E"] = 93;
 acUtCgMax["N67307"] = 0;
 acUtCgMax["N94565"] = 0;
 acUtCgMax["N8171V"] = 93;
 acUtCgMax["N15782"] = 0;
 acUtCgMax["N2865M"] = 93;
 acUtCgMax["N737EC"] = 40.5;
 acUtCgMax["N738GE"] = 40.5;
 acUtCgMax["N739ZL"] = 40.5;
 acUtCgMax["N738LF"] = 40.5;
 acUtCgMax["N8370U"] = 40.5;
 acUtCgMax["N6190R"] = 0;
 acUtCgMax["N3518F"] = 0;
 acUtCgMax["N25B"]   = 0;
 acUtCgMax["N7201N"] = 0;
 acUtCgMax["N4531F"] = 0;
 
 /* Now the array of envelopes
  *  Each point is a point on the envelope defined as 
  *  arm (inches), weight (lb)
  *  All vertices should be defined in the order
  *   in which they connect.
 */
 
 var acEnvel = new Array();
 
 acEnvel["choose"] = 0;
 acEnvel["N25B"] = [	new Coord(106.6,2400),
 						new Coord(106.6,3250),
 						new Coord(110.6,3900),
 						new Coord(117.5,3900),
 						new Coord(117.5,2400)];
 						
 acEnvel["N2865M"] = [	new Coord(82,1200),
 						new Coord(82,2050),
 						new Coord(88.6,2550),
 						new Coord(93,2550),
 						new Coord(93,1200)];
 						
 acEnvel["N3518F"] = [	new Coord(32.75,1800),
 						new Coord(33,2250),
 						new Coord(38.5,2800),
 						new Coord(47.25,2800),
 						new Coord(47.25,1800)];
 						
 acEnvel["N6190R"] = [	new Coord(36,1600),
 						new Coord(36,1950),
 						new Coord(39.5,2658),
 						new Coord(46.5,2658),
 						new Coord(46.5,1600)];
 						
 acEnvel["N67307"] = [	new Coord(31,1000),
 						new Coord(32.635,1670),
 						new Coord(36.5,1670),
 						new Coord(36.5,1000)];
 						
 acEnvel["N7201N"] = [	new Coord(77,2000),
 						new Coord(77,2900),
 						new Coord(82.1,3400),
 						new Coord(84.4,3400),
 						new Coord(85.7,3000),
 						new Coord(85.7,2000)];
 						
 acEnvel["N738GE"] = [	new Coord(35,1500),
 						new Coord(35,1950),
 						new Coord(38.5,2300),
 						new Coord(47.3,2300),
 						new Coord(47.3,1500)];
 						
 acEnvel["N739ZL"] = [	new Coord(35,1500),
 						new Coord(35,1950),
 						new Coord(38.5,2300),
 						new Coord(47.3,2300),
 						new Coord(47.3,1500)];
 						
 acEnvel["N8370U"] = [	new Coord(35,1500),
 						new Coord(35,1950),
 						new Coord(38.5,2300),
 						new Coord(47.3,2300),
 						new Coord(47.3,1500)];
 						
 acEnvel["N8256E"] = [	new Coord(83,1200),
 						new Coord(83,1950),
 						new Coord(87,2325),
 						new Coord(93,2325),
 						new Coord(93,1200)];
 						
 acEnvel["N94565"] = [	new Coord(31,1000),
 						new Coord(32.635,1670),
 						new Coord(36.5,1670),
 						new Coord(36.5,1000)];
 						

 /* *** *** *** *** *** *** *** *** *** *** *** *
    No adjustable parameters below this line 
  * *** *** *** *** *** *** *** *** *** *** *** */

 /*
  * Constants
  */
 var lbGal = 5.97;


 /*
  * Read user input from the html form
  */
 var currAcIdx = document.WB_form.ac_selection.selectedIndex;
 var currAc     = document.WB_form.ac_selection.options[currAcIdx].value;

 var ltFPaxLb  = parseFloat(document.WB_form.front_pax_lt.value);
 var rtFPaxLb  = parseFloat(document.WB_form.front_pax_rt.value);
 var ltRPaxLb  = parseFloat(document.WB_form.rear_pax_lt.value);
 var rtRPaxLb  = parseFloat(document.WB_form.rear_pax_rt.value);
 var ltFuelGal    = parseFloat(document.WB_form.fuel_lt.value);
 var rtFuelGal    = parseFloat(document.WB_form.fuel_rt.value);
 var bag1Lb  = parseFloat(document.WB_form.baggage_1.value);
 if (acRBagArm[currAc]) {
  aBagLb = parseFloat(document.WB_form.baggage_2.value);
 } else {
  aBagLb = 0;
 }

/* We will grey out the rear pax for a 2 place airplane */
 if (typeof twoPlace[currAc] !== "undefined") {
  document.getElementById("Rear").bgColor = "silver";
 } else {
  document.getElementById("Rear").bgColor = "white";
 }
/* alert("Set the color");  */

 /*
  * Calculate intermediate values from form data
  */
 var totFPaxLb = ltFPaxLb + rtFPaxLb;
 var totRPaxLb = ltRPaxLb + rtRPaxLb;
 var totFuelGal = ltFuelGal + rtFuelGal;
 var ltFuelLb    = ltFuelGal *  lbGal;
 var rtFuelLb    = rtFuelGal *  lbGal;
 var totFuelLb = ltFuelLb + rtFuelLb;
 var aBagLb;


 /*
  * Do the W/B calculations...
  */
 var fPaxMom    = 0.001 *  totFPaxLb * acFPaxArm[currAc];
 var rPaxMom    = 0.001 * totRPaxLb * acRPaxArm[currAc];
 var fuelMom      = 0.001 * totFuelLb   * acFuelArm[currAc];
 var bag1Mom = 0.001 * bag1Lb * acBagArm[currAc];
 var bag2Mom = 0.001 * aBagLb * acRBagArm[currAc];
 var eMom     = 0.001 * acBasEWt[currAc] *
                                acEArm[currAc];

 var totWt0Gas  = acBasEWt[currAc] +
                            totFPaxLb +
                            totRPaxLb +
                            bag1Lb +
                            aBagLb;

 var totWt         = totWt0Gas +
                            totFuelLb;

 var totMom0Gas  = eMom +
                            fPaxMom +
                            rPaxMom +
                            bag1Mom + 
                            bag2Mom;

 var totMom         = totMom0Gas +
                            fuelMom;

 var finArm            = (totMom * 1000) / 
                            totWt;
 var finArm0Gas           = (totMom0Gas * 1000) / 
                            totWt0Gas;

 /*
  * Using the data calculated above, repopulate the form's read-only
  * boxes with intermediate data.
  */
/* alert("Filling the form"); */
 document.WB_form.total_front_pax.value  = totFPaxLb.toFixed(2);
 document.WB_form.total_rear_pax.value   = totRPaxLb.toFixed(2);
 document.WB_form.front_pax_arm.value    = acFPaxArm[currAc].toFixed(2);
 document.WB_form.rear_pax_arm.value     = acRPaxArm[currAc].toFixed(2);
 document.WB_form.fuel_lt_lbs.value      = ltFuelLb.toFixed(2);
 document.WB_form.fuel_rt_lbs.value      = rtFuelLb.toFixed(2);
 document.WB_form.fuel_total.value       = totFuelGal.toFixed(2);
 document.WB_form.fuel_total_lbs.value   = totFuelLb.toFixed(2);
 document.WB_form.fuel_arm.value         = acFuelArm[currAc].toFixed(2);
 document.WB_form.baggage_1_arm.value    = acBagArm[currAc].toFixed(2);
 document.WB_form.baggage_2_arm.value    = acRBagArm[currAc].toFixed(2);  
 document.WB_form.total_baggage_1.value  = bag1Lb.toFixed(2);
 document.WB_form.total_baggage_2.value  = aBagLb.toFixed(2);
 document.WB_form.front_pax_moment.value = fPaxMom.toFixed(2);
 document.WB_form.rear_pax_moment.value  = rPaxMom.toFixed(2);
 document.WB_form.fuel_moment.value      = fuelMom.toFixed(2);
 document.WB_form.baggage_1_moment.value = bag1Mom.toFixed(2);
 document.WB_form.baggage_2_moment.value = bag2Mom.toFixed(2);
 document.WB_form.total_weight.value     = totWt.toFixed(2);
 document.WB_form.final_moment.value     = totMom.toFixed(2);
 document.WB_form.final_arm.value        = finArm.toFixed(2);
 document.WB_form.empty_weight.value     = acBasEWt[currAc].toFixed(2);
 document.WB_form.empty_arm.value        = acEArm[currAc].toFixed(2);
 document.WB_form.empty_moment.value     = eMom.toFixed(2);
/* alert("Got this far"); */
 /*
  * Only the Cessnas have an aft baggage area. This takes care of that.
  */
 if (!acRBagArm[currAc]) {
  document.WB_form.baggage_2_arm.value    = "n/a";
  document.WB_form.baggage_2_moment.value = "n/a";
  document.WB_form.total_baggage_2.value  = "n/a";
  document.WB_form.baggage_2.value        = "n/a";
  document.getElementById("aftBag").bgColor = "silver";
 } else {
  document.getElementById("aftBag").bgColor = "white";
  document.WB_form.baggage_2.value        = 0;
 }

/* alert("Past the Cessnas. Airplane is "+currAc); */
 /* 
  * Finally, determine the WB status of the AC
  *
  * This part is new
  */
 var overweight  = (totWt > acMaxWts[currAc]);
 var utility_ok  = ((finArm < acUtCgMax[currAc]) &&
                    (totWt < acUtMaxWt[currAc]));
 var too_much_gas = (totFuelGal > acFuelCap[currAc]);
 /*
  * Now see if we are in the envelope
 */
 
 var currWB = new Coord(finArm,totWt);
 var currWB0Gas = new Coord(finArm0Gas,totWt0Gas);
 var vertices = acEnvel[currAc].length;
 var inEnvelope = inPolygon(currWB,acEnvel[currAc],vertices);
 var inEnvelope0Gas = inPolygon(currWB0Gas,acEnvel[currAc],vertices);
 var minCG = 10000;
 var maxCG = -10000;
 var lCG = 0;
 var ii = 0;
 for (ii=0; ii < vertices; ii++) {
 	lCG = acEnvel[currAc][ii].x;
 	if (lCG < minCG) {minCG = lCG;};
 	if (lCG > maxCG) {maxCG = lCG;};
 	}
 /*
  * Generate a string with interesting W/B information. This is 
  * not the final "result" -- it's mostly for debugging and people
  * who do not trust the script
  */
 var info_message = 
   "Empty weight for " + currAc + " is " +
   acBasEWt[currAc].toFixed(0) + 
   " lbs.\n" + "Max weight is " + 
   acMaxWts[currAc].toFixed(0) + " lbs.\n" +
   "Minimum CG, Normal Category: " +
   minCG.toFixed(1) + 
   "\nMaximum CG, Normal Category: " + 
   maxCG.toFixed(1) + " in.\n";

 if (acUtCgMax[currAc] > 0) {
  info_message += 
   "Maximum CG, Utility Category: " +
   acUtCgMax[currAc].toFixed(1) + " in.\n";
 };
 if (acUtMaxWt[currAc] > 0) {
  info_message += 
   "Max weight, Utility Category: " +
   acUtMaxWt[currAc].toFixed(0) + 
   " lbs.\n";
 }
/*	alert("Result:" + info_message); */
 

 /*
  * Generate a string that tells the overall W/B disposition of the AC,
  * okay or not okay, utility or not, and a warning of whether the tanks
  * can be flown down to empty.
  */
 var stat_message = "";
 if (!overweight && 
     inEnvelope && 
     !too_much_gas) {
  if (inEnvelope0Gas) {
   /* that's a greenish color 
   document.getElementById("status").bgColor = "#33c33";	*/
   stat_message += "Weight and Balance OKAY.\n";
   if (document.images) {
    document.gonogo.src = "go.jpg";
   }
  } else {
/*   document.bgColor = "yellow";	*/
   if (document.images) {
    document.gonogo.src = "warning.gif";
   }
   stat_message += "Takeoff Weight and Balance OKAY -- landing may not be.\n";
  }
  if (utility_ok) {
   stat_message += "Utility Category operation also OK.\n";
  }
 } else {
  if (document.images) {
   document.gonogo.src = "stop.gif";
  }
  /* a reddish color a little easier on the eyes than "red"
  document.bgColor = "#ff3333";	*/
  stat_message = "AIRCRAFT IS NOT WITHIN W/B LIMITS\n";
  if (overweight) {
   stat_message += "Aircraft is OVERWEIGHT.\n";
  } else {
   stat_message += "CG is out of envelope.\n";
  }
  if (too_much_gas) {
   stat_message +=  "Too much fuel. Load exceeds capacity by " +
                    (totFuelGal - acFuelCap[currAc]) +
                    " gals.";
  }
  if (!inEnvelope0Gas) {
   stat_message += "WARNING! CG out of range at zero fuel.\n";
  }
 }

 
 /*
  * Generate a message with some ancillary notes in it.
  */
 var useful_load = (acMaxWts[currAc] - acBasEWt[currAc]);
 var  note_message = "";
 note_message += acTp[currAc] + "\n" +
                 "Useful load: " + useful_load.toFixed(0) +
                 " lbs.\n"  +
                 acNot[currAc];

 /*
  * Display all the messages.
  */
 document.results.min_max_report.value = info_message;
 document.results.status.value = stat_message;
 document.results.notes.value = note_message;
                                
}

function clearUserFields() {
 document.WB_form.front_pax_lt.value = 0;
 document.WB_form.front_pax_rt.value = 0;
 document.WB_form.rear_pax_lt.value = 0;
 document.WB_form.rear_pax_rt.value = 0;
 document.WB_form.fuel_lt.value     = 0;
 document.WB_form.fuel_rt.value     = 0
 document.WB_form.baggage_1.value   = 0;
 document.WB_form.baggage_2.value   = 0;
 wb_calc();
}
