

var v = false;

if (typeof(Option)+"" != "undefined") v=true;

// Universal Related Select Menus - cascading popdown menus
// by Andrew King. v1.34 19990720
// Copyright (c) 1999 internet.com LLC. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Originally published and documented at http://www.webreference.com
// Contact aking@internet.com for all other uses.
//
// Change History
// 3-3-96 code optimized, modularized, and made universal-abk
// 3-8-97 jumpPage1 and 2 consolidated into generalized func
// 3-10-97 changeMenu added jumpPage(0,0), for non-ns3 browsers
// make popup act like a live popup when no related functionality
// 8-5-97 browser detect script changed to check for option functionality
// 9-11-97 aln changed to options.length, as IE 4 does not support
// nulling out options
// 12-22-98 added SIZE att to make select lists, used named forms/elements
// 1-1-99 added getFormNum to fix netscape 2 bug (doesn't support named fms)
//  pass formName, elementNum to relate and jmp as workaround
//  pass this.form,0 and increment instead of decrement in relate to get 
//   next form
//  assumes menu2 follows menu1 in HTML
// tested in mac ie3.01/ns2.02 (live), ns 3.01,4.05,4.5 and ie4.01 related
// 990204 - adopted a more modular/oo approach using two D arrays, thanks
//   to Jacob Berner <leo7278@hotmail.com> for his input
// 990701 - fixed back button bug in ie4+ by resetting all forms onload-abk
//    thanks to peter belesis (pbel@internet.com) for this fix
// 990714 - added IE5 persistence - abk pb
// 990715 - went open source, created stripped down example files - abk
// 990720 - jmp() fix getFormNum(formName);->
//     var formNum = getFormNum(formName);
//  if (v) {..} added around newCat array creation for old browsers
// 990721 - getFormNum/relate tweaks, unnec code removed, 
//     ns2.0 mac fix - getFormNum returns -1 in nested tables
//     new jmp bypasses prob - thx to thanks to Michael Guitton
//     saramaca@mail.dotcom.fr
// 990722 - var for scope added, jmp chgd to named form for ns2.02 bug-abk

if(v){a=new Array(22);}

function getFormNum (formName) {
	var formNum =-1;
	for (i=0;i<document.forms.length;i++){
		tempForm = document.forms[i];
		if (formName == tempForm) {
			formNum = i;
			break;
		}
	}
	return formNum;
}

function jmp(form, elt)
// The first parameter is a reference to the form.
{
	if (form != null) {
		with (form.elements[elt]) {
			if (0 <= selectedIndex)
				location = options[selectedIndex].value;
		}
	}
}

var catsIndex = -1;
var itemsIndex;

if (v) { // ns 2 fix
function newCat(){
	catsIndex++;
	a[catsIndex] = new Array();
	itemsIndex = 0;
}

function O(txt,url) {
	a[catsIndex][itemsIndex]=new myOptions(txt,url);
	itemsIndex++;
}

function myOptions(text,value){
	this.text = text;
	this.value = value;
}

// fill array

newCat(); //Resting
O("N/A","0.0099");

newCat();  //Standing Light Work
O("N/A","0.0272");

newCat();//Backpacking
O("Moderate","0.0453");
O("Vigorous","0.0604");

newCat(); //Baseballl
O("Moderate","0.0318");
O("Vigorous","0.0391");

newCat(); //Basketball
O("Moderate","0.0294");
O("Vigorous","0.0656");

newCat(); //Bicycling
O("5 MPH","0.0194");
O("6 MPH","0.0272");
O("7 MPH","0.0316");
O("8 MPH","0.0347");
O("9 MPH","0.0378");
O("10 MPH","0.0425");
O("11 MPH","0.0503");
O("12 MPH","0.0578");
O("13 MPH","0.0681");

newCat(); //Bowling
O("N/A","0.0272");

newCat(); //Calisthenics
O("Moderate","0.0347");
O("Vigorous","0.0975");

newCat(); //Dancing
O("Moderate","0.0318");
O("Vigorous","0.0603");

newCat(); //Field Hockey
O("N/A","0.0603");

newCat(); //Football
O("Moderate","0.0331");
O("Vigorous","0.0553");

newCat(); //Ice Hockey
O("N/A","0.0662");

newCat(); //Ice Skating
O("N/A","0.0425");

newCat(); //Karate
O("N/A","0.0853");

newCat(); //Mountain Climbing
O("N/A","0.0656");

newCat(); //Racquetball
O("N/A","0.0656");

newCat(); //Roller Skating
O("N/A","0.0425");

newCat(); //Jogging
O("5 MPH","0.0603");
O("6 MPH","0.0728");
O("7 MPH","0.0853");
O("8 MPH","0.0975");
O("9 MPH","0.1084");
O("10 MPH","0.1212");
O("11 MPH","0.1337");
O("12 MPH","0.1459");

newCat(); //Soccer
O("N/A","0.0597");

newCat(); //Squash
O("Moderate","0.0672");
O("Vigorous","0.0772");

newCat(); //Swimming Bkstr
O("25 Yards/Min","0.0253");
O("30 Yards/Min","0.0353");
O("35 Yards/Min","0.0453");
O("40 Yards/Min","0.0553");
O("45 Yards/Min","0.0653");
O("50 Yards/Min","0.0753");

newCat(); //Swimming Brstr
O("20 Yards/Min","0.0315");
O("30 Yards/Min","0.0475");
O("40 Yards/Min","0.0637");

newCat(); //Swimming Crawl
O("20 Yards/Min","0.0316");
O("25 Yards/Min","0.0400");
O("35 Yards/Min","0.0484");
O("45 Yards/Min","0.0578");
O("50 Yards/Min","0.0706");

newCat(); //Tennis
O("Moderate","0.0347");
O("Vigorous","0.0648");

newCat(); //Volleyball
O("Moderate","0.0291");
O("Vigorous","0.0653");

newCat(); //Weight Training
O("N/A","0.0522");

newCat(); //Wrestling
O("N/A","0.0853");

} // if (v)

function relate(formName,elementNum,j) {
if(v){
var formNum = getFormNum(formName);
 if (formNum>=0) {
	// We ignore this since we are in the same form
	// formNum++; // reference next form, assume it follows in HTML
	// But we do increment the element
	elementNum++;
	with (document.forms[formNum].elements[elementNum]) {
		for(i=options.length-1;i>0;i--) options[i] = null; // null out in reverse order (bug workarnd)
		for(i=0;i<a[j].length;i++){
			options[i] = new Option(a[j][i].text,a[j][i].value); 
		}
		options[0].selected = true;
	}
 }
} else {
   //jmp(formName,elementNum);
}
}

// BACK BUTTON FIX for ie4+- or
// MEMORY-CACHE-STORING-ONLY-INDEX-AND-NOT-CONTENT
//
// it has to do with MEMORY CACHE (where form input is stored) and how
// IE stores information about SELECT menus.
//
// IE stores the selectedINDEX ONLY of the SELECT menu, not the
// CONTENTS-AT-THE-TIME-OF-SELECTION
//
// when we return to a page, it displays the default contents of each
// SELECT, grabs the stored index from cache and aligns the default
// contents to that index.
// 
// Netscape, on the other hand, seems to remember both INDEX and CONTENTS
// added ie5 persistence 990714

function IEsetup(){
	if(!document.all) return;
	IE5 = navigator.appVersion.indexOf("5.")!=-1;
	if(!IE5) {
		for (i=0;i<document.forms.length;i++) {
			document.forms[i].reset();
		}
	}
}

window.onload = IEsetup;

function cal_calc(ivl, wgt, time) {
//window.alert ("IVL: " + ivl);
   calc = (ivl * wgt * time);
   
   f_calc = Math.floor(calc);
   diff  = calc - f_calc;
   diff = diff * 10;
   diff = Math.round(diff);

   if (diff == 10) {
      // Need to bump up the whole thing instead
      f_calc += 1;
      diff = 0;
   }


   calc = f_calc + "." + diff;
   calc = Math.round(calc);
   return calc;
}

function compute() {
   var f = document.f1;
   var wgt = parseInt(f.wgt.value);
   var time = parseInt(f.time.value);
   var ivl = document.f1.ivl[document.f1.ivl.selectedIndex].value;
   var act = parseInt(f.act.options[f.act.selectedIndex].value);

   if (chkw(wgt)){
		if (chkw(time)){
			f.calc.value = cal_calc(ivl, wgt, time);
		} else {
			f.time.focus();
			alert("Please enter a number for the duration of your activity."); 
		}
   } else { 
			f.wgt.focus();
      alert("Please enter a number for your weight."); 
   }
   // Set cookies for future views of this page
   // Expires when user closes browser
   document.cookie = "calc=" + f.calc.value;
   document.cookie = "wgt=" + f.wgt.value;
   document.cookie = "time=" + f.time.value;
   document.cookie = "ivl=" + f.ivl.selectedIndex;
   document.cookie = "act=" + f.act.selectedIndex;
}

function chkw(w) {
   if (isNaN(parseInt(w))) {
      return false;
   } else if (w < 0) {
      return false;
   }
   else {
      return true;
   }
}

function load_cookies() {
	var f = document.f1;
	f.wgt.value = loadcookie("wgt");
	f.calc.value = loadcookie("calc");
	f.time.value = loadcookie("time");
	f.act.selectedIndex = loadcookie("act");
	relate(document.f1,0,f.act.selectedIndex)
	f.ivl.selectedIndex = loadcookie("ivl");
}

function loadcookie(name) {
    var allcookies = document.cookie;
	var value;
	var pos = allcookies.indexOf(name + "=");
	var len = name.length + 1;  // length of name + 1
	if (pos != -1) {
		var start = pos + len;  //Start of cookie value
		var end = allcookies.indexOf(";", start);  //End of cookie value
		if (end == -1) end = allcookies.length;
		value = allcookies.substring(start,end); //Extract the value
		value = unescape(value);  // Decode it, if encoded.
    	return value;
	}

    return 0;
}

