	function gup( name )
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
	    return "";
	  else
	    return results[1];
	}
	
	YAHOO.example.treeExample = function() {
		var tree;
		
		function constructQueryString(currNode, queryString) {
			if (currNode.depth == 0) {
				if (queryString.length > 0) {
					queryString += "&";
				}
				queryString += "topNode=" + currNode.data;
				
				return queryString;	
			}
			else {
				if (queryString.length > 0) {
					queryString += "&";
				}
				queryString += "node" + (currNode.depth - 1) + "=" + currNode.data;
				return constructQueryString(currNode.parent, queryString);
			}
		}		
		
		function constructHTML(currNode, htmlLabel) {
			if (currNode.depth == 0) {
				return;
			}
			else if (currNode.depth == 1) {
				htmlLabel += currNode.data + "&catUSDA=" + currNode.parent.data;
				return htmlLabel;
			}
			else {
				htmlLabel += currNode.data + " ";
				return constructHTML(currNode.parent, htmlLabel);
			}
		}
				
		function loadNodeData(node, fnLoadComplete) {	
			//We'll load node data based on what we get back when we
			//use Connection Manager topass the text label of the 
			//expanding node to the Yahoo!
			//Search "related suggestions" API.  Here, we're at the 
			//first part of the request -- we'll make the request to the
			//server.  In our success handler, we'll build our new children
			//and then return fnLoadComplete back to the tree.
			
			//Get the node's label and urlencode it; this is the word/s
			//on which we'll search for related words:
			var queryString = "";
			var newQueryString = encodeURI(constructQueryString(node, queryString));
			
			//prepare URL for XHR request:
			var sUrl = "ajax/USDATreeRetrieve.php?" + newQueryString;
			
			var uriLabel = currentDocument + "?";
			if (gup("dailyMealId") != "") {
				uriLabel += "dailyMealId=" + gup("dailyMealId") + "&";
			}
			uriLabel += "searchPrivate=on&searchUSDA=on&submit=Search&brand=&name=";
			var tempUriLabel;
			
			//prepare our callback object
			var callback = {
			
				//if our XHR call is successful, we want to make use
				//of the returned data and create child nodes.
				success: function(oResponse) {
					YAHOO.log("XHR transaction was successful.", "info", "example");
					//YAHOO.log(oResponse.responseText);
					var oResults = eval("(" + oResponse.responseText + ")");
					if((oResults) && (oResults.length)) {
						//Result is an array if more than one result, string otherwise
						if(YAHOO.lang.isArray(oResults)) {
							for (var i=0, j=oResults.length; i<j; i++) {
								if (oResults[i] != null) {
									var tempNode = new YAHOO.widget.TextNode(oResults[i], node, false);
									tempNode.data = oResults[i];
									tempUriLabel = encodeURI(constructHTML(tempNode, uriLabel));
									tempNode.href = tempUriLabel;	
								}						
							}
						} 
					}
					
					//When we're done creating child nodes, we execute the node's
					//loadComplete callback method which comes in via the argument
					//in the response object (we could also access it at node.loadComplete,
					//if necessary):
					oResponse.argument.fnLoadComplete();
				},
				
				//if our XHR call is not successful, we want to
				//fire the TreeView callback and let the Tree
				//proceed with its business.
				failure: function(oResponse) {
					YAHOO.log("Failed to process XHR transaction.", "info", "example");
					oResponse.argument.fnLoadComplete();
				},
				
				//our handlers for the XHR response will need the same
				//argument information we got to loadNodeData, so
				//we'll pass those along:
				argument: {
					"node": node,
					"fnLoadComplete": fnLoadComplete
				},
				
				//timeout -- if more than 7 seconds go by, we'll abort
				//the transaction and assume there are no children:
				timeout: 7000
			};
			
			//With our callback object ready, it's now time to 
			//make our XHR call using Connection Manager's
			//asyncRequest method:
			YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
		}
	
	       function buildTree() {
		   //create a new tree:
		   tree = new YAHOO.widget.TreeView("treeDiv1");
		   
		   //turn dynamic loading on for entire tree:
		   tree.setDynamicLoad(loadNodeData, 1);
		   
		   //get root node for tree:
		   var root = tree.getRoot();
		   
		   //add child nodes for tree; our top level nodes are
		   //all the states in India:
		   var aCategories = ["Baby Foods", "Baked Products", "Beef Products",
		    "Beverages", "Breakfast Cereals", "Cereal Grains and Pasta",
		    "Dairy and Egg Products", "Ethnic Foods", "Fast Foods",
		    "Fats and Oils", "Finfish and Shellfish Products",
		    "Fruits and Fruit Juices", "Lamb, Veal, and Game Products",
		    "Legumes and Legume Products", "Meals, Entrees, and Sidedishes", 
		    "Nut and Seed Products", "Pork Products", "Poultry Products",
		    "Sausages and Luncheon Meats", "Snacks", "Soups, Sauces, and Gravies",  
		    "Spices and Herbs", "Sweets", "Vegetables and Vegetable Products"];
		   var aIDNums = [300, 1800, 1300, 1400, 800, 2000, 100, 3500, 2100, 
		    400, 1500, 900, 1700, 1600, 2200, 1200, 1000, 500, 700, 2500, 600,
		    200, 1900, 1100];
		   
		   for (var i=0, j=aCategories.length; i<j; i++) {
		   		var tempNode = new YAHOO.widget.HTMLNode(aCategories[i], root, false, true);
				tempNode.data = aIDNums[i];
			}
		   
		   //render tree with these toplevel nodes; all descendants of these nodes
		   //will be generated as needed by the dynamic loader.
		   tree.draw();
		}
	
	
		return {
			init: function() {
	            buildTree();
	        }
	
		}
	} ();
	
	//once the DOM has loaded, we can go ahead and set up our tree:
	YAHOO.util.Event.onDOMReady(YAHOO.example.treeExample.init, YAHOO.example.treeExample,true)