function delChild(p) {
	var _pO = document.getElementById(p);
	if (_pO.childNodes[1]) {
		while (_pO.childNodes[1]) {//遍历子结点
			_pO.removeChild(_pO.childNodes[1]);//删除子结点
		}
	}
}

function createNode(oEvent){
	oEvent = oEvent || window.event;
	var txtField = oEvent.target || oEvent.srcElement;
	var _node = txtField.id;

	var oXmlHttp = zXmlHttp.createRequest();
	var _node_arr = _node.split("_");
	var _id = _node_arr[_node_arr.length - 1];
	
	if (txtField.id == ("p_node_img_" + _id)) {
		_node = "p_node_div_" + _id;
		if (document.getElementById(_node).childNodes.length > 1) {
			document.getElementById("p_node_img_" + _id).src = "images/tree_folder4.gif";
			delChild(_node);
		}
		else {
			document.getElementById("p_node_img_" + _id).src = "images/tree_folder3.gif";
			delChild(_node);
			
			var strSend = 'id=' + _id;
			oXmlHttp.open("post", "admin/product/n_json.php");
			oXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			oXmlHttp.onreadystatechange = function(){
				if (oXmlHttp.readyState == 4) {
					if (oXmlHttp.status == 200) {
						var node_list = (oXmlHttp.responseText).parseJSON();
						if (node_list.List) {
							var _nl = node_list.List.length;
							for (var i = 0; i < _nl; i++) {
								var _c_div = document.createElement("div");
								var _c_span = document.createElement("span");
								var _c_img = document.createElement("img");
								var _oTextNode = document.createTextNode(node_list.List[i].name);
								
								_c_div.id = "p_node_div_" + node_list.List[i].id;
								_c_div.className = "div_list_node";
								
								_c_span.id = "p_node_span_" + node_list.List[i].id;
								_c_span.style.cssText = "width:100%;"
								_c_img.id = "p_node_img_" + node_list.List[i].id;
								_c_img.src = "images/tree_folder4.gif";
								
							
								
								_c_span.appendChild(_c_img);
								_c_span.appendChild(_oTextNode);
								_c_div.appendChild(_c_span);
								document.getElementById(_node).appendChild(_c_div);
							}
						}
						else {
							return false;
						}
					}
				}
			};
			oXmlHttp.send(strSend);
		} 
	} else if (txtField.id == ("p_node_span_" + _id)) {
		var strSend = 'id=' + _id;
		oXmlHttp.open("post", "jugeid.php");
		oXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		oXmlHttp.onreadystatechange = function(){
			if (oXmlHttp.readyState == 4) {
					if (oXmlHttp.status == 200) {
						if (oXmlHttp.responseText > 0) {
							window.location.href = "products_class.php?id=" + _id;
						} else {
							window.location.href ="show_xilie.php?id="+_id;
						}
					}
			}
		};
		oXmlHttp.send(strSend);
		
	}
}

window.onload = function(){
	var td_node = document.getElementById("p_node_td");
	var _chil_l = td_node.childNodes.length;
	
	for (var i = 0;i < _chil_l;i++) {
		td_node.childNodes[i].onclick = createNode;
	}
};
