var xmlHttpF

function getFoods(url, doFunc, id){
    document.getElementById(id).innerHTML=""
	xmlHttpF=GetXmlHttpObject();
    xmlHttpF.onreadystatechange= function() { 
        if (xmlHttpF.readyState==4)
            if (xmlHttpF.status==200)
                doFunc(xmlHttpF, id);
    }

    xmlHttpF.open("GET",url,true);
    xmlHttpF.send(null);
}

function foodChanged(req, id) {
	pr=req;
	co=id;
	
	document.getElementById(id).innerHTML="<img src='ajaxloader.gif' />"
	setTimeout('loadpageT(pr, co)', 500);    
	//document.getElementById(id).innerHTML = req.responseText;
}

function loadpageT(page_request, containerid){

	document.getElementById(containerid).innerHTML=page_request.responseText
	
}

function toggleFood(id){
	//url, function, Element id
	
	getFoods('getlevels.asp?id='+id, foodChanged,'foods');	
}

var foodText;
var foodCal;
var CalloryArray = new Array;
function checkValidation(){    

if(document.getElementById('FoodCategory').value==0)    {        
alert("Select Activity");        
return false;    
}   

if(document.getElementById('FoodCategory').value!=0)    {
	var Helping=document.frmNew.Helping
	Helping.value = EliminateSpaces(Helping.value);
	
	if ((Helping.value==null)||(Helping.value=="")){
		alert("Please Enter Helping.")
		Helping.focus()
		return false
	}					
	if (isNaN(Helping.value)){
		alert("Helping should be numeric.")
		Helping.focus()
		return false
	}	
	addRowToTable();    
}

return false
}

function addRowToTable(){
	getValues();    
	//alert(foodText)
	//alert(foodCal) 
	AFtext=activityText+" >> "+foodText
	var Helping = document.getElementById("Helping").value;
	var tbl = document.getElementById('tblCalorieCalculator');        
	var lastRow = tbl.rows.length;        
	var iteration = lastRow-1;         
	var row = tbl.insertRow(lastRow);        
	var cellLeft = row.insertCell(0);        
	var textNode = document.createTextNode(iteration+1);        
	cellLeft.appendChild(textNode);        
	CalloryArray[iteration] = Math.round(foodCal*1*Helping*1);        
	var cellLeft2 = row.insertCell(1);        
	var textNode2 = document.createTextNode(AFtext);        
	cellLeft2.appendChild(textNode2);        
	var cellLeft3 = row.insertCell(2);        
	var totlcal=Math.round(foodCal*1*Helping*1);        
	var textNode3 = document.createTextNode(totlcal);        
	cellLeft3.appendChild(textNode3);	
		
}    

function removeRowFromTable(){    
var tbl = document.getElementById('tblCalorieCalculator');    
var lastRow = tbl.rows.length;    
if (lastRow > 1){        
tbl.deleteRow(lastRow-1);        
CalloryArray.pop();            
}
}

function getValues()    {                       
var food = document.getElementById("FoodName");                           
foodText=food.options[food.selectedIndex].text;         
foodCal=food.options[food.selectedIndex].value;    
var activity=document.getElementById("FoodCategory");
activityText=activity.options[activity.selectedIndex].text;    
}	

function ValidationsForCalculating(){    
if(document.getElementById('FoodCategory').value==0)    {        
alert("Select Activity.");        
return false;    
}    
else    {        
TotalCal();    
}
}

function TotalCal(){    
var totalCal = 0;    
for(i=0;i<CalloryArray.length;i++)    {        
totalCal = totalCal + CalloryArray[i];    
}         

if(document.getElementById('urbmr').value!="")    {        
	bmrvalue=document.getElementById('urbmr').value 
} 
else{
	bmrvalue=0	
}
totalresult=parseInt(totalCal)+parseInt(bmrvalue)
document.getElementById('calorieCalculatorResult').innerHTML="<br/><h5 style='border:0px solid #0A4862;padding:0px' class='text_green_bold2'>Your body burns "+totalCal+" number of calories as a result of all the activities you perform.</h5>";		
document.getElementById('TotalResult').innerHTML="<span style='margin-top:30px;' class='text_green_bold2'>"+addCommas(totalresult)+" is your daily calorie requirement as a result of your body’s metabolism and your activity level.</span><br /><br /><span class='text' style='margin-top:30px;'>At your current level of fitness your body requires "+addCommas(totalresult)+" number of calories. We have calculated your BMI which stands for body mass index, and is a calculation of your weight to height ratio. It is useful to estimate a healthy body weight based on how tall you are.</span><br /><br />";

height=document.frmNews.height.value
weight=document.frmNews.weight.value
ajaxpage('getbmindex.asp?weight='+weight+'&height='+height+'&tcal='+totalresult,'BMIcolumn',500,'ajaxloader.gif');
return false;    
} 

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function GetXmlHttpObject()
{
var xmlHttpF=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttpF=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttpF=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttpF=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttpF;
}

// eliminating the spaces in the control string
function EliminateSpaces(str){    
//leading spaces    
      var flag = 0;    
	  while (flag == 0){
	    substr = str.substring(0,1);	       
	    if (substr == " " ){
	      str = str.substring(1);	   
	    }
	    else{
	      flag = 1;
	    }    
	  }
	  // trailing Spaces
	  flag = 0;    
	  while (flag == 0){	  
	    substr = str.substring(str.length-1);	       
	    if (substr == " " ){
	       str = str.substring(0,str.length-1);	   
	    }
	    else{
	      flag = 1;
	    }    
	  }	

      var newstr = new String("");
	  for (var i=0; i<=str.length; i++){	  	   
	    substr = new String(str.substring(i-1,i+1));	      
	    if ((substr != "  " ) && (i != 0)) {
	     newstr = newstr.concat(substr.substring(0,1));
	    }
	  }
	  return newstr;
} 
