 $(document).ready(_load);

// start variables

var totalDays = 1;
var calorieIndex = 0;
var foodIndex = 0;
var drinkIndex = 0;
var directWaterIndex = 0;
var indirectWaterIndex = 0;


// calorie calculations

function calcCaloriesRequired() {
	var age = $("#sAges").val();
	if (age=="") {
		calorieFail();		
		return;
	}

	var gender = $("#sGender").val();
	if (gender=="") {
		calorieFail();	
		return;
	}

	var lifestyle = $("#sLifestyle").val();
	if (lifestyle=="") {
		calorieFail();	
		return;
	}

	gender = eval("calories" + gender);
	
	theCalories = parseInt(gender[lifestyle][age]);

	$("#CaloriesRequiredText").html(theCalories + "  Calories required" );
	$("#CaloriesRequired").val(theCalories);
	
	calorieIndex = 0;
	
	while ( (calorieIndex<calorieSpread.length-1) && (parseInt(calorieSpread[calorieIndex])< theCalories) )
		calorieIndex++;
		
	calcFood();
}


// page methods

function calAgeChange() {
	calcCaloriesRequired();
}

function calGenderChange() {
	calcCaloriesRequired();
}

function calLifestyleChange() {
	calcCaloriesRequired();
}

function calcFoodTotal() {
	$("#fFruitsVal").html(parseFloat($("#fFruits").val()) +  parseFloat($("#fFruitsInc").val()));
	$("#fVegetablesVal").html(parseFloat($("#fVegetables").val()) +  parseFloat($("#fVegetablesInc").val()));
	$("#fGrainsVal").html(parseFloat($("#fGrains").val()) +  parseFloat($("#fGrainsInc").val()));
	$("#fMeatBeansVal").html(parseFloat($("#fMeatBeans").val()) +  parseFloat($("#fMeatBeansInc").val()));
	$("#fMilkVal").html(parseFloat($("#fMilk").val()) +  parseFloat($("#fMilkInc").val()));
	$("#fOilsVal").html(parseFloat($("#fOils").val()) +  parseFloat($("#fOilsInc").val()));
}

function calcFood() {
	$("#fFruits").val((dietValues[foodIndex].fruit * calorieSpreadFruit[calorieIndex]).toFixed(1));
	$("#fVegetables").val((dietValues[foodIndex].vegetables * calorieSpreadVegetables[calorieIndex]).toFixed(1));
	$("#fGrains").val((dietValues[foodIndex].grains * calorieSpreadGrains[calorieIndex]).toFixed(1));
	$("#fMeatBeans").val((dietValues[foodIndex].meatbeans * calorieSpreadMeatBeans[calorieIndex]).toFixed(1));
	$("#fMilk").val((dietValues[foodIndex].milk * calorieSpreadMilk[calorieIndex]).toFixed(1));
	$("#fOils").val((dietValues[foodIndex].oils * calorieSpreadOils[calorieIndex]).toFixed(1));

	calcFoodTotal();
	
	totalLitres = 0;
	totalLitres = parseFloat($("#fFruitsVal").html()) * vwcFruit[foodIndex] * 100/1000;
	totalLitres += parseFloat($("#fVegetablesVal").html()) * vwcVegetables[foodIndex]  * 25/1000;
	totalLitres += parseFloat($("#fGrainsVal").html()) * vwcGrains[foodIndex]  * (28.35/2)/1000;
	totalLitres += parseFloat($("#fMeatBeansVal").html()) * vwcMeatBeans[foodIndex]  * 28.35/1000;
	totalLitres += parseFloat($("#fMilkVal").html()) * vwcMilk[foodIndex]  *(6*28.35)/1000;
	totalLitres += parseFloat($("#fOilsVal").html()) * vwcOils[foodIndex]  *14/1000;
	
//	alert(totalLitres);

	$("#f_" + foodIndex + "_1").html(parseInt((totalLitres/10).toFixed(0)*10) * totalDays);
	$("#f_" + foodIndex + "_2").html("A");-
	$("#f_" + foodIndex + "_3").html((10/1000).toFixed(2));
	$("#f_" + foodIndex + "_4").html(($("#f_" + foodIndex + "_1").html() * 10 / 1000).toFixed(2));
	
	
	$("#f_t_1").html(	$("#f_" + foodIndex + "_1").html() );
	$("#f_t_2").html(	$("#f_" + foodIndex + "_4").html() );
	
	calcTotal();
}

function _foodChanged() {
	var i = $(this).val();
	if (i==foodIndex)
		return;
	
	if (foodIndex!=0) {
		for (count=1;count<=4;count++) {
			$("#f_" + foodIndex + "_" + count).html("&nbsp;");
		}
	}
	
	foodIndex = i;

	calcFood();
}

function daysChange() {
	totalDays = parseInt($("#fDays").val());
	if (isNaN(totalDays)) totalDays = 0;

	calcFood();
	calcDrinks();
	calcWater();
	calcIWater();	
}



// drinks functions

function calcDrinks() {
	totalWater = 0;
	totalCost = 0;
	
	for (count=1;count<6;count++) {
		value = parseInt($("#fDrink"+count).val());
		if (value>0) {
			$("#d_"+count+"_1").html((value * totalDays * drinksCalc[count]).toFixed(1)) ;
			totalWater += parseFloat(value * totalDays * drinksCalc[count]);
			
			if (count==1) 
				sVal = "B";
			else
				sVal = "A";
			$("#d_"+count+"_2").html(sVal);
			$("#d_"+count+"_3").html(drinkCosts[count]);
			
			$("#d_"+count+"_4").html( (value * totalDays * drinksCalc[count] * drinkCosts[count]).toFixed(1));
			totalCost += parseFloat(value * totalDays * drinksCalc[count] * drinkCosts[count]);			
		} else {
			$("#d_"+count+"_1").html("&nbsp;");
			$("#d_"+count+"_2").html("&nbsp;");
			$("#d_"+count+"_3").html("&nbsp;");
			$("#d_"+count+"_4").html("&nbsp;");
		}
	}
	
	if (totalWater==0)
		$("#d_t_1").html("&nbsp;");
	else
		$("#d_t_1").html(totalWater.toFixed(0));
	if (totalCost==0)
		$("#d_t_2").html("&nbsp;");
	else
		$("#d_t_2").html(totalCost.toFixed(1));
	
	calcTotal();
}

function calcWater() {
	totalWater = 0;
	totalCost = 0;
	
	for (count=1;count<5;count++) {
		value = parseInt($("#fWater"+count).val());
		if (value>0) {
			value = value * totalDays * directWaterUsage[count] / 7;
			
			$("#dw_"+count+"_1").html(value.toFixed(0)) ;
			totalWater += parseFloat(value.toFixed(0));
			
			$("#dw_"+count+"_2").html("B");
			$("#dw_"+count+"_3").html((100/1000).toFixed(1));
			
			$("#dw_"+count+"_4").html( (value * 100/1000).toFixed(1));
			totalCost += parseFloat(value * 100/1000);			
		} else {
			$("#dw_"+count+"_1").html("&nbsp;");
			$("#dw_"+count+"_2").html("&nbsp;");
			$("#dw_"+count+"_3").html("&nbsp;");
			$("#dw_"+count+"_4").html("&nbsp;");
		}
	}
	
	if (totalWater==0)
		$("#dw_t_1").html("&nbsp;");
	else
		$("#dw_t_1").html(totalWater.toFixed(0));
	if (totalCost==0)
		$("#dw_t_2").html("&nbsp;");
	else
		$("#dw_t_2").html(totalCost.toFixed(1));
	
	calcTotal();
}

function calcIWater() {
	totalWater = 0;
	totalCost = 0;
	
	for (count=1;count<2;count++) {
		value = parseInt($("#fIWater"+count).val());
		value = 50 + 20 * value * totalDays;
		
		$("#iw_"+count+"_1").html(value.toFixed(1)) ;
		totalWater += value;
		
		$("#iw_"+count+"_2").html("B");
		$("#iw_"+count+"_3").html((100/1000).toFixed(1));
		
		$("#iw_"+count+"_4").html( (value * 100/1000).toFixed(1));
		totalCost += parseFloat(value * 100/1000);			
	}
	
	if ( (totalWater==0) || (isNaN(totalWater)) )
		$("#iw_t_1").html("&nbsp;");
	else
		$("#iw_t_1").html(totalWater.toFixed(0));
	if (totalCost==0)
		$("#iw_t_2").html("&nbsp;");
	else
		$("#iw_t_2").html(totalCost.toFixed(1));
	
	calcTotal();
}

function drinksChange(i) {
	calcDrinks();	
}

function waterChange(i) {
	calcWater();
}

function waterIChange(i) {
	calcIWater();
}

// general functions

function calcTotal() {
	totalWater = 0;
	totalCost = 0;
	
	if ($("#f_t_2").html()!="&nbsp;") {
		totalWater += parseInt($("#f_t_1").html());
		totalCost += parseFloat($("#f_t_2").html());
	}
	
	if ($("#d_t_2").html()!="&nbsp;") {
		totalWater += parseInt($("#d_t_1").html());
		totalCost += parseFloat($("#d_t_2").html());
	}
	
	if ($("#dw_t_2").html()!="&nbsp;") {
		totalWater += parseInt($("#dw_t_1").html());
		totalCost += parseFloat($("#dw_t_2").html());
	}
	
	if ($("#iw_t_2").html()!="&nbsp;") {
		totalWater += parseInt($("#iw_t_1").html());
		totalCost += parseFloat($("#iw_t_2").html());
	}
	
	if (!(isNaN(totalCost))) {
		totalWater = (totalWater/1000).toFixed(0)
		totalCost = (totalCost/100).toFixed(2)
	} else {
		totalWater = "&nbsp;"
		totalCost = "&nbsp;"
	}

	if (isNaN(totalWater)) totalWater="0";
	if (isNaN(totalCost)) totalCost="0";

	$("#f_totalWater").html(totalWater);
	$("#f_totalCost").html(totalCost);
	
}


function calorieFail() {
	calorieIndex = 0;
	
	$("#CaloriesRequiredText").html("");
	$("#CaloriesRequired").val("");

	calcFood();
}

function resetDays() {
	$("#fDays").val("1");
	totalDays = 1;
	calcFood();
	calcDrinks();
	calcWater();
	calcIWater();	
}

function _resetCalorieCalc() {
	$("#sAges").removeOption(/./);
	$("#sAges").addOption("", "Select ---", true);
	
	for (count=0;count<calAges.length;count++) {
		$("#sAges").addOption(count, calAges[count], false);
	}

	$("#sGender").selectOptions(""); 
	$("#sLifestyle").selectOptions("");
}

function _resetFoods() {
	calcCaloriesRequired()
	$("#foodcalc"+foodIndex).click();

	$("#fFruitsInc").val("0");
	$("#fVegetablesInc").val("0");	
	$("#fGrainsInc").val("0");
	$("#fMeatBeansInc").val("0");
	$("#fMilkInc").val("0");
	$("#fOilsInc").val("0");

	$("#fFruitsVal").html("");
	$("#fVegetablesVal").html("");
	$("#fGrainsVal").html("");
	$("#fMeatBeansVal").html("");
	$("#fMilkVal").html("");
	$("#fOilsVal").html("");

	calcFood();
}

function resetDrinks() {
	$("#fDrink1").val("3");
	$("#fDrink2").val("2");
	$("#fDrink3").val("1");
	$("#fDrink4").val("1");
	$("#fDrink5").val("1");
	
	calcDrinks();
}

function resetWater() {
	$("#fWater1").val("3");
	$("#fWater2").val("7");
	$("#fWater3").val("1");
	$("#fWater4").val("1");
	
	calcWater();
}

function resetIWater() {
	$("#fIWater1").val("0");
	
	calcIWater();
}

function _reset() {
	totalDays = 1;
	foodIndex = 1;
	drinkIndex = 0;
	directWaterIndex = 0;
	indirectWaterIndex = 0;
	
	resetDays();
	_resetCalorieCalc();
/* test data */
	foodIndex = 1;
	$("#sAges").selectOptions("21"); 
	$("#sGender").selectOptions("Male"); 
	$("#sLifestyle").selectOptions("2");
/* end test data */
	_resetFoods();
	resetDrinks();
	resetWater();
	resetIWater();
}


function _load() {
	// load default values in to the page
	$("input.spin-button").SpinButton({min:-10, step: 0.5, valuechange: calcFood});
//	$("input.days-spin-button").SpinButton({min:1, step: 1, valuechange: daysChange});

	$("#fDays").keyup(daysChange);

	$("input.drinks-spin-button").SpinButton({min:0, step: 1, valuechange: drinksChange});
	$("input.water-spin-button").SpinButton({min:0, step: 1, valuechange: waterChange});
	$("input.iwater-spin-button").SpinButton({min:0, max:7, step: 1, valuechange: waterIChange});
	
	_reset();
	
	// set up event handlers
	
	$("#sAges").change(calAgeChange);
	$("#sGender").change(calGenderChange);
	$("#sLifestyle").change(calLifestyleChange);
	
	$("#sLifestyle").change(calLifestyleChange);
	
	$(".foodcalc").click(_foodChanged);
}


dietValues = new Array (
		{fruit: 1, vegetables: 1, grains: 1, meatbeans: 1, milk: 1, oils: 1}, // Standard consunption
		{fruit: 1, vegetables: 1, grains: 1.1, meatbeans: 0.5, milk: 1, oils: 1}, // No meat & modest cereal consumption
		{fruit: 1, vegetables: 1, grains: 1.32, meatbeans: 0.6, milk: 1, oils: 1}, // No meat & high cereal consumption
		{fruit: 1, vegetables: 1, grains: 1, meatbeans: 1, milk: 1, oils: 1}, // Low meat consumption, primarily chicken, pork or other
		{fruit: 1, vegetables: 1, grains: 1, meatbeans: 1, milk: 1, oils: 1}, // Low meat consumption, primarily beef
		{fruit: 1, vegetables: 1, grains: 1, meatbeans: 1.5, milk: 1, oils: 1}, // High meat consumption, primarily chicken, pork or other
		{fruit: 1, vegetables: 1, grains: 1, meatbeans: 1.5, milk: 1, oils: 1} // High meat consumption, primarily beef
);


// calrqd sheet

var calAges = new Array("2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19-20", "21-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55 ", "56-60", "61-65", "66-70", "71-75", "76 and up");

var caloriesMale = new Array(
	new Array("1000", "1000", "1200", "1200", "1400", "1400", "1400", "1600", "1600", "1800", "1800", "2000", "2000", "2200", "2400", "2400", "2400", "2600", "2400", "2400", "2400", "2400", "2200", "2200", "2200", "2200", "2000", "2000", "2000", "2000"),
	new Array("1000", "1400", "1400", "1400", "1600", "1600", "1600", "1800", "1800", "2000", "2200", "2200", "2400", "2600", "2800", "2800", "2800", "2800", "2800", "2600", "2600", "2600", "2600", "2400", "2400", "2400", "2400", "2200", "2200", "2000"),
	new Array("1000", "1400", "1600", "1600", "1800", "1800", "2000", "2000", "2200", "2200", "2400", "2600", "2800", "3000", "3200", "3200", "3200", "3000", "3000", "3000", "3000", "2800", "2800", "2800", "2800", "2600", "2600", "2600", "2600", "2400")
);

var caloriesFemale = new Array(
	new Array("1000", "1000", "1200", "1200", "1200", "1200", "1400", "1400", "1400", "1600", "1600", "1600", "1800", "1800", "1800", "1800", "1800", "2000", "2000", "1800", "1800", "1800", "1800", "1800", "1600", "1600", "1600", "1600", "1600", "1600"),
	new Array("1000", "1200", "1400", "1400", "1400", "1600", "1600", "1600", "1800", "1800", "2000", "2000", "2000", "2000", "2000", "2000", "2000", "2200", "2200", "2000", "2000", "2000", "2000", "2000", "1800", "1800", "1800", "1800", "1800", "1800"),
	new Array("1000", "1400", "1400", "1600", "1600", "1800", "1800", "1800", "2000", "2000", "2200", "2200", "2400", "2400", "2400", "2400", "2400", "2400", "2400", "2400", "2200", "2200", "2200", "2200", "2200", "2200", "2000", "2000", "2000", "2000")
);							

// foodPattern sheet

var calorieSpread = new Array(0, "1000","1200","1400","1600","1800","2000","2200","2400","2600","2800","3000","3200");
var calorieSpreadFruit = new Array(0, "1", "1", "1.5", "1.5", "1.5", "2", "2", "2", "2", "2.5", "2.5", "2.5");
var calorieSpreadVegetables = new Array(0, "1", "1.5", "1.5", "2", "2.5", "2.5", "3", "3", "3.5", "3.5", "4", "4");
var calorieSpreadGrains = new Array(0, "3", "4", "5", "5", "6", "6", "7", "8", "9", "10", "10", "10");
var calorieSpreadMeatBeans = new Array(0, "2", "3", "4", "5", "5", "5.5", "6", "6.5", "6.5", "7", "7", "7");
var calorieSpreadMilk = new Array(0, "2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3");
var calorieSpreadOils = new Array(0, "3", "4", "4", "5", "5", "6", "6", "7", "8", "8", "10", "11");

// fp_food sheet

vwcFruit = new Array(0, "498","498","498","498","498","498");
vwcVegetables = new Array(0, "455","455","455","455","455","455");
vwcGrains = new Array(0, "2348","2348","2348","2348","2348","2348");
vwcMeatBeans = new Array(0, "1924","1924","4891","19183","4891","19183");
vwcMilk = new Array(0, "1267","1267","1267","1267","1267","1267");
vwcOils = new Array(0, "5237","5237","5237","5237","5237","5237"); 


// drink constants

drinksCalc = new Array(0, "0.2", "35", "150", "200", "100");
drinkCosts = new Array(0, "0.1", "0.01", "0.01", "0.01", "0.01", "0.01");

// direct water constants 

directWaterUsage = new Array(0, "25", "150", "20", "20");



