showlib
////////////////////////////////////////
var dataobj;
var curr_deep=1;
var popbindobj;

function getpoplibpos(){
	var _width = $("libpop").offsetWidth;
	var _height = $("libpop").offsetHeight;

	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();

	//var theBody = document.documentElement;
	var theBody = document.getElementsByTagName("BODY")[0];
	//theBody.style.overflow = "hidden";
	var scTop = parseInt(theBody.scrollTop,10);
	var scLeft = parseInt(theBody.scrollLeft,10);
	var _top=getPos(popbindobj,"Top")+popbindobj.offsetHeight;

	//alert("scTop="+scTop+"&fullHeight="+fullHeight+"&_top="+_top+"_height="+_height+"&"+(scTop+fullHeight-_top-_height));
	if(scTop+fullHeight-_top-_height<0){
		$("libpop").style.top = (scTop + (fullHeight - _height)) + "px";
	}else{
		$("libpop").style.top =_top + "px";;
	}

	var _left=getPos(popbindobj,"Left");
	//alert("scLeft="+scLeft+"&fullWidth="+fullWidth+"&_left="+_left+"_width="+_width+"&"+(fullWidth-_left-_width<0));
	if(fullWidth-_left-_width<0){
		$("libpop").style.left =  (scLeft + (fullWidth - _width)) + "px";
	}else{
		$("libpop").style.left = _left + "px";
	}

}
/*
bindobj
_lib:要调用的lib名称
_width:弹出窗口的宽度
_cols:几列数据
_align:单列数据的对齐方式
_deep:几级选择
_parent:多级选择的父级id
*/
function showlib(bindobj,_lib,_width,_cols,_align,_deep,_parent){
	curr_deep=1;

	var libpop = $("libpop");
	if(libpop==null){
		libpop=document.createElement('div');
		libpop.setAttribute("id","libpop");
		libpop.setAttribute("class","libpop");
		libpop.setAttribute("className","libpop");
		libpop.style.display="none";
		libpop.innerHTML='<table border=0 class="head" align="center" width="100%"><tr><td id="headtext">&nbsp;</td><td align="right" nowrap id="headopt">[<a href="#" onClick="hiddenlibpop();return false;">关闭</a>]</td></tr></table><div class="data" id="datahtml"></div>';
		var body=document.getElementsByTagName("BODY")[0];
		body.appendChild(libpop);
	}

	if(_width){libpop.style.width=_width+"px";}
	_cols=parseInt(_cols);
	if(_cols<1 || isNaN(_cols)){_cols=1;}
	if(!_align){_align="center";}
	_deep=parseInt(_deep,10);
	if(_deep<1 || isNaN(_deep)){_deep=1;}


	popbindobj=bindobj;
	dataobj=_lib;
	var _title=popbindobj.getAttribute("helptext");

	var _default=bindobj.getAttribute("helptext");
	if(!_default){_default="不限";}
	if(!_title){_title=popbindobj.getAttribute("title");}

	if(bindobj.getAttribute("value") && bindobj.getAttribute("value")!=_title && bindobj.getAttribute("value").indexOf("不限")<0){

		if(_title.indexOf("城市")>=0){
			_title = "查找提示，其他城市请直接输入中文";
		}
		$("headtext").innerHTML=_title+': <b>'+bindobj.getAttribute("value")+'</b>';
		$("headopt").innerHTML='[<a href="#" onclick="libclear(\''+_default+'\');return false;">清除</a>]&nbsp;[<a href="#" onClick="hiddenlibpop();return false;">关闭</a>]';
	}else{

		if(_title.indexOf("城市")>=0){
			_title = "查找提示，其他城市请直接输入中文";
		}
		$("headtext").innerHTML=_title+': <b>'+bindobj.getAttribute("value")+'</b>';;
		$("headopt").innerHTML='[<a href="#" onClick="hiddenlibpop();return false;">关闭</a>]';
	}
	libpop.style.display="";


	/*
	if(Math.floor(i%_cols)){
	for(var j=0;j<(_cols-Math.floor(i%_cols));j++){
	datahtml+='<td>&nbsp;</td>';
	}}
	*/

	$("datahtml").innerHTML=mkdata(_parent,_width,_cols,_align,_deep);
	//alert(_width);
	getpoplibpos();
}

function libclear(dfvalue){
	setLibValue("",dfvalue);
	hiddenlibpop();
}
function mkdata(_parent,_width,_cols,_align,_deep){
	
	var _lib=dataobj;
	var datahtml='<table border="0" width="99%"><tr>';
	var j=0;
	for(var i=0;i<_lib.length;i++){
		if(_lib[i][2]==_parent ||(!_lib[i][2] && !_parent)){
			_text=_lib[i][0];
			//if(_text=="清空"){_text='';}
			_value=_lib[i][1];
			if(j%_cols===0 && j!==0){datahtml+='</tr><tr>';}
			if(hasson(_value,curr_deep,_deep)){
				datahtml+='<td align="'+_align+'" title="'+_text+'" width="'+(_width/_cols-1)+'"><a href="#" onClick="showsub(\''+_text+'\',\''+_value+'\','+_width+','+_cols+',\''+_align+'\','+_deep+');return false">'+_text+'</a></td>';
			}else{
				datahtml+='<td align="'+_align+'" title="'+_text+'" width="'+(_width/_cols-1)+'"><a href="#" onclick="libClick(this,\''+_value+'\');return false;">'+_text+'</a></td>';
			}
			j++;
		}
	}
	datahtml+='</tr></table>';
	return datahtml;
}

//是否有子项
function hasson(_value,curr_deep,all_deep){
	if(all_deep<2){return false;}
	if(curr_deep>=all_deep){return false;}

	var len=dataobj.length;
	var num=0;
	for(var i=0;i<len;i++){
		if(_value==dataobj[i][(curr_deep+1)]){num++;}
		if(num>1){return true;}
	}
	return false;
}


function setLibValue(_value,_text){
	popbindobj.value=_text;
	//代码值填入代码隐藏域
	//var bind=popbindobj.getAttribute("codeinput");
	//if($(bind)){$(bind).value=_value;}

	var frm=popbindobj.form.name;
	if(popbindobj.name.indexOf("_text")!=-1){
		var bind=popbindobj.name.replace("_text","");
		if(_value)
		document.forms[frm][bind].value=_value;
	}

	//隐藏或显示手动输入的输入框
	var def=popbindobj.getAttribute("def");
	if(_value=="-2"){
		var binddef=document.forms[frm][bind+"_def"];
		if(binddef){
			binddef.style.display="";
			binddef.value=binddef.getAttribute("helptext");
			binddef.select();
		}
	}else{
		var binddef=document.forms[frm][bind+"_def"];
		if(binddef){
			binddef.value="";
			binddef.style.display="none";
		}
	}

	//popbindobj.value=obj.getAttribute("value");
}

function libClick(obj,_value){
	alert(obj.firstChild.nodeValue);
	setLibValue(_value,obj.firstChild.nodeValue);
	
}

function showsub(_parentText,_parentValue,_width,_cols,_align,_deep){
	curr_deep++;
	var datahtml='<table border="0" width="100%"><tr><td colspan='+_cols+'><b>'+_parentText+'</b>(<a href="#" onclick="returnparent(\'\','+_width+','+_cols+',\''+_align+'\','+_deep+');return false">返回上级</a>)</td></tr></table>';
	datahtml+=mkdata(_parentValue,_width,_cols,_align,_deep);
	$("datahtml").innerHTML=datahtml;
}

function returnparent(_parent,_width,_cols,_align,_deep){
	curr_deep--;
	$("datahtml").innerHTML=mkdata(_parent,_width,_cols,_align,_deep);
}


function hiddenlibpop(el){

	curr_deep=1;
	var m=$("libpop");
	if (el && m && m.style.display=='') {
		if ((document.body.scrollLeft + window.event.x > el.offsetLeft)
		&& (document.body.scrollLeft + window.event.x < el.offsetLeft + el.offsetWidth)
		&& (document.body.scrollTop + window.event.y > el.offsetTop)
		&& (document.body.scrollTop + window.event.y < el.offsetTop + el.offsetHeight)
		|| (document.body.scrollLeft + window.event.x > m.offsetLeft)
		&& (document.body.scrollLeft + window.event.x < m.offsetLeft + m.offsetWidth)
		&& (document.body.scrollTop + window.event.y > m.offsetTop)
		&& (document.body.scrollTop + window.event.y < m.offsetTop + m.offsetHeight)) {
		} else {
			m.style.display = 'none';
		}
	}else if(!el){
		m.style.display = 'none';
	}
}
