
var AWG_Array = new Array(40);

//**************************************************************************
// Plate Capacitor Calculations
//**************************************************************************

function Cap_PL_Calc() {

   //***********************************************************************
   // Get the length of one plate. If mm or cm is specified, convert the
   // length to inches for calculating.
   //***********************************************************************

   if (document.Cap_PL.Length_Unit.options[0].selected) {
      PL_Length = parseFloat(document.Cap_PL.xLength.value);
      }
   else if (document.Cap_PL.Length_Unit.options[1].selected) {
      PL_Length = parseFloat(document.Cap_PL.xLength.value) / 25.4;
      }
   else {
      PL_Length = parseFloat(document.Cap_PL.xLength.value) / 2.54;
      }

   //***********************************************************************
   // Get the width of one plate. If mm or cm is specified, convert the
   // length to inches for calculating.
   //***********************************************************************

   if (document.Cap_PL.Width_Unit.options[0].selected) {
      PL_Width = parseFloat(document.Cap_PL.xWidth.value);
      }
   else if (document.Cap_PL.Width_Unit.options[1].selected) {
      PL_Width = parseFloat(document.Cap_PL.xWidth.value) / 25.4;
      }
   else {
      PL_Width = parseFloat(document.Cap_PL.xWidth.value) / 2.54;
      }

   //***********************************************************************
   // Calculate the plate area.
   //***********************************************************************

   PL_Area = PL_Length * PL_Width;
   PL_Area_Metric = (PL_Length*25.4) * (PL_Width*25.4);

   //***********************************************************************
   // Get the plate separation. If mm or cm is specified, convert the
   // length to inches for calculating.
   //***********************************************************************

   if (document.Cap_PL.Sep_Unit.options[0].selected) {
      PL_Sep = parseFloat(document.Cap_PL.xSep.value);
      }
   else if (document.Cap_PL.Width_Unit.options[1].selected) {
      PL_Sep = parseFloat(document.Cap_PL.xSep.value) / 25.4;
      }
   else {
      PL_Sep = parseFloat(document.Cap_PL.xSep.value) / 2.54;
      }

   //***********************************************************************
   // Get the number of plates
   //***********************************************************************

   Num_Plates = document.Cap_PL.xPla.value;

   //***********************************************************************
   // Loop to determine which item was selected for dialectric material
   //***********************************************************************

   for (var I = 0; I < document.Cap_PL.xDie.options.length; I++) {
      if (document.Cap_PL.xDie.options[I].selected) {
         Dielectric_Const = document.Cap_PL.xDie.options[I].value; } }

   //***********************************************************************
   // If Other was selected set the dielectric constant from the specified
   // value. Otherwise output the constant for the material selected.
   //***********************************************************************

   if ( Dielectric_Const == 0 ) {
      Dielectric_Const = document.Cap_PL.Die_Val.value; }
   else {
      document.Cap_PL.Die_Val.value = Dielectric_Const; }

   //***********************************************************************
   // Calculate the effective capacitance and output it.
   //***********************************************************************

   Capacitance = ((0.2248 * Dielectric_Const * PL_Area) / PL_Sep)*(Num_Plates - 1);

   Data_Out = "Overlapping Plate Area\n"
   Data_Out += DIN2FIN(PL_Area,64) + " sq. (" + Rnd(PL_Area_Metric,1) + " mm sq.)\n\n"
   Data_Out += "Capacitance = " + C_Scale(Capacitance*.000000000001);

   document.Cap_PL.Area_PF_Out.value = Data_Out;
}

//**************************************************************************
// Coaxial Capacitor Calculations
//**************************************************************************

function Cap_Coax_Calc() {

   //***********************************************************************
   // Set up the AWG array
   //***********************************************************************

   K = Math.pow((0.46/0.005),(1/39));  // Increment Multiplier
   for ( I = 1; I <= 40; I++ ) {
      N = I+3; AWG_Array[I] = .46/Math.pow(K,N); }

   //***********************************************************************
   // Get the outside diameter of the inner conductor (d). Convert to
   // inches for the calculations.
   //***********************************************************************

   if (document.Cap_Coax.Inner_Dia_Dim.options[0].selected) {
      Inner_Dia_Dim = document.Cap_Coax.Inner_Dia_Dim.options[0].value;
      Wire_Num = parseInt(document.Cap_Coax.Inner_Dia.value);
      Inner_Dia = AWG_Array[Wire_Num]; }
   else if (document.Cap_Coax.Inner_Dia_Dim.options[1].selected) {
      Inner_Dia_Dim = document.Cap_Coax.Inner_Dia_Dim.options[1].value;
      Inner_Dia = parseFloat(document.Cap_Coax.Inner_Dia.value); }
   else if (document.Cap_Coax.Inner_Dia_Dim.options[2].selected) {
      Inner_Dia_Dim = document.Cap_Coax.Inner_Dia_Dim.options[2].value;
      Inner_Dia = parseFloat(document.Cap_Coax.Inner_Dia.value)/2.54; }
   else {
      Inner_Dia_Dim = document.Cap_Coax.Inner_Dia_Dim.options[3].value;
      Inner_Dia = parseFloat(document.Cap_Coax.Inner_Dia.value)/25.4; }

   //***********************************************************************
   // Get the inside diameter of the outer conductor (d). Convert to
   // inches for the calculations.
   //***********************************************************************

   if (document.Cap_Coax.Outer_Dia_Dim.options[0].selected) {
      Outer_Dia_Dim = document.Cap_Coax.Outer_Dia_Dim.options[0].value;
      Outer_Dia = parseFloat(document.Cap_Coax.Outer_Dia.value); }
   else if (document.Cap_Coax.Outer_Dia_Dim.options[1].selected) {
      Outer_Dia_Dim = document.Cap_Coax.Outer_Dia_Dim.options[1].value;
      Outer_Dia = parseFloat(document.Cap_Coax.Outer_Dia.value)/2.54; }
   else {
      Outer_Dia_Dim = document.Cap_Coax.Outer_Dia_Dim.options[2].value;
      Outer_Dia = parseFloat(document.Cap_Coax.Outer_Dia.value)/25.4; }

   //***********************************************************************
   // Loop to determine which item was selected for dialectric material
   //***********************************************************************

   for (var I = 0; I < document.Cap_Coax.Die.options.length; I++) {
      if (document.Cap_Coax.Die.options[I].selected) {
       Die = document.Cap_Coax.Die.options[I].value; }
      }

//   OD_Outer = document.Cap_Coax.OD_Outer.value;
////   OD_Inner = document.Cap_Coax.OD_Inner.value;
//   OD_Inner = Wire_Dia;

   Numer = 7.47;
   C_Out = (Numer*Die)/Math.log(Outer_Dia/Inner_Dia);

   Numer = 24.5;
   C_Out_M = (Numer*Die)/Math.log(Outer_Dia/Inner_Dia);

   Design_Data = "d = " + DIN2FIN(Inner_Dia,32) + ", " + Rnd(Inner_Dia*2.54,3) + " cm, " + Rnd(Inner_Dia*25.4,3) + " mm \n";
//   Design_Data += "\n";
   Design_Data += "D = " + DIN2FIN(Outer_Dia,32 ) + ", " + Rnd(Outer_Dia*2.54,3) + " cm, " + Rnd(Outer_Dia*25.4,3) + " mm \n";
   Design_Data += "C = " + Math.round(C_Out*100)/100 + " pF/ft, " + Math.round(C_Out_M*100)/100 + " pF/Meter";
   document.Cap_Coax.C_Out_Data.value = Design_Data;



//   document.Cap_Coax.C_Out_Data.value = Math.round(C_Out*100)/100 + " pF/" + Dimension;
   }

//**************************************************************************
// Cylindrical Capacitor Calculations
//**************************************************************************

function Cap_Cyl_Calc() {

   //***********************************************************************
   // Set the Numerator based on the measurement type.
   //***********************************************************************

   if (document.Cap_Cyl.Units.options[0].selected) {
      Numer = 7.47;
      Dimension = "Ft";
     }
   else {
      Numer = 24.5;
      Dimension = "M";
      }

   //***********************************************************************
   // Loop to determine which item was selected for dialectric material
   //***********************************************************************

   for (var I = 0; I < document.Cap_Cyl.Die.options.length; I++) {
      if (document.Cap_Cyl.Die.options[I].selected) {
       Die = document.Cap_Cyl.Die.options[I].value; }
      }

   OD_Outer = document.Cap_Cyl.OD_Outer.value;
   OD_Inner = document.Cap_Cyl.OD_Inner.value;

   C_Out = (Numer*Die)/Math.log(OD_Outer/OD_Inner);

   document.Cap_Cyl.C_Out.value = Math.round(C_Out*100)/100 + " pF/" + Dimension;
   }

///**************************************************************************
// Calculate three capacitors in parallel.
//**************************************************************************

var Mul_Array = new Array(1e-6,1e-12)

function C_Par_Calc() {
//   C1_Val = parseFloat(document.Ser_Par.C1_Par_Val.value);

   //***********************************************************************
   // Get the value of C1 and the dimension specified. If there is a zero,
   // or a null, set the value to zero. Otherwise, multiply the value times
   // the dimension value and take the recipocal.
   //***********************************************************************

   if ( parseFloat(document.Ser_Par.C1_Par_Val.value) == 0 ||
      document.Ser_Par.C1_Par_Val.value == "" ) {
      C1_Val = 0;C1_Mult = 0;
      }
   else {
      for (var I = 0; I < document.Ser_Par.C1_Par_Dim.options.length; I++) {
         if (document.Ser_Par.C1_Par_Dim.options[I].selected) {
            C1_Mult = Mul_Array[I]
            }
         }

      C1_Val = parseFloat(document.Ser_Par.C1_Par_Val.value) * C1_Mult;
      }

//   C2_Val = parseFloat(document.Ser_Par.C2_Par_Val.value);

   //***********************************************************************
   // Get the value of C2 and the dimension specified. If there is a zero,
   // or a null, set the value to zero. Otherwise, multiply the value times
   // the dimension value and take the recipocal.
   //***********************************************************************

   if ( parseFloat(document.Ser_Par.C2_Par_Val.value) == 0 ||
      document.Ser_Par.C2_Par_Val.value == "" ) {
      C2_Val = 0;C2_Mult = 0;
      }
   else {
      for (var I = 0; I < document.Ser_Par.C2_Par_Dim.options.length; I++) {
         if (document.Ser_Par.C2_Par_Dim.options[I].selected) {
            C2_Mult = Mul_Array[I]
            }
         }

      C2_Val = parseFloat(document.Ser_Par.C2_Par_Val.value) * C2_Mult;
      }

//   C3_Val = parseFloat(document.Ser_Par.C3_Par_Val.value);

   //***********************************************************************
   // Get the value of C3 and the dimension specified. If there is a zero,
   // or a null, set the value to zero. Otherwise, multiply the value times
   // the dimension value and take the recipocal.
   //***********************************************************************

   if ( parseFloat(document.Ser_Par.C3_Par_Val.value) == 0 ||
      document.Ser_Par.C3_Par_Val.value == "" ) {
      C3_Val = 0;C3_Mult = 0;
      }
   else {
      for (var I = 0; I < document.Ser_Par.C3_Par_Dim.options.length; I++) {
         if (document.Ser_Par.C3_Par_Dim.options[I].selected) {
            C3_Mult = Mul_Array[I]
            }
         }

      C3_Val = parseFloat(document.Ser_Par.C3_Par_Val.value) * C3_Mult;
      }

   CT_Val = C1_Val + C2_Val + C3_Val;
   document.Ser_Par.CT_Par_Val.value = C_Scale(CT_Val);
}

//**************************************************************************
// Calculate three capacitors in series.
//**************************************************************************

function C_Ser_Calc() {

   //***********************************************************************
   // Get the value of C1 and the dimension specified. If there is a zero,
   // or a null, set the value to zero. Otherwise, multiply the value times
   // the dimension value and take the recipocal.
   //***********************************************************************

   if ( parseFloat(document.Ser_Par.C1_Ser_Val.value) == 0 ||
      document.Ser_Par.C1_Ser_Val.value == "" ) {
      IC1_Val = 0;C1_Mult = 0;
      }
   else {
      for (var I = 0; I < document.Ser_Par.C1_Ser_Dim.options.length; I++) {
         if (document.Ser_Par.C1_Ser_Dim.options[I].selected) {
            C1_Mult = Mul_Array[I]
            }
         }

      IC1_Val = 1/(document.Ser_Par.C1_Ser_Val.value * C1_Mult);
      }

   //***********************************************************************
   // Get the value of C2 and the dimension specified. If there is a zero,
   // or a null, set the value to zero. Otherwise, multiply the value times
   // the dimension value and take the recipocal.
   //***********************************************************************

   if ( parseFloat(document.Ser_Par.C2_Ser_Val.value) == 0 ||
      document.Ser_Par.C2_Ser_Val.value == "" ) {
      IC2_Val = 0;C2_Mult = 0;
      }
   else {
      for (var I = 0; I < document.Ser_Par.C2_Ser_Dim.options.length; I++) {
         if (document.Ser_Par.C2_Ser_Dim.options[I].selected) {
            C2_Mult = Mul_Array[I]
            }
         }

      IC2_Val = 1/(document.Ser_Par.C2_Ser_Val.value * C2_Mult);
      }

   //***********************************************************************
   // Get the value of C3 and the dimension specified. If there is a zero,
   // or a null, set the value to zero. Otherwise, multiply the value times
   // the dimension value and take the recipocal.
   //***********************************************************************

   if ( parseFloat(document.Ser_Par.C3_Ser_Val.value) == 0 ||
      document.Ser_Par.C3_Ser_Val.value == "" ) {
      IC3_Val = 0;C3_Mult = 0;
      }
   else {
      for (var I = 0; I < document.Ser_Par.C3_Ser_Dim.options.length; I++) {
         if (document.Ser_Par.C3_Ser_Dim.options[I].selected) {
            C3_Mult = Mul_Array[I]
            }
         }

      IC3_Val = 1/(document.Ser_Par.C3_Ser_Val.value * C3_Mult);
      }

   CT_Val = 1/(IC1_Val + IC2_Val + IC3_Val);

   document.Ser_Par.CT_Ser_Val.value = C_Scale(CT_Val);
}


