
var sCurrentSystem;





/* argument is string either imperial or metric
basically sets labels


*/
function setSystem(sSystem){
	
	switch (sSystem){
		case "imperial":
			show(sSystem);
			hide("metric");
			break;
		case "metric":
			show(sSystem);
			hide("imperial");
			break;
		default:
			alert("Please select either imperial or metric");
			return false;
	}
	
	sCurrentSystem = sSystem;
	document.getElementById(sSystem).checked = true;
	
}

function show(sSystem){
	document.getElementById(sSystem+"_height").style.display = "";
	document.getElementById(sSystem+"_weight").style.display = "";
}

function hide(sSystem){
	document.getElementById(sSystem+"_height").style.display = "none";
	document.getElementById(sSystem+"_weight").style.display = "none";
}


function calculateBMI(){
	
	switch (sCurrentSystem){
		case "imperial":
			iWeightPounds = calculateWeightPounds();
			iHeightInches = calculateHeightInches();
			updateBMI(calculateBMIImperial(iWeightPounds,iHeightInches));
			break;
		case "metric":
			iWeightKilograms = calculateWeightKilograms();
			iHeightMetres = calculateHeightMetres();
			updateBMI(calculateBMIMetric(iWeightKilograms,iHeightMetres));
			break;
		default:
			alert ("Please select either imperial or metric");
			return false;
	}
	
}

function calculateWeightPounds(){
	// 1 stone = 14 pounds
	iWeightStone = document.getElementById('weight_stone').value | 0;
	iWeightPounds = document.getElementById('weight_pounds').value | 0;
	iStoneToPounds = iWeightStone * 14 | 0;
	return (iStoneToPounds + iWeightPounds);
}

function calculateHeightInches(){
	// 1 foot = 12 inches
	iHeightFeet = document.getElementById('height_feet').value | 0;
	iHeightInches = document.getElementById('height_inches').value | 0;
	
	iFeetToInches = iHeightFeet * 12 | 0;
	return (iFeetToInches + iHeightInches);
	
}

function calculateWeightKilograms(){
	return document.getElementById('weight_kilograms').value | 0;
}

function calculateHeightMetres(){
	return (document.getElementById('height_centimetres').value | 0) / 100;
}

function calculateBMIImperial(iWeightPounds,iHeightInches){
	return (iWeightPounds * 703) / square(iHeightInches);
}


function calculateBMIMetric(iWeightKilograms,iHeightMetres){
	return (iWeightKilograms) / square(iHeightMetres);
}

function updateBMI(iResult){
	
	document.getElementById('bmiinfo').style.display='';
	
	// make result pretty
	iResult = iResult.toFixed(1);
	
	if(isNaN(iResult)) {
		document.getElementById('bmiresult').innerHTML  = 'We are unable to determine your BMI with these values.';
		return false;
	}
	document.getElementById('bmiinfo').style.display = "";
	document.getElementById('bmiresult').innerHTML = iResult;
	
	showDetailBox(iResult);
}

function showDetailBox(iResult){
	
	
	if (iResult < 18.5){
		
		//alert("underweight");
		
	} else if (iResult < 24.9) {
		
		//alert("normal");
		
	} else if (iResult < 29.9) {
		
		//alert("fatty");
		
	} else if (iResult >= 30.0){
		
		//alert("wow you're huge");
		
	}
	
}

/* can't believe there's no function in javascript for squaring a number! 
not that it's particularly hard to do of course...*/
function square(iNum) {
	
	return iNum * iNum;

}

