//**************************************************************************
// JavaScript for PreciRes.html
//**************************************************************************

var RA_Std, RB_Std, RC_Prec;

function Precision_Res_Calc() {

   if (document.Precision_Res.Res_Tol.options[0].selected) {
      Res_Tol = "05";
//      document.Precision_Res.Res_Tol_Out1.value = "5%";
      document.getElementById("Res_Tol_Out1").innerHTML = "5%";
//      document.Precision_Res.Res_Tol_Out2.value = "5%";
      document.getElementById("Res_Tol_Out2").innerHTML = "5%";
      }
   else {
      Res_Tol = "10";
//      document.Precision_Res.Res_Tol_Out1.value = "10%";
      document.getElementById("Res_Tol_Out1").innerHTML = "10%";
//      document.Precision_Res.Res_Tol_Out2.value = "10%";
      document.getElementById("Res_Tol_Out2").innerHTML = "10%";
      }

   //**************************************************************************
   // Get the resistor multiplier from the resistor select box.
   //**************************************************************************

   for (var I = 0; I < document.Precision_Res.R_Mult.options.length; I++) {
      if (document.Precision_Res.R_Mult.options[I].selected) {
         R_Mult = document.Precision_Res.R_Mult.options[I].value;
         }
      }

   R_Prec = document.Precision_Res.R_Prec.value*R_Mult;
   Data_Out = "";

   if ( R_Prec <= 10 ) {
      Data_Out = "Warning: Value less than 10 Ohms\n\n";
      }

   //**************************************************************************
   // Find next largest standard resistor.
   //**************************************************************************

   RA_Std = Get_Std_Res( R_Prec, "GT", Res_Tol );
//   document.Precision_Res.RA.value = R_Scale(RA_Std);
   document.getElementById("RA").innerHTML = R_Scale(RA_Std);
//   document.Precision_Res.RA_2.value = R_Scale(RA_Std);
   document.getElementById("RA_2").innerHTML = R_Scale(RA_Std);

   //**************************************************************************
   // Calculate the precise resistor value for the parallel resistor.
   //**************************************************************************

   RB_Prec = R_Prec * RA_Std / ( RA_Std - R_Prec );
//   document.Precision_Res.RB_Prec.value = R_Scale(RB_Prec);
   document.getElementById("xRB_Prec").innerHTML = R_Scale(RB_Prec);

   //**************************************************************************
   // Find the next hightest resistor for the previous calculation.
   //**************************************************************************

   RB_Std = Get_Std_Res( RB_Prec, "GT", Res_Tol );
//   document.Precision_Res.RB.value = R_Scale(RB_Std);
   document.getElementById("RB").innerHTML = R_Scale(RB_Std);
//   document.Precision_Res.RB_2.value = R_Scale(RB_Std);
   document.getElementById("RB_2").innerHTML = R_Scale(RB_Std);

   //**************************************************************************
   // Calculate the result of RA_Std and RB_Std in parallel.
   //**************************************************************************

   RC_Prec = 1 /( 1 / RA_Std + 1 / RB_Std );
//   document.Precision_Res.RPrec.value = Rnd(R_Prec,3) + " Ohms";
   document.getElementById("RPrec").innerHTML = Rnd(R_Prec,3) + " Ohms";
//   document.Precision_Res.RC_Prec.value = Rnd(RC_Prec,3) + " Ohms";
   document.getElementById("RC_Prec").innerHTML = Rnd(RC_Prec,3) + " Ohms";

   //**************************************************************************
   // Calculate difference from the required value using standard resistors
   // for RA and RB.
   //**************************************************************************

   R_Delta = Math.abs(R_Prec-RC_Prec)/R_Prec*100;
//   document.Precision_Res.Dev.value = Rnd(R_Delta,2)+ " %";
   document.getElementById("Dev").innerHTML = Rnd(R_Delta,2)+ " %";
//   document.Precision_Res.Dev_2.value = Rnd(R_Delta,2)+ " %";

   if ( RA_Std == R_Prec ) {
      Data_Out += "The resistor value of " + R_Prec + " Ohms is a standard resistor."; }
   else {
   Data_Out += "The value of Ra is " + R_Scale(RA_Std) + ". This is the next highest\n";
   Data_Out += "Standard Resistor greater than R.\n\n";
   Data_Out += "The value of a parallel resistor to obtain R precisely\n";
   Data_Out += "is " + Rnd(RB_Prec,1) + " Ohms. The Standard Resistor value closest to\n";
   Data_Out += "the precision value for Rb, is " + R_Scale(RB_Std) + "\n\n";
   Data_Out += "Using Ra = " + R_Scale(RA_Std) + " and Rb = " + R_Scale(RB_Std) + " yields a\n";
   Data_Out += "parallel resistance, between points X and Y,\n";
   Data_Out += "of " + Rnd(RC_Prec,3) + " Ohms. This deviates " + Rnd(R_Delta,2) + " % from the\n";
   Data_Out += "required value of " + R_Prec + " Ohms\n\n";

      }

   for (var I = 0; I < document.Precision_Res.E_Mult.options.length; I++) {
      if (document.Precision_Res.E_Mult.options[I].selected) {
         E_Mult = document.Precision_Res.E_Mult.options[I].value;
         }
      }

   V_In = document.Precision_Res.V_In.value * E_Mult;
//   document.Precision_Res.V_XY.value = E_Scale(V_In);
   document.getElementById("V_XY").innerHTML = E_Scale(V_In);

   //**************************************************************************
   // Calculate the resistor currents and power.
   //**************************************************************************

   I_RA_Std = V_In / RA_Std;
   P_RA_Std = I_RA_Std * V_In;
   I_RB_Std = V_In / RB_Std;
   P_RB_Std = I_RB_Std * V_In;
   I_RC_Prec = V_In / RC_Prec;

//   document.Precision_Res.IRA.value = I_Scale(I_RA_Std) + " / " + P_Scale(P_RA_Std);
//   document.Precision_Res.I_RA.value = I_Scale(I_RA_Std);
   document.getElementById("I_RA").innerHTML = I_Scale(I_RA_Std);
//   document.Precision_Res.P_RA.value = P_Scale(P_RA_Std);
   document.getElementById("P_RA").innerHTML = P_Scale(P_RA_Std);

//   document.Precision_Res.IRB.value = I_Scale(I_RB_Std) + " / " + P_Scale(P_RB_Std);
//   document.Precision_Res.I_RB.value = I_Scale(I_RB_Std);
   document.getElementById("I_RB").innerHTML = I_Scale(I_RB_Std);
//   document.Precision_Res.P_RB.value = P_Scale(P_RB_Std);
   document.getElementById("P_RB").innerHTML = P_Scale(P_RB_Std);

   Data_Out += "With " + E_Scale(V_In) + " between points X and Y, the current through\n";
   Data_Out += "Ra is " + I_Scale(I_RA_Std) + " and the power dissipation is " + P_Scale(P_RA_Std) + ".\n";
   Data_Out += "The current through Rb is " + I_Scale(I_RB_Std) + " and the power\n";
   Data_Out += "dissipation is " + P_Scale(P_RB_Std) + ".\n";

//   document.Precision_Res.Data_Out.value = Data_Out;


}


