// eload - Calorie Calulator


function cCalcCalorie(vActivity, vIntensity, vWeight, vWeightUnit, vHour, vMin) {

/*
	// Check variable values
	alert (vActivity);
	alert (vIntensity);
	alert (vWeight);
	alert (vWeightUnit);
	alert (vHour + " " + vMin);
*/	

	var vConstant = 0;

	// Determine activity - bodyweight calories/minute constant is expressed in kg
	switch (vActivity){
		case 'Aerobics': 
			if (vIntensity == 'Easy')	{vConstant = 0.091;};
			if (vIntensity == 'Medium')	{vConstant = 0.102;};
			if (vIntensity == 'Intense')	{vConstant = 0.134;};
		break;

		case 'Backpacking': 
			if (vIntensity == 'NoLoad')	{vConstant = 0.121;};
			if (vIntensity == '11-lb')	{vConstant = 0.129;};
			if (vIntensity == '22-lb')	{vConstant = 0.140;};
			if (vIntensity == '44-lb')	{vConstant = 0.148;};
		break;

		case 'Baseball': 
			if (vIntensity == 'Fielder')	{vConstant = 0.060;};
			if (vIntensity == 'Pitcher')	{vConstant = 0.090;};			
		break;

		case 'Basketball': 
			if (vIntensity == 'Game')		{vConstant = 0.147;};
			if (vIntensity == 'Practice')	{vConstant = 0.134;};
		break;

		case 'Boxing': 
			if (vIntensity == 'Match')				{vConstant = 0.211;};
			if (vIntensity == 'Sparring_Practice')	{vConstant = 0.138;};
		break;

		case 'Canoeing': 
			if (vIntensity == 'Leisure')		{vConstant = 0.044;};
			if (vIntensity == 'Racing')			{vConstant = 0.102;};
		break;

		case 'Cycling': 
			if (vIntensity == 'Leisure55')		{vConstant = 0.064;};
			if (vIntensity == 'Leisure94')		{vConstant = 0.102;};
			if (vIntensity == 'Racing')			{vConstant = 0.170;};
		break;
	
		case 'Football': 
			if (vIntensity == 'Game')	{vConstant = 0.131;};
		break;

		case 'Ice Hockey': 	
			vConstant = 0.157;
		break;

		case 'Mountain Climbing': 	
			vConstant = 0.157;
		break;
	
		case 'Rowing': 
			if (vIntensity == 'MachineModerate')	{vConstant = 0.121;};
			if (vIntensity == 'MachineRace')		{vConstant = 0.182;};
			if (vIntensity == 'SkullLeisure')		{vConstant = 0.100;};
			if (vIntensity == 'SkullRace')			{vConstant = 0.184;};
		break;

		case 'Running_Cross_Country': 	
			vConstant = 0.165;
		break;

		case 'Running_Flat_Surface': 
			if (vIntensity == '11-30')	{vConstant = 0.132;};
			if (vIntensity == '9')		{vConstant = 0.193;};
			if (vIntensity == '8')		{vConstant = 0.208;};
			if (vIntensity == '7')		{vConstant = 0.227;};
			if (vIntensity == '6')		{vConstant = 0.250;};
			if (vIntensity == '5-30')	{vConstant = 0.288;};
		break;

		case 'Soccer': 	
			vConstant = 0.138;
		break;

		case 'Softball': 	
			vConstant = 0.070;
		break;

		case 'Squash': 	
			vConstant = 0.212;
		break;

		case 'Skiing': 
			if (vIntensity == 'ModerateSpeed')	{vConstant = 0.119;};
			if (vIntensity == 'WalkingSpeed')	{vConstant = 0.142;};
			if (vIntensity == 'FastSpeed')		{vConstant = 0.274;};
		break;

		case 'Swimming': 
			if (vIntensity == 'FastCrawl')	{vConstant = 0.155;};
			if (vIntensity == 'SlowCrawl')	{vConstant = 0.127;};
		break;	

		case 'Tennis': 
			if (vIntensity == 'Competition')	{vConstant = 0.146;};
			if (vIntensity == 'Recreational')	{vConstant = 0.108;};
		break;		

		case 'Volleyball': 
			if (vIntensity == 'Competition')	{vConstant = 0.125;};
			if (vIntensity == 'Recreational')	{vConstant = 0.051;};
		break;		

		case 'Walking': 
			if (vIntensity == 'AsphaltRoad')	{vConstant = 0.080;};
			if (vIntensity == 'FieldsHillside')	{vConstant = 0.083;};
		break;		

	};	


	// Determine weight units entered by user and convert to kgs where required
	if (vWeightUnit == 'lbs') {
		vConstant = (+vConstant) / 2.2046226; 
	};


	// Total Calories Formula
	vCalorie = ((+vWeight) * (+vConstant) * ((+vHour*60) + (+vMin))); 

	// Round result
	vCalorie = round_decimals(vCalorie, 0);

	return vCalorie;
	
}



function cCalcReqCarbCalorie(vHeartRate, vTotalCalorie, vWeight, vWeightUnit) {

/*	
	alert(vHeartRate);
	alert(vTotalCalorie);
	alert(vWeight);
	alert(vWeightUnit);
*/

	var cHeartRate = 0;

	// Determine Heart Rate Constant
	switch (vHeartRate){
		case '50': 
			cHeartRate = 0.11;
		break;	

		case '60': 
			cHeartRate = 0.28;
		break;	

		case '70': 
			cHeartRate = 0.44;
		break;	

		case '80': 
			cHeartRate = 0.62;
		break;	

		case '90': 
			cHeartRate = 0.71;
		break;	

	};

	// alert (cHeartRate);


	// Convert weight units entered by user to kg for formula
	if (vWeightUnit == 'lbs'){
		vWeight = vWeight / 2.2046226;
	};


	// Required Carbohydrates Calories Formula
	var vReqCarbCalorie = (cHeartRate * vTotalCalorie) - (13 * vWeight);

	// Round result
	vReqCarbCalorie = round_decimals(vReqCarbCalorie, 0);

	return vReqCarbCalorie;
	
}


function cReqCarbCalorieHour(vReqCarbCalorie, vHour, vMin){

	// Calculate Required Calories of Carbohydrates per Hour
	vReqCarbCalorieHour = vReqCarbCalorie / ((+vHour)+(+vMin/60));
	//alert (vReqCarbCalorieHour);

	// Round result
	vReqCarbCalorieHour = round_decimals(vReqCarbCalorieHour, 1);

	return vReqCarbCalorieHour;

}


function cReqCarbHourGram(vReqCarbCalorieHour){

	// Calculate Required Grams of Carbohydrate Per Hour
	vReqCarbHourGram = vReqCarbCalorieHour / 4;
	//alert (vReqCarbHourGram);

	// Round result
	vReqCarbHourGram = round_decimals(vReqCarbHourGram, 0);

	return vReqCarbHourGram;
}



function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
