var Linkage = Class.create();
Linkage.prototype = {
	initialize : function(dataSrc, xmlFile) {
		this.dataSrc = dataSrc;
		this.xmlFile = xmlFile;
	},

	dataSrc : "" ,
	xmlFile : "" ,

	BLANK_SELECT : "----- SELECT -----" ,

	AllMenuArr : new Array() ,

	MenuIdArr : new Array() ,

	MenuInfoArr : new Array() ,

	iDepth : -1 ,

	tree : function(dataSrc, Element) {
		var node = "";
		if(Element.nodeType != 3) {
			node = Element;
			this.onElement(dataSrc, Element);
		}
		if(Element.hasChildNodes) {
			for(var i=0;i<Element.childNodes.length;i++) {
				if (Element.childNodes[i].nodeType != 3) {
					this.iDepth++;
					this.tree(dataSrc, Element.childNodes[i]);
				}
			}
		}
		if(node) {
			this.endElement();
		}
	} ,

	onElement : function(dataSrc, ele) {
		if ($V(ele, "Value") != null) {
			if (this.MenuInfoArr[dataSrc] == null) {
				this.MenuInfoArr[dataSrc] = new Array();
			}
			if (this.MenuInfoArr[dataSrc][this.iDepth] == null) {
				this.MenuInfoArr[dataSrc][this.iDepth] = new Array();
			}
			this.MenuInfoArr[dataSrc][this.iDepth].push(new MenuInfo($V(ele.parentNode, "Value") , $V(ele, "Value") , ($V(ele, "Desc")==null ? $V(ele, "Value") : $V(ele, "Desc"))));
		}
	} ,

	endElement : function() {
		this.iDepth--;
	} ,


	initBlank_P : function(element) {
		element.length = 0;
		element.options.add(new Option( this.BLANK_SELECT_2, "" ));
		element.selectedIndex = 0;
	} ,
	
	initBlank_C : function(element) {
		element.length = 0;
		element.options.add(new Option( this.BLANK_SELECT, "" ));
		element.selectedIndex = 0;
	} ,
	
	updateAllLast : function(dataSrc, nLevel) {
		for(i = nLevel+1; i < this.MenuIdArr[dataSrc].length; i++) {
			childNode = $(this.MenuIdArr[dataSrc][i]);
			this.initBlank_P(childNode);
			childNode.disabled = true;
		}
	} ,

	initLinkage : function(dataSrc, sValue, nLevel) {
		nLevel = Number(nLevel);

		if (nLevel > this.MenuIdArr[dataSrc].length || nLevel < 1) {
			return;
		}

		currNode = $(this.MenuIdArr[dataSrc][nLevel-1]);
		childNode = $(this.MenuIdArr[dataSrc][nLevel]);

		if (currNode.disabled) {
			return;
		}

		for (i=0; i<currNode.options.length; i++) {
			if  (currNode.options[i].value  ==  sValue) {
				currNode.selectedIndex = i;
				break;
			}
		}

		if (childNode != null) {
			currArr = this.AllMenuArr[dataSrc][nLevel];
			this.initBlank_P(childNode);
			for(i=0; i<currArr.length; i++) {
				if  (currArr[i].parentValue  ==  sValue) {
					childNode.options.add(new Option(currArr[i].Desc, currArr[i].Value));
				}
			}
			if ((sValue != '') && (childNode.length > 1)) {
				childNode.disabled = false;
			} else {
				childNode.disabled = true;
			}
		}

		this.updateAllLast(dataSrc, nLevel);
	} ,

	changeLinkage : function(element) {
		this.initLinkage($V(element , "USEDATA"), $F(element), $V(element , "SUBCLASS"));
	} ,

	setDataSrc : function(dataSrc) {
		this.dataSrc = dataSrc;
	} ,

	setXmlFile : function(xmlFile) {
		this.xmlFile = xmlFile;
	} ,

	init : function() {

		var rootEle = loadXML(this.dataSrc, this.xmlFile);
		this.tree(this.dataSrc, rootEle);

		this.iDepth = -1;


		for (i=0; i<this.MenuInfoArr[this.dataSrc].length; i++) {
			if (this.AllMenuArr[this.dataSrc] == null) {
				this.AllMenuArr[this.dataSrc] = new Array();
			}
			this.AllMenuArr[this.dataSrc].push(this.MenuInfoArr[this.dataSrc][i]);
		}


		var selectNodes = document.getElementsByTagName("select");
		for (i=0; i<selectNodes.length; i++) {
			if ($V(selectNodes[i] , "USEDATA") == this.dataSrc) {
				if (this.MenuIdArr[this.dataSrc] == null) {
					this.MenuIdArr[this.dataSrc] = new Array();
				}
				var subClass = Number($V(selectNodes[i] , "SUBCLASS")) - 1;
				this.MenuIdArr[this.dataSrc][subClass] = $V(selectNodes[i] , "id");
				Event.observe(selectNodes[i], "change", this.changeLinkage.bind(this, selectNodes[i]));
				new Form.Element.EventObserver(selectNodes[i], this.changeLinkage.bind(this));
			}
		}


		firstNode = $(this.MenuIdArr[this.dataSrc].first());
		this.initBlank_C(firstNode);
		for (i=0; i<this.AllMenuArr[this.dataSrc].first().length; i++) {
			firstNode.options.add(new Option(this.AllMenuArr[this.dataSrc].first()[i].Desc, this.AllMenuArr[this.dataSrc].first()[i].Value));
		}


		this.updateAllLast(this.dataSrc, 0);
	}
}


var MenuInfo = Class.create();
MenuInfo.prototype = {
	initialize : function(sParentValue, sValue, sDesc) {
		this.parentValue = sParentValue;
		this.Value = sValue;
		this.Desc = sDesc;
	}
}


function $V(ele, attr) {
	return ele.getAttribute(attr);
}


function createXMLDom() {
	if (window.ActiveXObject)
		var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
	else
		if (document.implementation&&document.implementation.createDocument)
			var xmldoc = document.implementation.createDocument("","doc",null);
	xmldoc.async = false;

	xmldoc.preserveWhiteSpace=true;
	return xmldoc;
} 


function loadXML(dataSrc, xmlFile) {
	if (xmlFile == null) {
		if (window.ActiveXObject) {
			return $(dataSrc).documentElement;
		} else {
			for (i=0; i<$(dataSrc).childNodes.length; i++) {
				if ($(dataSrc).childNodes[i].tagName != null) {
					return $(dataSrc).childNodes[i];
					break;
				}
			}
		}
	} else {
		var xmlDom = createXMLDom();
		try{
			xmlDom.load(xmlFile);
		}catch(e){
			alert("ŁĄ");
		}
		return xmlDom.documentElement;
	}
} 