var _is_click_item = false;
var _click_item_category = "";

function clickItem(el) {
	try	{
		if (_is_click_item) return;
		_is_click_item = true;
		_click_item_category = "";

		var ctr = getParent(el, "TR");
		var aim = GetE(ctr.id+"_child");
		if (aim == undefined) {
			if (!el.className.equals("expand")) {
				failureClickItem();
				return;
			}
			el.className = "collapse";
			var table = getParent(el, "TABLE");
			var index = getIndex(table, ctr);
			var newRow = table.insertRow(index+1);
			newRow.id = ctr.id+"_child";
			var newCell = newRow.insertCell(-1);
			newCell.innerHTML = "&nbsp;";

			newCell = newRow.insertCell(-1);
			newCell.innerHTML = "<span>loading...</span>";

			_click_item_category = ctr.id+"_child";

			var surl = "/product/category_menu";
			var postData = "category=" + ctr.id;
			var request = Connect.asyncRequest('POST', surl, { success:successClickItem, failure:failureClickItem }, postData);
			return;
		}
        if (el.className.equals("expand")) {
			el.className = "collapse";
			aim.style.display = "";
		} else {
			el.className = "expand";
			aim.style.display = "none";
		}

		initClickItem();
	} catch (ex) {
		failureClickItem();
	}
}

function successClickItem(o) {
	try { 	
		if(o.responseText == undefined) {
			failureClickItem(o);
			return;
		}
		var ctr = GetE(_click_item_category);
		if(ctr == undefined) {
			failureClickItem(o);
			return;
		}

		ctr.cells[1].innerHTML = o.responseText;
        initClickItem();
	} catch (ex) {
		failureClickItem(o);
		return;       
    }
}

function failureClickItem(o) {
	window.location.reload();
}

function initClickItem() {
    _is_click_item = false;
    _click_item_category = "";
}

function getIndex(table, tr) {
	try	{
		var trs = table.rows;	
		for (var i=0; i<trs.length ; i++ ) {
			if (trs[i]==tr)	{
				return i;
			}
		}
	} catch (ex) {
		return -1;
	}
}