
// CED subroutine for cleaning up JavaScript rounding errors 
// to any reasonable number of decimal places 5/5/1997 last mod 2/19/2004
// round for decimal of (value of precision) places, default is 3
// This routine can be used to pass a number and a number for precision
// or just a number only, that is to be rounded to a set number of decimal
// places. This routine supports leading and training zeros, leading and
// trailing spaces, and padding. To prevent errors, pass variables as a string.

function perRound(num, precision) {
	var precision = 3; //default value if not passed from caller, change if desired
	// remark if passed from caller
	precision = parseInt(precision); // make certain the decimal precision is an integer
    var result1 = num * Math.pow(10, precision);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, precision);
    return zerosPad(result3, precision);
}

function zerosPad(rndVal, decPlaces) {
    var valStrg = rndVal.toString(); // Convert the number to a string
    var decLoc = valStrg.indexOf("."); // Locate the decimal point
    // check for a decimal 
    if (decLoc == -1) {
        decPartLen = 0; // If no decimal, then all decimal places will be padded with 0s
        // If decPlaces is greater than zero, add a decimal point
        valStrg += decPlaces > 0 ? "." : "";
    }
    else {
        decPartLen = valStrg.length - decLoc - 1; // If there is a decimal already, only the needed decimal places will be padded with 0s
    }
     var totalPad = decPlaces - decPartLen;    // Calculate the number of decimal places that need to be padded with 0s
    if (totalPad > 0) {
        // Pad the string with 0s
        for (var cntrVal = 1; cntrVal <= totalPad; cntrVal++) 
            valStrg += "0";
        }
    return valStrg;
}
// send the value in as "num" in a variable

// clears field of default value
function clear_field(field) {
		if (field.value==field.defaultValue) {
			field.value=''
		}
	}

function c() {
   t = 20;//default percentage
   v = 3;//default voltage
   nF = 1000;
   uF = nF*nF;
   n1 = eval(document.ccc.first.selectedIndex);
   n2 = eval(document.ccc.second.selectedIndex);
   n3 = eval(document.ccc.mult.selectedIndex);
   v1 = eval(document.ccc.volt.selectedIndex);
   idx =eval(document.ccc.tol.selectedIndex);

// percentage tolerance
   if(idx == 1) t = 10;
   if(idx == 2) t = 5;

// voltage
   if(v1 == 1) v = 6.3;
   if(v1 == 2) v = 10;
   if(v1 == 3) v = 16;
   if(v1 == 4) v = 20;
   if(v1 == 5) v = 25;
   if(v1 == 6) v = 35;
  
   factor = ((n1 * 10)+n2)*(Math.pow(10,n3));
   if(factor == "") return;

   if(factor < nF) fact = "pF";
   if((factor >= nF) && (factor < uF)) {
      factor = factor/nF;
      fact = "nF";
   }
   if(factor >= uF) {
      factor = factor/uF;
      fact = "uF";
   }
   r1 = factor - ((t * factor)/100);
   r2 = factor + ((t * factor)/100);
   r1 = parseInt((r1*100)+0.5)/100;
   r2 = parseInt((r2*100)+0.5)/100;
   
   txt = " "+r1+" to "+r2+" "+fact;
   txt = txt + "    ("+factor+" "+fact+"  +/-"+t+"%) " +v+ " volts";
   document.ccc.capval.value = txt;
}
