﻿// Functions used for imperial / metric conversions
// in the BMI calculator

function poundsToKilos(pounds)
{
	var kilos = pounds / 2.2046;
	return kilos;		
}

function kilosToPounds(kilos)
{
	var pounds = kilos * 2.2046;
	return pounds;
}

function to2DecPlaces(value)
{
	value = value * 100;
	value = Math.round(value);
	value = value / 100;
	return value;
}

function convertKilos()
{
	var kilos = document.getElementById('ctl00_Content_kgs').value -0;
	var totalPounds = kilosToPounds(kilos);
	var pounds = totalPounds % 14;
	var stones = (totalPounds - pounds) / 14;
	
	// round the pounds because we don't seem to be dealing with ounces
	pounds = Math.round(pounds);
	
	document.getElementById('ctl00_Content_stones').value = stones;
	document.getElementById('ctl00_Content_pounds').value = pounds;	
}

function convertPounds()
{
	var stones = document.getElementById('ctl00_Content_stones').value -0;
	var pounds = document.getElementById('ctl00_Content_pounds').value -0;
	var totalPounds = (stones * 14) + pounds;
	
	var kilos = poundsToKilos(totalPounds);	
	kilos = to2DecPlaces(kilos);
	document.getElementById('ctl00_Content_kgs').value = kilos;
}

function inchesToCM(inches)
{
	var CM = inches * 2.54;
	return CM;
}

function CMToInches(CM)
{
	var inches = CM / 2.54;
	return inches;
}

function convertMetres()
{
	var CM = (document.getElementById('ctl00_Content_metres').value -0) * 100;
	var totalInches = Math.round(CMToInches(CM));
	var inches = totalInches % 12;
	var feet = (totalInches - inches) / 12;
	
	document.getElementById('ctl00_Content_feet').value = feet;
	document.getElementById('ctl00_Content_inches').value = inches;
}

function convertInches()
{
	var inches = document.getElementById('ctl00_Content_inches').value-0;
	var feet = document.getElementById('ctl00_Content_feet').value-0;
	var totalInches = (feet * 12) + inches;
	
	document.getElementById('ctl00_Content_metres').value = to2DecPlaces(inchesToCM(totalInches) / 100);
}

function validateInput()
{
	var CM = (document.getElementById('ctl00_Content_metres').value-0) * 100;
	var kilos = document.getElementById('ctl00_Content_kgs').value-0;
	if ((!isNaN(CM)) && (!isNaN(kilos)) && (CM > 0) && (kilos > 0)) {
		return true;
		
	} 
	return false;
}