// JavaScript Document
//2007.8.1
var InputBind=""

function _get(a){return document.getElementById?document.getElementById(a):null}

function creatSelectBox(){
	document.write("<div id=\"selectbox\"></div>");
	}
	
function creatSelect(n){
	var creatSelect=_get("selectbox").appendChild(document.createElement("select"));
		creatSelect.setAttribute("id",n);
		creatSelect.onchange=selectOnchange;
	}

function getSelectValue(selectobj){
	if(_get(selectobj)){
		var v = _get(selectobj).options[_get(selectobj).selectedIndex].value;
		if (v==""){
			return null;
		}else{
			return v;
		}
	}else{
		return null;
	}
}

function readTypeValue(){
var a;
var b;
	a= _get(InputBind).value;

	if (a!= null){
		a="0|"+a;
		return	b=a.split("|");
	}else{
		return null
	}
}

function readXml(url){
var xmlDoc;
	if(window.ActiveXObject){ 
		xmlDoc = new ActiveXObject('Microsoft.XMLDOM'); }
		else{
		xmlDoc = document.implementation.createDocument('', '', null);
	 }
	xmlDoc.async = false; 
	xmlDoc.load(url); 
	if(xmlDoc.getElementsByTagName("item").length>0){
	return xmlDoc;
	}else{
		return null;
	}
}

function creatOption(xmlDoc,selectid){
    _get(selectid).innerHTML="";
		var _optiononeobj =_get(selectid).appendChild(document.createElement("option"));
		_optiononeobj.innerHTML="----Select----";
		_optiononeobj.setAttribute("value", "");

		for (var i=0;i<xmlDoc.getElementsByTagName("item").length;i++){
	var optiononeobj =_get(selectid).appendChild(document.createElement("option"));
		optiononeobj.innerHTML=xmlDoc.getElementsByTagName("item")[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
		optiononeobj.setAttribute("value", xmlDoc.getElementsByTagName("item")[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
	}
}

function selectOnchange(onchangeEvent){
	var onchangeEvent = onchangeEvent || window.event;
	var target = onchangeEvent.target || onchangeEvent.srcElement;
	var selectSelf=target.id;
	var foundMatch =0; //状态：已经少到匹配对象
	var TypeValue=readTypeValue();
	var selectLoad;
	_get(InputBind).value=""; //把帮顶的输入INPUT=""
	for(i=0;i<10;i++){
		
		if (getSelectValue("select"+(i))!=null){
		if (i>0) {_get(InputBind).value += "|";}
			_get(InputBind).value += getSelectValue("select"+(i)); //
		}
		
		if("select"+i==target.id){
			selectLoad="select"+(i+1);
			foundMatch =1;
		}	

	
		if ( _get("select"+(i+1)) && foundMatch ==1 ){
		_get("select"+(i+1)).outerHTML="";
		}
	}
	if(getSelectValue(selectSelf)!=null){
		oxmlDoc = readXml(xmlURL+"?parentType="+getSelectValue(selectSelf));
		if(oxmlDoc!=null){
			if(!_get(selectLoad)){
			creatSelect(selectLoad);
			}
			creatOption(oxmlDoc,selectLoad);
		}
	}
}

function creatSelectItem(){ //初始化
	var TypeValue=readTypeValue();
	creatSelectBox();
	if (TypeValue!=null){
		for(i=0;i<TypeValue.length;i++){
			oxmlDoc = readXml(xmlURL+"?parentType="+TypeValue[i]);
			if(oxmlDoc!=null){
				creatSelect("select"+i);
				creatOption(oxmlDoc,"select"+i);
				var asasa=_get("select"+i).options.length;
				for(var x=0;x<asasa;x++){
					if(_get("select"+i).options[x].value==TypeValue[i+1]){
						_get("select"+i).options[x].selected= "selected";
					}
				}
			}
		}
	}
	else{
		var oxmlDoc = readXml(xmlURL);
		if(oxmlDoc!=null){
			creatSelect("select0");
			creatOption(oxmlDoc,"select0");
		}
	}
}