function layerWrite(id,text) {
     if (document.all) {
        document.all[id].innerHTML = text
     }
     else if (document.layers) {
        var lyr = document.layers[id].document;
        lyr.open();
        lyr.write(text);
        lyr.close();
     }
     else if (!document.all && document.getElementById) {
       document.getElementById(id).innerHTML = text;
     }
}


function calcIBW(){
        var lowerRange=0;
        var upperRange=0;

		with(document.forms[2]) {
			var userGender = selGender.selectedIndex;
			var userFrameSize = selFrame.selectedIndex;

			var numFeet = selFeet.selectedIndex+4;
			var numInches = selInches.selectedIndex;
        }

			numInchesOver5 = (numFeet-5)*12 + numInches;
		
		
			switch(userGender){
				
				case 1:
					if (numInchesOver5<0) {
						idealBodyWeight= 106 + numInchesOver5*2.5;
					} else {
						idealBodyWeight= 106 + numInchesOver5*6;
					}

					break;
				
				case 0:
					if (numInchesOver5<0) {
						idealBodyWeight= 100 + numInchesOver5*2.5;
					} else {
						idealBodyWeight= 100 + numInchesOver5*5;
					}
					break;

			}

			adjustment = idealBodyWeight*.10;
			if (userFrameSize==0) {	
				lowerRange = Math.round(idealBodyWeight - adjustment);
				upperRange = Math.round(idealBodyWeight);
			} else if (userFrameSize==1) {	
				lowerRange = Math.round(idealBodyWeight);
				upperRange = Math.round(idealBodyWeight + adjustment);
			} else if (userFrameSize==2) {	
				lowerRange = parseInt(Math.round(idealBodyWeight + adjustment));
				upperRange = parseInt(Math.round(lowerRange * 1.1));
			}

			//alert("Range is " + lowerRange + " - " + upperRange);

		return lowerRange + " - " + upperRange + " lbs";
}


function calcCals(){
        var oF=document.forms[1];
        var A=oF.selA.options[oF.selA.selectedIndex].value;
        var W=oF.weight.value*oF.selW.options[oF.selW.selectedIndex].value;
        var D=oF.dur.value*oF.selT.options[oF.selT.selectedIndex].value;
        //alert('A -- '+A+' W -- '+W+' D -- '+D);
		return Math.round(A*W*D/60);
}

function BMI_calc() {
    var form=document.forms[3]
	var Weight=0;
	var Height=0;
    var Correct=true;
	Weight = parseFloat(form.weight.value);
	
	if (form.weight.value == null || form.weight.value.length == 0) {
        alert ("Please fill in all of the form elements");
        Correct = false;
	}
	else {
			if (isNaN (Weight)) {
			alert("\nMake sure to enter the weight \nas a number.");
			Correct = false;
			form.weight.value="";
		}
		if (Weight <= 10) {
			alert("\nPlease enter a real weight");
			Correct = false;
			form.weight.value="";   
		}

	}	
	
	if (Correct) {
        var Feet = form.selFeet.selectedIndex+4;
        var Inches = form.selInches.selectedIndex;
        var h = (Feet*12 + Inches)*0.0254;
        var BMI=(Weight / 2.2)/(h*h);
        var average="";
        var BMI_1=""+BMI;
        
        var n = BMI_1.length - BMI_1.indexOf('.');
            if (BMI_1.indexOf('.') == -1) BMI_1 = BMI_1 + '.00';
            if (n == 1) BMI_1 = BMI_1 + '00';
            if (n == 2) BMI_1 = BMI_1 + '0';
            if (n > 3) BMI_1 = BMI_1.substring(0,BMI_1.length-n+3);
            
        if (BMI < 18.5)  average=' (underweight)';
        if ((BMI > 18.5 || BMI == 18.5)&& BMI < 24.9) average =' (normal)';
        if ((BMI > 25.0 || BMI == 25.0) && BMI < 29.9) average =' (overweight)';
        if ((BMI > 30.0 || BMI == 30.0)&& BMI < 39.9  ) average =' (obese)';
        if (BMI > 40.0 || BMI == 40.0) average =' (dangerously obese)';
    }
    return BMI_1 + average;
}


function THR_calc() {
    var form=document.forms[0]
    var Correct=true;
    
	if (form.age.value == null || form.age.value.length == 0 ) { 
	alert ("\nPlease enter your age");
	Correct = false;
	}	
	else {	
	var TestAge;		
	TestAge = parseFloat(form.age.value);
		if (isNaN (TestAge)) {
			alert("\nMake sure to enter the age \nas a number.");
			Correct = false;
			form.age.value="";
		}

		if (TestAge <= 0) {
			alert("\nEnter a real age.");
			Correct = false;
			form.age.value="";
		}

                if (TestAge >100) {
			alert("\nPlease enter an age\nbetween 1 and 100.");
			Correct = false;
			form.age.value="";
		}
	}
	
	if (Correct) {
		var Sec = parseInt(form.selSecs.options[form.selSecs.selectedIndex].value);
        var MHR=Math.round(((220 - TestAge)/60) * Sec);
        var MinT=Math.round(MHR * 0.60);
        var MaxT=Math.round(MHR * 0.85);   
        layerWrite('resultsDIV1',MinT+' and '+MaxT+' beats');
        layerWrite('resultsDIV2',MHR+' beats');

    }
}
