function selectMenu (selected)
{
   optionLoading(document.getElementById('item'));

   var xhr = XHR();

   if (xhr)
   {
      xhr.open("get", "demo2.php?menu="+selected, true);
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4)
        {
          if (xhr.status != 200)
          {
             alert("ERROR: Server returned error " + xhr.status);
             return;
          }
          else
          {
             getItems(xhr.responseXML);
          }
        }
      };
      xhr.send(null);
   }
   else
   {
      alert("Ajax not supported by browser");
   }
}

function getItems (xmlObj)
{
   var items = xmlObj.getElementsByTagName("item");
   var target = document.getElementById('item');
   clearSelect(target);
   selectItem(items[0].firstChild.data);
   for (i=0; i<items.length; i++)
   {
      addOption(target, items[i].firstChild.data, items[i].firstChild.data);
   }
}

function selectItem (selected)
{
   textLoading(document.getElementById('price'));
   var xhr = XHR();

   if (xhr)
   {
      xhr.open("get", "demo3.php?item="+selected, true);
      xhr.onreadystatechange = function () {
         if (xhr.readyState == 4)
         {
            if (xhr.status != 200)
            {
               alert("ERROR: Server returned error " + xhr.status);
               return;
            }
            else
            {
               getPrice(xhr.responseXML);
            }
         }
      };
      xhr.send(null);
   }
}

function getPrice (xmlObj)
{
   var price = xmlObj.getElementsByTagName("price")[0];
   var value = "";
   if (price && price.firstChild)
   {
      value = price.firstChild.data;
   }
   document.getElementById('price').firstChild.data = "$" + value;
}

function clearSelect (select)
{
   while(select.firstChild)
   {
      select.removeChild(select.firstChild);
   }
}
function addOption (select, value, display)
{
   var option = document.createElement("OPTION");
   option.setAttribute("value", value);
   option.appendChild(document.createTextNode(display));
   select.appendChild(option);
}

function optionLoading (select)
{
   clearSelect(select);
   addOption(select, '', 'Loading...');
}

function textLoading (container)
{
   container.firstChild.data = "Checking price...";
}

function XHR ()
{
   var obj = false;
   try
   {
      obj = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e)
   {
      try
      {
         obj = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
         try
         {
            obj = new XMLHttpRequest();
         }
         catch (e)
         {
            return false;
         }
      }
   }
   return obj;
}
