var arrFactors = new Array(5);
//var prevValue = 0;
var prevBox = null;
var decimalPlaces = 6;

arrFactors[1] = parseFloat('1.00000000000000E+0000');
arrFactors[2] = parseFloat('1.00000000000000E+0000');
//arrFactors[3] = parseFloat('1.00000000000000E+0000');
//arrFactors[4] = parseFloat('1.00000000000000E+0000');

function convertTemps(fromID, toID, val) {
  // do temperature conversions
  // 1 - C
  // 2 - F
  // 3 - K
  // 4 - R
  var FAH_RANK_DIFF = 459.67;
  var CEL_KELV_DIFF = 273.15;
  
  switch (fromID) {
  case 1: // deg C
    switch (toID) {
    case 1: return val;  // to deg C
    case 2: return val * 9 / 5 + 32;  // to deg F
    case 3: return val + CEL_KELV_DIFF;  // to deg K
    case 4: return val * 9 / 5 + 32 + FAH_RANK_DIFF; // to deg R
    }
  case 2: // deg F
    switch (toID) {
    case 1: return (val - 32) * 5 / 9;  // to deg C
    case 2: return val;  // to deg F
    case 3: return (val - 32) * 5 / 9 + CEL_KELV_DIFF;  // to deg K
    case 4: return val + FAH_RANK_DIFF; // to deg R
    }
  case 3: // deg K
    switch (toID) {
    case 1: return val - CEL_KELV_DIFF;  // to deg C
    case 2: return (val - CEL_KELV_DIFF) * 9 / 5 + 32;  // to deg F
    case 3: return val;  // to deg K
    case 4: return (val - CEL_KELV_DIFF) * 9 / 5 + 32 + FAH_RANK_DIFF; // to deg R
    }
  case 4: // deg R
    switch (toID) {
    case 1: return (val - FAH_RANK_DIFF - 32) * 5 / 9;  // to deg C
    case 2: return val - FAH_RANK_DIFF;  // to deg F
    case 3: return (val - FAH_RANK_DIFF - 32) * 5 / 9 + CEL_KELV_DIFF;  // to deg K
    case 4: return val; // to deg R
    }
  }
}

function roundDP(n, decplaces) {
  var t = Number('1e' + decplaces);
  return Math.round(n * t) / t;
}

function convert(form, id) {
  var val, fromFactor, i;
  
  // start at 1 since 0 is catid element
  val = parseFloat(form["input" + id].value);

  //if (isNaN(val) || prevValue == val) return;
  if (isNaN(val)) return;

  if (val == '' || isNaN(val)) val = 0;
  //fromFactor = arrFactors[id];
  for (i = 1; i <= 3; i++)    //5
    if (i != id) form["input" + i].value = roundDP(convertTemps(id, i, val), decimalPlaces);
}

function enterBox(form, id) {
  //prevValue = parseFloat(form["input" + id].value);
  // highlight the new box for visibility
  if (form["input" + id].style) {
    if (prevBox != null) {
      form["input" + prevBox].style.color = '';
      //document.getElementById('lbl' + prevBox).style.fontWeight = 'normal';
    }
    form["input" + id].style.color = '#0000ff';
    //document.getElementById('lbl' + id).style.fontWeight = 'bold';
  }
  prevBox = id;
}
