var StonesToKilos  = 6.35029318;
var PoundsToKilos  = 0.45359237;
var FeetToInches   = 12;
var FeetToCm       = 30.48;
var InchesToCm     = 2.54;
var GramsToOunces  = 0.0352739619;
var stonesToPounds = 14;

function CmToInch(Cm)
{
	if (0 < parseFloat(Cm))
	{
		return parseFloat(Cm)/InchesToCm;
	}
	else
	{
		return 0;
	}
}
function InchToCm(Inch)
{
	return parseFloat(Inch) * InchesToCm;
}

function kiloToPounds(k)
{
	var k=parseFloat(k);
	var total=0;
	if (!isNaN(k))
	{
		total += k/PoundsToKilos;
	}
	return total;
}
function poundsToKilos(p)
{
	var p=parseFloat(p);
	var total=0;
	if (!isNaN(p))
	{
		total += p*PoundsToKilos;
	}
	return total;
}

function stonesToKilo(Stones,Pounds)
{
	var total = 0;
	if(!isNaN(parseInt(Stones,10)))
	{
		total += parseInt(Stones,10)*StonesToKilos;
	}
	if(!isNaN(parseInt(Pounds,10)))
	{
		total += parseInt(Pounds,10)*PoundsToKilos;
	}
	return total;
}

function kiloToStonesArray(Kilos)
{
	var totalStones = 0;
	var totalPounds = 0;
	var Kilos = parseFloat(Kilos);

	if (0 <= Kilos)
	{
		totalStones += Math.floor(Kilos/StonesToKilos);
		if (0 < (Kilos-(totalStones*StonesToKilos)))
		{
			totalPounds += (Kilos-(totalStones*StonesToKilos))/PoundsToKilos
		}
		return new Array(totalStones,totalPounds);
	}
	else
	{
		return null;
	}
}

function feetToCm(Feet,Inches)
{
	var total = 0;
	if(!isNaN(parseInt(Feet,10)))
	{
		total += parseInt(Feet,10)*FeetToCm;
	}
	if(!isNaN(parseInt(Inches,10)))
	{
		total += parseInt(Inches,10)*InchesToCm;
	}
	return total;
}

function feetToInch(Feet,Inches)
{
	var total = 0;
	if(!isNaN(parseInt(Feet,10)))
	{
		total += parseInt(Feet,10)*FeetToInches;
	}
	if(!isNaN(parseInt(Inches,10)))
	{
		total += parseInt(Inches,10);
	}
	return total;
}


function inchToFeetArray(In)
{
	var totalFeet   = 0;
	var totalInches = 0;
	var In = parseInt(In,10);

	if (0 <= In)
	{
		totalFeet += Math.floor(In/FeetToInches);
		if (0 <= (In-(totalFeet*FeetToInches)))
		{
			totalInches += (In-(totalFeet*FeetToInches));
		}
		return new Array(totalFeet,totalInches);
	}
	else
	{
		return null;
	}
}

function cmToFeetArray(Cm)
{
	var totalFeet   = 0;
	var totalInches = 0;
	var Cm = parseFloat(Cm);

	if (0 <= Cm)
	{
		totalFeet += Math.floor(Cm/FeetToCm);
		if (0 < (Cm-(totalFeet*FeetToCm)))
		{
			totalInches += (Cm-(totalFeet*FeetToCm))/InchesToCm
		}
		return new Array(totalFeet,totalInches);
	}
	else
	{
		return null;
	}
}
function cmToInches(Cm)
{
	var totalInches = 0;
	var Cm = parseFloat(Cm);
	if (0 <= Cm)
	{
		totalInches += Cm/InchesToCm
		return 	totalInches;
	}
	else
	{
		return null;
	}
}

function metresToCm(m)
{
	var totalCm = 0;
	var m = parseFloat(m);
	if (0 <= m)
	{
		totalCm += m*100
		return Math.round(totalCm*10)/10;
	}
	else
	{
		return null;
	}
}
function cmToMetres(Cm)
{
	var totalMetres = 0;
	var Cm = parseFloat(Cm);
	if (0 <= Cm)
	{
		totalMetres += Cm/100
		return Math.round(totalMetres*1000)/1000;
	}
	else
	{
		return null;
	}
}


function doBMICalc(p_height_id,p_weight_id,p_bmi_id)
{
	if (null != $(p_height_id) && null != $(p_weight_id) && null != $(p_bmi_id) )
	{
		if (0<parseFloat($(p_height_id).value) && 0<parseFloat($(p_weight_id).value))
		{
			$(p_bmi_id).value = Math.round(calculateBMI(parseFloat($(p_height_id).value)/100,parseFloat($(p_weight_id).value))*100)/100
		}
	}
}

function calculateBMI(HeightCm,WeightKg)
{
	return WeightKg / (HeightCm*HeightCm);
}








function cupsToKilos()
{
	
}

function cupsToOunces()
{
	
}
function cupsToPounds()
{
	
}
function kiloToCups()
{
	
}

function gramsToKilos(g)
{
	total = 0;
	var g = parseFloat(g);
	if (!isNaN(g))
	{
		total = g/1000;
	}
	return total;
}
function kiloToGrams(k)
{
	total = 0;
	var k = parseFloat(k);
	if (!isNaN(k))
	{
		total = k * 1000;
	}
	return total;
}

function kiloToOunces(k)
{
	total = 0;
	var k = parseFloat(k);
	if (!isNaN(k))
	{
		total = kiloToGrams(k) * GramsToOunces;
	}
	return total;
}

function ouncesToCups()
{
	
}

function poundToStonesArray(p)
{
	var totalStones = 0;
	var totalPounds = 0;
	var p = parseFloat(p);

	if (0 <= p)
	{
		totalStones += Math.floor(p/stonesToPounds);
		if (0 < (p-(totalStones*stonesToPounds)))
		{
			totalPounds += (p-(totalStones*stonesToPounds))/stonesToPounds
		}
		return new Array(totalStones,totalPounds);
	}
	else
	{
		return null;
	}
}

function stonesToPound(s)
{
	var s = parseFloat(s);
	var total = 0;
	if(!isNaN(s))
	{
		total += s*stonesToPounds;
	}
	return total;
}

function poundsToOunces(p)
{
	var p=parseFloat(p);
	var total=0;
	if (!isNaN(p))
	{
		total += p*16;
	}
	return total;
}
function ouncesToPounds(o)
{
	var total = 0;
	var o = parseFloat(o);
	if (!isNaN(o))
	{
		total = o/16;
	}
	return total;
}
function ouncesToKilos(o)
{
	var total = 0;
	var o = parseFloat(o);
	if (!isNaN(o))
	{
		total = gramsToKilos((o/GramsToOunces));
	}
	return total;

}
