//------------------------------------------------------
// This function applies the appropriate conversion factor
// to the input value and displays the result.  Also fills
// the unit name boxes with the appropriate values.
//------------------------------------------------------
function Convert(form) {
  //  get the indexes of the selected options
  var i = form.toUnits.selectedIndex;
  var j = form.fromUnits.selectedIndex;

  // Get the selected from- and to-units names
  var toUnits = form.toUnits.options[i].text;
  var fromUnits = form.fromUnits.options[j].text;

  // Get the input value to convert
  var fromValue = form.fromValue.value;

  // Catenate the from- and to-units strings to form the conversion factor index
  var conversionFactor = conversionFactors[fromUnits + toUnits];

  //  Do the conversion
  var tmpValue = fromValue * conversionFactor;

  // Split the result into its integer and decimal fraction parts
  tmpString = new String(tmpValue);
  tmpValues = new Array();
  tmpValues = tmpString.split (".");

  // Just display the integer if there's no decimal fraction
  if (tmpValues[1] == null) {
     // Display a "tiny value" string if the integer contains e-notation
     if (tmpValues[0].lastIndexOf("e") != -1) {
	form.toValue.value = '0.000000...';
     } else {
	form.toValue.value = tmpValues[0];
     }
  } else {
     // Display a "tiny value" string if the fraction contains e-notation
     if (tmpValues[1].lastIndexOf("e") != -1) {
	form.toValue.value = '0.000000...';
     } else {
	// Get rid of tiny fractions (probably just floating point noise)
	var fracLength = tmpValues[1].length;
	if (fracLength > 8) {
	   tmpValues[1] = tmpValues[1].substring(0, 8);
	}
	// If the fraction is now zero, just display the integer
	if (tmpValues[1] == '00000000') {
	   form.toValue.value = tmpValues[0];
	} else {
	   // Truncate trailing zeroes from the fraction
	   while (tmpValues[1].substring(tmpValues[1].length - 1, tmpValues[1].length) == 0) {
	      tmpValues[1] = tmpValues[1].substring(0, tmpValues[1].length - 1);
	   }
	   // Display integer.fraction
	   form.toValue.value = tmpValues[0] + "." + tmpValues[1];
	}
     }
  }

  // Display the from- and to-units strings
  FillToUnitsBox(form, i);
  FillFromUnitsBox(form, j);

  return true;
}


//---------------------------------------------------------
//  Adds elements to the select list.  Uses the values 
//  hash in the appropriate conversions_xxxxx.js file
//---------------------------------------------------------
function CreateList(form) {
  var counter = 0;
  for (var key in values) {
    oNewOption = new Option();
    oNewOption.text = key;
    oNewOption.value = values[key];
    if (counter > 0) {
      document.write("<option value='" + values[key] + "'>" + key + "</option>");
    }
    else {
      document.write("<option selected value='" + values[key] + "'>" + key + "</option>");
    }
    counter += 1;
  }
}


//----------------------------------------------------------------------------
// Standard Functions
//----------------------------------------------------------------------------
function ClearValues(form) {
  form.toValue.value = "";
  var i = form.toUnits.selectedIndex;
  var j = form.fromUnits.selectedIndex;

  FillToUnitsBox(form, i);
  FillFromUnitsBox(form, j);
}

function FillFromUnitsBox(form, selectedIndex) {
  var fromUnitName = "";
  form.fromUnitName.value = form.fromUnits.options[selectedIndex].text;
}

function FillToUnitsBox(form, selectedIndex) {
  var toUnitName = "";
  form.toUnitName.value = form.toUnits.options[selectedIndex].text;
}

function CopyValue() {
  window.alert("in here");
  window.clipboardData.setData('Text', YahooConvert.toValue.value);
}
