var gUnits = 0;

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function numbersOnly(obj)
{
	var regex = /[^0-9\.]+/g;// /(\D|\.)+/g;
	var itemVal = obj.value;
	itemVal = itemVal.replace(regex, "");
	obj.value = itemVal;
}
function setSelectionOptionByValue(obj, val)
{
	//alert ("got here" + val);
	for (var i=0; i<obj.length; i++ ) {
		if ( obj.options[i].value == val ) {
			obj.options[i].selected = true;
			return;
		}
	}
}

function setRadioByValue(obj, val)
{
	var len = obj.length;
	for (var i=0; i<len; i++ ) {
		obj[i].checked = (obj[i].value == val );
	}
}

function radioGroupValue(radioGrp)
{
	
	for (var i = 0; i< radioGrp.length; i++) {
    	if (radioGrp[i].checked) {
      	  return radioGrp[i].value;
    	}
	} 
	return null;
}

function formattedHeight(hgtIn)
{
	var inches = hgtIn % 12;
	var ft = (hgtIn - inches) / 12;
	var str = "" + ft + "'" + inches + "\"";
	return str;
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
		sString = sString.substring(1, sString.length);

	while (sString.substring(sString.length-1, sString.length) == ' ')
		sString = sString.substring(0,sString.length-1);

	return sString;
}

function getDate() 
{
var dat = new Date();
var date = dat.getDate(); 
var year = dat.getYear();
	year = year + "";  // Convert to string.
	year = year.substring(year.length - 2, year.length)
var month = dat.getMonth();
month = month + 1;
var current_date = month + "/" + date + "/" + year;
document.data.todaydate.value = current_date;
}

function getCookie(name) {
   name = cookiePrefix + name;
   var result = null; 
   var myCookie = " " + document.cookie + ";";
   var searchName = " " + name + "=";
   var startOfCookie = myCookie.indexOf(searchName); 
   var endOfCookie; 
   if (startOfCookie != -1) {
      startOfCookie += searchName.length; 
      endOfCookie = myCookie.indexOf(";", startOfCookie); 
      result = unescape(myCookie.substring(startOfCookie,
      endOfCookie)); 
   }
   return result; 
}

function setCookie(name, value, lifeSpan) {
   name = cookiePrefix + name;
   var dat = new Date();
   dat.setTime(dat.getTime() + lifeSpan*24*60*60*1000); 
   var expString = "; expires=" + dat.toGMTString();
   document.cookie = name + "=" + escape(value) + expString; 
}



function setUnits(units)
{
	var usDiv = MM_findObj('us');
	var us_wgt = MM_findObj('us_wgt');
	var british_wgt = MM_findObj('british_wgt');
	var metricDiv = MM_findObj('metric');
	if ( units == 0 ) {
		usDiv.style.display = 'block';
		us_wgt.style.display = 'inline';
		british_wgt.style.display = 'none';
		metricDiv.style.display = 'none';
		
	}
	else if ( units == 1 )  {
		usDiv.style.display = 'block';
		us_wgt.style.display = 'none';
		british_wgt.style.display = 'inline';
		metricDiv.style.display = 'none';
	}
	else if ( units == 2 )  {
		usDiv.style.display = 'none';
		us_wgt.style.display = 'none';
		british_wgt.style.display = 'none';
		metricDiv.style.display = 'block';
	}
	gUnits = units;
	setCookie("units", units, 1000);
}

//Get stored items
function setUp() 
{
	var units = getCookie("units");
	if ( units != null )
		gUnits = units;
	setUnits(gUnits);
	var measureRadioGroup = MM_findObj('measure');
	setRadioByValue( measureRadioGroup, gUnits);
		
}

function getUSHeight()
{
	if ( gUnits == 2 ) 
		return;
	
	var hgtIn = 0;
	var haveFeet = trimAll(document.data.feet.value) == "" ? false : true;
	var haveInches = trimAll(document.data.inches.value) == "" ? false : true;
	
	if ( haveFeet )
		hgtIn = (parseInt(document.data.feet.value) * 12);
	if ( haveInches )
		hgtIn += parseFloat(document.data.inches.value);
	return hgtIn;
}


function getMetricHeight()
{
	// returns Centimeters
	
	if (  gUnits != 2 ) 
		return;
	
	var hgtCm = 0;

	hgtCm = parseFloat(document.data.cm.value);
	return hgtCm;
}



function getUSWeight()
{
	if (  gUnits != 0 ) 
		return 0;
	
	var wgt = 0;
	wgt = parseFloat(document.data.us_lb.value);
	
	return wgt;	
}
function getBritishWeight()
{
	if (  gUnits != 1 ) 
		return 0;
	
	var wgt = 0;
	
	wgt = parseInt(document.data.british_stone.value) * 14;
	wgt += parseFloat(document.data.british_lb.value);
	
	return wgt;	
}
function getMetricWeight()
{
	if (  gUnits != 2 ) 
		return 0;
	
	var wgt = 0;

	wgt = parseFloat(document.data.kg.value);
	return wgt;	
}


function Validate() {	

// Required fields:
//	sex = male | female
//	birth_year
//	birth_month
//	birth_date
// feet
//	inches
//	pounds
//meters
// kilograms

	document.data.pounds.value = "";
	document.data.meters.value = "";
	document.data.kilograms.value = "";
	
	
	if(document.data.birth_month.value == "e") {
		window.alert("Please fill in your birth month. Thanks.");
		return false;
	}
	if(document.data.birth_date.value == "e") {
		window.alert("Please fill in your birth day. Thanks.");
		return false;
	}
	if(document.data.birth_year.value == "e") {
		window.alert("Please fill in your birth year. Thanks.");
		return false;
	}


	var sex = radioGroupValue(document.data.sex);
	if ( ! ((sex == "male") || (sex == "female")) ) {
		window.alert("Please select your sex. Thanks.");
		return false;
	}
	

	if ( gUnits == 2 ) { // Metric
		if( trimAll(document.data.cm.value) == "") {
			window.alert("Please fill in your height. Thanks.");
			return false;
		}
		if(trimAll(document.data.kg.value) == "") {
			window.alert("Please fill in your weight. Thanks.");
			return false;
		}
		var mWgt = getMetricWeight();
		if (( mWgt < 9 ) || ( mWgt > 317 )) {
			window.alert("Please correct your weight. Thanks.");
			return false;
		}
		document.data.pounds.value = (mWgt * 2.2);//Math.round(mWgt * 2.2);		
		document.data.kilograms.value = mWgt;//Math.round(mWgt);		
		
		
		var mHgt = getMetricHeight();
		if ( ( mHgt < 61 ) || ( mHgt > 213 ) ) {
			window.alert("Please correct your height. Thanks.");
			return false;
		}
		document.data.meters.value = mHgt / 100;
		
		//document.data.height.value = formattedHeight( Math.round(mHgt / 2.54) );
		//document.data.height_in.value = Math.round(mHgt / 2.54);
		
	}
	else {
		var haveFeet = trimAll(document.data.feet.value) == "" ? false : true;
		var haveInches = trimAll(document.data.inches.value) == "" ? false : true;
		
		if (( false == haveFeet) && ( false == haveInches)) {
			window.alert("Please fill in your height. Thanks.");
			return false;
		}
		if ( haveInches && (false == haveFeet) ) {
			if(trimAll(document.data.inches.value) < 12) {
				window.alert("Please fill in your height (feet). Thanks.");
				return false;
			}
		}
		
		
		if ( gUnits == 0 ) {
			if(trimAll(document.data.us_lb.value) == "") {
				window.alert("Please fill in your weight. Thanks.");
				return false;
			}
			var usWgt = getUSWeight();
			if (( usWgt < 20 ) || ( usWgt > 700 ) ) {
				window.alert("Please correct your weight. Thanks.");
				return false;
			}
			document.data.pounds.value = usWgt;			
		}
		else {
		// british weight
			
			if(trimAll(document.data.british_stone.value) == "") {
				window.alert("Please fill in your weight. Thanks.");
				return false;
			}
			if(trimAll(document.data.british_lb.value) == "") {
				window.alert("Please fill in your weight. Thanks.");
				return false;
			}
			var britWgt = getBritishWeight();
			
			if (( britWgt < 20 ) || ( britWgt > 700 ) ) {
				window.alert("Please correct your weight. Thanks.");
				return false;
			}
			
			document.data.pounds.value = britWgt;
		}
		var usHgt = getUSHeight();
		if (( usHgt < 24 ) || ( usHgt > 84 ) ) {
			window.alert("Please correct your height. Thanks.");
			return false;
		}
	//	document.data.height.value = formattedHeight(usHgt);
	//	document.data.height_in.value = usHgt;
	}
	
	//alert ("US Lbs: " + document.data.pounds.value + "\nMeters: " + document.data.meters.value + "\nKG: " + document.data.kilograms.value + "\nFt: " + document.data.feet.value + "\nIn: " + document.data.inches.value);
	return true;
 }

