﻿function ns(s){return typeof(s)=="object"?s[0]:s;}
function init_code(o){
	var url=o.getAttribute("file"),domain=o.getAttribute("domain"),K={};
	try{
	if(url) K=eval(F.send("/js/"+url+".js",domain));
	else K=GlobalKind;
	}catch(e){}
	o.style.cursor="pointer";
	if(K.level){
		o.frag=new Array(K.level.length+1);
		for (var i=K.level.length-1,p=0; i>=0; i--){
			o.frag[i+1]=new BigInt("2").pow(p);
			p+=K.level[i];
		}
	}
	o.K=K.code?K.code:K;
	o.w=o.getAttribute("w");
	o.start=parseInt(o.getAttribute("start"));
	if(isNaN(o.start))o.start=0;
	o.maxsel=parseInt(o.getAttribute("maxsel"));
	if(isNaN(o.maxsel))o.maxsel=1;
	o.col=parseInt(o.getAttribute("col"));
	if(isNaN(o.col))o.col=3;
	o.free=o.getAttribute("free");
	o.full=o.getAttribute("full");
	o.pop=o.getAttribute("pop");
	o.limit=o.getAttribute("limit");
	o.set=function(v){this.value=this.data=v;if(this.inbox)this.inbox(v)};
	o.pcode=function(v,k){
		try{
		if(!this.frag) return 0;
		if(k==null)k=this.level(v)-1;
		if(k<=0) return 0;
		var m=this.frag.length,p=0;
		if(k>m)k=m;
		var num=this.frag[k];
		return new BigInt(v).divide(num).multi(num);
		}catch(e){return 0;}
	};
	o.level=function(v){
		if (!v||v<=0||!this.frag) return 0;
		var m=this.frag.length;
		for (var i=1;i<=m;i++)if(v==this.pcode(v,i)) return i;
		return 0;
	};
	o.seek=function(v){
		if(v==this.kid.length-1&&this.kid[v].value) return this.value=this.kid[v].value;
		var l=this.K[this.kid[v].value];
		for(var i=v+1;i<this.kid.length;i++)this.kid[i].length=parseInt(this.kid[i].getAttribute("min"));
		if(this.free&&this.kid[v].value&&!this.value)this.value=this.kid[v].value;
		if(!l) return;
		for(var e in l)this.kid[v+1].options[this.kid[v+1].length]=new Option(e,ns(l[e]));
	};
	var note="<span style='margin-left:20px;color:#999'>（ＣＴＲＬ＋Ｆ可以进行查找）</span>";
	o.map=function(v){
	try{
		var o=$("pop_map");
		if(!o) return;
		o.source=this;
		if(v==null)v=this.value;if(!v)v=this.start;
		var s=[],t=[],index=0,start=this.level(this.start),end=this.level(v),l=this.K[v]; 
		if(!l){
			end-=1;
			v=this.pcode(v,end);
			l=this.K[v];
			if(!l)return alert("没有找到该"+(this.title?this.title:"类别")+"！");
		}
		for(var i=start;i<=end;i++){
			var c=this.pcode(v,i);
			if(c==0) t[t.length]="<a href='#' onclick='return showmap(0)'><b>全部"+(this.title?this.title:"顶级类别")+"</b></a>";
			else{
				var code=this.pcode(c,i-1),p=this.K[code];
				for(var e in p){
					if(c==e)
					{
						if(t.length>0)t[t.length]=" > ";
						t[t.length]="<a href='#' onclick='return showmap("+c+")'><b>"+ns(p[e])+"</b></a>";
						break;
					}
				}
			}
		}
		s[s.length]="<table width=100%><tr><td style='padding:0px 8px'>当前位置："+t.join('')+note+"</td></tr></table>";
		s[s.length]="<table width=100% cellspacing=10><tr>";
		var num=0;
		for(var e in l){ num++; for(var t in this.K[e]) num++;}
		o.style.height=num>100?"450px":"";
		var max=parseInt((num-1)/this.col)+1,per=parseInt(100/this.col);
		for(var e in l){
			if(index==0)s[s.length]="<td width="+per+"% valign=top>";			
			index++;
			var t=this.K[e];
			if(t){
				s[s.length]="<a class=catalog href='#' onclick='return showmap("+e+")'>"+ns(l[e])+"</a>";
				s[s.length]="<div class=spacer></div>";
			}
			else{
				s[s.length]="<a class=item href='#'";
				if(e==this.value) s[s.length]=" style='color:red;font-weight:bold'";
				s[s.length]=" onclick='return selmap("+e+")'>"+ns(l[e])+"</a><br>";
				if(index>=max){s[s.length]="</td>"; index=0;}
				continue;
			}
			for(var f in t){
				if(index==0)s[s.length]="<td width="+per+"% valign=top>";
				s[s.length]="<a class=item href='#'";
				if(f==this.value) s[s.length]=" style='color:red;font-weight:bold'";
				if(this.K[f]) s[s.length]=" onclick='return showmap("+f+")'>";
				else s[s.length]=" onclick='return selmap("+f+")'>";
				s[s.length]=ns(t[f])+"</a><br>";
				index++;
				if(index>=max){s[s.length]="</td>"; index=0;}
			}
			if(index>0)s[s.length]="<div class=blank></div>";
		}
		if(index>0)s[s.length]="</td>";
		s[s.length]="</tr></table>";
		if(this.title)$("pop_title").innerHTML="请您选择"+this.title+"：";
		var pdiv=$("pop");
		if(this.w){
			pdiv.style.width=this.w+"px";
			o.style.width=(parseInt(this.w)-20)+"px";
		}
		o.innerHTML=s.join('');
		if(pdiv.style.display!="block")F.pop();
	}catch(e){alert(e.message);F.unpop();}
	};
	o.sel=function(){
	try{
		var t=$("pop_map"),tag=this.maxsel==1?"radio":"checkbox";
		if(!t) return;
		t.style.height="";
		t.source=this;
		var v=this.value?this.value:"",arr=v.toString().split(',');
		v=this.start;
		var s=[],z=[],index=0,start=this.level(this.start),end=this.level(v),l=this.K[v]; 
		if(!l){return alert("没有找到"+(this.title?this.title:"类别")+"！")}
		var total=0;
		for(var e in l)if(this.K[e])total++;
		if(total>0)s[s.length]="<table style='margin:5px 0px 5px 6px'><tr><td><select id=pop_select style='width:240px' onChange=''></select></td><td style='padding-left:10px;color:#666'>请先选择左侧类别，再勾选下列对应子项。</td></tr></table>";
		s[s.length]="<div id=pop_sub></div>";
		s[s.length]="<div style='color:#666;border-bottom:1px solid #999;padding-bottom:3px;overflow:hidden;margin:5px 8px'>您已经选择了以下类别：（"+(o.limit?"您可在同一类别中最多选择 "+o.maxsel+" 个子项":"您最多可以选择 "+o.maxsel+" 项")+"）</div>";
		s[s.length]="<div id=pop_sel style='margin:3px 5px 8px 5px;line-height:16px'></div>";
		s[s.length]="<div style='text-align:right;margin-right:30px'><input type=button class=btn style='padding:0px 10px;font-size:12px' value=确定 onClick='oksel()' />&nbsp;";
		s[s.length]="<input type=button class=btn style='padding:0px 10px;font-size:12px' value=取消 onclick='F.unpop()' /></div>";
		
		if(this.title)$("pop_title").innerHTML="请您选择"+this.title+"：";
		var pdiv=$("pop");
		if(this.w){pdiv.style.width=this.w+"px";t.style.width=(parseInt(this.w)-20)+"px";}
		t.innerHTML=s.join('');
		t=$("pop_sel");
		t.source=this;t.rec={};
		t.add=function(v,n){
			if(!v)return;
			var count=0;
			for(var e in this.rec)count++;
			if(o.maxsel==1)this.rec={};
			else if(count+1>o.maxsel)return alert("您最多只能选择"+o.maxsel+"项！");
			if(!n){
				var p=o.pcode(v),t=o.K[p];
				n="";
				if(o.full&&l[p])n=l[p]+"·";
				if(t[v])n+=t[v];
			}
			this.rec[v]=n;
			this.draw();
			return true;
		};
		t.del=function(v,b){
			if(!this.rec[v]) return;
			delete this.rec[v];
			if(b)this.draw();
		};
		t.erase=function(v){
			var l=$("pop_sub").getElementsByTagName("INPUT");
			for(var i=0;i<l.length;i++){
				if(l[i].value!=v) continue;
				l[i].checked=false;
				if(i>0) continue;
				var t=$("pop_tbl");if(!t)continue;
				t=$$(t,"INPUT");
				for(var j=0;j<t.length;j++)t[j].disabled=false;
			}
			this.del(v,true);
		};
		t.draw=function(){
			var s=["<table cellspacing=3>"],index=0;
			for(var e in this.rec){
				if(index==0)s[s.length]="<tr>";
				s[s.length]="<td style='padding-right:10px' nowrap><input type=checkbox checked style='width:15px;height:15px' value="+e;
				s[s.length]="> "+this.rec[e]+"</td>";
				index++;
				if(index==o.col){s[s.length]="</tr>";index=0;}
			}
			if(index>0)for(;index<o.col;index++)s[s.length]="<td>&nbsp;</td>";
			s[s.length]="</tr></table>";
			this.innerHTML=s.join('');
			var K=this.getElementsByTagName("INPUT");
			for(var i=0;i<K.length;i++)K[i].onclick=function(){$("pop_sel").erase(this.value);}
		};
		for(var i=0;i<arr.length;i++)t.add(arr[i]);
		function drawitems(v,a){
			var t=$("pop_sub"),l=o.K[v],s=[],index=0;
			if(a&&(o.free||!l))s[s.length]="<div style='margin:5px 0px 0px 8px'><input style='width:15px;height:15px;margin:2px 0px' type="+tag+" name=subitem value="+v+" title=\""+a+"\"> "+a+"</div>";
			if(!a){
				var p=o.pcode(v),pk=o.K[p];
				a=pk[v]?pk[v]:"";
			}
			if(l){
				s[s.length]="<table id=pop_tbl width=100% cellspacing=1 style='margin:2px 0px 3px 7px'>";
				for(var e in l){
					if(index==0)s[s.length]="<tr>";
					s[s.length]="<td><input style='width:15px;height:15px;' type="+tag+" name=subitem value="+e;
					s[s.length]=" title=\""+ns(l[e])+"\" data=\""+a+"\"> <span>"+ns(l[e])+"</span></td>";
					index++;
					if(index>=o.col){s[s.length]="</tr>"; index=0;}
				}
				if(index>0)for(;index<o.col;index++)s[s.length]="<td></td>";
				s[s.length]="</tr></table>";
			}
			t.innerHTML=s.join('');
			l=t.getElementsByTagName("INPUT");
			var sel=$("pop_sel");
			function swapstat(c){
				var o=$("pop_tbl"),sel=$("pop_sel");
				if(o){
					var l=F.tag("INPUT",o);
					for(var i=0;i<l.length;i++){
						if(c.checked){sel.del(l[i].value);l[i].checked=false}
						l[i].disabled=c.checked;
					}
				}
			};
			for(var i=0;i<l.length;i++){
				if(i>0||!o.free)l[i].onclick=function(){
					var d=this.getAttribute("data"),n=o.full?(d?d+"·":""):"";
					if(!this.checked) $("pop_sel").del(this.value,true);
					else if(this.checked){
						var combo=$("pop_select");
						if(o.limit&&combo){
							var p=combo.value,rec=$("pop_sel").rec;
							for(var e in rec){
								if(p!=o.pcode(e)){
									this.checked=false;
									return alert("您选择的项目在其它类别中，请先将底部已选择的项目勾除，才能继续选择。");
								}
							}
						}
						if(!$("pop_sel").add(this.value,n+this.title))this.checked=false;
					}
				};
				else l[i].onclick=function(){
					swapstat(this);
					if(!this.checked)sel.del(this.value,true);
					else sel.add(this.value,this.title);
				};
				if(sel.rec[l[i].value]){l[i].checked=true;if(i==0&&o.free)swapstat(l[i]);}
			}
		};
		t=$("pop_select");
		if(!t)drawitems(v);
		else{
			t.length=0;
			t.onchange=function(){drawitems(this.value,this.options[this.selectedIndex].text);};
			var num=0;
			for(var e in l){ for(var tt in this.K[e]) num++;}
			if(num==0){ for(var e in l){t.options[t.length]=new Option(ns(l[e]),e);break;} }
			else{ for(var e in l)t.options[t.length]=new Option(ns(l[e]),e); }
			v=this.pcode(arr[0]);
			t.value==v;
			if(t.length>0)t.onchange();
		}
		if(pdiv.style.display!="block")F.pop();
	}catch(e){alert(e.message);F.unpop();}
	};
	o.simple=function(){
	try{
		var t=$("pop_map"),tag=this.maxsel==1?"radio":"checkbox";
		if(!t) return;
		t.style.height="";
		t.source=this;
		var v=this.value?this.value:"",arr=v.toString().split(',');
		v=this.start;
		var s=[],index=0,K=this.K; 
		if(!K) return alert("没有找到该"+(this.title?this.title:"类别")+"！");
		s[s.length]="<table width=100%><tr><td style='padding:0px 8px'>当前位置：<a href='#' onclick='return false'><b>全部"+(this.title?this.title:"类别")+"</b></a></td></tr></table>";
		s[s.length]="<div id=pop_sub></div>";
		s[s.length]="<div style='color:#666;border-bottom:1px solid #999;padding-bottom:3px;overflow:hidden;margin:5px 8px'>您已经选择了以下类别：（您最多可以选择 "+o.maxsel+" 项）</div>";
		s[s.length]="<div id=pop_sel style='margin:3px 5px 8px 5px;line-height:16px'></div>";
		s[s.length]="<div style='text-align:right;margin-right:30px'><input type=button class=btn style='padding:0px 10px;font-size:12px' value=确定 onClick='oksel()' />&nbsp;";
		s[s.length]="<input type=button class=btn style='padding:0px 10px;font-size:12px' value=取消 onclick='F.unpop()' /></div>";
		
		if(this.title)$("pop_title").innerHTML="请您选择"+this.title+"：";
		var pdiv=$("pop");
		if(this.w){pdiv.style.width=this.w+"px";t.style.width=(parseInt(this.w)-20)+"px";}
		t.innerHTML=s.join('');
		t=$("pop_sel");
		t.source=this;t.rec={};
		t.add=function(v,n){
			if(!v)return;
			var count=0;
			for(var e in this.rec)count++;
			if(o.maxsel==1)this.rec={};
			else if(count+1>o.maxsel)return alert("您最多只能选择"+o.maxsel+"项！");
			if(!n){for(var e in K) if(v==e){n=K[e];break;}}
			this.rec[v]=n;
			this.draw();
			return true;
		};
		t.del=function(v,b){
			if(!this.rec[v]) return;
			delete this.rec[v];
			if(b)this.draw();
		};
		t.erase=function(v){
			var K=$("pop_sub").getElementsByTagName("INPUT");
			for(var i=0;i<K.length;i++){
				if(K[i].value!=v) continue;
				K[i].checked=false;
				if(i>0) continue;
				var t=$("pop_tbl");if(!t)continue;
				t=F.tag("INPUT",t);
				for(var j=0;j<t.length;j++)t[j].disabled=false;
			}
			this.del(v,true);
		};
		t.draw=function(){
			var s=["<table cellspacing=3>"],index=0;
			for(var e in this.rec){
				if(index==0)s[s.length]="<tr>";
				s[s.length]="<td style='padding-right:10px' nowrap><input type=checkbox checked style='width:15px;height:15px' value="+e;
				s[s.length]="> "+this.rec[e]+"</td>";
				index++;
				if(index==o.col){s[s.length]="</tr>";index=0;}
			}
			if(index>0)for(;index<o.col;index++)s[s.length]="<td></td>";
			s[s.length]="</tr></table>";
			this.innerHTML=s.join('');
			var K=this.getElementsByTagName("INPUT");
			for(var i=0;i<K.length;i++)K[i].onclick=function(){$("pop_sel").erase(this.value);}
		};
		for(var i=0;i<arr.length;i++){if(K[arr[i]])t.add(arr[i]);}
		t=$("pop_sub"),s=[],index=0;
		s[s.length]="<table id=pop_tbl width=100% cellspacing=1 style='margin:2px 0px 3px 7px'>";
		for(var e in K){
			if(index==0)s[s.length]="<tr>";
			s[s.length]="<td><input style='width:15px;height:15px' type="+tag+" name=subitem value="+e;
			s[s.length]=" title=\""+K[e]+"\" data=\""+e+"\"> "+K[e]+"</td>";
			index++;
			if(index>=o.col){s[s.length]="</tr>"; index=0;}
		}
		if(index>0)for(;index<o.col;index++)s[s.length]="<td></td>";
		s[s.length]="</tr></table>";
		t.innerHTML=s.join('');
		var l=t.getElementsByTagName("INPUT");
		var sel=$("pop_sel");
		for(var i=0;i<l.length;i++){
			l[i].onclick=function(){
				if(!this.checked) $("pop_sel").del(this.value,true);
				else if(this.checked&&!$("pop_sel").add(this.value,this.title))this.checked=false;
			};
			if(sel.rec[l[i].value])l[i].checked=true;
		}
		if(pdiv.style.display!="block")F.pop();
	}catch(e){alert(e.message);F.unpop();}
	};
	o.draw=function(){
		if(!this.rec)this.rec={};
		var s=[];
		for(var e in o.rec) s[s.length]=e;
		this.value=s.join(',');
		s=[],count=0;
		for(var e in this.rec){
			s[s.length]="<div style='float:none;overflow:hidden;width:"+(o.offsetWidth-8)+"px;height:17px;margin:0px;padding:0px 2px'>";
			s[s.length]="<span style='float:left'>"+ns(this.rec[e])+"</span><img data="+e+" src='/js/pic/tinyclose.gif' style='display:none;float:right;margin-top:3px'></div>";
		}
		this.innerHTML=s.join('');
		var l=this.getElementsByTagName("DIV");
		for(var i=0;i<l.length;i++){
			count+=l[i].offsetHeight;
			l[i].onmouseover=function(){this.style.background="#f0f0f0";this.getElementsByTagName("IMG")[0].style.display="";};
			l[i].onmouseout=function(){this.style.background="";this.getElementsByTagName("IMG")[0].style.display="none";};
		}
		l=this.getElementsByTagName("IMG");
		for(var i=0;i<l.length;i++){
			l[i].onmouseover=function(){this.src="/js/pic/tinyred.gif"};
			l[i].onmouseout=function(){this.src="/js/pic/tinyclose.gif"};
			l[i].onclick=function(){
				var v=this.getAttribute("data");
				if(o.rec[v]){delete o.rec[v];o.draw();
				Sel_Del(o);}
			};
		}
		this.style.height=parseInt(count<18?18:count)+"px";
	};

	o.inbox=function(v){
		this.value=v;
		this.rec={};
		var arr=v.toString().split(',');
		if(!this.frag){
			for(var i=0;i<arr.length;i++){if(this.K[arr[i]]) this.rec[arr[i]]=this.K[arr[i]];}
			return this.draw();
		}
		var r=this.K[this.start];
		for(var i=0;i<arr.length;i++){
			var n="",p=this.pcode(arr[i]),l=this.K[p];
			if(!l) break;
			if(o.full&&r[p]) n=ns(r[p])+"·";
			if(l[arr[i]]) this.rec[arr[i]]=n+ns(l[arr[i]]);
		}
		this.draw();
		Sel_Add(this);
	};
	o.onclick=function(){
		if(this.pop=="map") {if(!this.value)this.map();}
		else if(this.pop=="simple") {if(!this.value)this.simple();}
		else {if(!this.value)this.sel();}
	};
	if(t=$("btn_"+o.getAttribute("name"))) t.onclick=function(){
		if(o.pop=="map") o.map();
		else if(o.pop=="simple") o.simple();
		else o.sel();
	};
	if(v=o.getAttribute("value")) o.inbox(v);
	return o;
}
function selmap(v){
	var o=$("pop_map");if(!o) return false;
	F.unpop();
	o.source.inbox(v); 
	Check.exam(o.source);
	return false;
}
function showmap(v){
	var o=$("pop_map");if(!o) return false;
	o.source.map(v);o=$("pop");
	if(!F.ie||F.ie7)
	o.style.top=parseInt((screen.height-200-o.offsetHeight)/2)+"px";
}
function showsel(v){
	var o=$("pop_map");if(!o) return false;
	o.source.sel(v);
}
function oksel(){
	var o=$("pop_sel"); if(!o) return false;
	o.source.rec=o.rec;
	o.source.draw();
	F.unpop();
	Check.exam(o.source);
	Sel_Add(o.source);
}
function Sel_Add(o){}
function Sel_Del(o){}