

function round (inValue)
{
  return Math.round((inValue)*1000)/1000;
}

function selectField(currentForm) 
{
  var note = document.getElementById('note');
  var elems = currentForm.form.elements;
  for ( var elem, i = 0; ( elem = elems[i] ); i++ )
  {
    if ( elem.type == 'text' )
    {
        elem.style.backgroundColor="#FFF";
	 elem.style.borderColor="#A9B8C2";
    }
  }

  if (currentForm.value.length > 0 && currentForm.value.length < 15 )
  {
    note.innerHTML = "Last value selected: " + currentForm.value + " " + currentForm.id;
  }
  currentForm.value = "";
  currentForm.style.backgroundColor="#EAF2F9";
  currentForm.style.borderColor="#990606";
}

function calc(currentForm) 
{
  
  var elems = currentForm.form.elements;
  var inputValue = currentForm.value;
  var outputValue = 0;
  var inputFormula = eval(currentForm.name);

  
  for ( var elem, i = 0; ( elem = elems[i] ); i++ )
  {

    if (elem.type == 'text' && elem.id != currentForm.id)
    {
       elem.value = "";
       if (isNaN(inputValue) == false && inputValue.indexOf("-") == -1 && inputValue != "")
       {
	   outputFormula = eval(elem.name);
	   outputValue = eval(inputValue + "*(" +inputFormula + "/" + outputFormula + ")" ) ;
	   outputValue = round(outputValue);
          if (outputValue > 0 && String(outputValue).length < 15)
	   {
            elem.value = outputValue;
	   }
	} 

    }
  }
}

function calcOverride (currentForm)
{
  currentForm.onkeyup = new Function("calcTemp(this)");
}


function calcTemp(currentForm)
{
  var inputValue = currentForm.value;

  var c = document.getElementById("Celsius");
  var k = document.getElementById("Kelvin");
  var f = document.getElementById("Fahrenheit");

  var inputKelvin;

  switch (currentForm)
  {
     case c:
        inputKelvin = parseFloat(inputValue) + 273.15;
        break;
     case f:
        inputKelvin = ((5 * (parseFloat(inputValue) -32)) / 9) + 273.15 ;
        break;
     case k:
        inputKelvin = parseFloat(inputValue);
        break;
  }

  if (isNaN(inputValue) || inputKelvin < 0 || inputValue == "")
  {
     k.value = c.value = f.value = "";
  } else
  {
     k.value = round(inputKelvin);
     c.value = round(inputKelvin - 273.15);
     f.value = round(((9 * (inputKelvin - 273.15)) / 5) + 32);
  }

  currentForm.value = inputValue;
}


