//**************************************************************************
// JavaScript for lm317.html
//**************************************************************************

//**************************************************************************
// Define variables that need to be global between more than one function.
//**************************************************************************

var View_Prec_R4, View_Prec_VOut, View_Prec_IR4;
var View_SP_V_In, View_SP_R3, View_SP_R4, View_SP_RP;
var View_BP_V_In, View_BP_R3, View_BP_R4, View_BP_RP;

var R_Prec, V_XY;

//**************************************************************************
// Define page specific functions
//**************************************************************************

function LM317_1_Calc() {

   AWG_Array = new Array(40);
   R_Standard = new Array(10,11,12,13,15,16,18,20,22,24,27,30,33,36,39,
      43,47,51,56,62,68,75,82,91,100);
   R3_Array = new Array (100, 120, 150, 180, 220, 270, 330, 390, 470);
   RP_Standard = new Array (50, 100, 200, 250, 500, 1000);

   //***********************************************************************
   // Clear the error indicator and the message window.
   //***********************************************************************

   In_Err = 0;

   //***********************************************************************
   // Pick up the initial values.
   //***********************************************************************

   V_In = document.LM317_1.V_In.value;
   V_Out = document.LM317_1.V_Out.value;

   //**********************************************************************
   // Do some error checking.
   //**********************************************************************

   if ( V_In < 3.7 ) {
      In_Err = 1;
      In_V_Error_Msg = "<font color=\"#B22222\">Warning: The Input Voltage specified is";
      In_V_Error_Msg += " <font color=\"#0000af\"><I>less than</I></font>"
      In_V_Error_Msg += " the required 3.7 Volts. Please adjust the Input Voltage to a";
      In_V_Error_Msg += " value <font color=\"#0000af\"><I>greater than, or equal to,";
      In_V_Error_Msg += " </I></font> 3.7 Volts.</font>";
      document.getElementById("In_V_Error").innerHTML = In_V_Error_Msg;
      }
   else if ( (V_In-V_Out) > 40 ) {
      In_Err = 1;
      In_V_Error_Msg = "<font color=\"#B22222\">Warning: The Maximum Input/Output Differential";
      In_V_Error_Msg += " Voltage, for a LM317, is 40 Volts. Please adjust the Input and";
      In_V_Error_Msg += " Output Voltages specified so that the Output Voltage";
      In_V_Error_Msg += " <font color=\"#0000af\"><I>minus</I></font>";
      In_V_Error_Msg += " the Input voltage is <font color=\"#0000af\"><I>less than,";
      In_V_Error_Msg += " or equal to</I></font>, 40 Volts.</font>";
      document.getElementById("In_V_Error").innerHTML = In_V_Error_Msg;
      }
   else {
      In_V_Error_Msg = "Input/Output Voltage specifications are within the limits of";
      In_V_Error_Msg += " the LM317.";
      document.getElementById("In_V_Error").innerHTML = In_V_Error_Msg;
      }


   DV_Out = V_In - 2.5;

   //***********************************************************************
   // Loop to determine which resistor was selected for R3 and then calculate
   // the currents through R3 and R4.
   //***********************************************************************

   for (var I = 0; I < document.LM317_1.R3_In.options.length; I++) {
      if (document.LM317_1.R3_In.options[I].selected) R3_In = R3_Array[I];
      }

   IR3_Out = 1.25 / R3_In * 1000;   //current in mA. thru R3
   IR4_Out = IR3_Out + .1;          //current in mA. thru R4
   View_Prec_IR4 = IR4_Out;         //current for precision R window

   document.getElementById("R3_Out_Ini").innerHTML = R_Scale(R3_In);
   document.getElementById("IR3_Out_Ini").innerHTML = I_Scale(IR3_Out/1000);
   document.getElementById("V_Out_Ini").innerHTML = E_Scale(V_Out);
   document.getElementById("VR4_Ini").innerHTML = E_Scale(V_Out-1.25);
   document.getElementById("IR4_Ini").innerHTML = I_Scale(IR4_Out/1000);

   //***********************************************************************
   // Calculate R4 based on V_Out and R3. Then find the nearest low and
   // high resistor values and calculate the output voltage for each.
   //***********************************************************************

//   R4_Out = R3_In*(V_Out/1.25-1);
   R4_Out = (V_Out - 1.25) / (IR4_Out/1000);
   View_Prec_R4 = R4_Out;
   document.getElementById("R4_Prec_Ini").innerHTML = Rnd(R4_Out,3) + " Ohms";

   if (document.LM317_1.R4_Tol.options[0].selected) R4_Tol = "05";
   else R4_Tol = "10";

   R4_Low = Get_Std_Res( R4_Out, "LT", R4_Tol );
   document.getElementById("R4_Low_Ini").innerHTML = R_Scale(R4_Low);
   document.getElementById("R4_Low_Tol_Ini").innerHTML = R4_Tol;

   VR4_Low = 1.25*(1+R4_Low/R3_In);
   document.getElementById("VR4_Low_Ini").innerHTML = E_Scale(VR4_Low);

   R_Prec = R4_Out;

   VR4_Out = 1.25*(1+R4_Out/R3_In);
   V_XY = VR4_Out - 1.25;

   R4_High = Get_Std_Res( R4_Out, "GT", R4_Tol );
   document.getElementById("R4_High_Ini").innerHTML = R_Scale(R4_High);
   document.getElementById("R4_High_Tol_Ini").innerHTML = R4_Tol;

   VR4_High = 1.25*(1+R4_High/R3_In);
   document.getElementById("VR4_High_Ini").innerHTML = E_Scale(VR4_High);

   //***********************************************************************
   // Calculate some pot info.
   //***********************************************************************

   R4_Delta = R4_High - R4_Low;
//   document.getElementById("R4_Delta_End").innerHTML = R_Scale(R4_Delta);
   document.getElementById("VR4_Low_End").innerHTML = E_Scale(VR4_Low);
   document.getElementById("VR4_High_End").innerHTML = E_Scale(VR4_High);

   //***********************************************************************
   // Find next largest standard potentiometer.
   //***********************************************************************

   R_Mult = 1; I = 0; RP_Std = RP_Standard[0];

   while( RP_Std < R4_Delta ){
      if ( RP_Standard[I] == 1000 ) {
         R_Mult = R_Mult * 10; I = 0; }
      else {
         I = I + 1; }

      RP_Std = RP_Standard[I] * R_Mult;

      if ( RP_Std == R4_Delta ) {
         break;}
      }

   document.getElementById("RP_Std_End").innerHTML = R_Scale(RP_Std);
   document.getElementById("R4_Low_End").innerHTML = R_Scale(R4_Low);

   //***********************************************************************
   // Global variable assignments for View_SP().
   //***********************************************************************

   View_SP_R3 = R3_In;
   View_SP_R4 = R4_Low;
   View_SP_RP = RP_Std;
   View_SP_V_In = V_In;

   View_BP_R3 = R3_In;
   View_BP_R4 = R4_Low;
   View_BP_RP = RP_Std;
   View_BP_V_In = V_In;
}

//**************************************************************************
// Precision_Res_Calc()
//**************************************************************************

var RA_Std, RB_Std, RC_Prec;

function Precision_Res_Calc() {

   if (document.LM317_1.Res_Tol.options[0].selected) Res_Tol = "05";
   else Res_Tol = "10";

   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, "GE", Res_Tol );

   //**************************************************************************
   // Calculate the precise resistor value for the parallel resistor.
   //**************************************************************************

   RB_Prec = R_Prec * RA_Std / ( RA_Std - R_Prec );

   //**************************************************************************
   // Find the next hightest resistor for the previous calculation.
   //**************************************************************************

   RB_Std = Get_Std_Res( RB_Prec, "GT", Res_Tol );

   //**************************************************************************
   // Calculate the result of RA_Std and RB_Std in parallel.
   //**************************************************************************

   RC_Prec = 1 /( 1 / RA_Std + 1 / RB_Std );

   //**************************************************************************
   // Calculate difference from the required value using standard resistors
   // for RA and RB.
   //**************************************************************************

   R_Delta = Math.abs(R_Prec-RC_Prec)/R_Prec*100;

   //**************************************************************************
   // Calculate the resistor currents and power.
   //**************************************************************************

   I_RA_Std = V_XY / RA_Std;
   P_RA_Std = I_RA_Std * V_XY;
   I_RB_Std = V_XY / RB_Std;
   P_RB_Std = I_RB_Std * V_XY;
   I_RC_Prec = V_XY / RC_Prec;

   V_Ra_Rb_Out = ((RC_Prec/R3_In)+1)*1.25;

   document.getElementById("Targ_Res").innerHTML = R_Scale(R_Prec);
   document.getElementById("Targ_Volts").innerHTML = E_Scale(V_XY);
   document.getElementById("Targ_Tol").innerHTML = Res_Tol;
   document.getElementById("Targ_Ra_Val").innerHTML = R_Scale(RA_Std);
   document.getElementById("Targ_Rb_Val").innerHTML = R_Scale(RB_Std);
   document.getElementById("Targ_Ra_IVal").innerHTML = I_Scale(I_RA_Std);
   document.getElementById("Targ_Ra_PVal").innerHTML = P_Scale(P_RA_Std);
   document.getElementById("Targ_Rb_IVal").innerHTML = I_Scale(I_RB_Std);
   document.getElementById("Targ_Rb_PVal").innerHTML = P_Scale(P_RB_Std);
   document.getElementById("Targ_RXY_Val").innerHTML = R_Scale(RC_Prec);
   document.getElementById("Targ_VOut").innerHTML = E_Scale(V_Ra_Rb_Out);
   document.getElementById("Targ_VOut_Per").innerHTML = Rnd(R_Delta,2);

}

function View_SP2() {
   LM317_1_Calc();

   PS_OV_View = window.open('','PS_OV_Win',
      'menubar=yes,resizable=yes,scrollbars=yes,status=yes,width=650,height=650');

   with (PS_OV_View.document) {
      write("<HTML>\n")
      write("<HEAD>\n")
      write("<title>Variable Resistor Setting v.s. Output Voltage</title>\n\n")

      write("<link rel=stylesheet type=\"text/css\" href=\"../../dg.css\">\n\n")

      write("</HEAD>\n\n")
      write("<BODY ALINK=\"#0000FF\" BACKGROUND=\"../../lightppr.gif\" BGCOLOR=\"#EFEFEF\"\n")
      write(" LINK=\"#0000FF\" TEXT=\"#000000\" VLINK=\"#0000FF\"\n")

      write("<BODY bottommargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" topmargin=\"0\" marginheight=\"0\" marginwidth=\"0\">\n")

      write("<table width=\"100%\" height=\"100\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" bgcolor=\"black\">\n")
      write("  <tr valign=\"center\">\n")
      write("    <td align=center width=50%>\n")
      write("    <!----------your logo goes here---------->\n")
      write("    <img src=\"../../images/K7MEM_Logo.jpg\" border=\"0\" alt=\"\"></td>\n")
      write("    <!--------------------------------------->\n")
      write("    <td align=center width=50%><P class=std_16_aqua ALIGN=\"center\">Javascript Electronic Notebook<BR><font color=\"#fff2d2\">\n")
      write("    Variable Resistor Setting v.s. Output Voltage</font><BR>\n")
      write("    by Martin E. Meserve</td>\n")
      write("  </tr>\n")
      write("</table>\n")

      write("<table width=\"100%\" height=\"19\" cellpadding=\"0\" cellspacing=\"0\" background=\"../../design/topgrbg.gif\" border=\"0\">\n")
      write("  <tr valign=\"top\">\n")
      write("    <td align=right width=\"100%\" background=\"../../design/topgrbg.gif\">\n")
      write("</td></tr></table>\n")

      write("<TABLE BORDER=\"0\" WIDTH=\"100%\" CELLPADDING=\"0\" CELLSPACING=\"0\" align=\"center\">\n")
      write("  <TR>\n")
      write("    <TD bgcolor=\"#000000\" HEIGHT=\"1\">\n")
      write("      <TABLE BORDER=\"0\" WIDTH=\"100%\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n")
      write("        <TR>\n")
      write("          <TD>\n")
      write("      </TD></TR></TABLE>\n")
      write("</TD></TR></TABLE>\n")

      write("<table width=\"100%\" cellpadding=\"15\" cellspacing=\"0\" bgcolor=\"#C6F7BD\" border=\"0\">\n")
      write("  <tr valign=\"top\">\n")
      write("    <td width=\"100%\" height=\"100%\" bgcolor=\"#C6F7BD\" colspan=2>\n")
      write("      <P class=\"std_10\" align=justify>The diagram at the right is used with the following values:\n")
      write("    <TD ROWSPAN=3>\n")
      write("      <IMG SRC=\"images/lm_317_wsmallpot.jpg\">\n")
      write("  <TR>\n")
      write("    <TD><P class=\"std_10_bold\" align=center>VIN = " + E_Scale(View_SP_V_In) + "\n")
      write("    <TD><P class=\"std_10_bold\" align=center>R3  = " + R_Scale(View_SP_R3) + "\n")
      write("  <TR>\n")
      write("    <TD><P class=\"std_10_bold\" align=center>Rp  = " + R_Scale(View_SP_RP) + "\n")
      write("    <TD><P class=\"std_10_bold\" align=center>R4  = " + R_Scale(View_SP_R4) + "\n")
      write("<TR>\n")
      write("  <TD colspan=3><P class=std_10 ALIGN=\"justify\">\n")
      write("  Should any of the entries below get marked with<FONT COLOR=\"#AF0000\">\n")
      write("  <B>Dropout</B></FONT>, this means that the <B><TT>input-to-output</TT></B>\n")
      write("  differential voltage has fallen below <B><TT>2.5 Volts</TT></B> and that\n")
      write("  the output will now track changes in the input voltage\n")
      write("  and be <B><TT>unregulated</TT></B>.\n")
      write("</TABLE>\n")

      write("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"0\" bgcolor=\"#C6F7BD\" border=\"1\">\n")
//      write("<table align=center border=\"1\" cellpadding=\"2\" cellspacing=\"3\" width=\"450\">\n")
      write("<TR ALIGN=CENTER>\n")
      write("  <TH><P class=\"std_10_bold\" align=center>Resistance<BR>Percentage\n")
      write("  <TH><P class=\"std_10_bold\" align=center>Voltage<BR>Output\n")
      write("  <TH><P class=\"std_10_bold\" align=center>Notes\n")
      write("  <TH><P class=\"std_10_bold\" align=center>Resistance<BR>Percentage\n")
      write("  <TH><P class=\"std_10_bold\" align=center>Voltage<BR>Output\n")
      write("  <TH><P class=\"std_10_bold\" align=center>Notes\n")

      for ( Pot_Per = 100; Pot_Per >= 50; Pot_Per = Pot_Per - 5 ) {
         Note_A = "&nbsp;"; Note_B = "&nbsp;";
         Pot_Res_A = View_SP_RP * (Pot_Per/100);
         R4_Equiv_A = View_SP_R4 + Pot_Res_A;
         V_Out_A = (1.25 * (1 + R4_Equiv_A/View_SP_R3));

         Pot_Res_B = View_SP_RP * ((Pot_Per-60)/100);
         R4_Equiv_B = View_SP_R4 + Pot_Res_B;
         V_Out_B = (1.25 * (1 + R4_Equiv_B/View_SP_R3));

         if ( Pot_Per == 100 ) { Note_A = "<FONT COLOR=\"#0000AF\">RP = Max</FONT>"; }
         if ( Pot_Per-55 == 0 ) { Note_B = "<FONT COLOR=\"#0000AF\">RP = Min</FONT>"; }

         if ( (View_SP_V_In - V_Out_A) < 2.5 ) {
            V_Out_A = View_SP_V_In - 2.5;
            Note_A = "<FONT COLOR=\"#AF0000\">Dropout</FONT>"; }

         if ( (View_SP_V_In - V_Out_B) < 2.5 ) {
            V_Out_B = View_SP_V_In - 2.5;
            Note_B = "<FONT COLOR=\"#AF0000\">Dropout</FONT>"; }

         if ( Pot_Per == 50 ) {
            write("<TR ALIGN=CENTER>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + Pot_Per + " %</TT>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + E_Scale(V_Out_A) + "</TT>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + Note_A + "</TT>\n")
            }
         else {
            write("<TR ALIGN=CENTER>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + Pot_Per + " %</TT>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + E_Scale(V_Out_A) + "</TT>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + Note_A + "</TT>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + (Pot_Per-55) + " %</TT>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + E_Scale(V_Out_B) + "</TT>\n")
            write("  <TH><P class=\"std_10_bold\" align=center><TT>" + Note_B + "</TT>\n")
            }
         }
      write("</TABLE>\n")

      write("</body>\n")
      write("</html>\n")

      close()
      } // End of "with (PS_OV_View.document)" statement
}


