/* This function sets up an array of limits (limArray) used to establish a fitness index where n groups (such as age groups) can be sorted into categories based on limits (such as fitness levels) . One instance of the array will be created for each age range. To use it as part of a 2D array, set up a new array as in  ** maleLim = new Array(n) where n is the number of groups to be sorted. Then each instance must be populated with the construction maleLim[i] = new limArray ("limit 1 value", "limit 2 value"...).
*/
function limitArray(l0, l1, l2, l3, l4){
	this.l1=l1
	this.l2=l2
	this.l3=l3
	this.l4=l4
	this.l0=l0
}
//define male and female limit arrays for vMax

femaleLim = new Array(5)
femaleLim[0] = new limitArray(32, 35, 38, 42, "")//20-29
femaleLim[1] = new limitArray(30, 33, 36, 40, "")//30-39
femaleLim[2] = new limitArray(28, 31, 33, 37, "")//40-49
femaleLim[3] = new limitArray(25, 28, 30, 33, "")//50-59
femaleLim[4] = new limitArray(24, 26, 28, 32, "")//60 and greater

maleLim = new Array(5)
maleLim[0] = new limitArray(38, 42, 45, 49, "")//20-29
maleLim[1] = new limitArray(36, 40, 43, 48, "")//30-39
maleLim[2] = new limitArray(34, 38, 41, 45, "")//40-49
maleLim[3] = new limitArray(31, 35, 38, 42, "")//50-59
maleLim[4] = new limitArray(27, 31, 35, 39, "")//60 and greater

function puLimitArray(p0, p1, p2, p3, p4){
	this.p1=p1
	this.p2=p2
	this.p3=p3
	this.p4=p4
	this.p0=p0
}
//define male and female limit arrays for puMax

femalePuLim = new Array(5)
femalePuLim[0] = new limitArray(36, 30, 23, 17, "")//20-29
femalePuLim[1] = new limitArray(31, 24, 19, 11, "")//30-39
femalePuLim[2] = new limitArray(24, 18, 13, 6, "")//40-49
femalePuLim[3] = new limitArray(20, 17, 12, 6, "")//50-59
femalePuLim[4] = new limitArray(14, 12, 5, 2, "")//60 and greater

malePuLim = new Array(5)
malePuLim[0] = new limitArray(47, 37, 29, 22, "")//20-29
malePuLim[1] = new limitArray(39, 30, 24, 17, "")//30-39
malePuLim[2] = new limitArray(30, 24, 18, 11, "")//40-49
malePuLim[3] = new limitArray(25, 19, 13, 9, "")//50-59
malePuLim[4] = new limitArray(23, 18, 10, 6, "")//60 and greater


function flexLimArray(f0, f1, f2, f3, f4, f5, f6, f7, f8, f9) {
	this.f0=f0
	this.f1=f1
	this.f2=f2
	this.f3=f3
	this.f4=f4
	this.f5=f5
	this.f6=f6
	this.f7=f7
	this.f8=f8
	this.f9=f9
}
femaleFlexLim = new Array (3)
femaleFlexLim[0] = new flexLimArray(18.7, 17.9, 16.7, 16.2, 15.8, 14.8, 14.5, 13.7, 12.6, 10.1) // <36
femaleFlexLim[1] = new flexLimArray(19.2, 17.7, 16.2, 15.2, 14.5, 13.5, 12.8, 12.2, 11.0, 9.7) // 49-36
femaleFlexLim[2] = new flexLimArray(15.7, 15.0, 14.2, 13.6, 12.3, 11.1, 10.1, 9.2, 8.3, 7.5) // 50 and up

maleFlexLim = new Array (3)
maleFlexLim[0] = new flexLimArray(19.5, 17.9, 17.0, 15.8, 15.0, 14.4, 13.5, 13.0, 11.6, 9.2) // <36
maleFlexLim[1] = new flexLimArray(18.2, 16.1, 14.6, 13.9, 13.4, 12.6, 11.6, 10.8, 9.9, 8.3) // 49 - 36
maleFlexLim[2] = new flexLimArray(15.8, 15.0, 13.3, 12.3, 11.5, 10.2, 9.7, 9.3, 8.8, 7.8) // <50 and up
		
cmToIn = 0.3937
inToCm = 2.54
kgToLb = 2.24

var compFit = "-"
var cardioIndex = "-"
var flexIndex = "-"
var strengthIndex = "-"
var cardioAge = "-"
var flexAge = "-"
var strengthAge = "-"

function fatCalc(){
	isMale =(document.selectForm.gender[0].checked);
	//isCm = document.selectForm.heightUnit[1].checked
	//isKg = document.selectForm.WtUnit[1].checked
	isCm = false;
	isKg = false;
	weight = parseFloat(document.selectForm.weight.value);
	if (isKg) {
		mult = kgToLb;
		massUnit= " Kgs.";
	} else {
		mult = 1;
		massUnit = " lbs.";
	}
	lower = 75 / mult;
	upper = 500 / mult;
	isGood = validateNum(weight,lower,upper,massUnit,"Weight.");
	if (!isGood) {
		document.selectForm.weight.focus();
		return false;
	}
	weight= weight * mult;
	// calculate various lengths
	if (isCm) {
		mult = cmToIn;
		mult2 = 1;
		distUnit = " cm.";
	} else {
		mult = 1;
		mult2 = inToCm;
		distUnit = " in.";
	}
	
	height = eval(eval(document.selectForm.heightFeet.value) * 12) + eval(document.selectForm.heightInches.value);

	lower = 44 / mult;
	upper = 84 / mult;

	abd1 = parseFloat(document.selectForm.abd1.value);
	lower = 20 / mult;
	upper = 52/ mult;

	abd2 = parseFloat(document.selectForm.abd2.value);
	lower = 20 / mult;
	upper = 52 / mult;

	neck = parseFloat(document.selectForm.neck.value);
	lower = 10 / mult;
	upper = 22 / mult;

	heightIn = height * mult;
	heightCm = height * mult2;
	abd1In = abd1 * mult;
	abd1Cm = abd1 * mult2;
	abd2In = abd2 * mult;
	abd2Cm = abd2 * mult2;
	neckIn = neck * mult;
	neckCm = neck * mult2;
	
	if (isMale){
		fat1 = 495/(1.0324-.19077*((Math.log(abd2Cm - neckCm))/Math.LN10) +0.15456 * ((Math.log(heightCm))/Math.LN10)) - 450;
		fat2 = 100 * (-98.42 + 4.15*abd1In - 0.082 * weight)/weight;
		avgFat = (fat1 + fat2)/2;
		if ( avgFat < 6) {
			compFit = 40;
		} else if (avgFat <16)  {
			compFit = 90;
		} else if (avgFat < 25) {
			compFit = 60;
		} else {
			compFit = 40;
		}
	
	} else {
		fat1 = 495/(1.29579-.35004*((Math.log(abd1Cm + abd2Cm - neckCm))/Math.LN10) +0.221 * ((Math.log(heightCm))/Math.LN10)) - 450;
		fat2 = 100*(-76.76 + 4.15*abd1In - 0.082 * weight)/weight;
		avgFat = (fat1 + fat2)/2
		if ( avgFat < 9) {
			compFit = 40;
		} else if (avgFat <24)  {
			compFit = 90;
		} else if (avgFat < 32) {
			compFit = 60;
		} else {
			compFit = 40;
		}

	}
	BMI = (weight * 703) / (heightIn * heightIn);
	if (eval(avgFat)<5) {
		alert("Your entered measurements appear to not be correct. Please try again.");
		return;
	}
	document.selectForm.fatOut.value = Math.round(avgFat);
}

function calcFlex() {
	maleM = -7.09
	femaleM = -7.88
	maleB = 130
	femaleB = 145
	isMale = document.selectForm.gender[0].checked
	flexDist = parseFloat(document.selectForm.toeDist.value)
	isCm = false
	if (isCm) {
		mult =  cmToIn
		distUnit = " cm."
	} else {
		mult = 1
		distUnit = " in."
	}
	lower = 0
	upper = 20 / mult
	isGood = validateNum(flexDist,lower,upper,distUnit,"your stretch distance." )
	if (!isGood){
		return
	}
	flexDist = flexDist * mult
	age = document.selectForm.age.value;
	if (age< 36) {
		ageIndex = 0
	} else if (age < 50) {
		ageIndex = 1
	} else {
		ageIndex = 2
	}
		
	x = (isMale) ? maleFlexLim : femaleFlexLim
	if (flexDist > x[ageIndex].f0) {
		flexIndex = 95 
	} else if (flexDist > x[ageIndex].f1) {
		flexIndex = 90
	} else if (flexDist > x[ageIndex].f2) {
		flexIndex = 80
	} else if (flexDist > x[ageIndex].f3) {
		flexIndex =  70
	} else if (flexDist > x[ageIndex].f4) {
		flexIndex = 60
	} else if (flexDist > x[ageIndex].f5) {
		flexIndex = 50
	} else if (flexDist > x[ageIndex].f6) {
		flexIndex = 40
	} else if (flexDist > x[ageIndex].f7) {
		flexIndex = 30
	} else if (flexDist > x[ageIndex].f8) {
		flexIndex = 20
	} else {
		flexIndex = 10
	}
	document.selectForm.flexOut.value	= flexIndex
	//calculate flexibility age
	if (isMale) {
		flexAge = Math.round(flexDist * maleM + maleB)
	} else {
		flexAge = Math.round(flexDist * femaleM + femaleB)
	}
	flexAge = (flexAge<20) ? 22 : flexAge 
	document.selectForm.flexiAge.value = flexAge
}
function calcCardio(){
	maleM = -3.78
	maleB = 190
	femaleM = -4.06
	femaleB = 172
	isMale = document.selectForm.gender[0].checked
	rate=parseInt(document.selectForm.postRate.value)
	isGood = validateNum(rate, 15, 50," Beats", "your 15 second count." )
	if (!isGood){
		return
	}
	
	if (isMale) { 
		VMax = 111.33-(1.68*rate)
	} else {
		VMax = 65.81-(.7388*rate)
	}
	//alert("VO2 = " + VMax)
	age = document.selectForm.age.value;
	ageIndex=Math.floor(age/10) -2
	x = (isMale) ? maleLim : femaleLim
	if (VMax < x[ageIndex].l0) {
		index = 25
	} else if (VMax < x[ageIndex].l1){
		index = 50
	}	else if (VMax < x[ageIndex].l2){
		index = 75
	}	else if (VMax < x[ageIndex].l3){
		index = 85
	}	else {
		index = 95
	}	
	cardioIndex = index
	document.selectForm.cardioOut.value = cardioIndex
	//calculate cardio age
	if (isMale) {
		cardioAge = Math.round(VMax * maleM + maleB)
	} else {
		cardioAge = Math.round(VMax * femaleM + femaleB)
	}
	cardioAge = (cardioAge<20) ? 22 : cardioAge 
	document.selectForm.cardioAge.value = cardioAge
}
function calcPushup(){
	maleM = -1.99
	maleB = 90
	femaleM = -2.25
	femaleB = 85
	isMale = document.selectForm.gender[0].checked
	pushup=parseInt(document.selectForm.pushUps.value)
	isGood = validateNum(pushup, 0, 99,"", "your push-up total." )
	if (!isGood){
		return
	}
	
	age = document.selectForm.age.value;
	ageIndex=Math.floor(age/10) -2
	x = (isMale) ? malePuLim : femalePuLim
	if (pushup > x[ageIndex].l0) {
		index = 95
	} else if (pushup > x[ageIndex].l1){
		index = 85
	}	else if (pushup > x[ageIndex].l2){
		index = 75
	}	else if (pushup > x[ageIndex].l3){
		index = 50
	}	else {
		index = 20
	}
	strengthIndex = index
	document.selectForm.strengthIndexOut.value = strengthIndex 		
	//calculate strength age
	if (isMale) {
		strengthAge = Math.round(pushup * maleM + maleB)
	} else {
		strengthAge = Math.round(pushup * femaleM + femaleB)
	}
	strengthAge = (strengthAge<20) ? 22 : strengthAge 
	document.selectForm.strengthAge.value = strengthAge
}

function calcOverall() {
	indexTotal = 0
	if (isNaN(strengthIndex)||isNaN(cardioIndex)||isNaN(flexIndex)||isNaN(compFit)) {
		alert("All tests must be completed before an overall fitness level can be determined")
	} else {

		indexTotal = (strengthIndex + cardioIndex + flexIndex + compFit)/4
		document.selectForm.overallIndexOut.value = indexTotal
		ageTotal = (strengthAge + cardioAge + flexAge)/3
		document.selectForm.fitnessAge.value = ageTotal
	}
}


function validateNum(num,lowerLim, upperLim,unit,label){
	if(isNaN(num)){
		alert("Please enter a value between "+ Math.round(lowerLim) + " and " + Math.round(upperLim) + unit + " for " + label)
		return false
	} else if ((num<lowerLim)||(num>upperLim)){
		alert("Please enter a value between "+ Math.round(lowerLim) + " and " + Math.round(upperLim) + unit + " for " + label)
		return false
	} else {
		return true
	}
}
function writeCookie(form,cookieName)
{
	var cookieString = cookieName+"="
	var firstMatch = true
	formLen = form.elements.length
		/*
	Assemble the text string to be saved by adding the text value of each 
	checked item to the string
	*/
	for (i=0;i<formLen;i++)
		{
		startChar = (firstMatch) ? "" : ":"
		if(form.elements[i].type=="radio"){
			cookieString += startChar + "r," + i + "," +  form.elements[i].checked // buttons loaded
			firstMatch = false
		} else if (form.elements[i].type=="text"){
			cookieString +=  startChar + "t," + i + "," + form.elements[i].value// text items
			firstMatch = false
		}
	}//end for
	//alert(cookieString)//for debug purposes only
	//assemble entire cookie
	var timeX = new Date()
	var expTime = timeX.getTime() + (180*24*60*60*1000)//expire in 180 days
	timeX.setTime(expTime)
	document.cookie = cookieString + "; expires=" + timeX.toGMTString()
}

var profileList = ""
function populateProfile()
{
	//get profile list
	cookieList = getCookie("profile")
	if(!cookieList){
		alert("No Profile currently stored")
		return // no cookie stored for "profile"
	}
	// alert(cookieList)
	formSetter(cookieList, ":", ",")
}

function getCookie(label)
{
	label += "="
	var labelLen = label.length
	var cookieLen = document.cookie.length
	var cEnd ;//end of the cookie
	var i = 0
	//alert("label = "+ label)//for debugging
	while (i < cookieLen)
	{
		var j = i + labelLen
		if (document.cookie.substring(i, j)==label) //cookie was found
		{ 
			cEnd = document.cookie.indexOf(";", j)
			if (cEnd == -1)
			{ 
				cEnd = document.cookie.length
			} //end if that determines position of named cookie in total string
			return document.cookie.substring(j, cEnd)
		}//end if cookie was found
		i++
	} //end while, therefore cookie wasn't found
	return ""
}
function formSetter(cookieList,delim1,delim2){
var listArray = cookieList.split(delim1)
	listCount = listArray.length
	for (i = 0; i < listCount ; i++){ //splits cookie entries
		elemArray = listArray[i].split(delim2)// 0 is type, 1 is index, 2 is "value"
		j= elemArray[1]
		if (elemArray[0] == "r") { //radio
		
			document.selectForm.elements[j].checked = eval(elemArray[2])
		} else {
			document.selectForm.elements[j].value = elemArray[2]
		}
	}//end for 
}
function ClearEntries() {
	document.selectForm.age.value = "25";
	document.selectForm.weight.value = "150";
	document.selectForm.heightFeet.value = "5";
	document.selectForm.heightInches.value = "7";
	document.selectForm.toeDist.value = "";
	document.selectForm.flexOut.value = "";
	document.selectForm.flexiAge.value = "";
	document.selectForm.postRate.value = "";
	document.selectForm.cardioOut.value = "";
	document.selectForm.cardioAge.value = "";
	document.selectForm.abd1.value = "34";
	document.selectForm.abd2.value = "34";
	document.selectForm.neck.value = "16";
	document.selectForm.fatOut.value = "";
	document.selectForm.pushUps.value = "";
	document.selectForm.strengthIndexOut.value = "";
	document.selectForm.strengthAge.value = "";
	document.selectForm.overallIndexOut.value = "";
	document.selectForm.fitnessAge.value = "";
}
function CheckNumeric2(myfield, fieldName, min, max) {
	for (i=0; i<myfield.value.length; i++) {
		if ( ((myfield.value.charAt(i) < '0') || (myfield.value.charAt(i) > '9')) && myfield.value.charAt(i) !=	'.') {
			alert (fieldName + " must be numeric.");
      myfield.value="0";
			myfield.focus();
			return false;
		}
	}
	if (myfield.value.length==0) {
		myfield.value="0";
	}
	if (myfield.value > max) {
		alert("Please enter a value less than " + eval(max+1) + " for " + fieldName + ".");
		myfield.value = max;
		myfield.focus();
		return false;
	}
	if (myfield.value < min) {
		alert("Please enter a value greater than " + eval(min-1) + " for " + fieldName + ".");
		myfield.value = min;
		myfield.focus();
		return false;
	}
	return true;
}

