// PowerResistor.js          started: 11/14/2005       Last update: 11/14/2005


// 11/14/2005 - Ver 1.00 - 
//                         
//                         

var cookie_array = new Array();

// -------------------------------------------------------------------
// This function calculates the Resistor value in Ohms and the power
// rating in Watts.
// inputs: the form data from PowerResistor.htm, Volts & mA
// output: Displays computed results to a form text field
// Action: Waits for a click of the mouse on the [X]
// return: nothing
// -------------------------------------------------------------------
function PwrRCalcDisp() {

 // Nominal Voltage
 var volts = document.PwrR.Voltage.value;

 // desired current (mA)
 var mA = document.PwrR.Current.value;
 
 RValidate(volts, mA);
 
 var Ohm = Math.ceil(volts/mA * 10000)/10; // round UP to 1 decimal place
 var Pwr = Math.ceil((mA * volts) / 1000); // round UP to nearest Watt
 
 document.PwrR.RWvalue.value = "  " + Ohm + " ohms, and " + Pwr + " W";
 
 // --------------------------------------------------------------------
 // Set the cookie to expire in one month, not needed longer I'm sure 
 // --------------------------------------------------------------------
 expireDate = new Date;
 expireDate.setMonth(expireDate.getMonth()+1);

 // ----------------------------------------------------------
 // Write the Nominal Voltage & Current to the cookie 
 // ----------------------------------------------------------
 var txt = ""; // empty cookie string
 if(!cookie_array.length) { // No cookie for us, make one.
   txt += "NV="+volts+";I="+mA;
   txt += ";expires=" + expireDate.toGMTString(); // Add the expiration
   //alert("Make a cookie!");
 }
 else {
   var done1 = 0;
   var done2 = 0;
   for(j=0; j<cookie_array.length; ++j) {
      var tmpsplit = cookie_array[j].split("=");
      if (tmpsplit[0] == "NV") { // find our stuff
         txt += tmpsplit[0]+"="+volts+";"
         ++done1;
      }
      else if (tmpsplit[0] == "I") { // find our stuff
         txt += tmpsplit[0]+"="+mA+";"
         ++done2;
      }
      else if (tmpsplit[0] == "expires") { // find our stuff
         if(!done1 && !done2) { // New for this program
           txt += "NV="+volts+";I="+mA+";"; // Add our cookie data
         }
         txt += tmpsplit[0]+"="+expireDate.toGMTString();
      }
      else { // build cookie string back up
         txt += tmpsplit[0]+"="+tmpsplit[1]+";"
      }
   }
 }
 document.cookie = escape(txt); // Write the cookie
 //alert(txt); // pk debugging

} // End of: PwrRCalcDisp


// -------------------------------------------------------------------
// This function Checks the inputs to see if they are numbers
// inputs: a string
// output: nothing
// Action: none
// return: (0/1) = (Not a number / Is a number)
// -------------------------------------------------------------------
function RValidate(Volts, mA) {

   var Rtn = true;
   var me = -1;
   var m0 = 30, m1 = 999;  // min/max for freq
   var M0 = .7, M1 = .99;  // min/max for vf
   //alert("number of elements: "+document.Jpole.elements.length);
   //alert("Value of element[0] = "+document.Jpole.elements[0].value);

   if(isNaN(Volts)) {
       alert("The Nominal Voltage, MUST be a number, please try again.");
       Rtn = false;
       me = 1;
    }
    else if(isNaN(mA)) {
       alert("The Desired current, MUST be a number, please try again.");
       Rtn = false;
       me = 0;
    }

   if(Rtn == true) {
     var Volts = Number(Volts);
     var mA = Number(mA);

     if (document.PwrR.Voltage.value.length < 1) { // pk01
         alert("Please enter the Nominal Voltage.");
         Rtn = false;
         me = 0;
     }

     if (document.PwrR.Current.value.length < 1 && me == -1) { // pk01
         alert("Please enter the Current.");
         Rtn = false;
         me = 1;
     }
   }
   if (me >= 0) {
      document.PwrR.elements[me].focus();
   }
   return Rtn;
}


// -------------------------------------------------------------------
// This function checks to see if the cookie exists and if it does it
// then gets the saved data
// ------------
// inputs: none
// output: saved values to correct form variables
// Action: none
// return: none
// -------------------------------------------------------------------
// NOTE: If we have two programs that save cookies then they will
//       over-write each other. The only way to protect other cookie
//       info from other programs of ours is to first READ all of our
// cookie information saving the order and titles and values of each.
// Then change just this programs values and resave the entire 
// cookie.
//
// QUICK FIX: Check to be sure these are our data & only if they are,
//            set them into the form for use.
// -------------------------------------------------------------------
function LoadMe() {
   window.status="Power Resistor Calculation Program Ver 1.00";
   
   if(document.cookie != "") { // Cookie Exists!

      var tmptxt = "Our Cookie:\n\n";  // For debugging the cookie decoder

      // Copy the cookie info to a new string
      var cookie_string = unescape(document.cookie);
      //alert(cookie_string); // pk debugging

      // Split the string into a new array
      cookie_array = cookie_string.split(";");
      //alert(cookie_array);         // pk debugging
      //alert(cookie_array.length);  // pk debugging

      for(j=0; j<cookie_array.length; ++j) {
         tmptxt += cookie_array[j]+"\n"; // For debugging the cookie decoder

         var tmpsplit = cookie_array[j].split("=");
         if (tmpsplit[0] == "NV") { // find our stuff
            document.PwrR.Voltage.value = tmpsplit[1];
            // alert(tmpsplit[1]); // pk debugging
         }
         else if (tmpsplit[0] == "I") { // find our stuff
            document.PwrR.Current.value = tmpsplit[1];
         }
      }
      //alert(tmptxt); // pk debugging


      // Further split the first entry into the title and value 
      var volts = cookie_array[0].split("=");

      // Further split the second entry into the title and value
      var mA= cookie_array[1].split("=");

      //window.alert(volts + " - " + mA);  // pk debugging cookie decoder

   }
   else {
      // alert("No cookie found!"); // pk debugging
   }
}


// -------------------------------------------------------------------
// This function swaps two images so we can do a roll-over
// inputs: two image file names
// output: none
// Action: New image put in place of image being used
// return: none
// -------------------------------------------------------------------
function swapImages(ImageName, NewImage) {

   // test to see if the browser understands rollover
   // If it does swap one image for the other
   if (document.images) {
      document[ImageName].src = NewImage;
   }
}


// -------------------------------------------------------------------
// On Mouse Over we display the txt in the status bar, and myimage at
// the lnk position.
// inputs: txt, lnk, image
// output: none
// Action: calls swapImages to place the sent image at the lnk position
// return: none
// -------------------------------------------------------------------
function RollMeOver(txt, lnk, myimage) {
   window.status=txt;
   swapImages(lnk, myimage);
//   return true;  // is this needed? Seems to work without it.
}


// -------------------------------------------------------------------
// On Mouse Over we clear the status bar, and put myimage at
// the lnk position.
// inputs: lnk, image
// output: none
// Action: calls swapImages to place the sent image at the lnk position
// return: none
// -------------------------------------------------------------------
function ClearMe(lnk, myimage) {
   window.status="";
   swapImages(lnk, myimage);
}

