// MuscleHack.com 'How Long Does It Take To Get A Six Pack' Calculator
// Copyright June 25, 2010 by Jordan Hoskins
// Usage of this JavaScript on your website is granted provided that all code, including all links to www.MuscleHack.com, remains unaltered.

var borderColor = '#BBBBBB';
var fontSize = 15;
var fontSizeBig = 14;
var fontSizeSmall = 10;
var fontColor = '#000000';
var linkColor = '#2361A1';

function mhspcalc() {
	document.write('<form><table style="border: 1px solid ' + borderColor + '; font: ' + fontSize + 'px Verdana, Arial, sans-serif; color: ' + fontColor + '"> \
	<tr><td><label for="mhspweight">Weight (lbs): </label></td><td style="text-align: right;"><input type="text" id="mhspweight" name="mhspweight" value="" size="3" /></td></tr> \
	<tr><td><label for="mhspbodyfat">Body Fat (%): </label></td><td style="text-align: right;"><input type="text" id="mhspbodyfat" name="mhspbodyfat" value="" size="3" /></td></tr> \
	<tr><td colspan="2" style="text-align: center;"><input type="button" value="Calculate" onclick="calcsp(this.form);" /></td></tr> \
	<tr><td colspan="2" style="text-align: center; color: red;" id="mhsperr">&nbsp;</td></tr> \
	<tr><td colspan="2" style="text-align: center;">Number of weeks:</td></tr> \
	<tr><td><label for="mhspresbcs">Best Case: </label></td><td style="text-align: right;"><input type="text" id="mhspresbcs" name="mhspresbcs" value="" size="3" /></td></tr> \
	<tr><td><label for="mhspreswcs">Worst Case: </label></td><td style="text-align: right;"><input type="text" id="mhspreswcs" name="mhspreswcs" value="" size="3" /></td></tr> \
	</table></form>');
}

function mhspcalcCustom(cFontSize, cFontColor, cLinkColor, cBorderColor) {
	fontSize = cFontSize;
	fontSizeBig = cFontSize + 1;
	fontSizeSmall = cFontSize - 1;
	fontColor = cFontColor;
	linkColor = cLinkColor;
	borderColor = cBorderColor;
	mhspcalc();
}

function noBlanks(value) {
	return value.replace(/^\s*|\s*$/g,'');
}

function calcsp(thisform) {
	document.getElementById('mhsperr').innerHTML = '&nbsp;';
	var weight = noBlanks(thisform.mhspweight.value);
	if (weight == '') {
		document.getElementById('mhsperr').innerHTML = 'Enter your weight.';
		thisform.mhspweight.focus();
		return false;
	}
	if ((weight != Number(weight)) || (Number(weight) < 1)) {
		document.getElementById('mhsperr').innerHTML = 'Invalid weight entered.';
		thisform.mhspweight.focus();
		return false;
	}
	
	var bodyFatPercentage = noBlanks(thisform.mhspbodyfat.value);
	if (bodyFatPercentage == '') {
		document.getElementById('mhsperr').innerHTML = 'Enter your body fat %.';
		thisform.mhspbodyfat.focus();
		return false;
	}
	if ((bodyFatPercentage != Number(bodyFatPercentage)) || (Number(bodyFatPercentage) < 0)) {
		document.getElementById('mhsperr').innerHTML = 'Invalid body fat % entered.';
		thisform.mhspbodyfat.focus();
		return false;
	}
	
	var fatInPounds = weight * (bodyFatPercentage / 100);
	var leanWeightInPounds = weight - fatInPounds;
	var weightAtTenPercentFat = leanWeightInPounds / 0.9;
	var poundsToLose = weight - weightAtTenPercentFat;
	var bestCaseArrival = poundsToLose / 2.5;
	var worstCaseArrival = poundsToLose / 1.5;
	
	thisform.mhspresbcs.value = Math.round(bestCaseArrival * 100) / 100;
	thisform.mhspreswcs.value = Math.round(worstCaseArrival * 100) / 100;
}
