/* JavaScript Document */

/*
我对JS掌握不熟悉，所以方法写的很冗余
祝继
*/


function xmlTree(name) {
	this.name         = name;                   //实例名称
	this.xmlFile      = 'new_file_en.xml';          //默认xml文件
	this.html         = '';                     //最终输出html代码
	this.firstImg     ='images/help_08.jpg';	//第一层图像
	this.secondImg    ='images/help_11.gif'; 	//第二层图片
	this.secondClass  = 'hl_t1';				//第二层样式
	this.thirdClass   ='hl_t2';					//第三层样式
xmlTree.prototype.createTree = function() { //生成并打印
	var xmldom = this.createXMLDOM();
	document.write('<div id="tree"><\/div>'); // 树所用层	
	if (xmldom.load(this.xmlFile)) {
		this.createNodes(xmldom);
	} else {
		this.html = 'Load XML Error';
	}
	
	document.getElementById('tree').innerHTML = this.html;
	return;
}

xmlTree.prototype.createXMLDOM = function() { //生成XMLDOM对象
	var xmldom;
	if (window.ActiveXObject){
		var xmldom = new ActiveXObject("Microsoft.XMLDOM");
	} else {
		if (document.implementation && document.implementation.createDocument) {
			var xmldom = document.implementation.createDocument("","doc",null);
		}
	}
	xmldom.async = false;
	xmldom.resolveExternals = false;
	xmldom.validateOnParse = false;
	xmldom.preserveWhiteSpace = true;
	return xmldom;
}
xmlTree.prototype.getFirstChildData = function(obj, name) { //取得指定名称节点的第一个子节点的数据
	var result = '';
	if (typeof(obj) == 'object' && name != null && name != '') {
		var node = obj.getElementsByTagName(name);
		if (node != null && node.length > 0) {
			result = node[0].firstChild.data;
		}
	}
	return result;
}

xmlTree.prototype.getSecondData = function(obj, id) { //取得节点为n2的集合展开显示
	this.html+='<\div class="heip_shu" style="height:3px;border:0px">';
	this.html+='<\/div>'
	var nodes = obj.getElementsByTagName('n2');
	if (nodes != null && nodes.length > 0) {
		for (var i = 0; i < nodes.length; i++) {
			var stempid = id+'Child'+nodes[i].getAttribute('id');
			if (this.getFirstChildData(nodes[i],'parentid') == id) {
				this.html+='<\div class="heip_shu"';
				this.html+=' id=';
				this.html+=id+'Child'+nodes[i].getAttribute('id');
				this.html+=' style=display:block;border:0px';
				this.html+='>';
				this.html+='<\img src=';
				this.html+=this.secondImg;
				this.html+=' width="8" height="8"/>';
				this.html+=' <\a href="#"';
				this.html+=' onclick=showBole(';
				this.html+="'";
				this.html+= stempid;
				this.html+="'";
				this.html+=')';
				this.html+=' class='
				this.html+=this.secondClass;
				this.html+='>';
				this.html+=this.getFirstChildData(nodes[i],'name');
				this.html+='<\/a>';
				this.html+='<\/div>';
				this.html+='<\div class="heip_shu"';
				this.html+=' id=';
				this.html+=id+'Child'+nodes[i].getAttribute('id')+'GrandSon';
				this.html+=' style=display:block;border:0px';
				this.html+='>';
				var pid = nodes[i].getAttribute('id');
				this.getThirdData(obj,pid);
				this.html+='<\/div>';
				
			}
		}
	}
			
}

xmlTree.prototype.getThirdData = function(obj, id) { //取得节点为n3的集合展开显示
	var nodes = obj.getElementsByTagName('n3');
	if (nodes != null && nodes.length > 0) {
		for (var i = 0; i < nodes.length; i++) {
			if (this.getFirstChildData(nodes[i],'parentid') == id) {
				this.html+='&nbsp;&nbsp;'
				this.html+='.';
				this.html+=' <\a href=#';
				this.html+=' onclick=changemain(';
				this.html+="'";
				this.html+=this.getFirstChildData(nodes[i],'href');
				this.html+="'";
				this.html+=') class=';
				this.html+=this.thirdClass;
				this.html+='>';
				
				this.html+=this.getFirstChildData(nodes[i],'name');
				this.html+='<\/a>';
				this.html+='<br>';
			}
		}
	}
			
}

xmlTree.prototype.createNodes = function(obj) { //取得节点为n1的集合展开显示
	var nodes = obj.getElementsByTagName('n1');
	for(var i=0;i<nodes.length;i++){
		this.html+='<\div class="help_t"';
		this.html+=' id=';
		this.html+= nodes[i].getAttribute('id');
		this.html+=' onclick=showBranch(';
		this.html+=nodes[i].getAttribute('id');
		this.html+=')';
		this.html+=' style="width:179px;cursor:hand">';
		this.html+='<\img id="';
		this.html+= nodes[i].getAttribute('id')+"img";
		this.html+='" src=';
		this.html+=this.firstImg;
		this.html+=' width="17" height="16" />';
		this.html+=this.getFirstChildData(nodes[i],'name');
		this.html+='<\/div>';
		var id = nodes[i].getAttribute('id');
		this.getSecondData(obj,id);
	}
	
}
}

function changemain(value){
	parent.document.getElementById('main').src=value; 
}

function showBole(id){
	var idChild = id+'GrandSon';
	for (i=0; i<document.getElementsByTagName("div").length; i++) {        
	 	whichEl = document.getElementsByTagName("div")[i];
		var tempid = whichEl.id;
		if(tempid.indexOf(idChild)>=0||tempid==idChild){
			var show=document.getElementById(tempid).style.display;		
			if(show=="block"){
				document.getElementById(tempid).style.display="none";
			}else{
				document.getElementById(tempid).style.display="block";
			}
		}
	}	 
	
}

function showBranch(id){
	var idChild = id+'Child';
	var img = id+'img';
	var dis="show";
	var firstImgClose ='images/help_07.jpg';	//第一层图像
	var firstImg ='images/help_08.jpg';	//第一层图像
	
	for (i=0; i<document.getElementsByTagName("div").length; i++) {        
	 	whichEl = document.getElementsByTagName("div")[i];
		var tempid = whichEl.id;
		if(tempid.indexOf(idChild)>=0&&tempid.indexOf("GrandSon")<0){
			var show=document.getElementById(tempid).style.display;		
			if(show=="block"){
				document.getElementById(tempid).style.display="none";
				dis="none";
				document.getElementById(img).src=firstImgClose;
			}else{
				document.getElementById(tempid).style.display="block";
				dis="block";
				document.getElementById(img).src=firstImg;
			}
		}
		if(tempid.indexOf(idChild)>=0&&tempid.indexOf("GrandSon")>=0){
			show=document.getElementById(tempid).style.display;		
			if(dis=="none"){
				document.getElementById(tempid).style.display="none";
				
			}else{
				document.getElementById(tempid).style.display="block";
				
			}
		}
		
	}	 
	
}

