var xmlHttp;

function showpd(str)
{

pounds=showpd.arguments[1];
grams=pounds/0.002204585537;
grams=Math.round(grams*10)/10;

document.getElementById("wgm").value=grams;

shownvalues(grams,showpd.arguments);

}


function showgm(str)
{

grams=showgm.arguments[1];
pounds=grams*0.002204585537;
pounds=Math.round(pounds*100)/100;

document.getElementById("wpd").value=pounds;

shownvalues(grams,showgm.arguments);

}


function shownut(str)
{

grams=shownut.arguments[1];
pounds=grams*0.002204585537;
pounds=Math.round(pounds*100)/100;

document.getElementById("wgm").value=grams;
document.getElementById("wpd").value=pounds;

shownvalues(grams,shownut.arguments);

}

function shownvalues(grm,args)
{

kc=0;

spans = document.getElementsByTagName('span');
for(i=0;i<spans.length;i++) {
  if(spans[i].id != '') {

    oldval=args[i+2];
    newval=oldval*(grm/100);

    soldval=oldval.toString();

    pindex=soldval.lastIndexOf('.');
    if (pindex>0)
      fr=soldval.substring(pindex+1,soldval.length).length;
    else
      fr=0;  

    rnd=Math.pow(10,fr);
    newval=Math.round(newval*rnd)/rnd;
    spans[i].innerHTML = newval;
    if (i==1) kc=newval;
  }
}

document.getElementById("cburnlink").innerHTML='<a href="calorie-burn.shtml?180,'+kc+'">Click here to find out how much time it will take for you to burn these '+kc+' kcal.</a>';



}


// *******************************************



function showweight(str)
{
if (str.length==0)
  { 
  document.getElementById("weights").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 

document.getElementById("weights").innerHTML="<p><b>Searching. Please wait...</b></p>";

var url="weight.php";
url=url+"?"+str;
//url=url+",sid="+Math.random();
xmlHttp.onreadystatechange=showweightChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function showfoodgrps(str)
{
if (str.length==0)
  { 
  document.getElementById("foods").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 

document.getElementById("foods").innerHTML="<p><b>Searching. Please wait...</b></p>";

var url="food.php";
url=url+"?"+str;
//url=url+",sid="+Math.random();
xmlHttp.onreadystatechange=foodgrpstateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 

function foodgrpstateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("foods").innerHTML=xmlHttp.responseText;
}
}

function showfoodlist(str)
{

if (str.length==0)
  { 
  document.getElementById("foodlist").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 


document.getElementById("foodlist").innerHTML="<p><b>Searching. Please wait...</b></p>";

var url="foodlist.php";
url=url+"?"+str;
//url=url+",sid="+Math.random();
xmlHttp.onreadystatechange=foodliststateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 


function foodliststateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("foodlist").innerHTML=xmlHttp.responseText;
}
}


function showweightChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("weights").innerHTML=xmlHttp.responseText;
}
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
