var XPATHINC = "/NarrowerItems/Item";
var XPATHSTART = "ItemsHierarchy/Item";
//Removes all content from list box
function RemoveAll(ListBox)
{
	if (ListBox == null)
		return;
	ListBox.selectedIndex = -1;
	var iListBoxLength = ListBox.options.length;
	for (var i = 0; i < iListBoxLength; i++)
		{
		ListBox.options.remove(0);
		}
}

//Get list items for the given selected item
function GetListItems(SelItem, strXPath)
{
	var szRequest = "<Request SelItem=\"";
	
	var temp = "" + SelItem;
	var pos= temp.indexOf("&");
	if (temp.indexOf("&")>-1) 
		{
		SelItem = "" + (temp.substring(0, pos) + "*" + temp.substring((pos + 1), temp.length));
		}
	szRequest += escape(SelItem);
	szRequest += "\" XPath=\"" + strXPath;
	szRequest += "\"></Request>";
	var objHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	var szURL = "/nr/Wandsworth/LAWSXML.asp";
	var szHttpMethod = "POST";
	objHTTP.Open(szHttpMethod, szURL, false);
	objHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	objHTTP.Send(szRequest);

	var szReply = objHTTP.ResponseText;
	if (objHTTP.status != 200)
	{
		//failure
		alert("failure");
		szReply = "";
	}
	//alert(szReply);
	return szReply;
}

// Called when the page loads to populate first list box
function GetTopLevel(Level, SelItem, strXPath)
{

var xmlItems = GetListItems(SelItem, strXPath);
	var objXmlDom = new ActiveXObject("Microsoft.XMLDOM");
	if (!objXmlDom.loadXML(xmlItems))
	{
	    var sErr = "Response XML String is messed up\n" + xmlItems;
		alert(sErr);
	}
	else
	{
		var nodes = objXmlDom.selectNodes("/Response/Item");
		for (var i = 0; i < nodes.length; i++)
		{
			var objOption = document.createElement("option");
			objOption.text = unescape(nodes[i].text);
			document.forms[0].Level1.add(objOption);
		}
		SetValues();	
	}
return true;
}
//Sets the values of the listboxes based upon supplied string
function SetValues()
{
// DEBUG Placeholder for custom property 
//var StoredList = "Environment;Rubbish, waste and recycling;Rubbish and waste;Dangerous and //poisonous waste;Hazardous waste;";
// DEBUG
//document.forms[0].NCPH_CategoryList.value = StoredList;
var StoredList = document.forms[0].NCPH_CategoryList.value

//var StoredList = document.all("NCPHRICH_CategoryList").value;

var  mystrings = StoredList.split(";");
for (var j in mystrings)
	{ 
	if (j ==0)
		{    
		for(var i = 0; i < document.forms[0].Level1.options.length; i++) 
			{

			if (document.forms[0].Level1.options[i].text == mystrings[j])
				{
				//alert(mystrings[j]);
				document.forms[0].Level1.options[i].selected = true;
				DisplayList(document.forms[0].Level2, mystrings[j], XPATHSTART);
				}
			}  
		} 
	if (j ==1)
		{    
		for(var i = 0; i < document.forms[0].Level2.options.length; i++) 
			{

			if (document.forms[0].Level2.options[i].text == mystrings[j])
				{
				//alert(mystrings[j]);
				document.forms[0].Level2.options[i].selected = true;
				DisplayList(document.forms[0].Level3, mystrings[j], XPATHSTART + XPATHINC);
				}
			}  
		}  //end of if
	if (j ==2)
		{    
		for(var i = 0; i < document.forms[0].Level3.options.length; i++) 
			{

			if (document.forms[0].Level3.options[i].text == mystrings[j])
				{
				//alert(mystrings[j]);
				document.forms[0].Level3.options[i].selected = true;
				DisplayList(document.forms[0].Level4, mystrings[j], XPATHSTART + XPATHINC + XPATHINC);
				}
			}  
		}  //end of if
	if (j ==3)
		{    
		for(var i = 0; i < document.forms[0].Level4.options.length; i++) 
			{

			if (document.forms[0].Level4.options[i].text == mystrings[j])
				{
				//alert(mystrings[j]);
				document.forms[0].Level4.options[i].selected = true;
				DisplayList(document.forms[0].Level5, mystrings[j], XPATHSTART + XPATHINC + XPATHINC + XPATHINC);
				}
			}  
		}  //end of if
	if (j ==4)
		{    
		for(var i = 0; i < document.forms[0].Level5.options.length; i++) 
			{

			if (document.forms[0].Level5.options[i].text == mystrings[j])
				{
				//alert(mystrings[j]);
				document.forms[0].Level5.options[i].selected = true;
				DisplayList(document.forms[0].Level6, mystrings[j], XPATHSTART + XPATHINC + XPATHINC + XPATHINC + XPATHINC);
				}
			}  
		}  //end of if
	if (j ==5)
		{    
		for(var i = 0; i < document.forms[0].Level6.options.length; i++) 
			{

			if (document.forms[0].Level6.options[i].text == mystrings[j])
				{
				//alert(mystrings[j]);
				document.forms[0].Level6.options[i].selected = true;
				}
			}  
		}  //end of if
	}
}
//Display items in 'Level' listbox for the given Selected Item name
function DisplayList(Level, SelItem, strXPath)
{
	
	if (Level == null)
		return;
	Level.selectedIndex = -1;
	if (Level.name == "Level2") 
	{
		RemoveAll(document.forms[0].Level3) ; 
		RemoveAll(document.forms[0].Level4) ;
		RemoveAll(document.forms[0].Level5) ;
		RemoveAll(document.forms[0].Level6) ;
	}
	if (Level.name == "Level3") 
	{
		RemoveAll(document.forms[0].Level4) ;
		RemoveAll(document.forms[0].Level5) ;
		RemoveAll(document.forms[0].Level6) ;
	}
	if (Level.name == "Level4") 
	{
		RemoveAll(document.forms[0].Level5) ;
		RemoveAll(document.forms[0].Level6) ;
	}
	if (Level.name == "Level5") 
	{
		RemoveAll(document.forms[0].Level6) ;
	}
	RemoveAll(Level) 
	var xmlItems = GetListItems(SelItem, strXPath);
	var objXmlDom = new ActiveXObject("Microsoft.XMLDOM");
	if (!objXmlDom.loadXML(xmlItems))
	{
	    var sErr = "Response XML String is messed up\n" + xmlItems;
		alert(sErr);
	}
	else
	{
		var nodes = objXmlDom.selectNodes("/Response/Item");
		for (var i = 0; i < nodes.length; i++)
		{
			var objOption = document.createElement("option");
			objOption.text = unescape(nodes[i].text);
			Level.add(objOption);
		}
	}
	return true;
}
//Creates the list of selected items
function CreateList()
{
var selItem = "";
//selItem[0]="hello";
if (document.forms[0].Level1.selectedIndex != -1)
{ selItem += document.forms[0].Level1.options[document.forms[0].Level1.selectedIndex].text + ";";
}
if (document.forms[0].Level2.selectedIndex > -1)
{ selItem += document.forms[0].Level2.options[document.forms[0].Level2.selectedIndex].text + ";";
}
if (document.forms[0].Level3.selectedIndex > -1)
{ selItem += document.forms[0].Level3.options[document.forms[0].Level3.selectedIndex].text + ";";
}
if (document.forms[0].Level4.selectedIndex > -1)
{ selItem += document.forms[0].Level4.options[document.forms[0].Level4.selectedIndex].text + ";";
}
if (document.forms[0].Level5.selectedIndex > -1)
{ selItem += document.forms[0].Level5.options[document.forms[0].Level5.selectedIndex].text + ";";
}
if (document.forms[0].Level6.selectedIndex > -1)
{ selItem += document.forms[0].Level6.options[document.forms[0].Level6.selectedIndex].text + ";";
}

//document.forms[0].CategoryList.value = selItem;
document.forms[0].NCPH_CategoryList.value = selItem;
//document.all("NCPHRICH_CategoryList").value = selItem;
return true; 
}

