//	Javascript to implement The Zone Body Composition Calculator - Male Version
//  Tables and Methodology are taken from the book The Zone by Barry Sears, Ph.D.
//	Daniel R. Kibler October 20, 1997, 6:18 PM
//


/////////////////////////////////////////////////////////
// setup fat table
/////////////////////////////////////////////////////////
// makeRow() function:
// select row based on weight
/////////////////////////////////////////////////////////
function makeRow(row) {
	if (row == 120)
		return new Array(4,6,8,10,12,14,16,18,20,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,50,52,54)
	if (row == 125)
		return new Array(4,6,7,9,11,13,15,17,19,20,22,24,26,28,30,32,33,35,37,39,41,43,45,46,48,50,52,54)
	if (row == 130)
		return new Array(3,5,7,9,11,12,14,16,18,20,21,23,25,27,28,30,32,34,36,37,39,41,43,44,46,48,50,52,53,55)
	if (row == 135)
		return new Array(3,5,7,8,10,12,13,15,17,19,20,22,24,26,27,29,31,32,34,36,38,39,41,43,44,46,48,50,51,53,55)
	if (row == 140)
		return new Array(3,5,6,8,10,11,13,15,16,18,19,21,23,24,26,28,29,31,33,34,36,38,39,41,43,44,46,48,49,51,53,54)
	if (row == 145)
		return new Array(null,4,6,7,9,11,12,14,15,17,19,20,22,23,25,27,28,30,31,33,35,36,38,39,41,43,44,46,47,49,51,52,54,55)
	if (row == 150)
		return new Array(null,4,6,7,9,10,12,13,15,16,18,19,21,23,24,26,27,29,30,32,33,35,36,38,40,41,43,44,46,47,49,50,52,53,55)
	if (row == 155)
		return new Array(null,4,5,6,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35,37,38,40,41,43,44,46,47,49,50,52,53,55)
	if (row == 160)
		return new Array(null,4,5,6,8,9,11,12,14,15,17,18,19,21,22,24,25,27,28,30,31,33,34,35,37,38,40,41,43,44,46,47,48,50,51,53,54)
	if (row == 165)
		return new Array(null,3,5,6,8,9,10,12,13,15,16,17,19,20,22,23,24,26,27,29,30,31,33,34,36,37,38,40,41,43,44,45,47,48,50,51,52,54,55)
	if (row == 170)
		return new Array(null,3,4,6,7,9,10,11,13,14,15,17,18,19,21,22,24,25,26,28,29,30,32,33,34,36,37,39,40,41,43,44,45,47,48,49,51,52,54,55)
	if (row == 175)
		return new Array(null,null,4,6,7,8,10,11,12,12,15,16,17,19,20,21,23,24,25,27,28,29,31,32,33,35,36,37,39,40,41,43,44,45,47,48,49,51,52,53,55)
	if (row == 180)
		return new Array(null,null,4,5,7,8,9,10,12,13,14,16,17,18,19,21,22,23,25,26,27,28,30,31,32,34,35,36,37,39,40,41,43,44,45,47,48,49,50,52,53,54)
	if (row == 185)
		return new Array(null,null,4,5,6,8,9,10,11,13,14,15,16,18,19,20,21,23,24,25,26,28,29,30,31,33,34,35,36,38,39,40,41,43,44,45,46,48,49,50,51,53,54,55)
	if (row == 190)
		return new Array(null,null,4,5,6,7,8,10,11,12,13,15,16,17,18,19,21,22,23,24,26,27,28,29,30,32,33,34,35,37,38,39,40,41,43,44,45,46,48,49,50,51,52,54,55)
	if (row == 195)
		return new Array(null,null,3,5,6,7,8,9,11,12,13,14,15,16,18,19,20,21,22,24,25,26,27,28,30,31,32,33,34,35,37,38,39,40,41,43,44,45,46,47,49,50,51,52,53,55)
	if (row == 200)
		return new Array(null,null,3,4,6,7,8,9,10,11,12,14,15,16,17,18,19,21,22,23,24,25,26,28,29,30,31,32,33,35,36,37,38,39,40,41,43,44,45,46,47,48,50,51,52,53,54,55)
	if (row == 205)
		return new Array(null,null,null,4,5,6,8,9,10,11,12,13,14,15,17,18,19,20,21,22,23,25,26,27,28,29,30,31,32,34,35,36,37,38,39,40,41,43,44,45,46,47,48,49,51,52,53,54,55)
	if (row == 210)
		return new Array(null,null,null,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,28,29,30,32,33,34,35,36,37,38,39,40,42,43,44,45,46,47,48,49,50,51,53,54,55)
	if (row == 215)
		return new Array(null,null,null,4,5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21,22,23,24,25,26,28,29,30,31,32,33,34,35,36,37,38,39,40,42,43,44,45,46,47,48,49,50,51,52,53,54,55)
	if (row == 220)
		return new Array(null,null,null,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55)
	if (row == 225)
		return new Array(null,null,null,3,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55)
	if (row == 230)
		return new Array(null,null,null,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,30,31,32,33,34,35,36,37,38,39,40,41,42,44,44,45,46,47,48,49,50,51,52,53,54,55)
	if (row == 235)
		return new Array(null,null,null,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,51,52,53,54,55)
	if (row == 240)
		return new Array(null,null,null,null,4,5,6,7,8,9,10,11,12,13,14,15,16,17,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,46,47,48,49,50,51,52,53,54)
	if (row == 245)
		return new Array(null,null,null,null,4,5,6,7,8,9,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,44,45,46,47,48,49,50,51,52,53)
	if (row == 250)
		return new Array(null,null,null,null,4,5,6,6,7,8,9,10,11,12,13,14,15,16,17,18,18,19,20,21,22,23,24,25,26,27,28,29,30,31,31,32,33,34,35,36,37,38,39,40,41,42,43,44,44,45,46,47,48,49,50,51,52)
	if (row == 255)
		return new Array(null,null,null,null,3,4,5,6,7,8,9,10,11,12,13,14,14,15,16,17,18,19,20,21,22,23,24,24,25,26,27,28,29,30,31,32,33,34,34,35,36,37,38,39,40,41,42,43,44,44,45,46,47,48,49,50,51)
	if (row == 260)
		return new Array(null,null,null,null,3,4,5,6,7,8,9,10,10,11,12,13,14,15,16,17,18,19,19,20,21,22,23,24,25,26,27,27,28,29,30,31,32,33,34,35,35,36,37,38,39,40,41,42,43,43,44,45,46,47,48,49,50)
	if (row == 265)
		return new Array(null,null,null,null,null,4,5,6,7,8,8,9,10,11,12,13,14,15,15,16,17,18,19,20,21,22,22,23,24,25,26,27,28,29,29,30,31,32,33,34,35,36,36,37,38,39,40,41,42,43,43,44,45,46,47,48,49)
	if (row == 270)
		return new Array(null,null,null,null,null,4,5,6,7,7,8,9,10,11,12,13,13,14,15,16,17,18,19,19,20,21,22,23,24,25,25,26,27,28,29,30,31,31,32,33,34,35,36,37,37,38,39,40,41,42,43,43,44,45,46,47,48)
	if (row == 275)
		return new Array(null,null,null,null,null,4,5,5,6,7,8,9,10,11,11,12,13,14,15,16,16,17,18,19,20,21,22,22,23,24,25,26,27,27,28,29,30,31,32,32,33,34,35,36,37,38,38,39,40,41,42,43,43,44,45,46,47)
	if (row == 280)
		return new Array(null,null,null,null,null,4,4,5,6,7,8,9,9,10,11,12,13,14,14,15,16,17,18,19,19,20,21,22,23,24,24,25,26,27,28,29,29,30,31,32,33,33,34,35,36,37,38,38,39,40,41,42,43,43,44,45,46)
	if (row == 285)
		return new Array(null,null,null,null,null,4,4,5,6,7,8,8,9,10,11,12,12,13,14,15,16,17,17,18,19,20,21,21,22,23,24,25,26,26,27,28,29,30,30,31,32,33,34,34,35,36,37,38,39,39,40,41,42,43,43,44,45)
	if (row == 290)
		return new Array(null,null,null,null,null,3,4,5,6,7,7,8,9,10,11,11,12,13,14,15,15,16,17,18,19,19,20,21,22,23,23,24,25,26,27,27,28,29,30,31,31,32,33,34,35,35,36,37,38,39,39,40,41,42,43,43,44)
	if (row == 295)
		return new Array(null,null,null,null,null,3,4,5,6,6,7,8,9,10,10,11,12,13,14,14,15,16,17,17,18,19,20,21,21,22,23,24,25,25,26,27,28,28,29,30,31,32,32,33,34,35,36,36,37,38,39,39,40,41,42,43,43)
	if (row == 300)
		return new Array(null,null,null,null,null,3,4,5,5,6,7,8,9,9,10,11,12,12,13,14,15,16,16,17,18,19,19,20,21,22,22,23,24,25,26,26,27,28,29,29,30,31,32,33,33,34,35,36,36,37,38,39,39,40,41,42,43)
}


///////////////////////////////////////////////////////////
// table() function:
// create row and select column
///////////////////////////////////////////////////////////
function table(row,column) {
	theRow = makeRow(row)
	column = (column - 22) * 2
	return theRow[column]
}


///////////////////////////////////////////////////////////
// validate() function:
// validates float and range of the passed arguments
///////////////////////////////////////////////////////////
function validate(obj,min,max) {
  //convert to float if necessary
  var theValue = parseFloat(obj.value)
  if ((!theValue) ||  ( theValue < min) || ( theValue > max)){
      alert("Please enter a value between " + min + " and " + max + " for " + obj.name + ".") 
      obj.focus()
      obj.select()
	}
	else obj.value = theValue 
}


///////////////////////////////////////////////////////////
// calcTotal() function:
// Calculates the totals
///////////////////////////////////////////////////////////
function calcTotal() {
	var wwMin = 22
	var wwMax = 50
	var activityFactorIndex = 1

	if (!validateForm())
		return

	var ww = document.calcForm.waist.value - document.calcForm.wrist.value
	if ( (ww < wwMin) || ( ww > wwMax)){
		alert("Waist - Wrist is out of range (" + wwMin + " -> " + wwMax + ").")
		return
	}
	var row = Math.round(document.calcForm.totalWeight.value / 5) * 5
	var column = Math.round( (document.calcForm.waist.value - document.calcForm.wrist.value) * 2) / 2
	
	document.calcForm.percentFat.value = table(row,column);
	
	document.calcForm.fat.value =
    	Math.round(document.calcForm.totalWeight.value * document.calcForm.percentFat.value / 100);
	
	document.calcForm.lean.value =
    	(document.calcForm.totalWeight.value - document.calcForm.fat.value);
    
    activityFactorIndex = document.calcForm.activityFactor.selectedIndex;
	
	document.calcForm.protein.value =
	    (document.calcForm.lean.value * document.calcForm.activityFactor.options[activityFactorIndex].value);
	
	document.calcForm.blocks.value =
	    (Math.round (document.calcForm.protein.value / 7));
}


//////////////////////////////////////////////////////////////////////
// validateForm() function:
// Validates input of entire form. 
//////////////////////////////////////////////////////////////////////
function validateForm() {
  var fixThis = "";

  if (!(exists(document.calcForm.waist.value))) {
    fixThis += "Please enter a numeric value for the waist field.\n";
  }

  if (!(exists(document.calcForm.wrist.value))) {
    fixThis += "Please enter a numeric value for the wrist field.\n";
  }

  if (!(exists(document.calcForm.totalWeight.value))) {
    fixThis += "Please enter a numeric value for the weight field.\n";
  }
  
  if (fixThis != "") {
    alert(fixThis);
    return 0
  } else {
    return 1;
  }
} 


//////////////////////////////////////////////////////////////////////
// exists() function:
// Validates there has been a entry. 
//////////////////////////////////////////////////////////////////////
function exists(s) {
	if (s == "")
		return 0
	else
		return 1
}

//////////////////////////////////////////////////////////////////////
// saveValues() function:
// Saves user entered values to a cookie named "zoneCalcMale". 
//////////////////////////////////////////////////////////////////////
function saveValues() {
	with( document.calcForm ) {
		myCookieValue = waist.value + ":" + wrist.value + ":" + totalWeight.value + ":" + activityFactor.selectedIndex
	}
	today = new Date()
	var theYear = today.getYear() < 100 ? today.getYear() + 1900 : today.getYear()
	myCookieDate = new Date( theYear + 5,today.getMonth(),today.getDate())
	//alert( myCookieDate)
	SetCookie ("zoneCalcMale", myCookieValue, myCookieDate, "/")
}

//////////////////////////////////////////////////////////////////////
// getValues() function:
// Retrieves user entered values from a cookie named "zoneCalcMale". 
//////////////////////////////////////////////////////////////////////
function getValues() {
	var savedValues = GetCookie("zoneCalcMale")
	if ( savedValues != null) {
		var a = savedValues.split (":")
		with( document.calcForm ) { 
			waist.value = a[0]
			wrist.value = a[1]
			totalWeight.value = a[2]
			activityFactor.selectedIndex = a[3]
		}
	}
}

//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {  document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
} 


//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
}

//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
// 
function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) { 
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
} 

