/**
***
***The script checks if the fields for the BMI main page are valid
***
**/



var input_sys;
var img_sys_us;
var img_sys_si;
var height_input1;
var height_input2;
var weight_input;
var height_units="feet";
var weight_units="pounds";

function init() {
	input_sys = document.getElementById( 'input_sys' );
	img_sys_us = document.getElementById( 'sys_us' );
	img_sys_si = document.getElementById( 'sys_si' );
	height_input1 = document.getElementById('height_input1');
	height_input2 = document.getElementById('height_input2');
	weight_input = document.getElementById('weight_input');
}

function changeToUS() {
	init();
	input_sys.value = "us";

	img_sys_us.src = "images/sys_us_on.gif";
	img_sys_si.src = "images/sys_si_off.gif";

	height_units = "feet";
	height_input1.value = height_units;
	height_input1.style.width="56px";
	height_input2.style.display="";
	weight_units = "pounds";
	weight_input.value = weight_units;
}

function changeToSI() {
	init();
	input_sys . value = "si";

	img_sys_us.src="images/sys_us_off.gif";
	img_sys_si.src="images/sys_si_on.gif";

	height_units = "centimeters";
	height_input1.value = height_units;
	height_input1.style.width="130px";
	height_input2.style.display="none";
	weight_units = "kilograms";
	weight_input.value = weight_units;
}

function validateForm() {
	init();
	var age = document.getElementById('age');

	if ( !age.value.match(/^\d{1,3}$/) ) {
		alert("The BMI category is calculated differently for people under 20 years old. Please provide your age.");
		age.focus();
		return false;
	}

	if ( (age.value <= 19) && !(document.getElementById('gender_male').checked) && !(document.getElementById('gender_female').checked) ) {
		alert("For people under 20 years old, the BMI category is dependent of the gender. Please select you gender.");
		return false;
	}

	if ( !height_input1.value.match(/^\d+(\.\d+)?$/) ) {
		alert("Please provide your height.");
		height_input1.focus();
		return false;
	}

	if ( (input_sys.value == "us") && !height_input2.value.match(/^\d+(\.\d+)?$/) ) {
		alert("Please provide your height (inches).");
		height_input2.focus();
		return false;
	}

	if ( !weight_input.value.match(/^\d+(\.\d+)?$/) ) {
		alert("Please provide your weight.");
		weight_input.focus();
		return false;
	}

	return true;
}
