$(document).ready(function() {
	$("#lnkStandard").css({"text-decoration" : "none", "color": "#006cff"});	
	$('#pnlMetricWeight').hide();
	$('#pnlMetricHeight').hide();
	$("#lblWaist").text(' ' + $("#lblInches").text());
	$("#txtRedirect").val(parent.location.href);
	if ($('#d').length > 0) {
		if (!($('#d').val())) {
			$("#d").val("2.54,2.2");
		}
		
		if ($('#d').val() == '1,1') {
			$('#pnlMetricWeight').show();
			$('#pnlMetricHeight').show();
			$('#pnlStandardWeight').hide();
			$('#pnlStandardHeight').hide();
			$('#selSFeet').removeClass('required');
			$('#selSInches').removeClass('required');
			$('#txtSWeight').removeClass('required');
			$('#txtMWeight').addClass('required');
			$('#txtMHeight').addClass('required');
			$('#SuccesschartMetric').val('1');
			$("#lblWaist").text(' ' + $("#lblMInches").text());
			$('#lnkMetric').css({
				"text-decoration" : "none", 
				"color": "#006cff"
			});
			$('#lnkMetric').prev().css({
				"text-decoration": "underline",
				"color": "inherit"
			});
		}
		
	}
	
	if ($('.date-pick').length > 0) {
		Date.firstDayOfWeek = 7;
		Date.format = 'yyyy-mm-dd';	
		$('.date-pick').bind('click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		)
		.datePicker({
			startDate:'2008-01-01',
			clickInput:true,
			createButton:false
		
		})
		.bind('dateSelected',
			function(e, selectedDate, $td)
			{
				//alert(selectedDate);
				Date.format = jsDateFormat; //'mmm. dd, yyyy';
				$('#showdate').text(new Date(selectedDate).asString());
				Date.format = 'yyyy-mm-dd';
				$('#SuccesschartEntrydate').val(new Date(selectedDate).asString()).trigger('change');
				//alert($('#DiaryEntrydate').val());
			}
		);
		Date.format = jsDateFormat; //'mmm. dd, yyyy';
		$('#showdate').text(new Date().asString());
		Date.format = 'yyyy-mm-dd';
		if ($('#SuccesschartEntrydate').length > 0) {
			if (!($('#SuccesschartEntrydate').val())) {
				Date.format = jsDateFormat;
				$('#showdate').text(new Date().asString());
				Date.format = 'yyyy-mm-dd';
				$('#SuccesschartEntrydate').val(new Date().asString());
			}
			else {
				Date.format = 'yyyy-mm-dd';
				dShowDate = Date.fromString($('#SuccesschartEntrydate').val());
				Date.format = jsDateFormat;
				$('#showdate').text(new Date(dShowDate).asString());
			}
		}	
	}
	$("#bmiForm").validate({
	   messages: {
			txtMWeight: "",
			txtMHeight: "",
			txtSWeight: "",
			selSFeet: "",
			selSInches: ""
		},  
		submitHandler: function(form) {
			var oForm = setFormVals(form);
			var vBMI = calc(oForm);
			if (!(vBMI == false)) {
				$('#SuccesschartHeight').val(oForm.h.value);
				$('#SuccesschartWeight').val(oForm.w.value);
				$('#SuccesschartBmi').val(vBMI);
				
				form.submit();
			}
		}

	});
	
	$('#lnkStandard').click(function(e) {
			e.preventDefault();
			document.bmiForm.d.value = "2.54,2.2";
			$('#pnlStandardWeight').show();
			$('#pnlStandardHeight').show();
			$('#pnlMetricWeight').hide();
			$('#pnlMetricHeight').hide();
			$('#selSFeet').addClass('required');
			$('#selSInches').addClass('required');
			$('#txtSWeight').addClass('required');
			$('#txtMWeight').removeClass('required');
			$('#txtMHeight').removeClass('required');
			$('#SuccesschartMetric').val('0');
			$("#lblWaist").text(' ' + $("#lblInches").text());
			$(this).css({"text-decoration" : "none", "color": "#006cff"});
			$(this).next().css({
				"text-decoration" : "underline",
				"color": "inherit"
			});
   	});
	$('#lnkMetric').click(function(e) {
			e.preventDefault();
			$("#d").val("1,1");
			$('#pnlMetricWeight').show();
			$('#pnlMetricHeight').show();
			$('#pnlStandardWeight').hide();
			$('#pnlStandardHeight').hide();
			$('#selSFeet').removeClass('required');
			$('#selSInches').removeClass('required');
			$('#txtSWeight').removeClass('required');
			$('#txtMWeight').addClass('required');
			$('#txtMHeight').addClass('required');
			$('#SuccesschartMetric').val('1');
			$("#lblWaist").text(' ' + $("#lblMInches").text());
			$(this).css({
				"text-decoration" : "none", 
				"color": "#006cff"
			});
			$(this).prev().css({
				"text-decoration": "underline",
				"color": "inherit"
			});
   	});
	if ($('#btnBMICalc').length > 0) {
		$('#btnBMICalc').click(function(e) {
			var oForm = setFormVals(document.bmiForm);
			var vBMI = calc(oForm);
			if (!(vBMI == false)) {
				if ($('#lblBMI').length > 0) {
					$('#lblBMI').text("Your BMI is: " + vBMI);
				}else {
					$('#SuccesschartHeight').val(oForm.h.value);
					$('#SuccesschartWeight').val(oForm.w.value);
					$('#SuccesschartBmi').val(vBMI);
				}
				
				if (vBMI < 18.5) {
					$('#pnlBMI').html("<img src='../img/rightcontent/bmi-underweight.jpg' alt='Underweight' width='210' height='107' />"); 
				} else if (vBMI >= 18.5 && vBMI <= 24.99) {
					$('#pnlBMI').html("<img src='../img/rightcontent/bmi-normal.jpg' alt='Normal' width='210' height='107' />"); 
				} else if (vBMI >= 25 && vBMI <= 29.99) {
					$('#pnlBMI').html("<img src='../img/rightcontent/bmi-overweight.jpg' alt='Overweight' width='210' height='107' />"); 
				} else {
					$('#pnlBMI').html("<img src='../img/rightcontent/bmi-very-overweight.jpg' alt='Very Overweight' width='210' height='107' />"); 
				}
			}
			return false;
			
		});
	}
	

	
});
/**
 * @author rgriffith
 */
function setFormVals(oForm) {
	if (oForm.d.value == "1,1") {
		oForm.w.value = oForm.txtMWeight.value;
		oForm.h.value = oForm.txtMHeight.value;
	} else {
		oForm.w.value = oForm.txtSWeight.value;
		oForm.h.value = convertToInches(stripBlanks(oForm.selSFeet.value), stripBlanks(oForm.selSInches.value));
	}
	return oForm;
}
// Body Mass Index Calculator
// copyright 28th April 2006, by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
function valButton(btn) {
	var cnt = - 1;
	// MODIFIED RTG 10/22/08
	if (btn.type == 'hidden') {
		return btn.value;
	} else { //MODIFIED RTG 10/22/08
		for (var i = btn.length - 1; i > - 1; i--) {
			if (btn[i].checked) {
				cnt = i;
				i = - 1;
			}
		}
		if (cnt > - 1) return btn[cnt].value;
		else return null;
	}
}//MODIFIED RTG 10/22/08

function stripBlanks(fld) {
	var result = "";
	var c = 0;
	for (i = 0; i < fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") c = result.length;
		}
	}
	return result.substr(0, c);
}

function calc(thisform) {
	var d = valButton(thisform.d);
	if (d == null) {
		alert("You must choose centimetres/kg or inches/lb");
		return false;
	}
	cv = d.split(',');
	h = stripBlanks(thisform.h.value);

	if (h == '') {
		alert("You must enter your height");
		//thisform.h.focus();
		return false;
	}
	if (h != Number(h) || (h = Number(h * cv[0]/100)) < 1 || h > 2.5) {
		alert("Invalid height entered");
		//ORIGINAL LINE thisform.h.focus(); 
		thisform.h.value = h; //ADDED RTG 10/22/08
		return false;
	}
	var w = stripBlanks(thisform.w.value);
	if (w == '') {
		alert("You must enter your weight");
		//thisform.w.focus();
		return false;
	}
	if (w != Number(w) || (w = Number(w / cv[1])) < 25 || w > 250) {
		alert("Invalid weight entered");
		//thisform.w.focus();
		return false;
	}
	return Math.round(w / (h * h) * 100) / 100;
}


//ADDED RTG 10/22/08
function convertToInches(fldFeet, fldInches) {
	return  Math.round((fldFeet * 12) + (fldInches * 1));
}

function convertToCentimeters(fldFeet, fldInches) {
	var nConverted = convertToInches(fldFeet, fldInches);
	return Math.round(nConvered * 2.54);
}
