// Javascript for unit conversions (Gauss <=> Tesla)
// Added Jan 13, 2007 by Justin Faulk

  var Input = new Object();
  var Output = new Object();
  Input.Value = 0;
  Input.Unit  = "nT";
  Output.Unit = "mG";

  function CalculateAnswer() {

    Input.subUnit   = Input.Unit.substring(0,1);
    Input.Exponent  = getExponent(Input.subUnit);
    Input.mainUnit  = Input.Unit.substring(1,2);

    Output.subUnit  = Output.Unit.substring(0,1);
    Output.Exponent = getExponent(Output.subUnit);
    Output.mainUnit = Output.Unit.substring(1,2);

    var ConversionExponent = 0;
    if ((Input.mainUnit == "G") && (Output.mainUnit == "T")) {
      ConversionExponent = -4;
    } else if ((Input.mainUnit == "T") && (Output.mainUnit == "G")) {
      ConversionExponent = 4;
    }

    // Do the conversion by moving the decimal point
    Output.Value = Input.Value * Math.pow(10,Input.Exponent + ConversionExponent - Output.Exponent);

    // Scale to three decimal points
    //Output.Value = Output.Value.toFixed(3);

    // Make sure answer is a valid number (ie, if they entered characters instead of numbers)
    if (isNaN(Output.Value)) {
      document.getElementById("Calculation").innerHTML = "Invalid input - numbers only!";
      document.getElementById("Answer").innerHTML = "";
      return;
    }

    // Set the output display
    document.getElementById("Answer").innerHTML = Output.Value;

    var ConversionText = "";
    if (ConversionExponent == -4) {
      ConversionText = " x .0001 T/G";
    } else if (ConversionExponent == 4) {
      ConversionText = " x 10000 G/T";
    }

    /*
    document.getElementById("Calculation").innerHTML =
      Input.Value + " " + Input.Unit + " = <br>" +
      Input.Value + "<font size=-1>E</font>" + Input.Exponent + " " + Input.mainUnit + ConversionText + " = <br>" +
      Output.Value + "<font size=-1>E</font>" + Output.Exponent + " " + Output.mainUnit + " = <br>" +
      Output.Value + " " + Output.Unit;
    */
  }


  // Based on passed subUnit (m for milli, u for micro, n for nano, p for pico), return N for 10^N exponent
  function getExponent(subUnit) {
    var Exponent = null;
    switch (subUnit) {
      case "m": Exponent = -3;  break;
      case "u": Exponent = -6;  break;
      case "n": Exponent = -9;  break;
      case "p": Exponent = -12; break;
      default: Exponent = 0;
    }
    return Exponent;
  }