CatMenu = new Array;

// catMenu is called by the select boxes, and by init
// if this is a new category
// if we are re-populating, we need to follow anonther process

function catMenu(loc) {
	var Id;
	var hasChild;
	
 // make sure user didn't click on an empty box
  if ((loc != -1) && (CatMenu[loc].length <= 0) )
   {
   	return;
   }
  // get the chosen category from this CatMenu
  if (loc == -1)
   {
   	Id = 0;
    hasChild = 1;
   }
  else
   {
    if (CatMenu[loc].selectedIndex == -1)
     {
      return;
     }
    else
    	{
  	   var IdB = CatMenu[loc].options[CatMenu[loc].selectedIndex].value;
  	   var IdSplit = IdB.split("=");
  	   Id = IdSplit[0];
  	   hasChild = IdSplit[1];
    	}
   } 

    for (i = loc + 1; i < CatMenu.length; i++)
     { 
      CatMenu[i].length = 0;
      CatMenu[i].options[0] = new Option("---------------------------");
     }   

  
  if (hasChild == 1)
  	{ 
    // clear out expanded menus, if there are some
  
    
    if (xmlhttp) 
     {
     	var pos = loc + 1;
      loadIt(Id,pos);
     }
    else 
     {
      alert("Your browser is outdated, and no longer supported.");
     }
   }
  else
  	{
  	 document.PostListing.Category1.value = Id;
  	}
}

function loadIt(Id,loc,sel) {
	var getUrl = "Categories?id=" + Id;
	xmlhttp.open("GET", getUrl,false);
 xmlhttp.send(null) 
 var xResponse = xmlhttp.responseText;
 var sResponse = xResponse.split("\n");
 // loop through the sResponse array
 for (var i=0; i<sResponse.length; i++)
  {
   //split the row into its values
   var sValues = sResponse[i].split("||")
   CatId = sValues[0] + "=" + sValues[2];
   if (sValues[2] > 0 )
    {
     CatName = sValues[1] + " ->";
    }
   else
   	{
   	 CatName = sValues[1];
   	}
   if (CatName)
    {	
 	   CatMenu[loc].options[i] = new Option(CatName, CatId);
     if ((CatId == sel) && (sel))
      {	 	 
       CatMenu[loc].options[i].selected = true;
      }
    }  
  }
}  


function initMenu() {
 j = 0;
 for (i = 0; i < document.PostListing.elements.length; i++)
  {
   if (document.PostListing.elements[i].name.indexOf("CatMenu") != -1)
   CatMenu[j++] = document.PostListing.elements[i];
  }
 var initialId = document.PostListing.Category1.value;
 if (initialId)
  {
   if (xmlhttp) 
    {
    	var getUrl = "Categories?cmd=reverse&id=" + initialId;
     xmlhttp.open("GET", getUrl,false);
     xmlhttp.send(null) 
     var xResponse = xmlhttp.responseText;
     var sResponse = xResponse.split("\n");
     var parentId = 0;
     for (var i=0; i<sResponse.length; i++)
      {
       //split the row into its values
       var sValues = sResponse[i].split("||")
       if (sValues[0])
        {
         CatId = sValues[0] + "=" + sValues[1];
        }
       else
       	{
       		CatId = 0;
       	}
       loadIt(parentId,i,CatId);
       parentId = sValues[0];
      }     
  	 }
  	else
  		{
  			alert("Your browser is outdated, and no longer supported.");
  		}
  }
 else
 	{ 
   catMenu(-1);
  } 
}
