//下拉框选项对象

var SelectOptions = function(_value,_text,_textValue){
	this.value = _value;
	this.textValue = _textValue;
	this.text =_text;
	this.childs =null;
	this.parent=null;
	this.setChilds	= function(childs){
		try{
		for(var i=0 ;i<childs.length; i++){
				childs[i].parent=this;
			}
		this.childs = childs;
	}catch(e){}
	}
	
	this.getChilds= function(){return this.childs;}
	
	this.getChildsByValue = function(value){
			for(var i = 0; i < this.childs.length; i++){
				if(this.childs[i].value == value)
					return this.childs[i];
			}
			return null;
		}

	this.getChildsByTextValue = function(textValue){
		for(var i=0; i < this.childs.length; i++){
			if(this.childs[i].textValue == textValue)
				return this.childs[i];
		}
		return null;
	}
}

var SelectOptionsUtil = new Object();
SelectOptionsUtil.getChilds= function(opt){return opt.childs;}
SelectOptionsUtil.getChildsByValue = function(value,opt){
		for(var i = 0; i < opt.childs.length; i++){
			if(opt.childs[i].value == value)
				return opt.childs[i];
		}
		return null;
	}

SelectOptionsUtil.getChildsByTextValue = function(textValue,opt){
	for(var i=0; i < opt.childs.length; i++){
		if(opt.childs[i].textValue == textValue)
			return opt.childs[i];
	}
	return null;
}

SelectOptionsUtil.getLevel = function(opt){
	var	ind = opt.parent;
	if(ind == null)
		return 0;
	i = 1;
	while(ind!=null && ind.value!=-1){
		ind = ind.parent;
		i++;
	}
	return i;
}

SelectOptionsUtil.getPath = function(opt){
	var path = opt.text;
	var ind = opt.parent;
	while(ind!=null && ind.value!=-1){
		path = ind.text +" >> " +path;
		ind = ind.parent;
	}
	return path;
}
SelectOptionsUtil.searchIndustry = function(array, value){
	 var ind;
	 var lev2,lev3,lev4,lev5;
	for(var i=0; i<array.length; i++){
		if(array[i].value == value){
			return array[i];
		}
		lev2 = array[i].childs;
		for(var j=0; lev2!=null && j<lev2.length; j++){
			if(lev2[j].value == value){
				return lev2[j];
			}
			lev3 = lev2[j].childs;
			for(var k=0; lev3!=null && k<lev3.length; k++){
				if(lev3[k].value == value){
					return lev3[k];
				}
				lev4 = lev3[k].childs;
				for(var L4=0; lev4!=null && L4<lev4.length; L4++){
					if(lev4[L4].value == value){
						return lev4[L4];
					}
					lev5 = lev4[L4].childs;
					for(var L5=0; lev5!=null && L5<lev5.length; L5++){
						if(lev5[L5].value == value){
							return lev5[L5];
						}
					}
				}
			}
		}		
	}
	return null;
}

/**************************************************	
*
*	级联下拉框对象 
*	用法:  var 级联下拉框对象变量名=SelectItem("构造参数",下拉框的所有数据,"级联下拉框对象变量名")
*	var a=new SelectItem("国:province:0:callbackfunctionName;省:city:0:callbackfunctionName;市1:city1::callbackfunctionName;县2:city2:callbackfunctionName",region,"a");
*
**************************************************/

var SelectItem = function(lable_name_value,region,varName){
	this.lable_name_values = 	lable_name_value.split(";");
	this.length				=	this.lable_name_values.length;
	this.name				=	"span_value_" + parseInt(Math.random()*1000000);
	this.rootElement		=	null;
	this.datas				=	region;
	this.varName			=	varName;
    this.initValuesArr=function(){
         var arr=new Array();
         var j=0;
         for(var i=0;i<this.lable_name_values.length;i++){
               var tv=this.lable_name_values[i].split(":")[2];
               if(tv!="0"){
         	   		arr[j]=tv;
         	   		j++;
         	  }
         }
         return arr;
    
    }
    this.getSelectIds=function(){
        var arr=new Array();
        for(var i=0;i<this.lable_name_values.length;i++){
            arr[i]=this.lable_name_values[i].split(":")[1];
        }
        return arr;
    }
    
    this.reset=function(){
           var arr=this.initValuesArr();
           if(arr.length>0){
                for(var i=0;i<arr.length;i++){
                    document.getElementById(this.getSelectIds()[i]).value=arr[i];
                    document.getElementById(this.getSelectIds()[i]).fireEvent("onchange");
                }
           }
    
    }
    
	this.init = function(){
		document.write("<span isCaseCade='yes' id='"+this.name+"' name='"+this.name+"'></span>");
		this.rootElement=document.getElementById(this.name);
		var innerHtml=this.rootElement.innerHTML;
		var currentData = this.datas;
		for(var i=0; i<this.length;i++){
			if(!currentData.childs)
				break;
			if(currentData.childs.length==0)
				break;
			var tempArray=this.lable_name_values[i].split(":");
			innerHtml+="<select isCaseCade='yes' id='"+tempArray[1]+"' name='"+tempArray[1]+"' onchange='javascript:"+this.varName+".changeOption("+i+");' style='width:120px;'>";
			innerHtml+=this.getOptionsHtml(currentData.childs,tempArray[2])+"</select>";
			innerHtml+="<span id='"+this.name+tempArray[1]+"' name='"+this.name+tempArray[1]+"'>"+tempArray[0]+"</span>";
			this.rootElement.innerHTML=innerHtml;
			try{
				if(eval(tempArray[3])=="SKIP") return ;
			}catch(e){}
			if(isNaN(tempArray[2])){
				if(!currentData.getChildsByTextValue(tempArray[2]))
					currentData = currentData.childs[0];
				else
					currentData = currentData.getChildsByTextValue(tempArray[2]);
			}else{
				if(!currentData.getChildsByValue(tempArray[2]))
					currentData = currentData.childs[0];
				else
					currentData = currentData.getChildsByValue(tempArray[2]);
			}
		}
	 }
	 
	this.getOptionsHtml=function(optionsArray,initValue){
		html="";
		var value="";
		var flag=false;
		html+="<option value=''>请选择</option>";
	 	for(var i=0;i<optionsArray.length;i++){
			if(isNaN(initValue)){
				value=optionsArray[i].textValue;
			}else{
				value=optionsArray[i].value;
			}
			
	 		if(value == initValue){
	 			html+="<option value='"+value+"' selected='selected'>"+optionsArray[i].text+"</option>";
	 			flag=true;
	 		}else{
	 			html+="<option value='"+value+"'>"+optionsArray[i].text+"</option>";
	 		}
		}
		if(flag==false){
			html=	"<option selected='selected' "+html.substring(7,html.length);
		}
		return html;

	}	 
	
	this.changeOption=function(level){
		try{
			var tempArray=this.lable_name_values[level].split(":");
			try{
				if(eval(tempArray[3])=="SKIP") return ;
			}catch(e){}
			for(var i=level+1; i<this.length;i++){
				tempArray=this.lable_name_values[i].split(":");
				span=document.getElementById(this.name+tempArray[1]);
				if(span){
					this.rootElement.removeChild(span);	
				}
				sel=this.getElementById(tempArray[1]);
				if(sel)
					this.rootElement.removeChild(sel);			
			}
			var currentData = this.datas;
			for(var i=0; i<=level;i++){
				var tempArray=this.lable_name_values[i].split(":");
				if(isNaN(tempArray[2])){
	//				alert(this.getElementById(tempArray[1]).value);
					currentData = currentData.getChildsByTextValue(this.getElementById(tempArray[1]).value);
				}else{
					currentData = currentData.getChildsByValue(this.getElementById(tempArray[1]).value);
				}
			}

			for(var i=level+1; i<level+2;i++){
				if(!currentData.childs)
					break;
				if(currentData.childs.length==0)
					break;
				var innerHtml=this.rootElement.innerHTML;
				var tempArray=this.lable_name_values[i].split(":");
				innerHtml+="<select isCaseCade='yes' id='"+tempArray[1]+"' name='"+tempArray[1]+"' onchange='javascript:"+this.varName+".changeOption("+i+");' style='width:120px;'>";
				innerHtml+=this.getOptionsHtml(currentData.childs,tempArray[2])+"</select>";
				innerHtml+="<span name='"+this.name+tempArray[1]+"' id='"+this.name+tempArray[1]+"'>"+tempArray[0]+"</span>";
				this.rootElement.innerHTML=innerHtml;
				currentData = currentData.childs[0];
			}
		}catch(e){}
	}
	
	this.getElementById=function(elementId){
			es=document.getElementsByTagName("select");
			for(var l=0; l<es.length; l++){
				//alert(es[l]+" == "+es[l].getAttribute("isCaseCade")+" == "+ es[l].getAttribute("name"));
				if(es[l].getAttribute("isCaseCade")=="yes" && es[l].getAttribute("name")==elementId){
						return es[l];
					}				
			}
			return null;
		}
	 this.init();
}
