﻿function ns(s){if(!s) return "";return typeof(s)=="object"?s[0]:s;}
var Tran={
To10:function(A){
	var len = A.length, B = new Array(len + 1);
	for(var i=0;i<B.length;i++) B[i]=0;
	for (var i = 0; i < len; i++){
		for (var k = len; k >= 0; k--) B[k] *= 16;
		B[len] += A[i];
		for (var k = len; k >= 1; k--){B[k - 1] += Math.floor(B[k] / 10);B[k] %= 10;}
	}
	for(var i=0;i<B.length;i++){if(B[i]>0) break; else B[i]='';}
	return B.join('');
},
To16:function(s){
	s = s.toString();
	var len=s.length, A = new Array(len), B = new Array(len);
	len--;
	for(var i=0;i<s.length;i++)A[i]=s.charCodeAt(i)-48;
	for(var i=0;i<B.length;i++) B[i]=0;
	for (var i = 0; i < A.length; i++){
		for (var k = len; k >= 0; k--) B[k] *= 10;
		B[len] += A[i];
		for (var k = len; k >= 1; k--){	B[k - 1] += Math.floor(B[k] / 16);	B[k] %= 16;	}
	}
	return B;
},
And:function(a,b){ return Tran.And2(Tran.To16(a),Tran.To16(b)); },
And2:function(A,B){
	var len = A.length>B.length?B.length:A.length; arr = new Array(len);
	for(var i=1;i<=arr.length;i++) arr[arr.length-i]=A[A.length-i]&B[B.length-i];
	return Tran.To10(arr);
}};
function init_code(o){
	o.refer=o.getAttribute("refer");
	o.pop=o.getAttribute("pop");
	o.code= o.pop=="simple" ? false : true;
	o.w=o.getAttribute("w");
	o.start=parseInt(o.getAttribute("start"));
	if(o.getAttribute("flag")) o.flag=parseInt(o.getAttribute("flag"));
	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.limit=o.getAttribute("limit");
	o.set=function(v){ this.data=this.value=v; this.put(v); if(this.inbox)this.inbox(v);};
	if(o.code){
		o.pcode=function(v,d){
			if(d==null) d=this.level(v)-1;
			if(d<=0) return "0";
			return Tran.And(v,C[this.refer].mask[d-1])
		};
		o.level=function(v){
			if (!v||v<=0) return 0;
			var k=C[this.refer].mask, m=k.length;
			for (var i=0;i<m;i++)if(v==Tran.And(v,k[i])) return i+1;
			return 0;
		};
	}
	if(o.pop) {
		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);
			var kk=C[this.refer].code, l=kk[v]; 
			if(!l){
				end-=1;
				v=this.pcode(v,end);
				l=C[this.refer].code[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=kk[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 C[this.refer].code[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=C[this.refer].code[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(kk[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=[],index=0;
			if(!K[this.refer]) return alert("没有找到该"+(this.title?this.title:"类别")+"！");
			s.push("<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.push("<div id=pop_sub></div>");
			if(this.maxsel>1){
				s.push("<div style='color:#666;border-bottom:1px solid #999;padding-bottom:3px;overflow:hidden;margin:5px 8px'>您已经选择了以下类别：（您最多可以选择 "+o.maxsel+" 项）</div>");
				s.push("<div id=pop_sel style='margin:3px 5px 8px 5px;line-height:16px'></div>");
			}
			else s.push("<div class='line' style='margin:8px 0'></div>");
			s.push("<div style='text-align:right;margin-right:30px'><input type=button class=btn style='padding:0px 10px;font-size:12px' value=确定 />&nbsp;");
			s.push("<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('');
			var btn = t.getElementsByTagName("INPUT");
			btn=btn[btn.length-2];
			btn.source=this;
			btn.onclick=function(){
				var o=$("pop_sel");
				if(this.source.getAttribute("stop")) return alert(this.source.getAttribute("stop"));
				if(o) this.source.rec=o.rec;
				this.source.draw();
				F.unpop();
				Check.exam(this.source);
				if(this.source.onchange)this.source.onchange();
			}
			t=$("pop_sel");
			if(t){
				t.source=this;t.rec={};
				t.add=function(v,n){
					if(!v)return;
					if(this.rec[v]) return true;
					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[this.refer]) if(v==e){n=K[this.refer][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 l=$("pop_sub").getElementsByTagName("INPUT");
					for(var i=0;i<l.length;i++){
						if(l[i].value!=v) continue;
						l[i].checked=false;
						var span = $$(Dom.top(l[i],"TD"),"SPAN")[0];
						span.style.color="black";
						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 id='pop_picked' 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]="> <span>"+this.rec[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>";
					this.innerHTML=s.join('');
					var l=this.getElementsByTagName("INPUT");
					for(var i=0;i<l.length;i++)l[i].onclick=function(){$("pop_sel").erase(this.value);}
					l=this.getElementsByTagName("SPAN");
					for(var i=0;i<l.length;i++)l[i].onclick=function(){$$(Dom.top(this,"TD"),"INPUT")[0].click();}
				};
				//for(var i=0;i<arr.length;i++){if(K[this.refer][arr[i]])t.add(arr[i]);}
			}
			t=$("pop_sub"),s=[],index=0;
			s[s.length]="<table id=pop_tbl width=100% cellspacing=0 cellpadding=0 style='margin:2px 0px 3px 7px'>";
			for(var e in K[this.refer]){
				if(index==0)s[s.length]="<tr>";
				s.push("<td><input style='margin-bottom:-2px;margin-right:3px' type="+tag+" name=subitem value="+e+" title=\""+K[this.refer][e]+"\" data=\""+e+"\"><span>"+K[this.refer][e]+"</span></td>");
				index++;
				if(index>=o.col){s.push("</tr>"); index=0;}
			}
			if(index>0)for(;index<o.col;index++)s.push("<td></td>");
			s.push("</tr></table>");
			t.innerHTML=s.join('');
			var sel=$("pop_sel"),l=t.getElementsByTagName("INPUT");
			for(var i=0;i<l.length;i++){
				l[i].onclick=function(){
					var p=$("pop_sel");
					if(p){
						if(!this.checked) p.del(this.value,true);
						else if(this.checked&&!p.add(this.value,this.title))this.checked=false;
					}
					else{
						o.rec={};
						o.rec[this.value]=this.title;
					}
					var l=$$($("pop_sub"),"SPAN");
					for(var i=0;i<l.length;i++){
						var p=$$(Dom.top(l[i],"TD"),"INPUT")[0];
						l[i].style.color=p.checked?"red":"black";
					}
					this.blur();
				};
				if(sel&&sel.rec[l[i].value])l[i].click();
				else if(o.rec&&o.rec[l[i].value])l[i].click();
			}
			l=t.getElementsByTagName("SPAN");
			for(var i=0;i<l.length;i++){
				l[i].onclick=function(){
					$$(Dom.top(this,"TD"),"INPUT")[0].click();
				}
			}
			if(pdiv.style.display!="block")F.pop();
		//}catch(e){alert(e.message);F.unpop();}
		};
		
		var note="<span style='margin-left:20px;color:#999'>（ＣＴＲＬ＋Ｆ可以进行查找）</span>";
		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();
					if(o.onchange)o.onchange();}
				};
			}
			this.style.height=parseInt(count<18?18:count)+"px";
		};

		o.style.cursor="pointer";
		o.put=function(v){
			var kk=this.pcode?C[this.refer].code:K[this.refer];
			this.value=v;
			this.rec={};
			var arr=v.toString().split(',');
			if(!this.pcode){
				for(var i=0;i<arr.length;i++){if(kk[arr[i]]) this.rec[arr[i]]=kk[arr[i]];}
				return this.draw();
			}
			for(var i=0;i<arr.length;i++){
				var n="",d=this.level(arr[i]),p=this.pcode(arr[i],d-1),l=kk[p];
				if(!l) break;
				var x=this.pcode(p,d-2);
				while(o.full && d>1 && kk[x]){
					n=ns(kk[x][p])+"·"+n;
					d--;
					p=this.pcode(p,d-1);
					x=this.pcode(p,d-2);
				}
				if(l[arr[i]]) this.rec[arr[i]]=n+ns(l[arr[i]]);
			}
			this.draw();
			if(this.onchange)this.onchange();
		};
		o.onclick=function(){
			if(o.getAttribute("stop")) return alert(o.getAttribute("stop"));
			if(this.pop=="map") {if(!this.value)this.map();}
			else {if(!this.value)this.sel();}
		};
		if(t=$("btn_"+o.getAttribute("name"))) t.onclick=function(){
			if(o.getAttribute("stop")) return alert(o.getAttribute("stop"));
			if(o.pop=="map") o.map();
			else o.sel();
		};
	}
	else if(o.getAttribute("op")=="select"){
		o.kid=[];
		var sels=o.getElementsByTagName("SELECT")
		for(var i=0;i<sels.length;i++)o.kid.push(sels[i]);
		o.clear=function(v){
			for(var i=v+1;i<this.kid.length;i++){
				var m = parseInt(this.kid[i].getAttribute("min"));
				this.kid[i].length=isNaN(m)?0:m;
			}
		}
		o.seek=function(v){
			if(v>=this.kid.length-1&&this.kid[v].value) return;
			this.clear(v);
			var l=C[this.refer].code[this.kid[v].value];
			if(!l) return;
			for(var e in l){
				this.kid[v+1].options[this.kid[v+1].length]=new Option(ns(l[e]),e);
			}
		};
		o.put=function(v){
			var t=this.getAttribute("need"),kk=C[this.refer].code;
			this.value=void(0);
			//this.fill();
			for(var i=0;i<this.kid.length;i++)
			{	var p=this.pcode(v,this.level(this.start)+i+1);
				this.kid[i].value=p;
				this.value=v;
				this.seek(i);
			}
			//if(this.onchange)this.onchange();
		};
		o.fill=function(){
			this.value=void(0);
			var kk=C[this.refer].code,l=kk[this.start];
			if(!l) return;
			for(var i=0;i<this.kid.length;i++){
				var m = parseInt(this.kid[i].getAttribute("min"));
				this.kid[i].length=isNaN(m)?0:m;
			}
			for(var e in l) this.kid[0].options[this.kid[0].length]=new Option(ns(l[e]),e);
		}
		if(o.kid.length==0) return;
		o.fill();
		for(var i=0;i<o.kid.length;i++){
			o.kid[i].index=i;
			o.kid[i].onchange=function(){
				o.value=void(0);
				o.seek(this.index);
				var t=o.getAttribute("need");
				if(!t) o.value=this.value;
				else if(!C[o.refer].code[this.value]) o.value=this.value;
				//if(o.onchange)o.onchange();
			};
		}		
	}
	else{
		o.kid=[];
		var divs=o.getElementsByTagName("DIV");
		for(var i=0;i<divs.length;i++){
			if(divs[i].getAttribute("group")=="select"){
				init_select(divs[i]);
				divs[i].setAttribute("stop",o.getAttribute("stop"));
				o.kid.push(divs[i]);
			}
		}
		o.stop=function(v){for(var i=0;i<this.kid.length;i++)this.kid[i].setAttribute("stop",v);}
		o.clear=function(v){for(var i=v+1;i<this.kid.length;i++)this.kid[i].clear();}
		o.seek=function(v){
			if(v>=this.kid.length-1&&this.kid[v].value) return;
			this.clear(v);
			var l=C[this.refer].code[this.kid[v].value];
			if(!l) return;
			for(var e in l){
				if(o.flag==void(0) || l[e][1]==o.flag)
				this.kid[v+1].add(ns(l[e]),e);
			}
		};
		o.put=function(v){
			var t=this.getAttribute("need"),kk=C[this.refer].code;
			this.value=void(0);
			//this.fill();
			for(var i=0;i<this.kid.length;i++)
			{	var p=this.pcode(v,this.level(this.start)+i+1);
				this.kid[i].put(p);
				if(!t) this.value=v;
				else if(!kk[v]) this.value=v;
				this.seek(i);
			}
			if(this.onchange)this.onchange();
		};
		o.fill=function(){
			this.value=void(0);
			var kk=C[this.refer].code,l=kk[this.start];
			if(!l) return;
			for(var i=0;i<this.kid.length;i++) this.kid[i].clear();
			for(var e in l){
				//if(o.flag==void(0) || l[e][1]==o.flag)
				this.kid[0].add(ns(l[e]),e);
			}
		}
		if(o.kid.length==0) return;
		o.fill();
		for(var i=0;i<o.kid.length;i++){
			o.kid[i].index=i;
			o.kid[i].onchoose=function(){
				o.value=void(0);
				o.seek(this.index);
				var t=o.getAttribute("need");
				if(!t) o.value=this.value;
				else if(!C[o.refer].code[this.value]) o.value=this.value;
				if(o.onchange)o.onchange();
			};
		}
	}
	o.get=function(){
		var v=this.value,t=this.getAttribute("need");
		if(t&&!v){
			Check.warn(this,{message:t});
			throw this.error;
		}
		return v;
	};
	if(v=o.getAttribute("value")) o.put(v);
	return o;
}
function selmap(v){
	var o=$("pop_map");if(!o) return false;
	F.unpop();
	o.source.put(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);
}