var selectedId = new Array();

function T(prodId, prodName){
	this.id = prodId;
	this.name = prodName;
	this.parent = getParent(this);
	this.children = null;	//children
	this.objName = "productType";
	
	this.addChild = function(child)
	{
		if(this.children == null)
			this.children = new Array();
		this.children[this.children.length] = child;
	}
	
	//初始化控件
	//Set parent
	if(this.parent != null){
		this.parent.addChild(this);
	}

	this.checked = function()
	{
		var chked = false;

		if(selectedId != null){
			for(var idx=0;idx<selectedId.length;idx++){
				if(this.id == selectedId[idx]){
					chked = true;
					break;
				}
			}
		}
		return chked;
	}

	//显示控件
	this.display = function()
	{
		var obj = document.getElementById("productType");
		
		if(obj == null){
			document.writeln("<select id='productType' name='" + this.objName + 
					"' onchange='selectType(this.value)'>");
			document.writeln("</select>");
			obj = document.getElementById("productType");
		}else{			
			obj.options.length = 0; 
		}

		if(this.parent != null)
			obj.options.add(new Option("--上一级--", this.parent.id));
		
		var thisOpt = new Option("--" + this.name + "--", this.id);
		thisOpt.selected = true;
		obj.options.add(thisOpt);

		//下一级
		for(var i=0;i<this.children.length;i++){
			obj.options.add(new Option(this.children[i].name, this.children[i].id));
		}
	}

	this.displayInTree = function(blnHideParent)
	{
		var mydiv = document.getElementById("pid" + this.id);

		if(mydiv == null){
			var innerHtml = "<table id='pid" + this.id + "'><tr><td>";
			
			if(!blnHideParent){
				if(this.children != null && this.children.length > 0)
					innerHtml += "<a href='javascript:expandIt(\"" + this.id + "\")'><img src='images/tminus.gif' alt='收缩' width='9' height='9' border='0'></a>";
				else
					innerHtml += "<input type='checkbox' name='" + this.objName + "' id='cid" + this.id + "' value='" + this.id + "' " + (this.checked()?"checked":"") + ">";

				innerHtml += "&nbsp;<span id='txt" + this.id + "'>" + this.name + "</span>";
			}
			
			innerHtml += "</td></tr></table>";
			document.writeln(innerHtml);
			mydiv = document.getElementById("pid" + this.id);
		}

		if(mydiv.loaded) 
			return;

		if(this.children != null){
			for(var i=0;i<this.children.length;i++){
				var cdiv = mydiv.insertRow();
				var newCell = cdiv.insertCell();
				
				var innerHtml = "<table id='pid" + this.children[i].id + "'><tr><td>";
				
				if(this.children[i].children != null && this.children[i].children.length > 0)
					innerHtml += "<a href='javascript:expandIt(\"" + this.children[i].id + "\")'><img src='images/tplus.gif' alt='展开' width='9' height='9' border='0'></a>";
				else
					innerHtml += "<input type='checkbox' name='" + this.children[i].objName + "' id='cid" + this.children[i].id + "' value='" + this.children[i].id + "' " + (this.children[i].checked()?"checked":"") + ">";
					
				innerHtml += "&nbsp;<span id='txt" + this.children[i].id + "'>" + this.children[i].name + "</span>";
				innerHtml += "</td></tr></table>";

				newCell.innerHTML = innerHtml;
				newCell.style.paddingLeft = "15px";
				//cdiv.style.display = "";
			}
		}
		
		mydiv.loaded = true;
	}
}
//取得Parent
function getParent(pt)
{
	if(pt == null || pt.id == null || pt.id.length != 10 || pt.id == "0000000000")
		return null;

	if(pt.id == "0000000000"){
		return null;
	}

	var parentId = "";
	if(pt.id.substr(2) == "00000000"){
		parentId = "0000000000";
	}else if(pt.id.substr(4) == "000000"){
		parentId = pt.id.substr(0,2) + "00000000";
	}else if(pt.id.substr(6) == "0000"){
		parentId = pt.id.substring(0,4) + "000000";
	}else if(pt.id.substr(8) == "00"){
		parentId = pt.id.substring(0,6) + "0000";
	}else{
		parentId = pt.id.substring(0,8) + "00";
	}

	return t[parentId];
}

function selectType(typeId){
	var cType = t[typeId];
	if(cType != null){
		if(cType.children != null && cType.children.length > 0){
			cType.display();
		}
	}
}

function expandIt(typeId){
	var mydiv = document.getElementById("pid" + typeId);
	if(!mydiv.loaded){
		if(t[typeId] != null)
			t[typeId].displayInTree();

		var img = mydiv.firstChild.firstChild.firstChild.firstChild.firstChild;

		if(img.alt == "展开"){
			img.src = "images/tminus.gif";
			img.alt = "收缩";
		}else{
			img.src = "images/tplus.gif";
			img.alt = "展开";
		}
	}else{

		//变换图片
		var img = mydiv.firstChild.firstChild.firstChild.firstChild.firstChild;
		
		for(var i=1;i<mydiv.rows.length;i++){
			if(mydiv.rows[i].style.display == "none"){
				img.src = "images/tminus.gif";
				img.alt = "收缩";
				mydiv.rows[i].style.display = "";
			}else{
				img.src = "images/tplus.gif";
				img.alt = "展开";
				mydiv.rows[i].style.display = "none";
			}
		}
	}
}

function displayTypeInTree(typeId, selType){
	if(selType != null){
		selectedId = selType.split(",");
	}

	var cType = t[typeId];
	if(cType != null){
		cType.displayInTree(false);
	}
}

var t=new Array();
var a="0000000000";
t[a]=new T(a,"全部");
a="0100000000";
t[a]=new T(a,"药品");
a="0101000000";
t[a]=new T(a,"处方药");
a="0102000000";
t[a]=new T(a,"非处方药");
a="0200000000";
t[a]=new T(a,"医疗器械");
a="0300000000";
t[a]=new T(a,"保健食品");
