var SET = "<ul type='circle'>";
var ERROR_MESSAGE_DIV = "error_message";
var ENGLISH_UNIT = "english";
var UNITS_LABEL_DIV = "units_label";
var DISPLAY_STYLE_NONE = "none";
var DISPLAY_STYLE_BLOCK = "block";
var FEMALE_SEX = "female";
var MALE_SEX = "male";
var SEDENTARY = "sedentary";
var LIGHTLY = "lightly";
var MODERATELY = "moderately";
var CALORIES_RESULT = "calories_result";
var EXTRA = "extra";
var HELP_UNIT = "help_unit";
var BLANK = "";
var weight;
var sex;
var age;
var activity;
var bmr;
var height;


/**
 * Calculate calories for every day subject to weight, age and lifestyle.
 * 
 * @param form
 *            action form
 */
function calcCalories(form) {
	var unitLabel = document.getElementById(UNITS_LABEL_DIV).innerHTML;
	document.getElementById(ERROR_MESSAGE_DIV).innerHTML = BLANK;

	if (validateInput(form)) {
		if (unitLabel == ENGLISH_UNIT) {
			if (sex == FEMALE_SEX) {
				bmr = 655 + (4.3 * weight / 0.45359237) + (4.7 * height / 2.54)
						- (4.7 * age);
			} else if (sex == MALE_SEX) {
				bmr = 66 + (6.3 * weight / 0.45359237) + (12.9 * height / 2.54)
						- (6.8 * age);
			}
		} else {
			if (sex == FEMALE_SEX) {
				bmr = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age);
			} else if (sex == MALE_SEX) {
				bmr = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
			}
		}

		if (activity == SEDENTARY) {
			bmr = bmr + bmr * 0.2;
		} else if (activity == LIGHTLY) {
			bmr = bmr + bmr * 0.3;
		} else if (activity == MODERATELY) {
			bmr = bmr + bmr * 0.4;
		} else {
			bmr = bmr + bmr * 0, 6;
		}

		document.getElementById(CALORIES_RESULT).innerHTML = "<strong>"
				+ bmr.toFixed(2) + "</strong>";
	}
}

/**
 * Clears error bar after changes.
 */
function clearErrorBar() {
	document.getElementById(ERROR_MESSAGE_DIV).innerHTML = BLANK;
}

/**
 * Validate input. Proof all parameters: <code>weight</code>,
 * <code>age</code>, and <code>life style</code>.
 * 
 * @param form
 *            action form
 */
function validateInput(form) {

	var errorMessage;
	var validationFlag;

	// Proof sex argument
	var sexRadio = form.sexgroup;
	if (!sexRadio) {
		return printFalseSexInputMessage("Sex");
	} else {
		var sexRadioLength = sexRadio.length;
		if (sexRadioLength == undefined) {
			if (sexRadio.checked) {
				sex = sexRadio.value;
			} else {
				return printFalseSexInputMessage("Sex");
			}
		} else {
			for ( var i = 0; i < sexRadioLength; i++) {
				if (sexRadio[i].checked) {
					sex = sexRadio[i].value;
				}
			}

			if (sex == undefined) {
				return printFalseSexInputMessage("Sex");
			}
		}
	}

	// Proof weight parameter.
	if (isBlank(form.weight.value)) {
		errorMessage = SET + "<li>Please enter Weight</li></ul>";
		form.weight.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else if (!isNum(form.weight.value)) {
		errorMessage = SET + "<li>Please enter a valid Weight</li></ul>";
		form.weight.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else {
		weight = parseFloat(form.weight.value);
		if (weight < 0) {
			errorMessage = SET + "<li>Enter positive Weight</li></ul>";
			form.weight.focus();
			document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
			return false;
		}
	}

	// Proof height parameter.
	if (isBlank(form.height.value)) {
		errorMessage = SET + "<li>Please enter Height</li></ul>";
		form.height.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else if (!isNum(form.height.value)) {
		errorMessage = SET + "<li>Please enter a valid Height</li></ul>";
		form.height.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else {
		height = parseFloat(form.height.value);
		if (height < 0) {
			errorMessage = SET + "<li>Enter positive Height</li></ul>";
			form.height.focus();
			document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
			return false;
		}
	}

	// Proof age parameter.
	if (isBlank(form.age.value)) {
		errorMessage = SET + "<li>Please enter Age</li></ul>";
		form.age.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else if (!isNum(form.age.value)) {
		errorMessage = SET + "<li>Please enter a valid Age</li></ul>";
		form.age.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else {
		age = parseFloat(form.age.value);
		if (age < 0) {
			errorMessage = SET + "<li>Enter positive Age</li></ul>";
			form.age.focus();
			document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
			return false;
		}
	}

	// Proof lifestyle argument
	var activityRadio = form.activity;
	if (!activityRadio) {
		return printFalseSexInputMessage("Activity");
	} else {
		var activityRadioLength = activityRadio.length;
		if (activityRadioLength == undefined) {
			if (activityRadio.checked) {
				activity = activityRadio.value;
			} else {
				return printFalseSexInputMessage("Activity");
			}
		} else {
			for (i = 0; i < activityRadioLength; i++) {
				if (activityRadio[i].checked) {
					activity = activityRadio[i].value;
				}
			}

			if (activity == undefined) {
				return printFalseSexInputMessage("Activity");
			}
		}
	}

	return true;
}

/**
 * Print error message after incorrect sex selection
 */
function printFalseSexInputMessage(parameter) {
	errorMessage = SET + "<li>" + parameter + " was not checked</li></ul>";
	document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
	return false;
}

/**
 * Checks if input the number input was.
 * 
 * @param x
 *            input parameter
 * @return <code>true</code> if number, <code>false</code> otherwise
 */
function isNum(x) {
	filter = /(^\-?\d+\.?$)|(^\-?\d*\.\d+$)/;
	if (filter.test(x)) {
		return true;
	}
	return false;
}