//*****************************************************************************
// Name:  adv2calc.js
//
// IMPORTANT:  
//	is.js is essential to the operation of some of the functions in 
//	this file.  is.js must be included in html if this file is to be 
//	used.
//
// Description:
//	The JavaScript file that runs the second advanced energy 
//	calculator.  This file must be included for the calculator to 
//	work.
//
// Author:  Kevin Mueller  mule@thinice.com
//
//	Initials		Date			Description
// ----------------------------------------------------------------------------
//	KRM				15-may-2001		Initial programming and design
//	KRM				31-may-2001		Changing way activities list is displayed
//
//*****************************************************************************

var resultCalculated = 0;   // Global set to 1 if result has been calculated and 0 if not
var sortingArray = new Array();   // Global array containing activity numbers sorted by category into subarrays
var showCatListArray = new Array(); // Global array containing binary values showing which categories are open and wich ones are collapsed.  


// Function to drop all but the first decimal place.  
// Use only when displaying a result.
function dropDecimalPlaces(theNumber){
	var theString, decimalIndex
	theString = theNumber.toString();
	decimalIndex = theString.indexOf('.');
	if (decimalIndex != -1){
		theString = theString.slice(0, decimalIndex+2);
		return theString;
	}else{
		return theString;
	}
}


/* Old activity list display function

// Function to draw whole activities list
var advActivitiesString = "";   // String containing the whole activities list page.  Must be global to be sent to browser.
function advDrawActivities(calcNum){
	var sortingArray, n, x;
	// Creating a sub-array in sortingArray for each category that will just contain actObj properties (eg, num1010)
	sortingArray = new Array();
	for (n = 0; n < categories.length; n++){
		sortingArray[n] = new Array();
	}
	// Stuffing the sortingArray array
	for (activity in actObj){
		subArrayIdx = actObj[activity].category 
		sortingArray[subArrayIdx][sortingArray[subArrayIdx].length] = activity;
	}
	// Top of the page
	advActivitiesString = "<html><head><style><!-- th { font-family: arial, sans-serif, helvetica; color: #000000; font-size: 12pt; font-weight: bold; } a { color: 0000EE; } a:hover { color: EE0000; } .smalltext { font-family: arial, sans-serif, helvetica; font-size: 9pt; }--></style></head><body bgcolor=#FFFFFF onLoad=\"parent.frames[0].activitiesList=0\"><table width=250 cellpadding=3 cellspacing=0 border=1><tr><th valign=top align=\"center\">Activies</th></tr><tr><td valign=\"top\"><table width=\"100%\" cellpadding=1 cellspacing=0 border=0>";
	// Writing the page to the advActivitiesString variable for displaying
	for (n = 0; n < sortingArray.length; n++){
		advActivitiesString += "<tr><td valign=\"top\" colspan=2 class=\"smalltext\"><b>" + categories[n] + ":</b></td></tr>";
		for (x = 0; x < sortingArray[n].length; x++){
			advActivitiesString += "<tr><td valign=\"top\">&nbsp;&nbsp;&nbsp;&nbsp;-</td><td valign=\"top\" class=\"smalltext\"><a href=\"javascript:parent.frames[0].adv" + calcNum + "SelectActivity('" + sortingArray[n][x] + "');\">" + actObj[sortingArray[n][x]].examples + "</a></td></tr>";
		}
	}
	advActivitiesString = advActivitiesString + "</table></td></tr></table></body></html>";
	// Displaying the list frame
	parent.frames['activitiesframe'].location.replace("javascript:parent.frames[0].advActivitiesString");
}
*/

// Function to stuff the sortingArray and the showCatListArray
function stuffSortingArray(){
	// Creating a sub-array in sortingArray for each category that will just contain actObj properties (eg, num1010)
	sortingArray = new Array();
	for (var n = 0; n < categories.length; n++){
		sortingArray[n] = new Array();
		showCatListArray[n] = 0;  // All categories will start collapsed
	}
	// Stuffing the sortingArray array
	for (activity in actObj){
		subArrayIdx = actObj[activity].category 
		sortingArray[subArrayIdx][sortingArray[subArrayIdx].length] = activity;
	}
	newDrawActivities();
}


// Function to expand a category list
function expandCat(catNum){
	showCatListArray[catNum] = 1;
	newDrawActivities();
}


// Function to collapse a category list
function collapseCat(catNum){
	showCatListArray[catNum] = 0;
	newDrawActivities();
}


// Function to show the new collapsable activities list
function newDrawActivities(){
	// Top of the page
	advActivitiesString = "<html><head><style><!-- th { font-family: arial, sans-serif, helvetica; color: #000000; font-size: 12pt; font-weight: bold; } a { color: 0000EE; } a:hover { color: EE0000; } .smalltext { font-family: arial, sans-serif, helvetica; font-size: 9pt; color: #000000; } .bigtext { font-family: arial, sans-serif, helvetica; font-size: 12pt; text-decoration: none;}--></style></head><body bgcolor=#FFFFFF onLoad=\"parent.frames[0].activitiesList=0\"><table width=275 cellpadding=3 cellspacing=0 border=1><tr><th valign=top align=\"center\">Activies</th></tr><tr><td valign=\"top\"><table width=\"100%\" cellpadding=1 cellspacing=0 border=0>";
	// Writing the page to the advActivitiesString variable for displaying
	for (var n = 0; n < sortingArray.length; n++){
		if (showCatListArray[n] == 0){
			// If this categories list is supposed to be closed then this line is displayed
			advActivitiesString += "<tr><td valign=\"top\" colspan=2><a class=\"bigtext\" href=\"javascript:parent.frames[0].expandCat(" + n + ");\"><b>+</b>&nbsp;<span class=\"smalltext\"><b>" + categories[n] + "</b></span></a></td></tr>\n";
		}else{
			// If this categories list is open, then this line is displayed as is the list of activities
			advActivitiesString += "<tr><td valign=\"top\" colspan=2 class=\"smalltext\"><a class=\"bigtext\" href=\"javascript:parent.frames[0].collapseCat(" + n + ");\"><b>-</b>&nbsp;<span class=\"smalltext\"><b>" + categories[n] + "</b></span></a></td></tr>";
			for (var x = 0; x < sortingArray[n].length; x++){
				advActivitiesString += "<tr><td valign=\"top\">&nbsp;&nbsp;&nbsp;&nbsp;-</td><td valign=\"top\" class=\"smalltext\"><a href=\"javascript:parent.frames[0].adv1SelectActivity('" + sortingArray[n][x] + "');\">" + actObj[sortingArray[n][x]].examples + "</a></td></tr>";
			}
		}
	}
	// Bottom of the page
	advActivitiesString = advActivitiesString + "</table></td></tr></table></body></html>";
	// Displaying the list frame
	parent.frames['activitiesframe'].location.replace("javascript:parent.frames[0].advActivitiesString");
}


// Function to display stuff in the first and second advanced calculator
function adv1Display(text, layerId){
	var lyr;   // the whole layer path for old NS browsers
	if (is_nav3 || is_nav4){   // For older Netscapes
		lyr = parent.frames['resultframe'].document.layers["ns" + layerId + "bs"].document.layers["ns" + layerId].document;
		lyr.open();
		lyr.write(text);
		lyr.close();
	}else if (is_nav6up || is_ie5up){   // For standards compliant browsers
		parent.frames['resultframe'].document.getElementById(layerId).innerHTML = text;
	}else{   // For old IEs
		parent.frames['resultframe'].document.all[layerId].innerHTML = text;
	}
	if (layerId == "result"){
		// If time was just calculated
		resultCalculated = 1;	
	}
}


// Function to reshow the calculate button/link
function clearResult(){
	if (!resultCalculated)
		return;
	if (is_nav3 || is_nav4){   // For older Netscapes
		adv1Display("<a class=\"nsresultlink\" href=\"javascript:parent.frames[0].adv2Calculate();\">Calculate&nbsp;Energy</a>", 'result');
	}else{   // For other browsers
		adv1Display("<span class=\"biggreen\">&nbsp;</span><input type=\"button\" name=\"calculate\" onMouseUp=\"javascript:parent.frames[0].adv2Calculate();\" value=\"Calculate Energy\"><span class=\"biggreen\">&nbsp;</span>", 'result');
	}
	resultCalculated = 0;
}


// Function to display selected activity for the first and second advanced calculator
// Recieves the activity's actObj property name 
function adv1SelectActivity(actProp){
	var activityText;
	activityText = "<span class=\"text\">" + actObj[actProp].examples + "</span>";
	adv1Display(activityText, 'activity');
	parent.frames['resultframe'].document.form1.activity.value = actProp;
	clearResult();
}


// Function to solve for energy
function adv2Calculate(){
	var activity, weight, weight_units, time, met, totalEnergy, resultString

	// First we get all of the values from the form
	activity = parent.frames['resultframe'].document.form1.activity.value;
	weight = parent.frames['resultframe'].document.form1.weight.value;
	if (parent.frames['resultframe'].document.form1.weight_units[0].checked){
		weight_units = parent.frames['resultframe'].document.form1.weight_units[0].value;
	}else if (parent.frames['resultframe'].document.form1.weight_units[1].checked){
		weight_units = parent.frames['resultframe'].document.form1.weight_units[1].value;
	}
	time = parent.frames['resultframe'].document.form1.time.value;

	// Then we make sure the important ones are all filled in.  If not, we alert the user.
	if (activity == ""){
		alert("You need to select an activity.");
		return;
	}
	if (weight == ""){
		alert("You need to enter your weight (be honest).");
		return;
	}else if(weight_units == 'lbs'){
		weight = weight / 2.2;
	}
	if (time == ""){
		alert("You need to enter how much time you spent.");
		return;
	}else{
		time = time / 60;
	}

	// Now we go for the calculation to get total energy in kilocalories
	met = actObj[activity].met;
	totalCalories = met * weight * time;
	totalCalories = dropDecimalPlaces(totalCalories);

	// Dispaly the result
	resultString = "<span class=\"biggreen\">" + totalCalories + " kcal</span>"
	adv1Display(resultString, 'result');

}