function set_gender(type) {
	document.getElementById("gender_value").value = type;
	/* display or hide the errors */
	validate_gender();
	/* display or hide the errors */
	validate_alcohol();
}

/*
 * @return true or false false means error, do not submit the form true means no
 * errors
 */
function validate_gender() {
	/* finds gender value from hidden field */
	var gender = document.getElementById("gender_value").value;

	/*
	 * if is set to male or female hide errors if exists otherwise pop the error
	 * msg
	 */
	if (gender == 'male' || gender == 'female') {
		hide_error("gender_error");
	} else {
		display_error("birth_date_section", "gender_error",
				"You will need to add your gender to proceed.");
		return false;
	}

	return true;
}

function set_birth_date(input) {
	hide_error("birth_date_error");

	var objRegExp = /\D/;

	var month = document.getElementById("birth_date_month").value;
	var day = document.getElementById("birth_date_day").value;
	var year = document.getElementById("birth_date_year").value;
	
	
	
	if (objRegExp.test(input.value)) {
		display_error("birth_date_section", "birth_date_error",
				"Wrong Birth Date format.");
		return false
	}
	
	/* if went to month field and is empty, display an error */
	if (input.id == "birth_date_month" && month == "") {
		display_error("birth_date_section", "birth_date_error",
				"Please enter your birth date.");
		return false;
	}

	/* if went to day field and is empty, display an error */
	if (input.id == "birth_date_day" && day == "") {
		display_error("birth_date_section", "birth_date_error",
				"Please enter your birth date.");
		return false;
	}

	/* if went to year field and is empty, display an error */
	if (input.id == "birth_date_year" && year == "") {
		display_error("birth_date_section", "birth_date_error",
				"Please enter your birth date.");
		return false;
	}

	/*
	 * Validate date again and then check an age.
	 */
	if (month != "" && day != "" && year != "") {
		validate_birth_date();
	}
	return true;
}

/*
 * @return true or false false means error, do not submit the form true means no
 * errors
 */
function validate_birth_date() {
	method = document.getElementById("get_method").value;
	if (method == 'true') {
		return true;
	}
	
	hide_error("birth_date_error");

	var month = document.getElementById("birth_date_month").value;
	var day = document.getElementById("birth_date_day").value;
	var year = document.getElementById("birth_date_year").value;
	
	var objRegExp = /([0-9])$/i;

	if (!objRegExp.test(month) || !objRegExp.test(day) || !objRegExp.test(year)) {
		display_error("birth_date_section", "birth_date_error",
				"Wrong Birth Date format.");
		return false
	}

	/*
	 * calculate age if we have all variables set if not display an error for
	 * birthdate again.
	 */
	if (month != "" && day != "" && year != "") {
		/* calculate age and limits */
		return set_age();
	} else {
		display_error("birth_date_section", "birth_date_error",
				"Please enter your birth date.");
		return false;
	}

}

function set_age() {
	var month = document.getElementById("birth_date_month").value;
	var day = document.getElementById("birth_date_day").value;
	var year = document.getElementById("birth_date_year").value;

	/* defind and caclucate user age */
	var date = new Date(year, month - 1, day);

	/* figure user age */
	var now = new Date();
	age_year = now.getFullYear() - date.getFullYear();

	/* ? <= 04 and ? < 12 + 1 */
	/* this will calculate years */
	if (now.getMonth() < date.getMonth()) {
		age_year = age_year - 1;
	} else if (now.getMonth() == date.getMonth()) {
		if (now.getDate() < date.getDate()) {
			age_year = age_year - 1;
		}
	}
	document.getElementById("age_year").value = age_year;

	/* this will calculate the months */
	if (date.getMonth() == now.getMonth()) {
		age_month = 0;
	} else if (date.getMonth() < now.getMonth()) {
		age_month = (now.getMonth() + 1) - (date.getMonth() + 1);
	} else {
		age_month = 12 - ((date.getMonth() + 1) - (now.getMonth() + 1));
	}
	document.getElementById("age_month").value = age_month;

	/* validate age for limits */
	return validate_age();
}

/*
 * @return true or false false means error, do not submit the form true means no
 * errors
 */
function validate_age() {
	hide_error("user_age");

	var age_year = document.getElementById("age_year").value;

	show_alcohol_section();

	/*
	 * First check age limits and pop the error messages for: 1. if age is less
	 * the 2 years old.
	 */
	if (age_year < 2) {
		display_error(
				"birth_date_section",
				"user_age",
				"You are less than 2 years of age and cannot use HELP Healthy Eating for Life Plan &#153.  Please contact your physician about their recommendations for what to eat for your height and weight.");
		return false;
	} else if (age_year > 99) {
		display_error("birth_date_section", "user_age",
				"Age can be between 2 years and 99 years.");
		return false;
	}

	return true;
}


function toogle_alcohol_boxes() {
var num = 0;
var disable_entire_second = false;
var disable_entire_first = false;

for (i = 1; i <= 7; i++) {
        /* check the number of selected from first column */
        if (document.getElementById(i + "_1").checked == true) {
		disable_entire_second = true;
		memory = document.getElementById(i + "_1");
		num++;
        } 
        if (document.getElementById(i + "_2").checked == true) {
		memory = document.getElementById(i + "_2");
		disable_entire_first = true;
	}
	
}

if (disable_entire_second == true) {
	for(i = 1;i <= 7; i++) {
		document.getElementById(i + "_2").disabled = true;
		if (document.getElementById(i + "_1").checked == false && num == 2) {
			document.getElementById(i + "_1").disabled = true;
		}
	}
}

if (disable_entire_first == true) {
	for(i = 1;i <= 7; i++) {
		document.getElementById(i + "_1").disabled = true;
		if (document.getElementById(i + "_2").checked == false) {
			document.getElementById(i + "_2").disabled = true;
		}
	}
}

}

function show_alcohol_section() {
	var age_year = document.getElementById("age_year").value;

	if (age_year >= 21) {
		//document.getElementById("alcohol_beverages_section0").style.display = '';
		document.getElementById("alcohol_beverages_section").style.display = '';
		document.getElementById("alcohol_beverages_section2").style.display = '';
		toogle_alcohol_boxes();
	} else {
		//document.getElementById("alcohol_beverages_section0").style.display = 'none';
		document.getElementById("alcohol_beverages_section").style.display = 'none';
		document.getElementById("alcohol_beverages_section2").style.display = 'none';
		document.getElementById("alcohol_beverages_section_error").style.display = 'none';
		/* just in case hide all alcohol errors */
		// hide_error("alcohol_error");
	}
}

function set_calories() {
	/* first check birthdate again, we need here age to calculate calorie target */
	if (validate_birth_date()) {
		hide_error("calories_range");

		/* set variable from fields form */
		var protein = document.getElementById("calorie_protein").value;
		var fat = document.getElementById("calorie_fat").value;
		var carb = document.getElementById("calorie_carb").value;
		var calories = document.getElementById("calories").value;
		var age_year = document.getElementById("age_year").value;

		/* set error default false */
		var error = false;

		if (calories != '') {
			/* round calories and reset in form field */
			calories = Math.round(calories);
			document.getElementById("calories").value = calories;
		}

		/*
		 * specify min/max values for protain fat and carbohydrate, set correct
		 * error
		 */
		if (age_year > 18) {
			prot_min = 10;
			prot_max = 35;
			prot_error_message = "Protein must be a number between 10 and 35.";

			fat_min = 20;
			fat_max = 35;
			fat_error_message = "Fat must be a number between 20 and 35.";

		} else if (age_year >= 1 && age_year <= 3) {
			prot_min = 5;
			prot_max = 20;
			prot_error_message = "Protein must be a number between 5 and 20.";

			fat_min = 30;
			fat_max = 40;
			fat_error_message = "Fat must be a number between 30 and 40.";

		} else if (age_year >= 4 && age_year <= 18) {
			prot_min = 10;
			prot_max = 30;
			prot_error_message = "Protein must be a number between 10 and 30.";

			fat_min = 25;
			fat_max = 35;
			fat_error_message = "Fat must be a number between 25 and 35.";

		}

		carb_min = 45;
		carb_max = 65
		carb_error_message = "Carbohydrate must be a number between 45 and 65.";

		/* check calories target with age */
		if (age_year >= 2 && age_year < 3) {
			if (calories < 1000 || calories > 5000) {
				error_message = "min 1000 and max 5.000" + "<br/>";
				display_error("calories_section", "calories_range",
						error_message);
				error = true;
			}
		} else if (calories < 1200 || calories > 5000) {
			display_error("calories_section", "calories_range",
					"min 1,200 and max 5,000" + "<br/>");
			error = true;
		}

		/* check min/max protein. fat and carbohydrate */
		if (protein < prot_min || protein > prot_max) {
			display_error("calories_section", "calories_prot",
					prot_error_message + "<br/>");
			error = true;
		} else {
			hide_error("calories_prot");
		}

		if (fat < fat_min || fat > fat_max) {
			display_error("calories_section", "calories_fat", fat_error_message
					+ "<br/>");
			error = true;
		} else {
			hide_error("calories_fat");
		}

		if (carb < carb_min || carb > carb_max) {
			display_error("calories_section", "calories_carb",
					carb_error_message + "<br/>");
			error = true;
		} else {
			hide_error("calories_carb");
		}

		/* check percentage of fat, protein and carbohydrate total */
		if (Number(protein) + Number(fat) + Number(carb) != 100) {
			display_error(
					"calories_section",
					"calories_total",
					"Your calorie distribution of protein, fat and carbohydrate does not equal 100%, please correct.");
			error = true;
		} else {
			hide_error("calories_total");
		}

		if (error == true) {
			return false;
		} else {
			/* display or hide the errors for alcohol */
			validate_alcohol();
		}

		return true;
	}

	return false;
}

function validate_calories() {
	return set_calories();
}

function set_vegeterian(input) {
	if (input.value == 'yes') {
		document.getElementById("vegetarian_value").value = 'yes';
	} else {
		document.getElementById("vegetarian_value").value = 'no';
	}

	validate_meat_and_vegetarian()
}

function validate_meat_and_vegetarian() {
	vegetarian = document.getElementById("vegetarian_value").value;
	meat = document.getElementById("meat_type_value").value;

	/* if vegetarian and choice is meat display an error */
	if (vegetarian == 'yes'
			&& (meat == 'lean' || meat == 'medium_fat' || meat == 'hight_fat')) {
		display_error(
				"vegetarian_error",
				"vegetarian_meat",
				"You selected meat and vegetarian.  Please choose meat above and vegetarian \"No\" here OR plant based protein above and vegetarian \"Yes\" here.");
		return false;
	} else {
		hide_error("vegetarian_meat");
	}

	/* means no meat selected, display an error */
	if (meat == "") {
		display_error('meat_type_error', 'meat_error',
				'You must select meat or plant proteins to proceed.');
		return false;
	} else {
		hide_error("meat_error");
	}

	return true;
}

function set_meal_type(input) {
	document.getElementById("meat_type_value").value = input.value;
	validate_meat_and_vegetarian()
}

function set_snack(input) {
	if (input.id != 'snack_none') {
		document.getElementById('snack_none').checked = false;
	} else {
		document.getElementById('snack_mid_morning').checked = false;
		document.getElementById('snack_mid_afternoon').checked = false;
		document.getElementById('snack_evening').checked = false;
	}
}

function set_alcohol(input) {

	if (input.id == 'alcohol_none') {
		/*
		 * if alcohol none checkbox has been selected, make it as checked, set
		 * beverages to 0 as 0 drinks, uncheck and undisabled all others
		 * checkboxes choices
		 */
		document.getElementById('alcohol_none').checked = true;
		drinks = 0;
		for (i = 1; i <= 7; i++) {
			document.getElementById(i + "_2").checked = false;
			document.getElementById(i + "_1").checked = false;
			document.getElementById(i + "_1").disabled = false;
			document.getElementById(i + "_2").disabled = false;
		}
	} else {
		document.getElementById('alcohol_none').checked = false;

		/* if selected box is just checked and comming from first column */
		if (input.checked == true && input.value == 1) {
			num = 0;
			for (i = 1; i <= 7; i++) {
				document.getElementById(i + "_2").disabled = true;
				/* check the number of selected from first column */
				if (document.getElementById(i + "_1").checked == true) {
					num++;
				}
			}
			/*
			 * if two alcohols has been selected from first column , disabled
			 * all boxed, leave that two selected
			 */
			if (num == 2) {
				for (i = 1; i <= 7; i++) {
					if (document.getElementById(i + "_1").checked == false) {
						document.getElementById(i + "_1").disabled = true;
					}

				}
			}
			drinks = num;
		}
		/* if selected box is just unchecked and comming from first column */
		else if (input.checked == false && input.value == 1) {
			num = 0;
			for (i = 1; i <= 7; i++) {
				/* check the number of selected from first column */
				if (document.getElementById(i + "_1").checked == true) {
					num++;
				}
			}
			/*
			 * if only one left, undisabled all boxed from first column, leave
			 * that last one
			 */
			if (num == 1) {
				for (i = 1; i <= 7; i++) {
					if (document.getElementById(i + "_1").checked == false) {
						document.getElementById(i + "_1").disabled = false;
					}

				}
			}
			/*
			 * if none boxes are selected from first column, undisable entire
			 * second column
			 */
			else if (num == 0) {
				for (i = 1; i <= 7; i++) {
					document.getElementById(i + "_2").disabled = false;
				}
			}

			drinks = num;

		}
		/* if selected box is just checked and comming from second column */
		else if (input.checked == true && input.value == 2) {
			drinks = 2;
			for (i = 1; i <= 7; i++) {
				/* disabl entire first column */
				document.getElementById(i + "_1").disabled = true;
				/* disabl all boxes on second column, leave that one */
				if (document.getElementById(i + "_2").checked == false) {
					document.getElementById(i + "_2").disabled = true;
				}
			}
		}
		/* if selected box is just unchecked and comming from second column */
		else if (input.checked == false && input.value == 2) {
			drinks = 0;
			/* undisable entire first and second column */
			for (i = 1; i <= 7; i++) {
				document.getElementById(i + "_1").disabled = false;
				document.getElementById(i + "_2").disabled = false;
			}
		}

	}
	
	if (drinks == 0) {
		document.getElementById('alcohol_none').checked = true;
	}
	
	document.getElementById('beverages').value = drinks;
	validate_alcohol();
}

function validate_alcohol() {
	drinks = document.getElementById('beverages').value;
	calories = document.getElementById("calories").value;
	gender = document.getElementById("gender_value").value;
	var error = false;
	
	if (calories < 1700 && drinks == 1) {
		error_message = "You have selected to drink alcohol which is not recommended for weight loss as it will replace nutritious food you should eat.  Alcohol provides no nutrients while food is a rich source of protein, vitamins and minerals.  Alcohol beverages will not be allowed for the weight loss plan you chose.  To proceed, please change your choice to drink alcohol to None.<br/><br/>"
		display_error("alcohol_beverages_section_error", "alcohol_calories1",
				error_message);
		error = true;

	} else {
		hide_error("alcohol_calories1");
	}

	if (calories < 2000 && drinks == 2) {
		error = true;
		error_message = "You have selected to drink more alcohol than is recommended for weight loss. Your eating plan will be limited to a maximum of 1 drink per day in the eating plan you chose.  To proceed, please change your choice to 1 alcohol beverage.<br/><br/>";
		display_error("alcohol_beverages_section_error", "alcohol_calories2",
				error_message);

	} else {
		hide_error("alcohol_calories2");
	}

	if (gender == 'female' && drinks > 1) {
		error_message = "You have selected to drink more alcohol than is healthy for women.  Your eating plan will be limited to a maximum of 1 drink for women per day in the plan you chose. To proceed, please change your choice to 1 alcohol beverage.<br/><br/>";
		display_error("alcohol_beverages_section_error", "alcohol_female",
				error_message);
		error = true;

	} else {
		hide_error("alcohol_female");
	}
	
	
	
	if (error == true) {
		return false;
		
	} else {
		return true;
	}

}

function submit_form() {
	/*
	 * validate entire form before submit. @returned false value means ERROR and
	 * do not submit the form.
	 */

	/* check gender */
	gender = validate_gender();

	/* check birth date first */

	birth_date = validate_birth_date();
	
	/* check age limitation but only if birth date is set */

	if (birth_date) {
		age = validate_age();
	} else {
		age = false;
	}

	/* check calories */
	cal = validate_calories();

	/* check meat and vegetarians */
	meat = validate_meat_and_vegetarian();

	alcohol = validate_alcohol();

	 //alert("g: " + gender + ", b: " + birth_date + ", a: " + age + ", c: "
	 //+ cal + ", m: " + meat + ", alc: " + alcohol);

	/* submit the form if above code doesn't return false */
	if (gender && birth_date && age && cal && meat && alcohol) {
		hide_error("primary_error_msg", error_message);
		// document.getElementById('calchelp_form').submit();
	} else {
		
		display_error("primary_error", "primary_error_msg",
				"Check and fix errors in red below.");
		window.location = "#";
		return false;
	}
	return true;
}

function display_error(section, id, msg) {
	/* if this id not exist add error to the section */
	if (document.getElementById(id) == null) {
		document.getElementById(section).style.display = '';
		document.getElementById(section).innerHTML += "<div id='" + id + "'>"
				+ msg + "</div>";
	}
}

function hide_error(id) {
	/* find our error */
	error = document.getElementById(id);

	/* if exists ? remove it */
	if (error) {
		/* define parent */
		var parent = error.parentNode;
		/* remove our child error */
		parent.removeChild(error);

		/* if error section empty make display as none */
		if (parent.innerHTML == "") {
			parent.style.display = "none";
		}
	}
}
