
	var CAL_PER_LBS = 3500;
	var MILLIS_PER_DAY = 864e5; // 1000*60*60*24 or 86400000
	var MONTH = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
				  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];

	var lastPage=0;
	var currPage=0;
	var itemCount=0;
	var testSlider=0;
	var exPerPage=3;

	function prevPage() {
		$('resultsPage'+currPage).style.display="none";
		currPage = currPage - 1;
		$('resultsPage'+currPage).style.display="block";
		$('nextResultsPage').style.visibility="visible";
		updatePagerDisplay();
		return false;
	}
	function nextPage() {
		$('resultsPage'+currPage).style.display="none";
		currPage = currPage + 1;
		$('resultsPage'+currPage).style.display="block";
		$('prevResultsPage').style.visibility="visible";
		updatePagerDisplay();
		return false;
	}
	function updatePagerDisplay() {
		if(currPage == 0) {
			$('prevResultsPage').style.display="none";
		} else if (currPage > 0) {
			$('prevResultsPage').style.display="block";
		}

		if(currPage == lastPage) {
			$('nextResultsPage').style.display="none";
		} else if (currPage < lastPage) {
			$('nextResultsPage').style.display="block";
		}
		$('currentRows').innerHTML=getRowRange(currPage);
		$('totalRows').innerHTML=itemCount;
	}
	function getRowRange(pageNum) {
		var maxExercises = itemCount;
		var firstRow = (pageNum * exPerPage) + 1;
		if(firstRow > maxExercises)
			firstRow = maxExercises;
		var lastRow = (pageNum+1) * exPerPage;
		if(lastRow > maxExercises)
			lastRow = maxExercises;
		return firstRow + "-" + lastRow;
	}
	function toggleDiv(divId) {
		var theDiv = $(divId);
		if(theDiv.style.display == 'none')
			Effect.BlindDown(theDiv);
		else
			Effect.BlindUp(theDiv);
	}
	function toggleExercise(checkbox, categoryId, exerciseId, isCardio) {
		/* Begin Coremetrics code */
		try {
			cmCreatePageElementTag(exerciseId + " - " +(checkbox.checked?'Add':'Remove'), categoryId, "Exercise Finder");
		}
		catch(e) {
			// fail silently
		}
		/* End Coremetrics code */
		var exerciseUpdater = new Ajax.Updater('searchResults', 'exercise-finder.html', { evalScripts: true, parameters: 'operation='+(checkbox.checked?'add':'remove')+'&categoryId='+categoryId+'&exerciseId='+exerciseId+"&minutes="+exerciseDuration+"&sessions="+exerciseSessions+"&calories="+reduceCalories+"&cardio="+isCardio });
//		lastPage = Math.floor(itemCount/exPerPage);
//		itemCount++;
//		updatePagerDisplay();
	}
	function toggleCat(checkbox, categoryId, isCardio)
	{
		var url = contextRoot + '/exerciseFinder.do';
		var pars = {
				operation: checkbox.checked ? 'addCat' : 'removeCat',
				categoryId: categoryId,
				cardio: isCardio
		};
		doJsonAjaxRequest(url, pars, function() {
			toggleDiv((isCardio ? 'cardio' : 'strength') + 'Div' + categoryId);
		});
	}

	// Called by html-onload. Ensures that the checkbox state of each category
	// matches whether the exercises within that category are shown.
	// This was necessary when the user hits the Back button - in both FF and IE
	// the browser would 'forget' which category blinddowns were open and which were closed.
	function fixCatDivs()
	{
		var cb = $$('.ExerciseCategory input');
		for (var i = 0; i < cb.length; i++)
		{
			var id = cb[i].id;
			var divId = id.replace('Cat', 'Div');
			$(divId).style.display = cb[i].checked ? 'block' : 'none';
		}
	}

	// Add cardio exercise to user's ExercisePlan
	function addToPlan(exerciseId, inExerciseFinder)
	{
		/* Begin Coremetrics code */
		try {
			cmCreateConversionEventTag(exerciseId, "2", "Exercise - Add To Plan");
		}
		catch(e) {
			// fail silently
		}
		/* End Coremetrics code */
		if (isStrengthProgram(exerciseId))
		{
			return addProgramToPlan(exerciseId, inExerciseFinder);
		}

		var timesPerWeek = $F('practice_' + exerciseId);
		var duration = $F('duration_' + exerciseId);
		var intensity = null;
		try
		{
			// certain exercises have only 1 intensity and it is not displayed
			intensity = $F('intensity_' + exerciseId);
		}
		catch(e) {}
		var variant = null;
		try
		{
			// certain exercises have only 1 variant and it is not displayed
			variant = $F('variant_' + exerciseId);
		}
		catch(e) {}

		var url = contextRoot + '/SubscribeExercise.do';
		var pars = {
				action: 'addCardio',
				exerciseId: exerciseId,
				variant: variant,
				intensity: intensity,
				timesPerWeek: timesPerWeek,
				fromFinder: 'YES',
			  	duration: duration
			};
		doJsonAjaxRequest(url, pars, function() {
					if (inExerciseFinder)
					{
						// Toggle visibility of Add/Remove sections
				   		$('ExerciseAdded_' + exerciseId).style.display = 'block';
				   		$('AddToPlan_' + exerciseId).style.display = 'none';

				   		// Show border around exercise summary
				   		$('ExerciseSummary_' + exerciseId).style.borderStyle = 'solid';
				   	}

				   	$('CongratsOverlay').style.display = "block";
		   			$('JoinOverlayArea').style.height="220px";
			   	});
	}

	// Add strength program to user's ExercisePlan
	function addProgramToPlan(exerciseId, inExerciseFinder)
	{
		var timesPerWeek = $F('practice_' + exerciseId); //TODO

		var url = contextRoot + '/SubscribeExercise.do';

		var pars = {
				action: 'addProgram',
				programId: exerciseId,
				fromFinder: 'YES',
				timesPerWeek: timesPerWeek
			};
		doJsonAjaxRequest(url, pars, function() {
					if (inExerciseFinder)
					{
						// Toggle visibility of Add/Remove sections
				   		$('ExerciseAdded_' + exerciseId).style.display = 'block';
				   		$('AddToPlan_' + exerciseId).style.display = 'none';

				   		// Show border around exercise summary
				   		$('ExerciseSummary_' + exerciseId).style.borderStyle = 'solid';
				   	}

				   	$('CongratsOverlay').style.display = "block";
		   			$('JoinOverlayArea').style.height="220px";
			   	});
	}

	// Remove cardio exercise from user's ExercisePlan
	function removeFromPlan(exerciseId)
	{
		if (isStrengthProgram(exerciseId))
		{
			return removeProgramFromPlan(exerciseId);
		}

		var url = contextRoot + '/SubscribeExercise.do';

		var pars = {
				action: 'removeCardio',
				exerciseId: exerciseId
			};
		doJsonAjaxRequest(url, pars, function() {
					// Toggle visibility of Add/Remove sections
			   		$('ExerciseAdded_' + exerciseId).style.display = 'none';
			   		$('AddToPlan_' + exerciseId).style.display = 'block';

			   		// Hide border around exercise summary
			   		$('ExerciseSummary_' + exerciseId).style.borderStyle = 'none';

			   		// Uncheck 'Add To My Success Plan' checkbox
			   		$('addToPlan' + exerciseId).checked = 0;
			   	});
	}

	// Remove strength program from user's ExercisePlan
	function removeProgramFromPlan(exerciseId)
	{
		var url = contextRoot + '/SubscribeExercise.do';

		var pars = {
				action: 'removeProgram',
				programId: exerciseId
			};
		doJsonAjaxRequest(url, pars, function() {
					// Toggle visibility of Add/Remove sections
			   		$('ExerciseAdded_' + exerciseId).style.display = 'none';
			   		$('AddToPlan_' + exerciseId).style.display = 'block';

			   		// Hide border around exercise summary
			   		$('ExerciseSummary_' + exerciseId).style.borderStyle = 'none';

			   		// Uncheck 'Add To My Success Plan' checkbox
			   		$('addToPlan' + exerciseId).checked = 0;
			   	});
	}

function isStrengthProgram(exerciseId)
{
	return typeof(exerciseMap[exerciseId]) == 'undefined';
}

// Recalculate Calories Burned
// called when changing dropdown values in Exercise Results (in right nav)
function updateBurned(exerciseId)
{
	if (isStrengthProgram(exerciseId))
	{
		return updateBurnedStrengthProgram(exerciseId);
	}

	var ex = exerciseMap[exerciseId];
	var practice = $F('practice_' + exerciseId);
	var duration = $F('duration_' + exerciseId);

	var variant = null;
	try
	{
		var variantName = $F('variant_' + exerciseId);
		variant = ex[variantName];
	}
	catch (e)
	{
		// no variant dropdown - use default variant
		for (var i in ex)
		{
			variant = ex[i];
			break;
		}
	}

	var calFactor = null;
	try
	{
		var intVal = $F('intensity_' + exerciseId);
		for (var i = 0; i < variant.length; i++)
		{
			if (variant[i].name == intVal)
			{
				calFactor = variant[i].calories;
				break;
			}
		}
	}
	catch (e)
	{
		// no intensity dropdown - use default variant
		calFactor = variant[0].calories;
	}

	var cal = Math.round((practice * calFactor * duration * currentWeight) / 60);

	$('burned_' + exerciseId).innerHTML = cal;

	var elim = parseInt($F('eliminating_' + exerciseId)); // daily cal. loss due to diet
	var targetDate = calcTargetDate(currentWeight, targetWeight, practice * calFactor * duration / 60, elim);
	displayTargetDate(exerciseId, targetDate);
}

// Recalculate Calories Burned
// called when changing dropdown values in Exercise Results (in right nav)
// for Strength Programs.
function updateBurnedStrengthProgram(exerciseId)
{
	var calFactor = programMap[exerciseId];

	var practice = $F('practice_' + exerciseId);

	var cal = Math.round(practice * calFactor * currentWeight);

	$('burned_' + exerciseId).innerHTML = cal;

	var elim = parseInt($F('eliminating_' + exerciseId)); // daily cal. loss due to diet
	var targetDate = calcTargetDate(currentWeight, targetWeight, practice * calFactor, elim);
	displayTargetDate(exerciseId, targetDate);
}

var weightChanged = function() {};

function weightSliderChanged()
{
	try {
		eval("weightChanged();");
	}
	catch (e) { alert(e.message); }
}

function displayTargetDate(exerciseId, targetDate)
{
	var finderField = $('targetDate_' + exerciseId);
	if (finderField) // in exercise-finder
	{
		if (targetDate == null)
		{
			finderField.innerHTML = "?";
		}
		else
		{
			// Jan. 17, 2007
			var html = MONTH[targetDate.getMonth()] + '. ' +
				targetDate.getDate() + ', ' + targetDate.getFullYear();
			finderField.innerHTML = html;
		}
	}
	else	// in description page
	{
		if (targetDate == null)
		{
			$('targetDate_mmm_dd').innerHTML = "?";
			$('targetDate_yyyy').innerHTML = "";
		}
		else
		{
			$('targetDate_mmm_dd').innerHTML = MONTH[targetDate.getMonth()].toLowerCase() + '.' +
				targetDate.getDate();
			$('targetDate_yyyy').innerHTML = targetDate.getFullYear();
		}
	}
}

function calcTargetDate(currentWeight, targetWeight, weeklyCalFactor, dailyDietCal)
{
	var loss = currentWeight - targetWeight;
	if (loss <= 0)
	{
		return null; // user wants to gain weight...
	}

	var dailyCal = dailyDietCal + weeklyCalFactor * currentWeight / 7;
	var days = loss * CAL_PER_LBS / dailyCal;
	var now = new Date();
	var targetDate = new Date(now.valueOf() + days * MILLIS_PER_DAY);

	return targetDate;
}

function selectFinderVariant(exerciseId)
{
	var div = $('intensity_' + exerciseId);
	if (div != null)
	{
		var variant = $F('variant_' + exerciseId);
		var intensities = exerciseMap[exerciseId][variant];
		var html = "";
		for (var i = 0; i < intensities.length; i++)
		{
			var name = intensities[i].name;
			html = html + '<OPTION VALUE="' + name + '">' + name + '<\/OPTION>\n';
		}
		div.innertext = html;
	}

	updateBurned(exerciseId);
}

// Shows the Select Exercise Overlay
function showSelectExerciseOverlay(exerciseId, exerciseName, inExerciseFinder) {

	var onSuccess = 'addToPlan(\'' + exerciseId + '\', ' + inExerciseFinder + ');';
	var isCardio = !isStrengthProgram(exerciseId);

	var theUrl = contextRoot + '/selectExercise.jsp?isCardio=' + isCardio +
		'&exerciseId=' + exerciseId +
		'&exerciseName=' + exerciseName +
		'&popupId=SelectExercise' +
		'&onSuccess=' + escape(onSuccess) +
		'&inExerciseFinder=' + inExerciseFinder;

	showFormSelectExercise(theUrl, '', '');

	if (!$('joinimg')) {
		addToPlan(exerciseId, inExerciseFinder);
    } else {
		showInnerSignin('Ex');
	}
}

// Shows the register (join) blind down from the selected exercise overlay
function registerWithSelectedExercise(exerciseId, exerciseName, inExerciseFinder) {
	$('SelectDietOverlay').style.display = "none";
	winSelectExercise.hide();

	var onSuccess = 'showSelectExerciseOverlay(\'' + exerciseId + '\', \'' + exerciseName + '\', ' +
						inExerciseFinder + ');';
	showJoin(onSuccess);
}

function takeToExerciseDetails(exerciseId,currentW,targetW,isCardio)
{
    if(isCardio=='true')
        gotoit('exercises/' + exerciseId +
		'/description.html&currentWeight=' + currentW + '&targetWeight=' + targetW);
    else
        gotoit('exercisePlans/' + exerciseId +
		'/description.html&currentWeight=' + currentW + '&targetWeight=' + targetW);
}

function showExerciseDetails(exerciseId)
{
	var isCardio = !isStrengthProgram(exerciseId);
	var u = (isCardio ? 'exercises' : 'exercisePlans') + '/' + exerciseId +
		'/description.html&currentWeight=' + currentWeight + '&targetWeight=' + targetWeight;

	u += '&practice=' + $F('practice_' + exerciseId);
	try {
		u += '&duration=' + $F('duration_' + exerciseId);
	} catch (e) {} // ignore

	try {
		u += '&intensity=' + escape($F('intensity_' + exerciseId));
	} catch (e) {} // ignore

	try {
		u += '&variant=' + escape($F('variant_' + exerciseId));
	} catch(e) {} // ignore
	try {
		u += '&elim=' + escape($F('eliminating_' + exerciseId));
	} catch(e) {} // ignore

	gotoit(u);
}

var weightSliderRange = calcWeightSliderRange();

function calcWeightSliderRange()
{
	// Johnny Angel 08/27/2007 Added additional weight values for under 350 lbs.

	var a = [];
	var i = 80;
	while (i <= 350)
	{
		a.push(i);
		var p = 0;
		while(p < 10)
		{
			a.push(i + "." + p);
			p++;
		}
		i++;
	}

	i = 352;
	while (i <= 500)
	{
		a.push(i);
		var p = 0;
		while(p < 10)
		{
			a.push(i + "." + p);
			p++;
		}
		i += 2;
	}

	i = 505;
	while (i <= 600)
	{
		a.push(i);
		i += 5;
	}
	return a;
}

function findSliderValue(val)
{
	// Johnny Angel 08/27/2007 Added Rounding for decimal values.
	// Johnny Angel 09/16/2007 Elimated Rounding values are now floating point

	for (var i = 0; i < weightSliderRange.length; i++)
	{
		if (weightSliderRange[i] == val)
			return i;
	}
	return -1;
}