// JavaScript for SuperCalc 7.0
// CG & MAC Calculator adapted from the Public Domain.
// Neutral Point and Weight & Balance Calculators developed by Dean A. Scott (c) 2005 - 2008, all rights reserved.
// Unauthorized pilfering of programming code and/or content is strictly prohibited. 
// NP equations courtesy Alasdair Sutherland (see C.G. Position article on Bloobird Radio Flyers)


function computeMAC(obj){

      var A = obj.A.value;
      var AA = obj.AA.value;
      var B = obj.B.value;
      var BB = obj.BB.value;
      var S = obj.S.value;
      var SS = obj.SS.value;
      var Y = obj.Y.value;
      var YY = obj.YY.value;
      var D = obj.D.value;
	  var Weed = obj.Weed.value;
      var SM = obj.SM.value;
	  
      if (A == "") {alert("Invalid value for Wing Root Cord (A)")}
      if (AA == "") {alert("Invalid value for Tail Root Cord (AA)")}
      if (B == "") {alert("Invalid value for Wing Tip Cord (B)")}
      if (BB == "") {alert("Invalid value for Tail Tip Cord (BB)")}
      if (S == "") {alert("Invalid value for Wing Sweep Distance (S).  Enter 0 to indicate a straight leading edge.")}
      if (SS == "") {alert("Invalid value for Tail Sweep Distance (SS).  Enter 0 to indicate a straight leading edge.")}
      if (Y == "") {alert("Invalid vlaue for Wing Half Span (Y)")}
      if (YY == "") {alert("Invalid vlaue for Tail Half Span (YY)")}
      if (D == "") {alert("Invlaid vlaue for Distance between Wing and Tail LE's (D)")}
	  if (eval(Weed) > 1) {alert("Weedhopper Correction must be 0 or 1")}
      if (SM < 5 && SM > 15) {alert("Invalid value for Static Margin.  Enter a value between 5 and 15")}

      W = eval(Weed);


// Wing sweep distance at MAC location
      C = (eval(S)  * (eval(A) + 2 * eval(B))) / (3 * (eval(A) + eval(B)));
      obj.result_C.value = Math.round(100 * C) / 100;
	  
// Tail sweep distance at Tmac location
      CC = (eval(SS) * (eval(AA)  + 2 *eval(BB))) / (3 * (eval(AA) + eval(BB)));
      obj.result_CC.value = Math.round(100 * CC) / 100;

// Wing and Tail taper ratios
      Wtr = eval(B) / eval(A);
      Ttr = eval(BB) / eval(AA);
      obj.result_Wtr.value = Math.round(100 * Wtr) / 100;
      obj.result_Ttr.value = Math.round(100 * Ttr) / 100;

// Wing MAC
//      MAC = (2 * eval(A) * (1 + Wtr + (Wtr * Wtr))) / (3 * (1 + Wtr));
      MAC = (2/3) * (eval(A) + (eval(B) - ((eval(A) * eval(B)) / (eval(A) + eval(B)))));
      obj.result_MAC.value = Math.round(100 * MAC) / 100;

// Tail MAC
      Tmac = (2 * eval(AA) * (1 + Ttr + (Ttr * Ttr))) / (3 * (1 + Ttr));
      obj.result_Tmac.value = Math.round(100 * Tmac) / 100;

// Wing MAC distance
      MACd = eval(Y) * ((eval(A) - MAC) / (eval(A) - eval(B)));
      obj.result_MACd.value = Math.round(100 * MACd) / 100;

// Tail MAC distance
      Tmacd = eval(YY) * ((eval(AA) - Tmac) / (eval(AA) - eval(BB)));
      obj.result_Tmacd.value = Math.round(100 * Tmacd) / 100;

// Wing AC aft of wing LE 
      Wac = (0.25 * MAC) + C; 
      if(W) {
	     obj.result_Wac.value = Math.round(100 * ((0.25 * MAC) + (C - 3.25))) / 100;
		 }
		 else {
	     obj.result_Wac.value = Math.round(100 * Wac) / 100;
		 }
	  
// Tail AC aft of tail LE
      Tac = (0.25 * Tmac) + CC;
	  
// Wing Area
      WA = eval(Y)  * (eval(A) + eval(B));
      obj.result_WA.value = Math.round(WA);

// Tail Area
      TA = eval(YY)  * (eval(AA) + eval(BB));
      obj.result_TA.value = Math.round(TA);

// Wing Aspect Ratio
      ARw = Math.pow((eval(Y) * 2), 2) / WA;
      obj.result_ARw.value = Math.round(100 * ARw) / 100;

// Tail Aspect Ratio
      ARt = Math.pow((eval(YY) * 2), 2) / TA;
      obj.result_ARt.value = Math.round(100 * ARt) / 100;
	  
// Tail Arm (distance btwn Wing AC and Tail AC)
      Tarm = (eval(D) - Wac) + Tac;
      obj.result_Tarm.value = Math.round(100 * Tarm) / 100;

// Tail Volume
      Vbar = (TA / WA) * (Tarm / MAC);
      obj.result_Vbar.value = Math.round(1000 * Vbar) / 1000;
	  
// Martin Simons equations for Adjusted Finite Lift Curve Slopes based on theoretical infinite
// wing lift curve slopes of 0.11 for large wings and 0.095 for small wings.  These ratios are
// very small and almost negligable as a multiplication factor of 1.0003 or so.
      As = (ARt * 0.095) / (ARt + 18.25 * 0.095);
      Aw = (ARw * 0.11) / (ARw + 18.25 * 0.11);
	  
// Neutral Point according to Martin Simons in "Model Aircraft Aerodynamics"
// wing AC + (tail efficiency * tail volume * (tail lift curve slope / wing lift curve slope) *  (1 - (delta tail downwash angle / delta wing AOA)))
   if(W) NP = 0.26 + (.8 * Vbar * (As / Aw) * ( 1 - 0.2));
   else  NP = 0.25 + (.8 * Vbar * (As / Aw) * ( 1 - 0.4));
// Neutral Point according to Alasdair Sutherland
// wing AC + (combined tail efficiency and downwash factor * tail volume * approximatation of lift curve slope ratio)  
//       NP = 0.25 + (0.25 * Vbar * Math.sqrt(Math.sqrt(ARw)));
      obj.result_NPmac.value = Math.round(1 * (100 * NP)) / 1 + "%";
      obj.result_NP.value = Math.round(100 * ((NP * MAC) + C)) / 100;
	  
// IDEAL Center of Gravity
      idealCG = NP - (eval(SM) / 100);
      obj.result_idealCGmac.value = Math.round(1 * (100 * idealCG)) / 1 + "%";
      obj.result_idealCGaftLE.value = Math.round(100 * (idealCG * MAC + (C - 3.25 * eval(Weed)))) / 100;

// Distance between Ideal CG and NP
      E = ((NP * MAC) + C) - ((idealCG * MAC) + C);
      obj.result_E.value = Math.round(100 * E) / 100;
	  
// Desired Static Margin
      obj.result_SM.value = eval(SM) + "%";

}

function computeWB(obj){

      var LeftMain = obj.LeftMain.value;
      var LeftArm = obj.LeftArm.value;
      var RightMain = obj.RightMain.value;
      var RightArm = obj.RightArm.value;
      var Center = obj.Center.value;
      var CenterArm = obj.CenterArm.value;
      var Pilot = obj.Pilot.value;
      var PilotArm = obj.PilotArm.value;
      var Pass = obj.Pass.value;
      var PassArm = obj.PassArm.value;
      var Tank = obj.Tank.value;
      var TankArm = obj.TankArm.value;
      var Datum2LE = obj.Datum2LE.value;
	  
      if (LeftMain == "") {alert("Invlaid Weight for Left Main Wheel")}
      if (LeftArm == "") {alert("Invlaid value for Left Wheel distance from Datum")}
      if (RightMain == "") {alert("Invlaid Weight for Right Main Wheel")}
      if (RightArm == "") {alert("Invlaid value for Right Wheel distance from Datum")}
      if (Center == "") {alert("Invlaid Weight for Nose or Tail Wheel")}
      if (CenterArm == "") {alert("Invlaid vlaue for Nose or Tail Wheel distance from Datum")}
      if (Pilot == "") {alert("Invlaid Weight for Pilot")}
      if (PilotArm == "") {alert("Invlaid value for Pilot distance from Datum")}
      if (Pass == "") {alert("Invlaid Weight for Passenger (enter 0 for single seat)")}
      if (PassArm == "") {alert("Invlaid value for Passenger distance from Datum (enter 0 for single seat)")}
      if (Tank == "") {alert("Invlaid Weight for Fuel Tank (enter gallons of gas)")}
      if (TankArm == "") {alert("Invlaid value for Fuel Tank distance from Datum")}
      if (Datum2LE == "") {alert("Invlaid value for Wing LE to Datum Point distance")}

computeMAC(obj);  // execute the function above to ensure variables exist in cases where only the W&B form button is pressed first.

// Weight and Balance
      Lmom = eval(LeftMain) * eval(LeftArm);
	  Rmom = eval(RightMain) * eval(RightArm);
	  Centermom = eval(Center) * eval(CenterArm);
	  P1mom = eval(Pilot) * eval(PilotArm);
	  P2mom = eval(Pass) * eval(PassArm);
      Tmom = (eval(Tank) * 6.12) * eval(TankArm);
	  obj.result_Lmom.value = Math.round(Lmom);
	  obj.result_Rmom.value = Math.round(Rmom);
	  obj.result_Centermom.value = Math.round(Centermom);
	  obj.result_P1mom.value = Math.round(P1mom);
	  obj.result_P2mom.value = Math.round(P2mom);
	  obj.result_Tmom.value = Math.round(Tmom);
	  
      Moment = Lmom + Rmom + Centermom + P1mom + P2mom + Tmom;
	  Weight = eval(LeftMain) + eval(RightMain) + eval(Center) + eval(Pilot) + eval(Pass) + (eval(Tank) * 6.12);
	  Empty = eval(LeftMain) + eval(RightMain) + eval(Center);
	  obj.result_GrossWt.value = Math.round(Weight);
	  obj.result_EmptyWt.value = Math.round(Empty);
	  obj.result_TotalMom.value = Math.round(Moment);
	  
// ACTUAL Center of Gravity 
	  actCG = (Moment / Weight - (eval(Datum2LE) + C)) / MAC;
	  obj.result_actCGmac.value = Math.round(10 * (100 * actCG)) / 10 + "%";
      actCGaftLE = Math.round(100 * (Moment / Weight - (eval(Datum2LE) + 3.25 * W))) / 100;
      obj.result_actCGaftLE.value = actCGaftLE;
      obj.result_actCGaxle.value = Math.round(100 * (eval(RightArm) - actCGaftLE - eval(Datum2LE))) / 100;
	  
// Distance between Actual CG and NP
      E2 = ((NP * MAC) + C) - (((actCG) * MAC) + C);
      obj.result_E2.value = Math.round(100 * E2) / 100;
//	  temp=((actCG/100) * MAC) + C;
//	  alert(temp); 
	  
// Actual Static Margin
      SM2 = (E2 / MAC) * 100;
      obj.result_SM2.value = Math.round(10 * SM2) / 10 + "%";

}