
//declare variables
var http = getHTTPObject();
var isWorking = false;
var cat;
var dep;
var fie;

function getChildCategories(fieldName, idCategory, depth, showRoot) {
	cat = idCategory;
	dep = depth;
	fie = fieldName;
	shRt = showRoot;
	document.getElementById('cat_'+cat+'_container').innerHTML = 'Loading...';
	if (!isWorking && http) {
		var lookup_url = '../includes/categoriesDisplay.asp?idCategory='+idCategory+'&depth='+depth+'&showRoot='+showRoot;
		http.open("GET", lookup_url, true);
		http.onreadystatechange = handleHttpResponse;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
				var xmlDocument = http.responseXML;
				//replace with new suggestions
				document.getElementById('cat_'+cat+'_container').innerHTML = '';
				for (x=0;x<xmlDocument.getElementsByTagName('category').length;x++) {
					
					var childcount = parseInt(xmlDocument.getElementsByTagName('category').item(x).getAttribute('childCount'));
					
					g = document.createElement('span');
					for (y=0;y<dep;y++)
						g.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
					
					a = document.createElement('a');
					a.id = 'cat_'+xmlDocument.getElementsByTagName('category').item(x).getAttribute('idCategory')+'_href';
					a.href = 'javascript:void(0)';
					a.title = 'Expand this category.';
					if (childcount==0)
						a.innerHTML = '<img src=tmpimages/minus.gif border=0 width=9 height=9 hspace=10>';
					else
						a.innerHTML = '<img src=tmpimages/plus.gif border=0 width=9 height=9 hspace=10>';
					
					c = document.createElement('span');
					
					if (childcount==0) {
						h = document.createElement('a');
						if (xmlDocument.getElementsByTagName('category').item(x).getAttribute('linkTo')!='') {
							h.href = xmlDocument.getElementsByTagName('category').item(x).getAttribute('linkTo') + '?idCategory=' + xmlDocument.getElementsByTagName('category').item(x).getAttribute('idCategory')+window.location.search.replace(/\?/g, '&').replace(/idCategory=\d+/g, '');
						} else {
							h.href = 'wpbec_listCategoriesAndProducts.asp?idCategory=' + xmlDocument.getElementsByTagName('category').item(x).getAttribute('idCategory')+window.location.search.replace(/\?/g, '&').replace(/idCategory=\d+/g, '');
						}
						h.title = 'List products in this category';
						h.style.color = '#339966';
					} else {
						h = document.createElement('a');
						h.href = 'javascript:void(0)';
						h.title = 'This category does not contain any products. Try one of its sub-categories.';
						h.style.color = '#666666';
					}
					h.id = 'cat_'+xmlDocument.getElementsByTagName('category').item(x).getAttribute('idCategory')+'_link';
					h.innerHTML = (xmlDocument.getElementsByTagName('category').item(x).getAttribute('categoryType')!='' ? xmlDocument.getElementsByTagName('category').item(x).getAttribute('categoryType') + ' ' : '') + xmlDocument.getElementsByTagName('category').item(x).firstChild.data;
					
					d = document.createElement('br');
					i = document.createElement('br');
					
					e = document.createElement('span');
					e.id = 'cat_'+xmlDocument.getElementsByTagName('category').item(x).getAttribute('idCategory')+'_container';
					
					f = document.createElement('br');
					
					document.getElementById('cat_'+cat+'_container').appendChild(g);
					document.getElementById('cat_'+cat+'_container').appendChild(a);
					c.appendChild(h);
					document.getElementById('cat_'+cat+'_container').appendChild(c);
					document.getElementById('cat_'+cat+'_container').appendChild(d);
					document.getElementById('cat_'+cat+'_container').appendChild(i);
					document.getElementById('cat_'+cat+'_container').appendChild(e);
					//document.getElementById('cat_'+cat+'_container').appendChild(f);
					
					//alert(document.getElementById('cat_1_container').innerHTML);
					
					if (childcount!=0) {
						addOnClick(a.id, fie, xmlDocument.getElementsByTagName('category').item(x).getAttribute('idCategory'), (dep+1), shRt);
						addOnClick(h.id, fie, xmlDocument.getElementsByTagName('category').item(x).getAttribute('idCategory'), (dep+1), shRt);
					}
				}
			} else {
				document.getElementById('cat_'+cat+'_container').innerHTML = '';
				for (y=0;y<dep;y++)
						document.getElementById('cat_'+cat+'_container').innerHTML += '&nbsp;&nbsp;&nbsp;';
				document.getElementById('cat_'+cat+'_container').innerHTML += 'No further sub categories available.';
			}
		isWorking = false;
	}
}

function addOnClick(obj, fieldName, idCategory, depth, showRoot) {
	document.getElementById(obj).onclick = function() {showHideChildren(fieldName, idCategory, depth, showRoot);};
}

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
			xmlhttp.overrideMimeType("text/xml"); 
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function showHideChildren(fieldName, idCategory, depth, showRoot) {
	if (document.getElementById('cat_'+idCategory+'_href').innerHTML.indexOf('plus.gif')>0) {
		document.getElementById('cat_'+idCategory+'_container').style.display = '';
		document.getElementById('cat_'+idCategory+'_href').innerHTML = '<img src=tmpimages/minus.gif border=0 width=9 height=9 hspace=10>'
		if (document.getElementById('cat_'+idCategory+'_container').innerHTML == '') {
			for (y=0;y<depth;y++)
				document.getElementById('cat_'+idCategory+'_container').innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				document.getElementById('cat_'+idCategory+'_container').innerHTML += 'Loading...';
			getChildCategories(fieldName, idCategory, depth, showRoot);
		}
	} else if (document.getElementById('cat_'+idCategory+'_href').innerHTML.indexOf('minus.gif')>0) {
		document.getElementById('cat_'+idCategory+'_href').innerHTML = '<img src=tmpimages/plus.gif border=0 width=9 height=9 hspace=10>'
		document.getElementById('cat_'+idCategory+'_container').style.display = 'none';
	}
	
}