// emend - Fluid Calulator

function cPostExerciseFluid(preWWeight, preWWeightUnit, fIngested, fIngestedUnit, postWWeight, postWWeightUnit){

	// Validate pre and post workout weights are in the same units
	if (preWWeightUnit != postWWeightUnit){ 
		alert ('You must enter your pre-workout weight and post-workout weight in the same units.');
		return '';
	};


	// Determine pre-workout weight units entered by user and convert to kg
	switch (preWWeightUnit){
		case 'kg': preWWeight = (+preWWeight);
		break;

		case 'lbs': preWWeight = (+preWWeight) / 2.2046226 ;
		break;

	};	


	// Determine ingested fluid units entered by user and convert to litres
	switch (fIngestedUnit){
		case 'mL': fIngested = (+fIngested) / 1000;
		break;

		case 'L': fIngested = (+fIngested);
		break;

		case 'oz': fIngested = (+fIngested) / 33.8100;
		break;
	};


	// Determine post-workout weight units entered by user and convert to kg
	switch (postWWeightUnit){
		case 'kg': postWWeight = (+postWWeight);
		break;

		case 'lbs': postWWeight = (+postWWeight) / 2.2046226 ;
		break;

	};	

	
	// Validate pre-workout weight + fluids ingested > post-workout weights
	if (preWWeight + fIngested < postWWeight){ 
		alert ('The numbers entered do not appear to be accurate, as the sum of your pre-workout weight + the weight of the volume of your ingested fluids must be greater than or equal to your post-workout weight.  \nPlease try again after another trial.');
		return '';
	};


	// Validate post-workout > pre-workout weights
	if (postWWeight > preWWeight){ 
		alert ('Based on the numbers you have entered, you have gained weight during this activity, meaning you are overhydrated.  Therefore, you do not need to worry about rehydrating during recovery.  Rather, your body needs to urinate out this excess fluid, and should do so over the next several hours, bringing your bodyweight back to normal.\n\nNext workout, try ingesting more sodium and potassium, which will help your body more accurately regulate its water content.  Using a balanced electrolyte drink like e load, and if needed, Zone Caps for extra electrolyte, will help.');
		return '';
	};

	


	// Calculate total rehydration requirements in litres
	var fluidDeficit = preWWeight + fIngested - postWWeight;
	//alert (fluidDeficit);

	// Convert results to approriate units
	if (fIngestedUnit == 'oz'){
		
		// Return result in ounces
		fluidDeficit = 33.8100 * fluidDeficit;
		// Round result
		fluidDeficit = round_decimals(fluidDeficit, 0);
		fluidDeficit = fluidDeficit + " oz";
	}

	else if (fIngestedUnit == 'mL'){

		// Return result in mL
		fluidDeficit = 1000 * fluidDeficit;
		// Round result
		fluidDeficit = round_decimals(fluidDeficit, 0);
		fluidDeficit = fluidDeficit + " mL";	
	}

	else {
		// Round result
		fluidDeficit = round_decimals(fluidDeficit, 0);
		fluidDeficit = fluidDeficit + " L";
	};



	//alert (fluidDeficit);
	//alert (preWWeight + ' ' + preWWeightUnit +  '/' + fIngested + ' ' + fIngestedUnit + '/' + postWWeight + ' ' + postWWeightUnit);

	return fluidDeficit;

};


function cHydrationReq(fluidDeficit) {

	// Parse string into fluid quantity and units
	var i = fluidDeficit.indexOf(' ');
	var sQuantity = fluidDeficit.substring(0,i);
	var sUnit = fluidDeficit.substring(i+1);

	// Calculate rehydration requirements	
	var fluidReq = (+sQuantity) * 1.5
	// Round result
	fluidReq = round_decimals(fluidReq, 1)

	// Append fluid units
	var fluidReq = fluidReq + ' ' + sUnit;
	//alert (fluidReq);
	

	return fluidReq;
	

};



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
}
