//**************************************************************************
// JavaScript for ohmslaw.html
//**************************************************************************


//**************************************************************************
// Functions for calculating Ohms Law parameters.
//**************************************************************************


function Ohms_Law_Calc() {


   Data_Out = "";


   for (var I = 0; I < document.Ohms_Law.Input_1.options.length; I++) {
      if (document.Ohms_Law.Input_1.options[I].selected) {
         Input_1 = document.Ohms_Law.Input_1.options[I].value;
         Value_Input_1 = parseFloat(document.Ohms_Law.Value_Input_1.value);
         }
      }

   for (var I = 0; I < document.Ohms_Law.Input_2.options.length; I++) {
      if (document.Ohms_Law.Input_2.options[I].selected) {
         Input_2 = document.Ohms_Law.Input_2.options[I].value;
         Value_Input_2 = parseFloat(document.Ohms_Law.Value_Input_2.value);
         }
      }

   //***********************************************************************
   // Clear the highlighted equations on the equation table.
   //***********************************************************************

   for (var I = 0; I < 4; I++) {
      for (var J = 0; J < 4; J++) {
      setCellBgColor ('tableId', J, I, ''); } }
//   setCellBgColor ('tableId', 0, 4, '');

   //***********************************************************************
   // E and I, R, P input
   //***********************************************************************

   if ( Input_1 == "E" && Input_2 == "I" ) {
      setCellBgColor ('tableId', 0, 1, '#fff2d2');
      R_Val = Value_Input_1 / Value_Input_2;
      setCellBgColor ('tableId', 3, 1, '#fff2d2');
      P_Val = Value_Input_1 * Value_Input_2;
      Data_Out += "   Voltage = " + E_Scale(Value_Input_1) + "<BR>";
      Data_Out += "   Current = " + I_Scale(Value_Input_2) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(R_Val) + "<BR>";
      Data_Out += "     Power = " + P_Scale(P_Val) + "\n";
      }

   if ( Input_1 == "E" && Input_2 == "R" ) {
      setCellBgColor ('tableId', 1, 1, '#fff2d2');
      I_Val = Value_Input_1 / Value_Input_2;
      setCellBgColor ('tableId', 3, 2, '#fff2d2');
      P_Val = Math.pow(Value_Input_1,2) / Value_Input_2;
      Data_Out += "   Voltage = " + E_Scale(Value_Input_1) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Current = " + I_Scale(I_Val) + "<BR>";
      Data_Out += "     Power = " + P_Scale(P_Val) + "\n";
      }

   if ( Input_1 == "E" && Input_2 == "P" ) {
      setCellBgColor ('tableId', 1, 2, '#fff2d2');
      I_Val = Value_Input_2 / Value_Input_1;
      setCellBgColor ('tableId', 0, 2, '#fff2d2');
      R_Val = Math.pow(Value_Input_1,2) / Value_Input_2;
      Data_Out += "   Voltage = " + E_Scale(Value_Input_1) + "<BR>";
      Data_Out += "     Power = " + P_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Current = " + I_Scale(I_Val) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(R_Val) + "\n";
      }

   //***********************************************************************
   // I and E, R, P input
   //***********************************************************************

   if ( Input_1 == "I" && Input_2 == "E" ) {
      setCellBgColor ('tableId', 0, 1, '#fff2d2');
      R_Val = Value_Input_2 / Value_Input_1;
      setCellBgColor ('tableId', 3, 1, '#fff2d2');
      P_Val = Value_Input_2 * Value_Input_1;
      Data_Out += "   Current = " + I_Scale(Value_Input_1) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(Value_Input_2) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(R_Val) + "<BR>";
      Data_Out += "     Power = " + P_Scale(P_Val) + "\n";
      }

   if ( Input_1 == "I" && Input_2 == "R" ) {
      setCellBgColor ('tableId', 2, 1, '#fff2d2');
      E_Val = Value_Input_1 * Value_Input_2;
      setCellBgColor ('tableId', 3, 3, '#fff2d2');
      P_Val = Math.pow(Value_Input_1,2) * Value_Input_2;
      Data_Out += "   Current = " + I_Scale(Value_Input_1) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(E_Val) + "<BR>";
      Data_Out += "     Power = " + P_Scale(P_Val) + "\n";
      }

   if ( Input_1 == "I" && Input_2 == "P" ) {
      setCellBgColor ('tableId', 2, 2, '#fff2d2');
      E_Val = Value_Input_2 / Value_Input_1;
      setCellBgColor ('tableId', 0, 3, '#fff2d2');
      R_Val = Value_Input_2 / Math.pow(Value_Input_1,2);
      Data_Out += "   Current = " + I_Scale(Value_Input_1) + "<BR>";
      Data_Out += "     Power = " + P_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(E_Val) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(R_Val) + "\n";
      }

   //***********************************************************************
   // R and E, I, P input
   //***********************************************************************



   if ( Input_1 == "R" && Input_2 == "E" ) {
      setCellBgColor ('tableId', 1, 1, '#fff2d2');
      I_Val = Value_Input_2 / Value_Input_1;
      setCellBgColor ('tableId', 3, 2, '#fff2d2');
      P_Val = Math.pow(Value_Input_2,2) / Value_Input_1;

      Data_Out += "Resistance = " + R_Scale(Value_Input_1) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Current = " + I_Scale(I_Val) + "<BR>";
      Data_Out += "     Power = " + P_Scale(P_Val) + "\n";
      }


   if ( Input_1 == "R" && Input_2 == "I" ) {
      setCellBgColor ('tableId', 2, 1, '#fff2d2');
      E_Val = Value_Input_1 * Value_Input_2;
      setCellBgColor ('tableId', 3, 3, '#fff2d2');
      P_Val = Math.pow(Value_Input_2,2) * Value_Input_1;
      Data_Out += "Resistance = " + R_Scale(Value_Input_1) + "<BR>";
      Data_Out += "   Current = " + I_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(E_Val) + "<BR>";
      Data_Out += "     Power = " + P_Scale(P_Val) + "\n";
      }


   if ( Input_1 == "R" && Input_2 == "P" ) {
      setCellBgColor ('tableId', 2, 3, '#fff2d2');
      E_Val = Math.sqrt(Value_Input_2 * Value_Input_1);
      setCellBgColor ('tableId', 1, 3, '#fff2d2');
      I_Val = Math.sqrt(Value_Input_2 / Value_Input_1);
      Data_Out += "Resistance = " + R_Scale(Value_Input_1) + "<BR>";
      Data_Out += "     Power = " + P_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(E_Val) + "<BR>";
      Data_Out += "   Current = " + I_Scale(I_Val) + "\n";
      }


   //***********************************************************************
   // P and E, I, R input
   //***********************************************************************


   if ( Input_1 == "P" && Input_2 == "E" ) {
      setCellBgColor ('tableId', 1, 2, '#fff2d2');
      I_Val = Value_Input_1 / Value_Input_2;
      setCellBgColor ('tableId', 0, 2, '#fff2d2');
      R_Val = Math.pow(Value_Input_2,2) / Value_Input_1;
      Data_Out += "     Power = " + P_Scale(Value_Input_1) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Current = " + I_Scale(I_Val) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(R_Val) + "\n";
      }


   if ( Input_1 == "P" && Input_2 == "I" ) {
      setCellBgColor ('tableId', 2, 2, '#fff2d2');
      E_Val = Value_Input_1 / Value_Input_2;
      setCellBgColor ('tableId', 0, 3, '#fff2d2');
      R_Val = Value_Input_1 / Math.pow(Value_Input_2,2);
      Data_Out += "     Power = " + P_Scale(Value_Input_1) + "<BR>";
      Data_Out += "   Current = " + I_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(E_Val) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(R_Val) + "\n";
      }


   if ( Input_1 == "P" && Input_2 == "R" ) {
      setCellBgColor ('tableId', 2, 3, '#fff2d2');
      E_Val = Math.sqrt(Value_Input_1 * Value_Input_2);
      setCellBgColor ('tableId', 1, 3, '#fff2d2');
      I_Val = Math.sqrt(Value_Input_1 / Value_Input_2);
      Data_Out += "     Power = " + P_Scale(Value_Input_1) + "<BR>";
      Data_Out += "Resistance = " + R_Scale(Value_Input_2) + "<BR>";
      Data_Out += "   Voltage = " + E_Scale(E_Val) + "<BR>";
      Data_Out += "   Current = " + I_Scale(I_Val) + "\n";
      }


   if ( Input_1 == Input_2 ) {
      Data_Out += "<font color=\"#b22222\">The same data type is selected in both of the select ";
      Data_Out += "areas. Each data type needs to be different to perform ";
      Data_Out += "any meaningfull calculations.</font>";
      }


//   document.Ohms_Law.Data_Out.value = Data_Out;
   document.getElementById("Table_Data_Out").innerHTML = Data_Out;


}


//**************************************************************************
// Functions for calculating Kirchhoff's Law current parameters.
//**************************************************************************


function K_Law_Par() {


   Data_Out = "";
   Space_Array = new Array ("", " ", "  ", "   ","    ","     ","      ",
   "       ", "        ", "         ", "          ");


   E_Par = parseFloat(document.Kirchhoffs_Law.E_Par.value);


   R_1_Par = comma_ins(parseFloat(document.Kirchhoffs_Law.R_1_Par.value)) + " Ohms";
   R_1_Out = "R1 = " + R_1_Par + Space_Array[16 - R_1_Par.length];
   I_1_Par = E_Par/parseFloat(document.Kirchhoffs_Law.R_1_Par.value);


   R_2_Par = comma_ins(parseFloat(document.Kirchhoffs_Law.R_2_Par.value)) + " Ohms";
   R_2_Out = "R2 = " + R_2_Par + Space_Array[16 - R_2_Par.length];
   I_2_Par = E_Par/parseFloat(document.Kirchhoffs_Law.R_2_Par.value);


   R_3_Par = comma_ins(parseFloat(document.Kirchhoffs_Law.R_3_Par.value)) + " Ohms";
   R_3_Out = "R3 = " + R_3_Par + Space_Array[16 - R_3_Par.length];
   I_3_Par = E_Par/parseFloat(document.Kirchhoffs_Law.R_3_Par.value);


   I_Total_Par = 0;


   if ( document.Kirchhoffs_Law.R_1_Par.value != "" ) {
      I_Total_Par = I_1_Par; }
   if ( document.Kirchhoffs_Law.R_2_Par.value != "" ) {
      I_Total_Par += I_2_Par; }
   if ( document.Kirchhoffs_Law.R_3_Par.value != "" ) {
      I_Total_Par += I_3_Par; }


   R_Total_Par = comma_ins(Rnd(E_Par/I_Total_Par,0)) + " Ohms";
   R_T_Out = "RT = " + R_Total_Par + Space_Array[16 - R_Total_Par.length];


   Data_Out += "Parallel Circuit Currents for an\n";
   Data_Out += "Applied Voltage of " + E_Scale(E_Par) + "\n\n";


   if ( document.Kirchhoffs_Law.R_1_Par.value != "" ) {
      Data_Out += R_1_Out + "I1 = " + I_Scale(I_1_Par) + "\n"; }


   if ( document.Kirchhoffs_Law.R_2_Par.value != "" ) {
      Data_Out += R_2_Out + "I2 = " + I_Scale(I_2_Par) + "\n"; }


   if ( document.Kirchhoffs_Law.R_3_Par.value != "" ) {
      Data_Out += R_3_Out + "I3 = " + I_Scale(I_3_Par) + "\n"; }


   Data_Out += "\n" + R_T_Out + "IT = " + I_Scale(I_Total_Par) + "\n\n";;


   document.Kirchhoffs_Law.K_Law_Par_Out.value = Data_Out;


}


//**************************************************************************
// Functions for calculating Kirchhoff's Law voltage parameters.
//**************************************************************************


function K_Law_Ser() {


   Data_Out = "";
   Space_Array = new Array ("", " ", "  ", "   ","    ","     ","      ",
   "       ", "        ", "         ", "          ");


   E_Ser = parseFloat(document.Kirchhoffs_Law.E_Ser.value);


   R_1_Ser = comma_ins(parseFloat(document.Kirchhoffs_Law.R_1_Ser.value)) + " Ohms";
   R_1_Out = "R1 = " + R_1_Ser + Space_Array[16 - R_1_Ser.length];


   R_2_Ser = comma_ins(parseFloat(document.Kirchhoffs_Law.R_2_Ser.value)) + " Ohms";
   R_2_Out = "R2 = " + R_2_Ser + Space_Array[16 - R_2_Ser.length];


   R_3_Ser = comma_ins(parseFloat(document.Kirchhoffs_Law.R_3_Ser.value)) + " Ohms";
   R_3_Out = "R3 = " + R_3_Ser + Space_Array[16 - R_3_Ser.length];


   R_Total_Ser = 0;


   if ( document.Kirchhoffs_Law.R_1_Ser.value != "" ) {
      R_Total_Ser = parseFloat(document.Kirchhoffs_Law.R_1_Ser.value); }
   if ( document.Kirchhoffs_Law.R_2_Ser.value != "" ) {
      R_Total_Ser += parseFloat(document.Kirchhoffs_Law.R_2_Ser.value); }
   if ( document.Kirchhoffs_Law.R_3_Ser.value != "" ) {
      R_Total_Ser += parseFloat(document.Kirchhoffs_Law.R_3_Ser.value); }


//   I_Total_Ser = I_Scale(E_Ser/R_Total_Ser);
   I_Total_Ser = E_Ser/R_Total_Ser;


//   Data_Out += "E_Ser = " + E_Ser + "\n";
//   Data_Out += "R_Total_Ser = " + R_Total_Ser + "\n";
//   Data_Out += "I_Total_Ser = " + I_Total_Ser + "\n";


   E_1_Ser = I_Total_Ser * parseFloat(document.Kirchhoffs_Law.R_1_Ser.value);
   E_2_Ser = I_Total_Ser * parseFloat(document.Kirchhoffs_Law.R_2_Ser.value);
   E_3_Ser = I_Total_Ser * parseFloat(document.Kirchhoffs_Law.R_3_Ser.value);




   R_Total_Ser = comma_ins(Rnd(R_Total_Ser,0)) + " Ohms";
   R_T_Out = "RT = " + R_Total_Ser + Space_Array[16 - R_Total_Ser.length];




   Data_Out += "Series Circuit Voltages for an\n";
   Data_Out += "Applied Voltage of " + E_Scale(E_Ser) + "\n\n";


   if ( document.Kirchhoffs_Law.R_1_Ser.value != "" ) {
      Data_Out += R_1_Out + "E1 = " + E_Scale(E_1_Ser) + "\n"; }


   if ( document.Kirchhoffs_Law.R_2_Ser.value != "" ) {
      Data_Out += R_2_Out + "E2 = " + E_Scale(E_2_Ser) + "\n"; }


   if ( document.Kirchhoffs_Law.R_3_Ser.value != "" ) {
      Data_Out += R_3_Out + "E3 = " + E_Scale(E_3_Ser) + "\n"; }


   Data_Out += "\n" + R_T_Out + "IT = " + I_Scale(I_Total_Ser) + "\n\n";;


   document.Kirchhoffs_Law.K_Law_Ser_Out.value = Data_Out;


}



