var round=1E8;
function sciVal(num){
 var exp=1, len=1, pose=0, posp=1, posz=1, r=0, rnd=" ", str=" ", out=" ";
 len=round.toString().length - 1;
 str=num.toString();
 pose=str.indexOf("e-");
 posp=str.lastIndexOf(".");
 if (pose == -1) {

// lastIndexOf method returns 0-relative index of last occurence of arg in string
   str=str.substr(posp+1);
   for (posz=0; str.substr(posz,1)=="0"; posz++) {continue};
   exp=posz+1;  // "E-" exponent value
   posz=posz-1; // pos of last leading 0 in fraction
   out=str.substring(posz+1,posz+2);
   if (len>0) {r = Math.round(str.substring(posz+2,posz+len+3)/10) * 10;
               rnd=r.toString();
               out=out + "." + rnd.substring(0,len) + "e-" + exp;}
   else       {out=out + "." + "000000000".substr(1,len+1) + "e-" + exp;}
   }
 else {
   exp=str.substr(pose+2);
   out=str.substring(0,posp); // any digits before the "."
   str=str.substring(posp+1,pose) + "0000000000";
   r=Math.round(str.substring(0,len+1)/10) * 10;
   rnd=r.toString();
   out=out + "." + rnd.substring(0,len) + "e-" + exp;
   }
 return out;
}

function reCalcTemp(){
var j=1, k=1, f=new String(" "), t=new String(" "), inn=0.0, out=3.1415926E1
with (document.tempForm) {
  j=from_temp.selectedIndex;
  k=to_temp.selectedIndex;
  f=from_temp.options[j].value;
  t=to_temp.options[k].value;
  inn=parseFloat(in_temp.value);
  if (inn==0 || parseFloat(inn)=='NaN') z=1.0;
  // Convert input temp to Celsius if necessary.  This saves many many
  // lines of if-else-if-else code.
  if (f=="C") inn += 0.0
   else 
   {if (f=="F") {inn = (inn - 32) / 1.8}
    else 
     {if (f=="K") {inn -= 273.15}
      else 
       {if (f=="RA") 
              {inn = ((inn - 491.67) / 1.8)} // -32, -459.67, *5/9 
         else {inn *= 1.25}
       }
     }
   }
  if (t=="C") {out = inn}
  else 
   {if (t=="F") {out = (1.8 * inn) + 32} // C->Fahr.
    else
     {if (t=="K") {out = inn + 273.15} // C->Kelvin
      else
       {if (t=="RA") {out = (1.8 * inn) + 491.67} // C->Fahr.->Rankine
        else {out = inn * 0.8} // C->Reaumur
       }
     }
   }
  outtemp.value=Math.round(out*round)/round;
  } // End of "with (document.tempForm)"
} // End of function

function reCalcLen(){
var j=1, k=1, w=0, x=0, y=0, z=3.1415926E1
with (document.lenForm) {
  j=from_len.selectedIndex;
  k=to_len.selectedIndex;
  x=from_len.options[j].value;
  y=to_len.options[k].value;
  z=parseFloat(in_len.value);
  if (z==0 || z=='NaN') z=1.0;
  w=z*(x/y);
// Math.round rounds argument to nearest INTEGER:
  x=Math.round(w*round)/round;
  if (x != 0) outlen.value=x; 
  else        outlen.value=sciVal(w)
  }
}

function reCalcArea(){
var j=1, k=1, w=0, x=0, y=0, z=3.1415926E1
with (document.areaForm) {
  j=from_area.selectedIndex
  k=to_area.selectedIndex
  x=from_area.options[j].value
  y=to_area.options[k].value
  z=parseFloat(in_area.value)
  if (z==0 || z=='NaN') z=1.0;
  w=z*(x/y);
  x=Math.round(w*round)/round;
  if (x != 0) outarea.value=x;
  else        outarea.value=sciVal(w)
  }
}

function reCalcWt(){
var j=1, k=1, w=0, x=0, y=0, z=3.1415926;
with (document.wtForm) {
  j=from_weight.selectedIndex;
  k=to_weight.selectedIndex;
  x=from_weight.options[j].value;
  y=to_weight.options[k].value;
  z=parseFloat(in_weight.value);
  if (z==0 || z=='NaN') z=1.0;
  w=z*(x/y);
  x=Math.round(w*round)/round;
  if (x != 0) outweight.value=x;
  else        outweight.value=sciVal(w)
  }
}

function reCalcVol(){
var j=1, k=1, x=0, y=0, z=3.1415926E1;
with (document.volForm) {
  j=from_vol.selectedIndex
  k=to_vol.selectedIndex
  x=from_vol.options[j].value
  y=to_vol.options[k].value
  z=parseFloat(in_vol.value)
  if (z==0 || z=='NaN') z=1.0;
  w=z*(x/y);
  x=Math.round(w*round)/round;
  if (x != 0) outvol.value=x;
  else        outvol.value=sciVal(w)
  }
}

function resetRnd(val){
round=val;
with (document) {
if ( parseFloat(tempForm.outtemp.value) != "NaN" && tempForm.outtemp.value !=0 ) reCalcTemp();
}
}
