// JavaScript Document

var req;

function loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function clearList(listElem) {
	//alert(listElem);
	var startTime = new Date() ;
	if(listElem!=0){
		
		while(listElem.hasChildNodes()) {
		listElem.removeChild(listElem.firstChild);
		}
	}
		var endTime = new Date() ;
		//alert((endTime - startTime) / 1000 + ' seconds to clear list');
}

//function clearList(listElem) {
//	//alert(listElem);
//	if(listElem!=0){
//		while(listElem.options.length > 0) {
//		listElem.options[0] = null;
//		
//		listElem.disabled="disabled"
//		}
//	}
//}
function clearEndDrop(currentList,middleList,endList) {
	
		while(endList.options.length > 0){
			endList.options[0] = null;
		}
	
		endList.options[0] = new Option('- Select a Sub Category -','_');
		endList.disabled="disabled";
		middleList.options[0] = new Option('- Select a Category -','_');
		
		middleList.disabled="disabled";
	


}

function changeDropDown(thisdd,otherdd,typeid,cleardd) {
	clearList(cleardd)
	if((otherdd=='' || otherdd==0) && typeid==2){ 
		clearList(otherdd)
	}
	if(thisdd.value!=0 && thisdd.value!='' && otherdd!='' && otherdd!=0){ 
		lstCategory = thisdd;
		sURL = "/include/ajax_lookup.asp?CategoryID=" + lstCategory.value + "&SectionID=" + otherdd.value + "&typeid=" + typeid;
		//if(confirm('Open: '+sURL)) {
		//	window.open(sURL);	
		//}
		loadXMLDoc(sURL)
	 }
}
function getProds(thisdd,typeid) {
	dropdown = thisdd;
	loadXMLDoc("/include/ajax_lookup.asp?CategoryID=" + dropdown.value +  "&typeid=" + typeid)
}
function processReqChange() 
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			
			xmldoc = req.responseXML
			
			
			sType = xmldoc.documentElement.attributes.getNamedItem("Type").nodeValue;
			
			if(sType==1){
				lstCategories = document.getElementById("CategoryID")
				lstCategories.options[0] = new Option('- Select a Category -','_');
				ndCategories = document.getElementById("SubCategory")
				nodes = xmldoc.getElementsByTagName("Category");
				for(i = 0; i < nodes.length; i++) {
					if (nodes.length != 0) {
						
						sNodeValue = nodes.item(i).childNodes.item(0).nodeValue;
						sAttrib = nodes.item(i).attributes.getNamedItem("CatID").nodeValue;
						lstCategories.options[i+1] = new Option(sNodeValue, sAttrib);
						
					}
				}
				lstCategories.disabled = "";
				//lstCategories2 = document.getElementById("subCategory")
				//lstCategories2.options[0] = new Option('- select a category -', '');
			}
			else if(sType==2){
				
				lstCategories = document.getElementById("subCategory")
				lstCategories.options[0] = new Option('- Select a Sub Category -','_');
				ndCategories = document.getElementById("CategoryID")
				ndCategories.options[0] = new Option('- Select a Sub Category -','_');
				nodes = xmldoc.getElementsByTagName("SubCategory");
				
				var x = 0
				for(i = 0; i < nodes.length; i++) {
					if (nodes.length != 0) {
						sNodeValue = nodes.item(i).childNodes.item(0).nodeValue;
						sAttrib = nodes.item(i).attributes.getNamedItem("CatID").nodeValue;
						sSubSubCount = nodes.item(i).attributes.getNamedItem("SubSubCatCount").nodeValue;
						
						if(sSubSubCount!='0') {
							//var CategoryGroup = document.createElement('optgroup');
							//CategoryGroup.label = sNodeValue;
							//lstCategories.appendChild(CategoryGroup);
						}else{
							lstCategories.options[x+1] = new Option(sNodeValue, sAttrib);	
							x++;
						}
					}
				}
				
				lstCategories.disabled = "";		
			}
			else if(sType==3){
				dd = document.getElementById("subCategory")
				menu = document.getElementById("List");
				nodes = xmldoc.getElementsByTagName("Product");
				
				for(i = 0; i < nodes.length; i++) {
					if (nodes.length != 0) {
						
						sNodeValue = nodes.item(i).childNodes.item(0).nodeValue;
						sAttrib = nodes.item(i).attributes.getNamedItem("ProdID").nodeValue;
						menu.options[i+1] = new Option(sNodeValue, sAttrib);
					}
				}
			}
		}
		else
		{
			
			lstCategories.options[0] = new Option('- Select a Sub Category -','_')
			//alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}