function convertunit(fromValCont, toValCont, fromUnitCont, toUnitCont) {
  var FromVal, ToVal, FromName, ToName, v1;

  v1 = fromValCont.value;
  v1 = reformatString(v1);
  
  eval('v1 = parseFloat(' + v1 + ');');
  if (isNaN(v1)) {
	  v1 = 1;
}
  fromValCont.value = v1;

  FromVal = fromUnitCont[fromUnitCont.selectedIndex].value;
  ToVal = toUnitCont[toUnitCont.selectedIndex].value;
  FromName = fromUnitCont.options[fromUnitCont.selectedIndex].text;
  ToName = toUnitCont.options[toUnitCont.selectedIndex].text;

  if ((FromVal == "") || (ToVal == "")) {
		toValCont.value = "";
		return;
	}

  FromVal = NewEval(eval(FromVal));
  ToVal = NewEval(eval(ToVal));

  var v3 = eval("FromVal / ToVal");
  // brute force rounding error fix
  if ((FromName == "cup [US]") & (ToName == "ounce [US, liquid]"))  {
	  v3 = eval("8");
  } else if ((FromName == "cup [US]") & (ToName == "pony"))  {
	  v3 = eval("8");
  }

  var v2 = eval("v1 * v3");
  v2 = NewEval(v2);

  toValCont.value = v2; //scientificnotation(v2);
}

function resetanswer(toValCont) {
    toValCont.value = "";
}

function reformatString(string) {

	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 scientificnotation (num)
{
	num = num + '';

	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 IsCharCodeNumeric(charCode)
{
    return ((charCode > 47) && (charCode < 58));
}

function NewEval(number)
{
	if (number == undefined) return;

	var strInput = number.toString();
	

	var maxLength = 12;
	var charCode;
	var count = 0;	
	var maxLengthIndex = 0;
	var isLeadingZero = true; 
	var currIndex = 0;


	for(count = 0; count < strInput.length; count++)
	{
		charCode = strInput.charCodeAt(count);

		if (IsCharCodeNumeric(charCode))		
		{

			if ((charCode == 48) && !(isLeadingZero))
			{				
					currIndex++;
			}
			else if (charCode != 48)
			{				

				currIndex++;
				isLeadingZero = false;
			}
		}

		else if (!((charCode == 44) || (charCode == 46)))
		{

			return FormatOutPut(number);
		}
		

		if (currIndex == maxLength)  {maxLengthIndex = count; break;}
	}
	

	var indexOfe = strInput.indexOf('e');
    var expPart = '';
	count = strInput.length-1;
	if (indexOfe != -1) 
	{	
	    for(count = strInput.length-1; count >-1; count--)
	     {
			expPart = strInput.charAt(count) + expPart;
	        if (count == indexOfe) {break;};
	     }
		count--;
	}


	var numberPartToIgnore = '';
	for( ;count > -1; count --)
	{
		 charCode = strInput.charCodeAt(count);
	     if (IsCharCodeNumeric(charCode))
		 {
			 numberPartToIgnore = '0' + numberPartToIgnore;
		 }
		 else 
		 {
		     numberPartToIgnore = strInput.charAt(count) + numberPartToIgnore;
		  };

	     if (count == maxLengthIndex) {break;};
	}
	count--;

	var numberPart = '';
	if (strInput.charCodeAt(maxLengthIndex)>52)
	{
		var carryOverFlag = 1;
		for (;count > -1 ; count-- )
		{
			charCode = strInput.charCodeAt(count);
			if (IsCharCodeNumeric(charCode))
			{
				charCode = charCode + carryOverFlag;
				carryOverFlag = 0;				

				if (charCode == 58)
				{					
					charCode = 48;
					carryOverFlag = 1;
				}
				
				numberPart = String.fromCharCode(charCode) + numberPart;
			}
			else
			{
				numberPart = strInput.charAt(count) + numberPart;
			}
		}

		if (carryOverFlag == 1)
		{
			numberPart = "1" + numberPart;
		}
	}
	else
	{
		for( ;count>-1; count--)
	    {
			numberPart = strInput.charAt(count) + numberPart;
		}
	}


	  var result = (numberPart + numberPartToIgnore + expPart).valueOf() * 1;

  	  var diff  = Math.abs((result - number)/number); 
	  return ((diff < 1e-10) ? FormatOutPut(result): strInput);
}


function FormatOutPut(number)
{
	var strInput = number.toString();


	var indexOfe = strInput.indexOf('e');
	if (indexOfe == -1) return number;
	

	var indexOfDot = strInput.substr(0, indexOfe).indexOf('.');
	if (indexOfDot > -1) return number;
	

	var result = strInput.substr(0, indexOfe) + '.0' + strInput.substr(indexOfe, strInput.length);
	return result;
}





function convertTemp(fromValCont, toValCont, fromUnitCont, toUnitCont) {
  var FromVal, ToVal, FromName, ToName, v1;

  v1 = fromValCont.value;
  v1 = stripBad(v1);
  v1 = parseFloat(v1);
  if (isNaN(v1)) v1 = 0;
  fromValCont.value = v1;

  FromVal = fromUnitCont[fromUnitCont.selectedIndex].value;
  ToVal = toUnitCont[toUnitCont.selectedIndex].value;
  FromName = fromUnitCont.options[fromUnitCont.selectedIndex].text;
  ToName = toUnitCont.options[toUnitCont.selectedIndex].text;

  var ConvertedTemp = get_fact(v1, FromVal, ToVal);
  if (ConvertedTemp == "Below Absolute Zero"){
      toValCont.value = "Your input cannot be below absolute zero.";
  } else {
  toValCont.value = ConvertedTemp;
  }
}

function get_fact(ff,from_val,to_val){
 // first convert to kelvin
 if (from_val == 0){
   ff = ff + 273.15;
 } else if (from_val == 1){
   ff = ((ff - 32)/ 1.8) + 273.15;
 } else if (from_val == 2){
   ff = ff / 1.8;
 } else if (from_val == 3){
   ff = (ff * 1.25) + 273.15;
 }

 if (ff < 0){
   // Below absolute zero
   return "Below Absolute Zero";
 }

 // now convert kelvin to unit
 if (to_val == 0){
   ff = ff - 273.15;
 } else if (to_val == 1){
   ff = (1.8 * (ff -273.15)) + 32;
 } else if (to_val == 2){
   ff = ff * 1.8;
 } else if (to_val == 3){
   ff = (ff - 273.15) / 1.25;
 }
 
 // round it off
 if (Number.prototype.toFixed) {
   ff = ff.toFixed(7);
   ff = parseFloat(ff);
 }
 else {
   var leftSide = Math.floor(ff);
   var rightSide = ff - leftSide;
   ff = leftSide + Math.round(rightSide *10000000)/10000000;
 }

return ff;
}

function stripBad(string) {
    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;
} 

