/*
ver 4.0
用法：
	var dc = new drawClass("dc","classid",arr_area,0);
	dc.write();
	
其中可以设定一些参数：
	dc.NullValue = "<option value=\"\">---请选择---</option>"; 这里是有默认值的，为"<option value=\"\">请选择</option>";
	不要的话，直接 dc.NullValue = ""; 就可以了
	
	dc.CssStyle = "width:120px;"; 直接写样式
	dc.CssClass = "classSelect";  直接写class
	
	dc.LoadDataByParentId = 10;	过滤数据功能；可以选定引用哪个parent下面的数据；只显示中国后面的东西；
	dc.MaxDepth = 2;	显示几级，只显示 浙江-杭州； 杭州后面的就不显示了。
*/



function drawClass(_className,_selectName,_Data,_defaultValue){
	this._className = _className;				//创建这个类的名字,一定要传这个值，这里不是 drawClass而是 var dc=new drawClass()中的dc
	this._selectName = _selectName;				//classid
	this._Data = _Data;							//数据数组_Data[n][m] = [id,ClassName,ParentId]
	this._defaultValue = _defaultValue;			//默认值
	this._NowData = this.LoadNowData(_defaultValue);
	this.ParentIdRoute = (this._NowData == null)?[]:[this._NowData[0]];
	
	this.LoadDataByParentId = 0;
	this.MaxDepth = 999;
	this.ShowSelectsCount = 1;				//设置一开始显示多少个select
	this.SelectRows = 1;					//显示Select几行
	this.CssStyle = "";						//样式
	this.CssClass = "";						//样式
	this.NullValue = "<option value=\"\">请选择</option>";	//是否显示 请选择 这个option，直接传入这个HTML代码，如果没有，则传入""
	this.isSingleSelect = false;			//如果为true 则全部的数据都绑定在一个select上。
}
drawClass.prototype.LoadNowData=function(_defaultValue){
	if(_defaultValue == this.LoadDataByParentId || _defaultValue == "")return null;
	for(n in this._Data)
		for(m in this._Data[n])
			if(this._Data[n][m][0] == _defaultValue)
				return this._Data[n][m];
	return null;
}
drawClass.prototype.LoadParentIdRoute=function(_NowData){
	if(_NowData == null || _NowData[2] == this.LoadDataByParentId){
		this.ParentIdRoute.unshift(this.LoadDataByParentId);
		return;	
	}
	var isFound = false;
	//if((this.ParentIdRoute.length+1)==(this.MaxDepth)){}
	for(n in this._Data){
		for(m in this._Data[n]){
			if(this._Data[n][m][0] == _NowData[2]){
				isFound = true;
				this.ParentIdRoute.unshift(this._Data[n][m][0]);
				_NowData = this.LoadNowData(this._Data[n][m][0]);
				break;
			}
		}
		if(isFound)break;
	}
	this.LoadParentIdRoute(_NowData);
}
drawClass.prototype.GetSelect=function(_Data,_selectedValue){
	var tmp;
	tmp = "";
	tmp += '<select size="'+this.SelectRows+'" onChange="changeSelect(this,'+this._className+');"'
	if(this.CssStyle != ""){tmp += ' style="'+this.CssStyle+'"';}
	if(this.CssClass != ""){tmp += ' class="'+this.CssClass+'"';}
	tmp += '>';
	if(this.NullValue!=""){tmp += this.NullValue;	}
	
	for(n in _Data){
		tmp += '<option value="'+_Data[n][0]+'"';
		if(_Data[n][0] == _selectedValue){tmp += ' selected';	}
		tmp += '>'+_Data[n][1]+'</option>';
	}
	tmp += '</select>';
	return tmp;
}
drawClass.prototype.write=function(){
	this.LoadParentIdRoute(this._NowData);
	var  s = "";
	s += '<span id="'+this._selectName+'_" name="'+this._selectName+'_">';
	s += '<input type="hidden" id="'+this._selectName+'" name="'+this._selectName+'" value="'+this._defaultValue+'">';
	for(var n=0; n< this.ParentIdRoute.length; n++){
		if(this.MaxDepth<=n){
			this.ParentIdRoute.splice(n,this.ParentIdRoute.length);
			break;
		}
		if( n == this.ParentIdRoute.length-1 ){
			if(this._Data[this.ParentIdRoute[n]] != undefined)
				s += this.GetSelect(this._Data[this.ParentIdRoute[n]],this._defaultValue);
			else
				this.ParentIdRoute.splice(n,1);
		}
		else
			s += this.GetSelect(this._Data[this.ParentIdRoute[n]],this.ParentIdRoute[n+1]);
	}
	for(var n=0;n<(this.ShowSelectsCount-this.ParentIdRoute.length);n++)
		s += this.GetSelect([],"");
	s += '</span>';
	document.write(s);
}
drawClass.prototype.$=function(_id){return document.getElementById(_id);}
drawClass.prototype.GetValueByDepth=function(_depth){
	try{
		var oSelect;
		oSelect = this.$(this._selectName+"_").childNodes(_depth);
		return oSelect.options[oSelect.selectedIndex].value;
	}
	catch(e){
		return "";
	}
}
drawClass.prototype.GetTextByDepth=function(_depth){
	try{
		var oSelect;
		oSelect = this.$(this._selectName+"_").childNodes(_depth);
		return oSelect.options[oSelect.selectedIndex].text;
	}
	catch(e){
		return "";
	}
}
drawClass.prototype.GetValueByLastOne=function(_isTrueLast){
	//_isTrueLast:是否确定是最后一个！
	try{
		var _length=(this.NullValue=="") ?  0 :  1;
		var Childs = this.$(this._selectName+"_").childNodes;
		if(_isTrueLast){
			for(var i=Childs.length;i>0;i--){
				if(Childs[i-1].options.length==_length)
					continue;
				else
					return Childs[i-1].value;
			}
		}else{
			return this.$(this._selectName).value;
		}
	}
	catch(e){
		return "";
	}
}
drawClass.prototype.GetTextByLastOne=function(_isTrueLast){
	//_isTrueLast:是否确定是最后一个！
	try{
		var _length=(this.NullValue=="") ?  0 :  1;
		var Childs = this.$(this._selectName+"_").childNodes;
		if(_isTrueLast){
			for(var i=Childs.length;i>0;i--){
				if(Childs[i-1].options.length==_length)
					continue;
				else
					return Childs[i-1].options[Childs[i-1].selectedIndex].text;
			}
		}else{
			return this.$(this._selectName).value;
		}
	}
	catch(e){
		return "";
	}
}
drawClass.prototype.GetRoute=function(_sFormat){
	try{
		//_sFormat = "{value},{text}"
		var oSelect = this.$(this._selectName+"_");
		var s = [], tmp = "";
		for(var i=1;i<oSelect.childNodes.length;i++){
			tmp = _sFormat;
			tmp = tmp.replace(/\{value\}/gi,oSelect.childNodes(i).value);
			tmp = tmp.replace(/\{text\}/gi,oSelect.childNodes(i).options[oSelect.childNodes(i).selectedIndex].text);
			s.push(tmp);
		}
		return s;
	}
	catch(e){
		return s;
	}
}

drawClass.prototype.SetValueByDepth=function(_depth,_value){
	try{
		var oSelect;
		oSelect = this.$(this._selectName+"_").childNodes(_depth);
		oSelect.value = _value;
		changeSelect(this.$(this._selectName+'_').childNodes(1),this)
	}
	catch(e){
	}
}

function removeSelect(_select,_className){
	var oSpan = _className.$(_className._selectName+"_");
	for(var i=oSpan.children.length;i>1;i--){
		if(oSpan.childNodes(i-1)==_select){
			break;
		}
		else{
			//开始判断；如果>ShowSelectsCount 则直接remove;否则执行清空select
			if(_className.ShowSelectsCount>=(i-1)){
				if(_className.NullValue == ""){
					oSpan.childNodes(i-1).innerHTML="";
				}else{
					for(var n=oSpan.childNodes(i-1).length;n>=1;n--){oSpan.childNodes(i-1).options[n]=null;}
				}
				oSpan.childNodes(i-1).selectedIndex=-1;
			}
			else{
				oSpan.removeChild(oSpan.childNodes(i-1));
			}
		}
	}
}

function changeSelect(_select,_className){
	var selectedValue = _select.value;
	var oSpan = _className.$(_className._selectName+"_");
	if(selectedValue == "" || _className._Data[selectedValue] == undefined){
		removeSelect(_select,_className);
		//return;
	}
	else{
		if(oSpan.lastChild == _select){
			//appendChild
			if(oSpan.childNodes<_className.MaxDepth){
				var oSelect = document.createElement("SELECT");
				oSelect.outerHTML = _className.GetSelect(_className._Data[selectedValue],selectedValue);
				oSpan.appendChild(oSelect);
			}
		}
		else{
			//removeChild And modify child  
			var _selectIndex = 1;
			var _selectNextNode = null;
			for(var i=1;i<oSpan.children.length;i++){
				if(oSpan.childNodes(i)==_select){
					_selectIndex = i;
				}				
			}
			_selectNextNode = oSpan.children(_selectIndex+1);
			removeSelect(_selectNextNode,_className);
			if(_selectIndex<_className.MaxDepth){
				//直接引用下面这局话，下拉框会有闪烁
				//_selectNextNode.outerHTML = _className.GetSelect(_className._Data[selectedValue],selectedValue);
				//但是引用下面这种方法，_selectNextNode.scrollTop有问题，值为0，但效果不为0；
				var tmp="",i=0;
				if(_className.NullValue!=""){i++;_selectNextNode.selectedIndex=-1;_selectNextNode.scrollTop=0;}
				for(var n=_selectNextNode.length;n>=i;n--){_selectNextNode.options[n]=null;}
				for(n in _className._Data[selectedValue]){
					_selectNextNode.add(new Option(_className._Data[selectedValue][n][1],_className._Data[selectedValue][n][0]));
				}
			}
		}
	}
	_className.$(_className._selectName).value = selectedValue;
	
	//通用接口
	ChangeApplication(_className)
}
