/*
	创建下拉框
*/
var _webUI=function(Id,setValueInputId,setTextInputId){
	var _this = this;
	var valueArray = new Array();//所有选项的值
	var textArray = new Array();//所有选项的文本内容
	_this.value = "";//默认的值
	_this.text = "--请选择--";//默认显示的文本

	_this.inputWidth = "100%";//选择框宽度
	_this.width = "100%";//下拉菜单宽度
	_this.height = "auto";
	_this.zIndex = 999;
	if(Id==undefined) Id = "ui-select";
	_this.id = Id;
	_this.className = "ui-select";
	_this.textClassName = "ui-select-text";
	_this.selectValue = "";//选择的值
	_this.selectText = "";//选择的文本
	_this.createInput = 0;//是否创建隐藏表单 0:不创建;1:创建一个(保存值);2:创建两个(保存值和文本)
	_this.valueInputId = _this.id + "-value";//保存值的隐藏表单名
	_this.textInputId = _this.id + "-text";//保存文本的隐藏表单名
	_this.isChange = false;//选择的值是否已经改变
	_this.onChange = new Function();//设置onChange事件
	_this.createTag = function(tagName){
		return document.createElement(tagName);
	};
	_this.removeTag = function(obj){
		document.body.removeChild(obj);
	};
	_this.$ = function(Id){
		return document.getElementById(Id)?document.getElementById(Id):null;
	};
	//展开下拉单内容
	_this.show=function(obj){
		obj.style.display="block";
		//alert(_this.ulObj.style.display);
	};
	var ltrim=function(val){return val.replace(/(^\s*)/g,"");} ;
	var rtrim=function(val){return val.replace(/(\s*$)/g,"");}; 
	var trim=function trim(val){return rtrim(ltrim(val));};

	//关闭下拉单内容
	_this.close=function(obj){
		obj.style.display="none";
	};

	//设置选项的值
	_this.append=function(value,text){
		if(text==undefined) text = value;
		if(valueArray.length<=0){
			valueArray.push(_this.value);
			textArray.push(_this.text);
		}
		valueArray.push(value);
		textArray.push(text);
	};
	//获取选中的值和文本
	_this.getValue=function(){
		return _this.selectValue;
	};
	_this.getText=function(){
		return _this.selectText;
	};
	var valueinput = textinput = false;
	_this.dbObj = false;
	var _showTextObj=false;
	_this._selectObj=false;
	//设置当前选中的值
	var setCurrSelect=function(obj,isOnChange){
		var value = obj.getElementsByTagName("DIV")[0].innerHTML;
		var text = obj.getElementsByTagName("DIV")[1].innerHTML;
		if(_this.selectValue!=value){
			_this.isChange = true;//选中的值已经改变
		}
		_this.selectValue = value;//选择的值
		_this.selectText = text;//选择的文本
		_showTextObj.innerHTML = text;
		if(_this.createInput==1){
			valueinput.value=value;
		}else if(_this.createInput>1){
			textinput.value=text;
		}
		//setValueInputId,setTextInputId
		if(setValueInputId!=undefined){
			_this.$(setValueInputId).value=value;
		}
		if(setTextInputId!=undefined){
			_this.$(setTextInputId).value=text;
		}
		if(_this.isChange && isOnChange) _this.onChange();//触发onChange事件
		//alert(value);
	};
	var setSelect=function(obj){
		setCurrSelect(obj,true);
	};
	//
	var getEvent=function(event){
		return event ? event : (window.event ? window.event : null);
	};
	//
	_this.divclick=function(event){
		event = getEvent(event);
		if(document.all) event.target=event.srcElement;
		//alert(event.target.id + "/" + _this._selectObj.id);
		if(event.target!=_showTextObj && event.target!=_this._selectObj) _this.close(_this.dbObj);
	};
	//创建UL
	_this.ulObj = _this.createTag("UL");
	//清除数组
	_this.clear=function(index,len){
		if(index==undefined) index = 1;
		if(len==undefined) len = valueArray.length-index;
		if(valueArray.length>0){
			valueArray.splice(index,len);
			textArray.splice(index,len);
		}
	};
	//此方法必须在调用_this.inti()方法后再调用
	_this.setSelectValue=function(val,isChange){
		var nodes=_this.ulObj.getElementsByTagName("LI");
		if(isChange==undefined) isChange = false;
		for(var i=0;i<nodes.length;i++){
			var value = nodes[i].getElementsByTagName("DIV")[0].innerHTML;
			if(trim(value.toLowerCase())==trim(val.toString().toLowerCase())){
				setCurrSelect(nodes[i],isChange);
			}
		}
	};
	//重置
	_this.resetOption=function(){
		_this.ulObj.innerHTML="";
		for(var i=0;i<valueArray.length;i++){
			//alert(valueArray.length);
			var _liObj = _this.createTag("LI");
			if(i==0){
				_liObj.style.textAlign="center";
				_liObj.className="select";
			}
			var _valueObj = _this.createTag("DIV");
			_valueObj.style.display="none";
			var __value=document.createTextNode(valueArray[i]);
			_valueObj.appendChild(__value);//值
			
			var _textObj = _this.createTag("DIV");
			var __text=document.createTextNode(textArray[i]);
			_textObj.appendChild(__text);//文本
			
			_liObj.title=textArray[i];//鼠标提示
			_liObj.appendChild(_valueObj);
			_liObj.appendChild(_textObj);
			
			if(i!=0){
				_liObj.onmouseover=function(){
					this.className="select";
				};
				_liObj.onmouseout=function(){
					this.className="";
				};
			}
			_liObj.onclick=function(){
				setSelect(this);
			};
			_this.ulObj.appendChild(_liObj);
			if(i==0) setSelect(_liObj);
			//alert(valueArray.length);
		}
		
	};
	//初始化
	_this.init = function(parentObj){
		if(parentObj==undefined) parentObj = document.body;
		if(_this.createInput==1){
			//保存value值(隐藏)
			valueinput = _this.createTag("input");
			valueinput.type = "hidden";
			valueinput.id = _this.valueInputId;
			valueinput.name = _this.valueInputId;
		}else if(_this.createInput>1){
			//保存text值(隐藏)
			textinput = _this.createTag("input");
			textinput.type = "hidden";
			textinput.id = _this.textInputId;
			textinput.name = _this.textInputId;
		}
		//用来显示的选择框
		var obj = _this.createTag("DIV");
		_this._selectObj = obj;
		obj.id = _this.id;
		obj.className = _this.className;
		obj.style.zIndex = _this.zIndex;
		obj.style.position="relative";
		obj.style.float="none";
		obj.style.clear="both";
		//obj.style.zIndex="999";
		obj.style.width=_this.inputWidth;
		//obj.style.width=_this.width + "px";
		//obj.style.height=_this.height + "px";
		obj.style.cursor="pointer";
		var textObj = _this.createTag("DIV");
		_showTextObj = textObj;
		textObj.title="单击此处展开/关闭选项";//鼠标提示
		textObj.className=_this.textClassName;
		//textObj.style.textAlign="center";
		textObj.style.textIndent="5px";
		textObj.style.height="20px";
		//textObj.style.width="100%";
		textObj.style.overflow="hidden";
		//
		var oText=document.createTextNode(_this.text);
		textObj.appendChild(oText);//文本标签
		//初始化所有选项
		_this.dbObj = _this.ulObj;
		_this.ulObj.style.display="none";
		_this.ulObj.style.width=_this.width;//选择列表的宽度
		_this.ulObj.style.height=_this.height;//选择列表的高度
		//
		_this.ulObj.style.padding = 0;
		_this.ulObj.style.margin = 0;
		//alert(valueArray.length);
		for(var i=0;i<valueArray.length;i++){
			//alert(valueArray.length);
			var _liObj = _this.createTag("LI");
			if(i==0){
				_liObj.style.textAlign="center";
				_liObj.className="select";
			}
			//
			var _valueObj = _this.createTag("DIV");
			_valueObj.style.display="none";
			var __value=document.createTextNode(valueArray[i]);
			_valueObj.appendChild(__value);//值
			
			var _textObj = _this.createTag("DIV");
			var __text=document.createTextNode(textArray[i]);
			_textObj.appendChild(__text);//文本
			
			_liObj.title=textArray[i];//鼠标提示
			_liObj.appendChild(_valueObj);
			_liObj.appendChild(_textObj);
			
			if(i!=0){
				_liObj.onmouseover=function(){
					this.className="select";
				};
				_liObj.onmouseout=function(){
					this.className="";
				};
			}
			_liObj.onclick=function(){
				setSelect(this);
			};
			_this.ulObj.appendChild(_liObj);
			if(valueArray[i]==_this.selectValue) setSelect(_liObj);
			//alert(valueArray.length);
		}
		//_liObj.onmouseover
		//新建的标签插入页面
		if(_this.createInput==1){
			obj.appendChild(valueinput);
		}else if(_this.createInput>1){
			obj.appendChild(textinput);
		}
		obj.appendChild(textObj);
		obj.appendChild(_this.ulObj);
		//var 
		parentObj.style.background="#fff";
		parentObj.appendChild(obj);
		//alert(parentObj.innerHTML);
		parentObj.onclick=function(){
			if(_this.ulObj.style.display=="none")
				_this.show(_this.ulObj);
			else
				_this.close(_this.ulObj);
		};
		Utils.addEventHandler(document,"click",function(event){
						_this.divclick(event);
						});
	};
};