/*_-------------------------------------------------------_*/
function showHide(block) {
	
	//var result = document.getElementById("zigResults");
	if (block.className == "displayNone") {
		block.className = "displayBlock";
		isVis = true;
	} else {
		block.className = "displayNone";
		isVis = false;
	}
	
	return true;
}	

function toggleMe(img,toggle) {
	try {
	   if ($(toggle)){
		  if ($(toggle).getStyle('display') == 'block'){
		    $(toggle).setStyle({display:'none'});
		    $(img).src = "./img/rightarrow.gif";
		  }  
		  else
		    $(toggle).setStyle({display:'block'});
		    $(img).src = "./img/downarrow.gif";
	   }	
	}catch(e){}
}




// Numeric test
function isNumeric(num, fieldPrompt, fieldName) {
	var regTest=/(^\d+$)|(^\d+\.\d+$)/
	if (regTest.test(num))
		return true;
	else {
		if (fieldPrompt) { 
			alert(fieldPrompt + " Must be a valid number");
			Get(fieldName).focus();
			Get(fieldName).select();
		}
		return false;
	}
}
function Get( oName, oFrame, oDoc ) {
	if( !oDoc ) { if( oFrame ) { oDoc = oFrame.document; } else { oDoc = window.document; } }
	if( oDoc[oName] ) { return oDoc[oName]; } if( oDoc.all && oDoc.all[oName] ) { return oDoc.all[oName]; }
	if( oDoc.getElementById && oDoc.getElementById(oName) ) { return oDoc.getElementById(oName); }
	for( var x = 0; x < oDoc.forms.length; x++ ) { if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }
	for( var x = 0; x < oDoc.anchors.length; x++ ) { if( oDoc.anchors[x].name == oName ) { return oDoc.anchors[x]; } }
	for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) {
		var theOb = Get( oName, null, oDoc.layers[x].document ); if( theOb ) { return theOb; } }
	if( !oFrame && window[oName] ) { return window[oName]; } if( oFrame && oFrame[oName] ) { return oFrame[oName]; }
	for( var x = 0; oFrame && oFrame.frames && x < oFrame.frames.length; x++ ) {
		var theOb = Get( oName, oFrame.frames[x], oFrame.frames[x].document ); if( theOb ) { return theOb; } }
	return null;
}
	
function calcIt() {
oForm = document.getElementById('calc');
weight = parseInt(oForm.weight.value*1);
age = parseInt(oForm.age.value*1);
feet = parseInt(oForm.feet.value*1);
inches = parseInt(oForm.inches.value*1);
var bfPer = parseInt(oForm.txtBF.value*1);

if (!isNumeric(age,"Age:","age"))
	return false;
else if ( (age <= 12) || (age > 80)) {
	alert ("Age must be between 13 and 80");
	return false;
}

if (!isNumeric(weight,"Weight:","weight"))
	return false;
else if ( (weight <= 40) || (weight > 500) ) {
	alert ("Please enter a valid weight");
	return false;
}

if (!isNumeric(feet,"Feet:","feet"))
	return false;
else if ( (feet < 4) || (feet > 7) ) {
	alert ("Height must be between 4 and 7 feet");
	return false;
}

if (!isNumeric(inches,"Inches:","inches"))
	return false;
else if (inches > 11) {
	alert ("Inches should be less than 12");
	return false;
}

if (!isNumeric(bfPer,"Body Fat %","txtBF"))
	return false;
	
if (oForm.weighttype[0].checked) 
	weight = weight/2.2;

height = ((feet*12) + inches) * 2.54;

if (oForm.optFormula[2].checked) {
	//Harris Benedict
	if (oForm.sex[0].checked) {
		result = 66.5 + (13.75 * weight) + (5.003 * height) - (6.775 * age); //Men
	} else {
		result = 655.1 + (9.563 * weight) + (1.850 * height) - (4.676 * age); //Women
	}
} else {

	if (oForm.optFormula[1].checked) {
		var leanMass = weight - (weight * (bfPer / 100));
		//result = 21.6 * weight  + 370; //Katch Mcardle
		//result = 500 + (22 * weight); /?
		result = 19.7 * leanMass + 413;
	} else {
		//Mifflin St-Jeor
		if (oForm.sex[0].checked) 
			result = 5 + (10 * weight) + (6.25 * height) - (5 * age);
		else 
			result = -161 + (10*weight) + (6.25 * height) - (5 * age);		
	}
}

var maintain = result * oForm.activity.options[oForm.activity.selectedIndex].value;
var isBMR = false;
if (oForm.activity.options[oForm.activity.selectedIndex].value == 1)
	isBMR = true;
	
rockBottom = (weight*2.2)*8;
var suffix = " Cals/day";

//kJ conversion
if (!oForm.optResults[0].checked) {
	maintain = maintain * 4.184;
	rockBottom = rockBottom * 4.184; 
	suffix = " KJ/day";
} else {
	/*var linkNut = Get("linkNutrient").href;
	if (linkNut.indexOf("?") != -1)
		linkNut = linkNut.substring(0,linkNut.indexOf("?"));
	linkNut += "?cals=" + Math.round(maintain);
	Get("linkNutrient").href = linkNut;
	Get("linkNutrient2").href = linkNut;*/
}

s = Math.round(maintain) + suffix;
Get("answer").innerHTML = s;

loseFat = maintain - (maintain*0.20)
if (loseFat < rockBottom && rockBottom <= maintain)
	loseFat = rockBottom;
s = Math.round(loseFat) + suffix;
Get("lose").innerHTML = s;

var extLoseFat = maintain - (maintain*0.40)
if (extLoseFat < rockBottom  && rockBottom <= maintain)
	extLoseFat = rockBottom;
s = Math.round(extLoseFat) + suffix;
Get("loseExt").innerHTML = s;

var gain = maintain + (maintain*0.20)
s = Math.round(gain) + suffix;
//Get("gain").innerHTML = s;

if (isBMR) {
	Get("loseExt").innerHTML =  "-";
	Get("lose").innerHTML = "-";
}	


//try walking the DOM
var zigzag = new Array(1,0.8,1.2,1,0.9,1.1,1);
var tbl =document.getElementById('zigResultsTable');
var sZig;

for (r = 1; r < tbl.rows.length; ++r) {
  	thisRow = tbl.rows[r];
  	for (c = 1; c < thisRow.cells.length; ++c) {

		if (c==1)
			sZig = extLoseFat * zigzag[r-1];
		if (c==2)
			sZig = loseFat * zigzag[r-1];
		if (c==3)
			sZig = maintain * zigzag[r-1];
		if (c==4)
			sZig = gain * zigzag[r-1];
			
		if (sZig < rockBottom)
			sZig = rockBottom
			
		thisRow.cells[c].innerHTML = Math.ceil(sZig);
		if (isBMR)
			thisRow.cells[c].innerHTML =  "-";
	}
  }
  $('cal_calc_res').removeClassName('displayNone');
  $('cal_calc_res').addClassName('displayBlock');
  $('cal_calc_res').scrollTo();
  
  return true;
}
//  End -->
/*----------------------------------------------------------------------------------*/
function fade(){
   if( !$('showLog') ) {
        new Effect.Fade($('flashMessage'),{duration:10.0});
   }
}

function initFunctions(){
 try{
     if ($('msgDiv')){
	    if($('msgDiv').innerHTML.strip() != ''){
		  $('msgDiv').scrollTo();
		  new Effect.Fade($('msgDiv'),{duration:15.0});
	   }
     }	
   }catch(e){alert(e);}
   
  try{
       if( $('flashMessage') ) {
         //new Effect.Pulsate($('flashMessage'),{afterFinish:fade});  
       }
    
    if( $('showLog') ) {
         $('showLog').setStyle({cursor:'pointer'});
         $('showLog').onmouseover = function(){$('errorMsg').setStyle({display:'block'});}
         $('showLog').onmouseout = function(){$('errorMsg').setStyle({display:'none'});}
         $('flashMessage').setStyle({background:'#9D1503'});        
    }
    $('showLog').scrollTo();
   }catch(e){//alert(e);
   };

   
}

document.observe('dom:loaded', initFunctions);
