  // This code is copyright 2003 LED Supply

  function my_onclick()
  {
	  // Standard 5% resistor values
	  stdVals = new Object();
	  stdVals[0] = 10;
	  stdVals[1] = 11;
	  stdVals[2] = 12;
	  stdVals[3] = 13;
	  stdVals[4] = 15;
	  stdVals[5] = 16;
	  stdVals[6] = 18;
	  stdVals[7] = 20;
	  stdVals[8] = 22;
	  stdVals[9] = 24;
	  stdVals[10] = 27;
	  stdVals[11] = 30;
	  stdVals[12] = 33;
	  stdVals[13] = 36;
	  stdVals[14] = 39;
	  stdVals[15] = 43;
	  stdVals[16] = 47;
	  stdVals[17] = 51;
	  stdVals[18] = 56;
	  stdVals[19] = 62;
	  stdVals[20] = 68;
	  stdVals[21] = 75;
	  stdVals[22] = 82;
	  stdVals[23] = 91;
	  
	  vSource = eval(document.f.vsource_field.value);
	  vDrop = eval(document.f.vdrop_field.value);
	  iMax = eval(document.f.current_field.value) / 1000;

	  if ( vDrop > vSource ) {
		  alert("The source voltage must be greater than the LED forward voltage drop!");
		  return;
	  }
	  
	  rExact = ( vSource - vDrop ) / ( iMax );

	  // Calculate closest std resistor value
	  x = 0;
	  while (1) {
	    if ( x > 23 ) { x = 0; }
	    if (rExact <= stdVals[x]) {
	      rStd = stdVals[x];
              break;
	    }
	    stdVals[x] *= 10;
	    ++ x;
          }

	  irStd = ( vSource - vDrop ) / rStd;
	  plrStd = vDrop * irStd;
          prrStd = ( vSource - vDrop ) * irStd;

	  // Calculated closest std resistor wattage
	  if ( prrStd <= .125 )
	    { prrStd_string = "1/8"; }
	  else if ( prrStd <= .25 )
	    { prrStd_string = "1/4"; }
	  else if ( prrStd <= .5 )
	    { prrStd_string = "1/2"; }
	  else if ( prrStd <= 1 )
	    { prrStd_string = "1"; }
	  else {
		 prrStd_string = "At Least " + Math.round(prrStd + 1);
	 } 

          document.f.rExact_field.value = Math.round(rExact * 10000) / 10000;
          document.f.rStd_field.value = rStd;
          document.f.irStd_field.value = Math.round(irStd * 10000) / 10000;
          document.f.plrStd_field.value = Math.round(plrStd * 10000) / 10000;
          document.f.prrStd_field.value = Math.round(prrStd * 10000) / 10000;
          document.f.prrStd_string_field.value = prrStd_string;
  }
  
  document.f.mybutton.onclick = my_onclick;


