var tabLevel = 0;

function getProductList(listingType, parentCatId, refid, reftype)
{
	var ao = getAjaxObj();
	ao.onreadystatechange = function() { handleChange('uberProdListing', ao); }; 
	ao.open("GET", wwwDocRoot + "includes/incUberListing.php?items=products&type=" + listingType + "&parentCat=" + parentCatId + "&refid=" + refid + "&reftype=" + reftype, true)
	ao.send(null) 
}

function getCategoryList(listingType, parentCatId, refid, reftype, level)
{
	var ao = getAjaxObj();
	ao.onreadystatechange = function() { handleChange('row' + level, ao); }; 
	ao.open("GET", wwwDocRoot + "includes/incUberListing.php?items=categories&type=" + listingType + "&parentCat=" + parentCatId + "&refid=" + refid + "&reftype=" + reftype + "&level=" + level, true)
	ao.send(null) 
}

function addToProductList(pid)
{
	existingIds = document.getElementById('ids').value;
	
	if (existingIds.indexOf(pid) == -1)
	{
		/* add to list */
		if (existingIds.length == 0)
			document.getElementById('ids').value = "'" + pid + "'";
		else 
			document.getElementById('ids').value = existingIds + ",'" + pid + "'";
	}
}

function removeFromProductList(pid)
{
	existingIds = document.getElementById('ids').value;

	if ((index = existingIds.indexOf(pid)) != -1)
	{
		document.getElementById('ids').value = existingIds.substring(0, index) +
						existingIds.substring(index + pid.length + 3, existingIds.length);
	}
}

function toggleStatus(pid, item)
{
	existingIds = document.getElementById('ids').value;

	if ((index = existingIds.indexOf(pid)) == -1)
	{
		/* add to list */
		item.className = "pickedItem";
		if (existingIds.length == 0)
			document.getElementById('ids').value = pid;
		else 
			document.getElementById('ids').value = existingIds + "," + pid;
	} else {
		item.className = "notPickedItem";
		document.getElementById('ids').value = existingIds.substring(0, index - 1) +
					existingIds.substring(index + pid.toString().length, existingIds.length);
		if (index == 0)
		/* have to remove first comma */
			document.getElementById('ids').value = document.getElementById('ids').value.substring(1, document.getElementById('ids').value.length); 
	}
}

/* options: id="itemOption34" 
** selected item: id="selectedItem51"
*/

function removeItem(pid, item /*which is the <a> element */)
{
	var listHTML;
	var theul;
	var existingIds;
	
	/* bah. the only way to do this is traverse the list */
	theul = document.getElementById('selectedItemsUL');

	listHTML = '';
	for (i = 0; i < theul.childNodes.length; i++)
	{
		if (theul.childNodes[i].id != 'selectedItem' + pid)
			listHTML += "<li id='" + theul.childNodes[i].id + "'>" + theul.childNodes[i].innerHTML + "</li>";
	}
	
	theul.innerHTML = listHTML;

	if (el = document.getElementById('itemOption' + pid))
		el.firstChild.className = 'notPickedItem';

	/* deal with the hidden field */
	existingIds = document.getElementById('ids').value;
	index = existingIds.indexOf(pid);
	item.className = "notPickedItem";
	document.getElementById('ids').value = existingIds.substring(0, index - 1) +
				existingIds.substring(index + pid.toString().length, existingIds.length);
	if (index == 0)
	/* have to remove first comma */
		document.getElementById('ids').value = document.getElementById('ids').value.substring(1, document.getElementById('ids').value.length); 

}

function addItem(pid, item /*which is the <a> element */)
{
	var theul;
	existingIds = document.getElementById('ids').value;
	if ((index = existingIds.indexOf(pid)) == -1)
	{
		theul = document.getElementById('selectedItemsUL');
		theul.innerHTML = '<li id="selectedItem' + pid + '"><a href="#" onclick="removeItem(' + pid + ', this); return false;">' + item.innerHTML + '</a></li>' + theul.innerHTML;
	
		/* deal with the hidden field */
		item.className = "pickedItem";
		if (existingIds.length == 0)
			document.getElementById('ids').value = pid;
		else 
			document.getElementById('ids').value = existingIds + "," + pid;
	}	
}

function toggleProductList(item)
{
	document.getElementById('uberProdListing').style.display = (item.value == "product") ? "block" : "none";
	document.getElementById('row0').style.display = (item.value == "product") ? "block" : "none";
}