// This file is copyright 2007 by OnlineConversion.com
// Not for use on any other website.
// Protected by CopyScape, http://www.copyscape.com/about.php


function eval_and_fix(formula){
  var input_number;
  
  input_number = eval(formula) + '';

  // If scientific notation, return
  if ((input_number.indexOf("e") > -1) || (input_number.indexOf("E") > -1)){
	return input_number;
  }

  // if an integer, return
  if (input_number.indexOf('.') == -1){
	return input_number;
  }

  // Seperate and round to 12
  var first, last, split_at;

  split_at = input_number.indexOf('.');

  first = input_number.substr(0,split_at);

  last = input_number.substr(split_at, input_number.length - split_at);
  last = Math.round(last * 1000000000000);
  last /= 1000000000000;

  return parseInt(first) + last;

}

function netscapeKeyPress(e) {
    if (e.which == 13)
        myCon();
}

function microsoftKeyPress() {
    if (window.event.keyCode == 13)
        myCon();
}

if (navigator.appName == 'Netscape') {
    window.captureEvents(Event.KEYPRESS);
    window.onKeyPress = netscapeKeyPress;
}

function myCon() {
  var FromVal, ToVal, FromName, ToName, v1, Factor;

  v1 = document.MainForm.what.value;
  v1 = stripBad(v1);
  document.MainForm.what.value = v1;
  
  eval('v1 = parseFloat(' + v1 + ');');
  if (isNaN(v1)) {
	  v1 = 1;
	  document.MainForm.what.value = v1;
  }
  //v1 = Math.abs(v1);
  
  FromVal = document.MainForm.from[document.MainForm.from.selectedIndex].value;
  ToVal = document.MainForm.to[document.MainForm.to.selectedIndex].value;
  FromName = document.MainForm.from.options[document.MainForm.from.selectedIndex].text;
  ToName = document.MainForm.to.options[document.MainForm.to.selectedIndex].text;

	if ((FromVal == "") || (ToVal == "")){
		document.MainForm.answer.value = "";
		return;
	}

  FromVal = eval_and_fix(FromVal);
  ToVal = eval_and_fix(ToVal);

  // calculate the factor
  var times_under = 0;
  if (ToVal < 1e-6){
	  while (ToVal < 1e-1){
		  ToVal *= 10;
		  times_under++;
	  }
   ToVal = eval_and_fix(ToVal);
  }

  Factor = eval_and_fix(FromVal / ToVal);

  while (times_under > 0){
	  Factor *= 10;
	  times_under--;
  }

  document.MainForm.answer.value = space(v1) + " " + FromName + " = " + space(eval_and_fix(v1 * Factor)) + " " + ToName;
}

function resetanswer() {
  document.MainForm.answer.value = "";
}

function stripBad(string) {
	// If a space is next to two numbers, change to a + sign, for fractions
	// But only if number also contains a / character
	if (string.match(/\//)){
	  while (string.match(/(\d) (\d)/)){
		string = string.replace(/(\d) (\d)/, "$1\+$2");
	  }
	}

    for (var i=0, output='', valid="eE+/*-0123456789.()"; i<string.length; i++)
       if (valid.indexOf(string.charAt(i)) != -1)
          output += string.charAt(i)
    return output;
} 

function space (num)
{
	num = num + '';
	// exit if scientific notation
	if (num.indexOf('e') > -1){ return num; }

	var dec = num.indexOf('.');

	var left, right = '';
	if (dec >= 0)
	{
		left = num.substring(0, dec);
		right = num.substring(dec + 1);
	}
	else
		left = num;

	var new_left = '', new_right = '';
	for (var i = 0; i < right.length; i++)
	{
		new_right += right.charAt(i);
		if (i % 3 == 2 && i != right.length - 1)
			new_right += ' ';
	}
	for (var i = left.length - 1; i >= 0; i--)
	{
		new_left = left.charAt(i) + new_left;
		if ((left.length - 1 - i) % 3 == 2 && i != 0)
			new_left = ' ' + new_left;
	}

	return (dec >= 0) ? new_left + '.' + new_right : new_left;
}


function helpQty(){
	var myMessage = '';
	myMessage = "- Conversion Help -\n\n";
	myMessage += "This is the amount of the 'From' unit you want to convert to the 'To' unit.\n\n";
	myMessage += "You can enter a basic quantity value, such as\n10\n100\n1267.9874\n\n";
	myMessage += "To enter fractions, seperate fractions from whole numbers with a + sign.\n7/8\n2+3/4\n1+1/2\n\n";
	myMessage += "Or you may enter formulas, such as\n100+37+99\n33 * 56 + (100-23) * 17\n93*56-13*4\n\n";
	myMessage += "To enter numbers in scientific notation, be sure to use the 'e' style of notation. For example\n";
	myMessage += "1.0e-15\n9.3478e+23\n1.0e+18";

	alert(myMessage);
	return;
}

function helpResult(){
	var myMessage = '';
	myMessage = "- Conversion Help -\n\n";
	myMessage += "This is the result of your conversion, suitable for copy and paste as needed. ";
	myMessage += "Select the text you wish to copy and press Ctrl-C to copy then press Ctrl-V to paste it into the application of your choice.";
	myMessage += "\n\nThe format used follows the international standard called the SI standard. ";
	myMessage += "Digits are separated by spaces in groups of three, this makes them easier to read. ";
	myMessage += "Some countries use the comma as a separator which may be confused with the period so the SI standard is preferred.";
	myMessage += "\n\nScientific notation is displayed in the 'e' format, such as 1.0e-18 or 1.0e+24";

	alert(myMessage);
	return;
}

