//	Javascript to implement The Zone Body Composition Calculator - Female 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 Constant tables
/////////////////////////////////////////////////////////
hipsTable = new table(30,new Array(33.48,33.83,34.87,35.22,36.27,36.62,37.67,38.02,39.06,39.41,40.46,40.81,41.86,42.21,43.25,43.60,44.65,45.32,46.05,46.40,47.44,47.79,48.84,49.19,50.24,50.59,51.64,51.99,53.03,53.41,54.53,54.86,55.83,56.18,57.22,57.57,58.62,58.97,60.02,60.37,61.42,61.77,62.81,63.16,64.21,64.56,65.61,65.96,67.00,67.35,68.40,68.75,69.80,70.15,71.19,71.54,72.59,72.94,73.99,74.34,75.39));
waistTable = new table(20,new Array(14.22,14.40,14.93,15.11,15.64,15.82,16.35,16.53,17.06,17.24,17.78,17.96,18.49,18.67,19.20,19.38,19.91,20.27,20.62,20.80,21.33,21.51,22.04,22.22,22.75,22.93,23.46,23.64,24.18,24.36,24.89,25.07,25.60,25.78,26.31,26.49,27.02,27.20,27.73,27.91,28.44,28.62,29.15,29.33,29.87,30.05,30.58,30.76,31.29,31.47,32.00,32.18,32.71,32.89,33.42,33.60,34.13,34.31,34.84,35.02,35.56));
heightTable = new table(55,new Array(33.52,33.67,34.13,34.28,34.74,34.89,35.35,35.50,35.96,36.11,36.57,36.72,37.18,37.33,37.79,37.94,38.40,38.70,39.01,39.16,39.62,39.77,40.23,40.38,40.84,40.99,41.45,41.60,42.06,42.21,42.67,42.82,43.28,43.43,43.89,44.04,44.50,44.65,45.11,45.26,45.72,45.87,46.32));


///////////////////////////////////////////////////////////
// maketable() function:
// makes a table object
///////////////////////////////////////////////////////////
function table(base,arr) {
	this.base = base; //first argument is base
	this.col = arr;
	this.element = tableElement;
}


///////////////////////////////////////////////////////////
// tableElement() function :
// select column for table object
///////////////////////////////////////////////////////////
function tableElement(c) {
	c = Math.round(c);
	var column = (c - this.base) * 2;
	return this.col[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 activityFactorIndex = 1;

	if (!validateForm())
		return;

	var A = hipsTable.element(document.calcForm.hips.value);
	var B = waistTable.element(document.calcForm.waist.value);
	var C = heightTable.element(document.calcForm.height.value);
	
	document.calcForm.percentFat.value = Math.round(A + B - C);
	document.calcForm.fat.value =
    	Math.round(document.calcForm.totalWeight.value * document.calcForm.percentFat.value / 100);

	document.calcForm.lean.value =
    	Math.round(document.calcForm.totalWeight.value - document.calcForm.fat.value);

    activityFactorIndex = document.calcForm.activityFactor.selectedIndex;
	document.calcForm.protein.value =
    	Math.round(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.hips.value))) {
    fixThis += "Please enter a numeric value for the hips field.\n";
  }

  if (!(exists(document.calcForm.height.value))) {
    fixThis += "Please enter a numeric value for the height 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 "zoneCalcFemale". 
//////////////////////////////////////////////////////////////////////
function saveValues() {
	with( document.calcForm ) {
		myCookieValue = hips.value + ":" + waist.value + ":" + height.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());
	SetCookie ("zoneCalcFemale", myCookieValue, myCookieDate, "/");
}

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

//
//  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;
} 
