/* Weight Converter */

function forward_pound() {
	with (document.weight_converter) {
		unit1_pound.value = unit1_pound.value.toString().replace(/[^\d\.eE-]/g,'');
		if (unit1_pound.value*0.45359237 != 0)
			unit2_pound.value = unit1_pound.value*0.45359237;
	}
		
}
function backward_pound() {
	with (document.weight_converter) {
		unit2_pound.value = unit2_pound.value.toString().replace(/[^\d\.eE-]/g,'');
		if (unit2_pound.value/0.45359237 != 0)
			unit1_pound.value = unit2_pound.value/0.45359237;
		}
}

function forward_stone() {
	with (document.weight_converter) {
		unit1_stone.value = unit1_stone.value.toString().replace(/[^\d\.eE-]/g,'');
		if (unit1_stone.value*6.35029318 != 0)
			unit2_stone.value = unit1_stone.value*6.35029318;
	}
}
function backward_stone() {
	with (document.weight_converter) {
		unit2_stone.value = unit2_stone.value.toString().replace(/[^\d\.eE-]/g,'');
		if (unit2_stone.value/6.35029318 != 0)
			unit1_stone.value = unit2_stone.value/6.35029318;
	}
}
function clearForm2(){
	with (document.weight_converter) {
		unit1_stone.value = 'Stone';
		unit2_stone.value = 'Kilos';
		unit1_pound.value = 'Pounds';
		unit2_pound.value = 'Kilos';	
	}
}

/* BMI */


function CalcBMI() {
    Weight = document.Input.Weight.value;
	Weight = Weight.toString().replace(/[^\d\.eE-]/g,'');
	
    WeightOunces = document.Input.weight2.value;
	WeightOunces = WeightOunces.toString().replace(/[^\d\.eE-]/g,'');
	
	WeightPounds = document.Input.weight2.value;
	WeightPounds = WeightPounds.toString().replace(/[^\d\.eE-]/g,'');
	
    Height = document.Input.Height.value;
	Height = Height.toString().replace(/[^\d\.eE-]/g,'');
	
    HeightInches = document.Input.height2.value;
	HeightInches = HeightInches.toString().replace(/[^\d\.eE-]/g,'');

    // 1: Kilograms
    // 2: Pounds
    // 3: Stones

    if ( document.Input.WeightUnits.value == 1 )
	{
			WeightK =  Weight;
			Units = 'kg';
        }
    else
    if ( document.Input.WeightUnits.value == 2 ) 
        {
			WeightK =  Weight / 2.2 + WeightOunces * 0.02835;
			Units = 'lbs';
	}
    else
    if ( document.Input.WeightUnits.value == 3 )
        {
                        WeightK =  (Weight * 14.0 )/ 2.2 + WeightPounds * 0.45359237;
                        Units = 'stones';
        };
	 
    // 1: Meters
    // 2: Centimeters
    // 3: Feet
    // 4: Inches

    if ( document.Input.HeightUnits.value == 1 )
	{
			HeightM =  Height;
        }
    else
    if ( document.Input.HeightUnits.value == 2 ) 
        {
			HeightM =  Height / 100.0;
	}
    else
    if ( document.Input.HeightUnits.value == 3 )
        {
                        HeightM =  Height * 0.3048 + (HeightInches / 39.37) ;
	}
    else
    if ( document.Input.HeightUnits.value == 4 )
        {
                        HeightM =  Height / 39.37 ;
        };
	 
 
    var b_m_i = ( WeightK / (HeightM * HeightM ));
	var answer = Math.round(10 * b_m_i)/10;
	var compare = parseInt(answer * 10);

	if (answer){
		document.getElementById('answer').innerHTML = answer;
			
		if (compare <185){ 
			document.getElementById('comment').innerHTML = "Underweight";
		}else if (compare >=185 && compare <=249){
			document.getElementById('comment').innerHTML = "Normal";
		}else if (compare >=250 && compare <=299) {
			document.getElementById('comment').innerHTML = "Overweight";
		}else if (compare >=300 && compare <=400) {
			document.getElementById('comment').innerHTML = "Obese";
		}else if (compare >400) {
			document.getElementById('comment').innerHTML = "Extremely obese";
		}
	}
}

function revealW(type){
	if (type == "pounds"){
		document.getElementById('weight2').disabled = false;
		document.getElementById('weight2').value = 'ounces';
	}
	else if (type == "stones"){
		document.getElementById('weight2').disabled = false;
		document.getElementById('weight2').value = 'pounds';
	}
	else{
		document.getElementById('weight2').disabled = 'disabled';
		document.getElementById('weight2').value = '';
	}
}

function revealH(type){
	if (type == "feet"){
		document.getElementById('height2').disabled = false;
		document.getElementById('height2').value = 'inches'
	}
	else{
		document.getElementById('height2').disabled = 'disabled';
		document.getElementById('height2').value = '';
	}
}

function clearForm(){
	revealW('w');
	revealH('h');
	document.getElementById('answer').innerHTML = '';
	document.getElementById('comment').innerHTML = '';
}


