//**************************************************************************
// JavaScript for resistors.html
//**************************************************************************

//**************************************************************************
// Define global variables
//**************************************************************************

var RA_Std;
var RB_Std;
var RC_Prec;

///**************************************************************************
// Calculate resistance and tolerance values
//**************************************************************************

var Digit_Array = new Array(0,1,2,3,4,5,6,7,8,9)
var Tol_Array = new Array(5,10,20)
var Mul_Array = new Array(1,10,100,1000,10000,100000,1000000,10000000,
   100000000,1000000000,0.1,0.01)
//var Color_Array = new Array("pics/black.gif", "pics/brown.jpg",
//   "pics/red.gif", "pics/orange.gif", "pics/yellow.gif",
//   "pics/green.gif", "pics/blue.gif", "pics/violet.gif",
//   "pics/gray.gif", "pics/white.gif", "pics/gold.gif",
//   "pics/silver.gif", "pics/body.gif" )
var Color_Array = new Array("images/black.gif", "images/brown.jpg",
   "images/red.gif", "images/orange.gif", "images/yellow.gif",
   "images/green.gif", "images/blue.gif", "images/violet.gif",
   "images/gray.gif", "images/white.gif", "images/gold.gif",
   "images/silver.gif", "images/body.gif" )

function Code_Calc() {

   for (var I = 0; I < document.Resistor.Digit_1.options.length; I++) {
      if (document.Resistor.Digit_1.options[I].selected) Digit_1 = I; }

   document.Resistor.Tens_Band.src = Color_Array[Digit_1];

   for (var I = 0; I < document.Resistor.Digit_2.options.length; I++) {
      if (document.Resistor.Digit_2.options[I].selected) Digit_2 = I; }

   document.Resistor.Ones_Band.src = Color_Array[Digit_2];

   for (var I = 0; I < document.Resistor.Mult.options.length; I++) {
      if (document.Resistor.Mult.options[I].selected) {
         Mult = Mul_Array[I];
         Digit_3 = I;
         }
      }

   document.Resistor.Mul_Band.src = Color_Array[Digit_3];

   for (var I = 0; I < document.Resistor.Tol.options.length; I++) {
      if (document.Resistor.Tol.options[I].selected) {
         Tol = Tol_Array[I];
         Digit_4 = I+10;
         }
      }

   document.Resistor.Tol_Band.src = Color_Array[Digit_4];

   R_Val = ((Digit_1 * 10) + Digit_2)*Mult;
   document.Resistor.Result.value = R_Scale(Rnd(R_Val,3)) + ", +/- " + Tol + "%";
}

///**************************************************************************
// Calculate three resistors in series.
//**************************************************************************

function R_Ser_Calc() {
   R1_Val = parseFloat(document.Resistor.R1_Ser_Val.value);
   R2_Val = parseFloat(document.Resistor.R2_Ser_Val.value);
   R3_Val = parseFloat(document.Resistor.R3_Ser_Val.value);
   RT_Val = R1_Val + R2_Val + R3_Val;
   document.Resistor.RT_Ser_Val.value = R_Scale(RT_Val);
}

//**************************************************************************
// Calculate three resistors in parallel.
//**************************************************************************

function R_Par_Calc() {
   if ( parseFloat(document.Resistor.R1_Par_Val.value) == 0 ||
      document.Resistor.R1_Par_Val.value == "" ) IR1_Val = 0;
   else IR1_Val = 1/document.Resistor.R1_Par_Val.value;

   if ( parseFloat(document.Resistor.R2_Par_Val.value) == 0 ||
      document.Resistor.R2_Par_Val.value == "" ) IR2_Val = 0;
   else IR2_Val = 1/document.Resistor.R2_Par_Val.value;

   if ( parseFloat(document.Resistor.R3_Par_Val.value) == 0 ||
      document.Resistor.R3_Par_Val.value == "" ) IR3_Val = 0;
   else IR3_Val = 1/document.Resistor.R3_Par_Val.value;

   RT_Val = Rnd(1/(IR1_Val + IR2_Val + IR3_Val),1);

   document.Resistor.RT_Par_Val.value = R_Scale(RT_Val);
}

//**************************************************************************
// Calculate resistance from volts and amperes.
//**************************************************************************

function R_Calc() {
   E_Val = document.Ohms_Law.E_Val_1.value;
   for (var I = 0; I < document.Ohms_Law.E_Mult_1.options.length; I++) {
      if (document.Ohms_Law.E_Mult_1.options[I].selected) {
         E_Mult = document.Ohms_Law.E_Mult_1.options[I].value;
         }
      }

   I_Val = document.Ohms_Law.I_Val_1.value;
   for (var I = 0; I < document.Ohms_Law.I_Mult_1.options.length; I++) {
      if (document.Ohms_Law.I_Mult_1.options[I].selected) {
         I_Mult = document.Ohms_Law.I_Mult_1.options[I].value;
         }
      }

   R_Val = (E_Val*E_Mult)/(I_Val*I_Mult);

   document.Ohms_Law.R_Val_1.value = R_Scale(R_Val);
}

//**************************************************************************
// Calculate current from volts and ohms.
//**************************************************************************

function I_Calc() {
   E_Val = document.Ohms_Law.E_Val_2.value;
   for (var I = 0; I < document.Ohms_Law.E_Mult_2.options.length; I++) {
      if (document.Ohms_Law.E_Mult_2.options[I].selected) {
         E_Mult = document.Ohms_Law.E_Mult_2.options[I].value;
         }
      }

   R_Val = document.Ohms_Law.R_Val_2.value;
   for (var I = 0; I < document.Ohms_Law.R_Mult_2.options.length; I++) {
      if (document.Ohms_Law.R_Mult_2.options[I].selected) {
         R_Mult = document.Ohms_Law.R_Mult_2.options[I].value;
         }
      }

   I_Val = (E_Val*E_Mult)/(R_Val*R_Mult);

   document.Ohms_Law.I_Val_2.value = I_Scale(I_Val);
}

//**************************************************************************
// Calculate voltage from current and ohms.
//**************************************************************************

function E_Calc() {
   I_Val = document.Ohms_Law.I_Val_3.value;
   for (var I = 0; I < document.Ohms_Law.I_Mult_3.options.length; I++) {
      if (document.Ohms_Law.I_Mult_3.options[I].selected) {
         I_Mult = document.Ohms_Law.I_Mult_3.options[I].value;
         }
      }

   R_Val = document.Ohms_Law.R_Val_3.value;
   for (var I = 0; I < document.Ohms_Law.R_Mult_3.options.length; I++) {
      if (document.Ohms_Law.R_Mult_3.options[I].selected) {
         R_Mult = document.Ohms_Law.R_Mult_3.options[I].value;
         }
      }

   E_Val = (I_Val*I_Mult)*(R_Val*R_Mult);

   document.Ohms_Law.E_Val_3.value = E_Scale(E_Val);
}

//**************************************************************************
// Calculate Power from voltage and current.
//**************************************************************************

function P_Calc_1() {
   E_Val = document.Power.E_Val_1.value;
   for (var I = 0; I < document.Power.E_Mult_1.options.length; I++) {
      if (document.Power.E_Mult_1.options[I].selected) {
         E_Mult = document.Power.E_Mult_1.options[I].value;
         }
      }

   I_Val = document.Power.I_Val_1.value;
   for (var I = 0; I < document.Power.I_Mult_1.options.length; I++) {
      if (document.Power.I_Mult_1.options[I].selected) {
         I_Mult = document.Power.I_Mult_1.options[I].value;
         }
      }

   P_Val = (E_Val*E_Mult)*(I_Val*I_Mult);

   document.Power.P_Val_1.value = P_Scale(P_Val);
}

//**************************************************************************
// Calculate Power from voltage and resistance.
//**************************************************************************

function P_Calc_2() {
   E_Val = document.Power.E_Val_2.value;
   for (var I = 0; I < document.Power.E_Mult_2.options.length; I++) {
      if (document.Power.E_Mult_2.options[I].selected) {
         E_Mult = document.Power.E_Mult_2.options[I].value;
         }
      }

   R_Val = document.Power.R_Val_2.value;
   for (var I = 0; I < document.Power.R_Mult_2.options.length; I++) {
      if (document.Power.R_Mult_2.options[I].selected) {
         R_Mult = document.Power.R_Mult_2.options[I].value;
         }
      }

   P_Val = Math.pow((E_Val*E_Mult),2)/(R_Val*R_Mult);

   document.Power.P_Val_2.value = P_Scale(P_Val);
}

//**************************************************************************
// Calculate Power from current and resistance.
//**************************************************************************

function P_Calc_3() {
   I_Val = document.Power.I_Val_3.value;
   for (var I = 0; I < document.Power.I_Mult_3.options.length; I++) {
      if (document.Power.I_Mult_3.options[I].selected) {
         I_Mult = document.Power.I_Mult_3.options[I].value;
         }
      }

   R_Val = document.Power.R_Val_3.value;
   for (var I = 0; I < document.Power.R_Mult_3.options.length; I++) {
      if (document.Power.R_Mult_3.options[I].selected) {
         R_Mult = document.Power.R_Mult_3.options[I].value;
         }
      }

   P_Val = Math.pow((I_Val*I_Mult),2)*(R_Val*R_Mult);

   document.Power.P_Val_3.value = P_Scale(P_Val);
}

function Get_R_Val() {
   R_Val = document.OhmsLaw_1.R_Val.value;
   return R_Val;
   }

function Get_R_Mult() {
   for (var I = 0; I < document.OhmsLaw_1.R_Mult.options.length; I++) {
      if (document.OhmsLaw_1.R_Mult.options[I].selected) {
         R_Mult = document.OhmsLaw_1.R_Mult.options[I].value;
         }
      }
   return R_Mult;
   }

function Get_E_Val() {
   E_Val = document.OhmsLaw_1.E_Val.value;
   return E_Val;
   }

function Get_E_Mult() {
   for (var I = 0; I < document.OhmsLaw_1.E_Mult.options.length; I++) {
      if (document.OhmsLaw_1.E_Mult.options[I].selected) {
         E_Mult = document.OhmsLaw_1.E_Mult.options[I].value;
         }
      }
   return E_Mult;
   }

function Get_I_Val() {
   I_Val = document.OhmsLaw_1.I_Val.value;
   return I_Val;
   }

function Get_I_Mult() {
   for (var I = 0; I < document.OhmsLaw_1.I_Mult.options.length; I++) {
      if (document.OhmsLaw_1.I_Mult.options[I].selected) {
         I_Mult = document.OhmsLaw_1.I_Mult.options[I].value;
         }
      }
   return I_Mult;
   }





