//*******************************************************************
// Name:  adv3calc.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 third 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
//
//*******************************************************************

// 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;
	}
}


// Function to drop all decimal places.
// Use only when displaying a result.
function dropAllDecimals(theNumber){
	var theString, decimalIndex
	theString = theNumber.toString();
	decimalIndex = theString.indexOf('.');
	if (decimalIndex != -1){
		theString = theString.slice(0, decimalIndex);
		return theString;
	}else{
		return theString;
	}
}


var resultCalculated = 0;   // Global set to 1 if result has been calculated and 0 if not


// Function to draw "Possible Activities" table.  actStr is passed from the calculation function and 
//  is only used if resultCalculated = 1
var advActivitiesString = "";   // String containing the whole activities list page.  Must be global to be sent to browser.
function advDrawActivities(actStr){
	// Top of the page
	advActivitiesString = "<html><head><style><!-- .greentext { font-family: arial, sans-serif, helvetica; color: #07BB07; font-size: 11pt; } td { font-family: arial, sans-serif, helvetica; color: #000000; font-size: 11pt; } th { font-family: arial, sans-serif, helvetica; color: #000000; font-size: 12pt; font-weight: bold; } --></style></head><body bgcolor=#FFFFFF leftmargin=0 marginwidth=0><table width=320 cellpadding=3 cellspacing=0 border=1><tr><th valign=top align=\"center\">Possible Activies</th></tr><tr><td valign=\"top\"><table width=\"100%\" cellpadding=1 cellspacing=0 border=0>";

	if (!resultCalculated){
		// HTML for table that appears first with description and "Display Activities" button.
		advActivitiesString += "<tr><td valign=\"top\"><form name=\"form2\">This table will display all activities that can burn the entered calories in the given time or less, and show how long you will need to do each activity.<br><br><div align=\"center\"><input type=\"button\" name=\"calculate\" onMouseUp=\"javascript:parent.frames[0].adv3Calculate();\" value=\"Display Activities\"></div></form></td></tr>";
	}else{
		// HTML passed from the calculation function containing all of the activities and their respective times
		advActivitiesString += actStr;
	}

	// Bottom of the page
	advActivitiesString = advActivitiesString + "</table></td></tr></table></body></html>";

	// Displaying the list frame
	parent.frames['resultframe'].location.replace("javascript:parent.frames[0].advActivitiesString");
}


// Function to reshow the start "Possible Activities" table with the "Display Activities" button
function clearResult(){
	if (!resultCalculated)
		return;
	resultCalculated = 0;
	advDrawActivities();
}


// Function that calculates possible activities
function adv3Calculate(){
	var calories, weight, weight_units, time, actTime, minMET, MET, workingString, resultString
	var possibleActivityArray = new Array();

	// First we get all of the values from the form
	weight = parent.frames['inputframe'].document.form1.weight.value;
	if (parent.frames['inputframe'].document.form1.weight_units[0].checked){
		weight_units = parent.frames['inputframe'].document.form1.weight_units[0].value;
	}else if (parent.frames['inputframe'].document.form1.weight_units[1].checked){
		weight_units = parent.frames['inputframe'].document.form1.weight_units[1].value;
	}
	calories = parent.frames['inputframe'].document.form1.calories.value;
	time = parent.frames['inputframe'].document.form1.time.value;

	// Then we make sure the important ones are all filled in.  If not, we alert the user.
	if (weight == ""){
		alert("You need to enter your weight (be honest).");
		return;
	}else if(weight_units == 'lbs'){
		// Pounds to kilograms
		weight = weight / 2.2;
	}
	if (calories == ""){
		alert("You need enter how many calories you wish to burn.");
		return;
	}
	if (time == ""){
		alert("You need to enter how much time you wish to spend.");
		return;
	}else{
		// minutes to hours
		time = time / 60;
	}

	// Now we find the minimum MET that will fit the entered data
	minMET = calories / (weight * time);

	// Go through actObj and find which activities have an MET >= minMET
	
	for (activity in actObj){
		if (actObj[activity].met < minMET){
			continue;
		}
		// Calculating time for each valid activity and puting number and time into an array
		actTime = (calories / (actObj[activity].met * weight)) * 60;
		possibleActivityArray[possibleActivityArray.length] = [activity, actTime];
	}

	// Now we sort the array from shortest to longest time and make the HTML for the Possible Actions table
	if (!is_nav3 && !is_nav4){
		// Old Navigators don't understand the sort method
		possibleActivityArray.sort(function(a,b){return a[1] - b[1];});
	}

	resultCalculated = 1;
	resultString = "";
	if (possibleActivityArray.length == 0){
		// If there are no possible activities, we say so
		resultString = "<tr><td valign=\"top\"><form name=\"form2\">Sorry, but there are no activities that fit your parameters.</div></form></td></tr>";
		advDrawActivities(resultString);
	}else{
		if (is_nav6up){
			// This must be used for Netscape 6 due to an Access Denied bug when writing to another frame
			// If there are possible activities, we put them into HTML and display them
			for (var n = 0; n < possibleActivityArray.length; n++){
				// if n is even the table row will have a dark background otherwise it will be white
				if (n % 2 == 0){
					//resultString += "blah";
					resultString += "<tr bgcolor=#EEEEEE><td valign=\"top\">-</td><td valign=\"top\">" + actObj[possibleActivityArray[n][0]].examples + "</td><td valign=\"top\">&nbsp;</td><td class=\"greentext\" valign=\"top\" align=\"right\">" + dropAllDecimals(possibleActivityArray[n][1]) + "&nbsp;min</td></tr>";
				}else{
					//resultSring += "blah2";
					resultString += "<tr bgcolor=#FFFFFF><td valign=\"top\">-</td><td valign=\"top\">" + actObj[possibleActivityArray[n][0]].examples + "</td><td valign=\"top\">&nbsp;</td><td class=\"greentext\" valign=\"top\" align=\"right\">" + dropAllDecimals(possibleActivityArray[n][1]) + "&nbsp;min</td></tr>";
				}
			}
			advDrawActivities(resultString);
		}else{
			// This works for the other browsers
			parent.frames['resultframe'].document.open();
			parent.frames['resultframe'].document.writeln("<html><head><style><!-- .greentext { font-family: arial, sans-serif, helvetica; color: #07BB07; font-size: 11pt; } td { font-family: arial, sans-serif, helvetica; color: #000000; font-size: 11pt; } th { font-family: arial, sans-serif, helvetica; color: #000000; font-size: 12pt; font-weight: bold; } --></style></head><body bgcolor=#FFFFFF leftmargin=0 marginwidth=0><table width=320 cellpadding=3 cellspacing=0 border=1><tr><th valign=top align=\"center\">Possible Activies</th></tr><tr><td valign=\"top\"><table width=\"100%\" cellpadding=1 cellspacing=0 border=0>");

			for (var n = 0; n < possibleActivityArray.length; n++){
				// if n is even the table row will have a dark background otherwise it will be white
				if (n % 2 == 0){
					parent.frames['resultframe'].document.writeln("<tr bgcolor=#EEEEEE><td valign=\"top\">-</td><td valign=\"top\">" + actObj[possibleActivityArray[n][0]].examples + "</td><td valign=\"top\">&nbsp;</td><td class=\"greentext\" valign=\"top\" align=\"right\">" + dropAllDecimals(possibleActivityArray[n][1]) + "&nbsp;min</td></tr>");
				}else{
					parent.frames['resultframe'].document.writeln("<tr bgcolor=#FFFFFF><td valign=\"top\">-</td><td valign=\"top\">" + actObj[possibleActivityArray[n][0]].examples + "</td><td valign=\"top\">&nbsp;</td><td class=\"greentext\" valign=\"top\" align=\"right\">" + dropAllDecimals(possibleActivityArray[n][1]) + "&nbsp;min</td></tr>");
				}
			}
			parent.frames['resultframe'].document.writeln("</table></td></tr></table></body></html>");
			parent.frames['resultframe'].document.close();
		}


/*
		// If there are possible activities, we put them into HTML and display them
		for (var n = 0; n < possibleActivityArray.length; n++){
			// if n is even the table row will have a dark background otherwise it will be white
			if (n % 2 == 0){
				//resultString += "blah";
				resultString += "<tr bgcolor=#EEEEEE><td valign=\"top\">-</td><td valign=\"top\">" + actObj[possibleActivityArray[n][0]].examples + "</td><td valign=\"top\">&nbsp;</td><td class=\"greentext\" valign=\"top\" align=\"right\">" + dropAllDecimals(possibleActivityArray[n][1]) + "&nbsp;min</td></tr>";
			}else{
				//resultSring += "blah2";
				resultString += "<tr bgcolor=#FFFFFF><td valign=\"top\">-</td><td valign=\"top\">" + actObj[possibleActivityArray[n][0]].examples + "</td><td valign=\"top\">&nbsp;</td><td class=\"greentext\" valign=\"top\" align=\"right\">" + dropAllDecimals(possibleActivityArray[n][1]) + "&nbsp;min</td></tr>";
			}
			alert(n);
		}
		alert("before draw");
		advDrawActivities(resultString);
		alert("after draw");
*/

	
	}

}