// This calculates the time left to sober up (bac.php)
function soberUp( curBAC )
{
	timeLeft = curBAC / 0.015;
	hours = Math.floor( timeLeft );
	minutes = Math.round( ( timeLeft - hours ) * 100 ) * 0.6;
	minutes = Math.floor( minutes );
	return hours + " hr " + minutes + " min";
}

// This function calculates your BAC
// This is used in the Blood Alcohol Content Page (bac.php)
function updateBAC()
{
	// This is the conversion factor to change from Pounds (lbs) to Kilograms (kgs)
	convFactor = 0.45359237;		
	weight = 0;			
	numDrinks = document.getElementById( "numDrinks" );
	t = document.getElementById( "type" );
	s = document.getElementById( "sex" );
	tm = document.getElementById( "time" );
	
	drinks = numDrinks.options[ numDrinks.selectedIndex ].value;
	weight = parseInt( document.getElementById( "weight" ).value );		
	type = t.options[ t.selectedIndex ].value;
	sex = s.options[ s.selectedIndex ].value;
	time = tm.options[ tm.selectedIndex ].value;
    
    // This changes Pounds (lbs) to Kilograms (kg)
	if( type == "lbs" )
	{
		weight = weight * convFactor;
	}
	
	// This checks that the weight is a number
	if( !isNaN( weight ) && weight > 0 )
	{
	    // This is the formula for males
		if( sex == "m" )
		{
	         BAC = ( ( 12 * drinks ) / ( 7 * weight ) ) - ( 0.015 * time );
		}
		// This is the formula for females
	    else
	    {
			BAC = ( ( 2 * drinks ) / weight ) - ( 0.015 * time );
	    }
			
		// This is done to round the BAC to the nearest thousandth
		BAC = Math.round( BAC * 1000 ) / 1000;				
		// if the BAC is negative, display 0
		if( BAC <= 0 )
		{
			document.getElementById( "bac" ).value = 0;
				document.getElementById( "timeLeft" ).value = "";
		}
		// Display the BAC
		else
		{
			document.getElementById( "bac" ).value = BAC;
				document.getElementById( "timeLeft" ).value = soberUp( BAC );
		}
	}
	else
	{
		// The input weigt is not a number. Display Nothing
			document.getElementById( "bac" ).value = "";
			document.getElementById( "timeLeft" ).value = "";				
	}		
}