var BMICalculator = {
	
	Input_Onfocus:{
		TextTransaction:function( id )
		{
			if( !isNumeric( $(id).value ) )
			{
				$(id).value = '';
			}
		}
	},
	
	Input_Onblur : {
		TextTransaction:function( id )
		{
			if( $(id).value == '' )
			{
				switch(id)
				{
					case 'heightFeet':
						$(id).value = 'feet';
						break;
					case 'heightInches':
						$(id).value = 'inches';
						break;
					case 'weightStones':
						$(id).value = 'stones';
						break;
					case 'weightPounds':
						$(id).value = 'pounds';
						break;
					case 'heightCentimetres':
						$(id).value = 'centermetres';
						break;
					case 'weightKilograms':
						$(id).value = 'kilograms';
						break;			
					default:		
						$(id).value = '';
				}
			}
		}
	},
	
	BMIContent : {
		Switch_Standard:function( id ){
			if( id == "contentStandard" )
			{
				if( $(id ).style.display == "none" )
				{
					$(id ).show();
					$("contentMetric").hide();
					$("tabStandard").setAttribute( "class","sel" );
					$("tabMetric").setAttribute( "class","" );
				}
			}
			else if ( id == "contentMetric" )
			{
				if( $(id ).style.display == "none" )
				{	
					$(id ).show();
					$("contentStandard").hide();
					$("tabMetric").setAttribute( "class","sel" );
					$("tabStandard").setAttribute( "class","" );
				}	
			}
			return false;
		}	
	},
	
	BMIstandard : {

		BMIcalculation:function()
		{
			var feet = 0;
			var	inches = 0;
			var stones = 0;
			var pounds = 0;
			
			if( (!isNumeric( $("heightFeet").value) && !isNumeric( $("heightInches").value )) || ($("heightFeet").value == 0 && $("heightInches").value== 0 ))
			{
				return false;
			}			

			if( !isNumeric( $("heightFeet").value ) )
			{
			
				$("heightFeet").value = feet;
			}
			else
			{
				feet = parseFloat( $("heightFeet").value );
			} 
			
			if( !isNumeric( $("heightInches").value ) )
			{
				$("heightInches").value = inches;
			}
			else
			{	
				inches = parseFloat( $("heightInches").value );
			} 
			
			if( !isNumeric( $("weightStones").value ) )
			{
				$("weightStones").value = stones;
			}
			else
			{
				stones = parseFloat( $("weightStones").value );
			} 

			if( !isNumeric( $("weightPounds").value ) )
			{
				$("weightPounds").value = pounds;
			}
			else
			{
				pounds = parseFloat( $("weightPounds").value );
			} 		
			
			var result = ( stones * 14 + pounds ) * 703 /(( feet * 12 + inches )*( feet * 12 + inches ));
			var status = BMIrange( result.toFixed(1) );

			$("standardResult").update( "Your BMI is <span class='result'>"+result.toFixed(1)+"</span>.<br /><span class='BMIstatus'>"+status+"</span>" );
			$("standardResult").style.display = "block";
			$("formStandard").hide();
			$("standard_backBT").style.display = "block";
			return false;
		}							
	},
	
	BMImetric : {
		BMIcalculation:function()
		{	
			var centermetres = 0;
			var kilograms = 0;
			
			if( !isNumeric( $("heightCentimetres").value ) || $("heightCentimetres").value == 0 )
			{
				return false;
			}
			else
			{
				centermetres = parseFloat( $("heightCentimetres").value );
			} 
					
			if( !isNumeric( $("weightKilograms").value ) )
			{
				$("weightKilograms").value = kilograms;
			}
			else
			{
				kilograms = parseFloat( $("weightKilograms").value );
			} 

			var result = kilograms * 10000 /( centermetres * centermetres );
			var status = BMIrange( result.toFixed(1) );
			
			$("metricResult").update( "Your BMI is <span class='result'>"+result.toFixed(1)+"</span>.<br /><span class='BMIstatus'>"+status+"</span>");
			$("metricResult").style.display = "block";
			$("formMetric").hide();
			$("metric_backBT").style.display = "block";
			return false;
		}	
	},
	
	Back_Button : {
		Load:function( id ){
			if( id == "standard_backBT")
			{
				$("standardResult").update("");
				$("standardResult").hide();
				$(id).hide();
				$("formStandard").show();
			}
			else if( id == "metric_backBT" ) 
			{
				$("metricResult").update("");
				$("metricResult").hide();
				$(id).hide();
				$("formMetric").show();			
			}
		}
	}
	
}


function isNumeric(x) { 
	var y=parseFloat(x); 
	if (isNaN(y)) {
		return false; 
	} else {
		return true;
	}
} 

function BMIrange(x) {
	var y=parseFloat(x);
	var status = "";
	
	if( y < 18.5)
	{
		status = "You are underweight.";
	}
	else if( (y >= 18.5) && (y <= 24.9) )
	{
		status = "You are normal.";
	}
	else if( (y >= 25) && (y <= 29.9) )
	{
		status = "You are overweight. ";
	}
	else if( y >= 30 ) 
	{
		status = "You are obese.";
	}
	return status;
}
