// This file contains all the functions need for the Unit Converter

// 1 Liter = 33.81402270184300 Ounces
// This is the conversion factor for Liter to Ounces
factor = 33.81402270184300;

// This will change the value of C
function changeC( amount )
{
	temp = 0;
	l = document.getElementById( "unitLeft" );
	r = document.getElementById( "unitRight" );
	
	leftUnit = l.options[ l.selectedIndex ].value;	
	rightUnit = r.options[ r.selectedIndex ].value;
	
	// If both units are the same don't change anything
	if( leftUnit == rightUnit )
	{
		return amount;
	}
	// Standard to Standard
	else if( leftUnit < 11 && rightUnit < 11 )
	{
		temp = standardToOunce( amount, leftUnit );
		temp = ounceToStandard( temp, rightUnit );
		return temp;
	}
	// Metric to Metric
	else if( leftUnit > 10 && rightUnit > 10 )
	{
		temp = metricToLiter( amount, leftUnit );
		temp = literToMetric( temp, rightUnit );
		return temp;
	}
	// Standard to Metric
	// Change standard amount to ounces
	// Change ounces to liters
	// Change liters to the correct metric unit
	else if( leftUnit < 11 && rightUnit > 10 )
	{
		temp = standardToOunce( amount, leftUnit );
		temp = ounceToLiter( temp );
		temp = literToMetric( temp, rightUnit );
		return temp;
	}
	// Metric to Standard
	// Change metric amount to liters
	// Change liters to ounces
	// Change ounces to the correct standard unit
	else if( leftUnit > 10 && rightUnit < 11 )
	{
		temp = metricToLiter( amount, leftUnit );
		temp = literToOunce( temp );
		temp = ounceToStandard( temp, rightUnit );
		return temp;
	}
	else
	{
		return 0;
	}
}

// This will change the value of A
function changeA( amount )
{
	temp = 0;
	l = document.getElementById( "unitLeft" );
	r = document.getElementById( "unitRight" );
	
	leftUnit = l.options[ l.selectedIndex ].value;	
	rightUnit = r.options[ r.selectedIndex ].value;	
	
	// If both units are the same don't change anything
	if( leftUnit == rightUnit )
	{
		return amount;
	}
	// Standard to Standard
	else if( leftUnit < 11 && rightUnit < 11 )
	{
		temp = standardToOunce( amount, rightUnit );
		temp = ounceToStandard( temp, leftUnit );
		return temp;
	}
	// Metric to Metric
	else if( leftUnit > 10 && rightUnit > 10 )
	{
		temp = metricToLiter( amount, rightUnit );
		temp = literToMetric( temp, leftUnit );
		return temp;
	}
	// Standard to Metric
	// Change standard amount to ounces
	// Change ounces to liters
	// Change liters to the correct metric unit
	else if( leftUnit > 10 && rightUnit < 11 )
	{
		temp = standardToOunce( amount, rightUnit );
		temp = ounceToLiter( temp );
		temp = literToMetric( temp, leftUnit );
		return temp;
	}
	// Metric to Standard
	// Change metric amount to liters
	// Change liters to ounces
	// Change ounces to the correct standard unit
	else if( leftUnit < 11 && rightUnit > 10 )
	{
		temp = metricToLiter( amount, rightUnit );
		temp = literToOunce( temp );
		temp = ounceToStandard( temp, leftUnit );
		return temp;
	}
	else
	{
		return 0;
	}
}

// This will change the a metric amount (ml, cl, dl, L) to Liters
function metricToLiter( amount, unit )
{
	switch( unit )
	{
		// Milliliter to Liter
		case "11":
		  return amount * 0.001;
		  break;
		// Centiliter to Liter
		case "12":
		  return amount * 0.01;
		  break;
		// Deciliter to Liter
		case "13":
		  return amount * 0.1;
		  break;
		// Liter to Liter
		case "14":
		  return amount;
		  break;
		// Nothing
		default:
		  return 0;
	}
}

// This will change a Liter to a metric (ml, cl, dl, L)
function literToMetric( amount, unit )
{
	switch( unit )
	{
		// Liter to Milliliter
		case "11":
		  return amount * 1000;
		  break;
		// Liter to Centiliter
		case "12":
		  return amount * 100;
		  break;
		// Liter to Deciliter
		case "13":
		  return amount * 10;
		  break;
		// Liter to Liter
		case "14":
		  return amount;
		  break;
		// Nothing
		default:
		  return 0;
	}
}

// This function changes liters to ounces
function literToOunce( liter )
{
	return liter * factor;
}

// This function changes ounces to liters
function ounceToLiter( ounce )
{
	return ounce / factor;
}

// This function changes a standard unit to ounces
function standardToOunce( amount, unit )
{
	switch( unit )
	{
		// Dash to Ounces
		case "1":
		  return amount / 32;
		  break;
		// Teaspoon to Ounces
		case "2":
		  return amount / 8;
		  break;
		// Tablespoon to Ounces
		case "3":
		  return amount * (3 / 8);
		  break;
		// Ounce to Ounce
		case "4":
		  return amount;
		  break;
		// Pony to Ounces
		case "5":
		  return amount;
		  break;
		// Jigger to Ounces
		case "6":
		  return amount * 1.5;
		  break;
		// Cup to Ounces
		case "7":
		  return amount * 8;
		  break;
		// Pint to Ounces
		case "8":
		  return amount * 16;
		  break;
		// Quart to Ounces
		case "9":
		  return amount * 32;
		  break;
		// Gallon to Ounces
		case "10":
		  return amount * 128;
		  break;
		default:
		  return 0;
	}
}

// This function changes a ounces to standard unit
function ounceToStandard( amount, unit )
{
	switch( unit )
	{
		// Ounces to Dashes
		case "1":
		  return amount * 32;
		  break;
		// Ounces to Teaspoons
		case "2":
		  return amount * 8;
		  break;
		// Ounces to Tablespoons
		case "3":
		  return amount * (8/3);
		  break;
		// Ounce to Ounce
		case "4":
		  return amount;
		  break;
		// Ounce to Ponys
		case "5":
		  return amount;
		  break;
		// Ounces to Jiggers
		case "6":
		  return amount / 1.5;
		  break;
		// Ounces to Cups
		case "7":
		  return amount / 8;
		  break;
		// Ounces to Pints
		case "8":
		  return amount / 16;
		  break;
		// Ounces to Quarts
		case "9":
		  return amount / 32;
		  break;
		// Ounces to Gallons
		case "10":
		  return amount / 128;
		  break;
		default:
		  return 0;
	}
}

function changeRight()
{
    amountLeft = 0.0;
		amountLeft = parseFloat( document.getElementById( "amountLeft" ).value );
		if( !isNaN( amountLeft ) )
		{
			 document.getElementById( "amountRight" ).value = changeC( amountLeft );
		}
		else
		{
		    document.getElementById( "amountRight" ).value = "";
		}    
}

function changeLeft()
{
    amountRight = 0.0;
		amountRight = parseFloat( document.getElementById( "amountRight" ).value );
		if( !isNaN( amountRight ) )
		{
				document.getElementById( "amountLeft" ).value = changeA( amountRight );
		}
		else
		{
				document.getElementById( "amountLeft" ).value = "";
		} 
}


