// JavaScript Document
/********************************************************************
主题:基于XML的下拉菜单操作

作者:redarmy
时间:Oct.2007
说明:
	1.包括创建菜单以及级连菜单,兼容IE&Mozilla;
	2.调用importXml(XML文件名,引用函数名,引用函数参数);
	3.问题:对于级连菜单可进行输入框扩展.
********************************************************************/

/******引入XML文件及相关函数******/
function importXml(xmlFile,funcName,funcParameter) 
{
	var xmlDoc;
	if (document.implementation && document.implementation.createDocument) 
	{	//For Mozilla FireFox
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.async = false;
		xmlDoc.onload = function() 
		{
			eval(""+funcName+"(xmlDoc,"+funcParameter+")");		//已默认引入xmlDoc
		}
	}
	else if (window.ActiveXObject) {											//For Internet Explorer
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			//readyState:0(未初始化/uninitialized);1(正在加载/loading);2(加载完毕/loaded);3(交互/interactive);4(完成/complete)
			if (xmlDoc.readyState == 4) {
				eval(""+funcName+"(xmlDoc,"+funcParameter+")");
			}
		}
 	}
	else {																		//For other Browser
		alert("很抱歉,您的浏览器暂不支持该脚本,建议使用IE6!");
		return;
	}
	xmlDoc.load(xmlFile);
}

/******选择域的基本操作******/
/*清空选择域选项*/
function clearSelOp(field) {
	for (var i=field.options.length -1; i>=0; i--)
		field.options[i] = null;
}
/*创建选择域选项*/
function createSelOp(field,node,attribArray,type,dftStr) {
	var op;
	var attribText = attribArray[0];
	var attribKey = attribArray[1];
	if (dftStr==null)
		dftStr = "请选择...";
	var opNode = new Array();
	var num = -1;
	switch (type) {
		case -1:	//自定义默认首项,不生成列表项.
			op = window.document.createElement("option");
			op.value = "-1";
			op.innerHTML = dftStr;
			field.appendChild(op);
			break;
		case 0:		//取首节点作为默认首项,不生成列表项.
			opNode[0] = node;
			num = 1;
		case 1:		//无默认首项,生成列表项.
		default:
			if (num!=1) {
				opNode = node;
				num = node.length;
			}
			for (var i=0; i<num; i++) {
				op = window.document.createElement("option");
				if (attribKey!=null)
					op.value = opNode[i].getAttribute(attribKey);
				if (attribText!=null)
					op.innerHTML = opNode[i].getAttribute(attribText);
				else {
					alert("创建选择列表项失败,请检查XML文件完整性!");
					return false;
				}
				if (field !=null) {
				  field.appendChild(op);
			  }
			
			}
			break;
	}
}

/******创建菜单项函数******/
function createSelData(xmlDoc, oId, tagName, attribArray) {
	var oField = document.getElementById(oId);
	if (tagName == null)
		tagName = "add";
	var nodeElement = xmlDoc.getElementsByTagName(tagName);
	if (attribArray==null)
		attribArray = ["value","key"];
	createSelOp(oField,null,attribArray,-1);
	createSelOp(oField,nodeElement,attribArray,1);
}
/*批处理函数*/
function batchCreateSelData (oArray) {
	var xmlPath = "/xml/cn/";
	for (i=0; i<oArray.length; i++) {
		var xmlFileName = oArray[i][0];
		var oId = oArray[i][1];
		eval("importXml(\""+xmlPath+xmlFileName+".xml\",\"createSelData\",\"'"+oId+"'\")");
	}
}

/******级连菜单函数******/
/*全局变量*/
var oField = new Array();		//表单域对象

var nodeTagName = new Array();	//xml节点名

var nodeElement = new Array();	//xml节点元素
/*初始化级连菜单*/
function initGDL(xmlDoc,idArray, tagNameArray, attribArray, dfltStr) 
{
//['ddlCountry','ddlProvince','ddlCity'],['Country','Province','City'],['name',null],'中国|浙江省|杭州市'
	//初始化变量
	var total = idArray.length;
	for (i=0; i<total; i++) 
	{
		oField[i] = document.getElementById(idArray[i]);
		nodeTagName[i] = tagNameArray[i];
	}
	nodeElement[0] = xmlDoc.getElementsByTagName(nodeTagName[0]);//所有Count的节点
	createSelOp(oField[0],nodeElement[0],attribArray,1);	//创建首级菜单选项
	if (dfltStr != null && dfltStr!='')	//e.g. dfltStr = "中国|浙江省|杭州市"
	{
		selectGDL(total,attribArray,dfltStr);
	}
	else 
	{
		var dfltNode = nodeElement[0][0].getElementsByTagName(nodeTagName[1])[0]; //创建其余级连动菜单默认项
		for (i=1; i<total; i++) 
		{
			createSelOp(oField[i],dfltNode,attribArray,0);
			dfltNode = dfltNode.getElementsByTagName(nodeTagName[i+1])[0];
		}
	}
	

}
/*变动级连菜单*/
function changeGDL(curr,total,attribArray) {
	var next = curr + 1;
	var oField = new Array();
	oField[0] = document.getElementById("ctl00_ContentPlaceHolder1_dropProvince");
	oField[1] = document.getElementById("ctl00_ContentPlaceHolder1_dropCity");
	oField[2] = document.getElementById("ctl00_ContentPlaceHolder1_dropCounty");
	for (i=next; i<total; i++)	//后所有级连动菜单可见
		oField[i].style.display = "block";
	for (i=0; i<nodeElement[curr].length; i++) {
		if(nodeElement[curr][i].getAttribute(attribArray[0]) == oField[curr].options[oField[curr].options.selectedIndex].innerHTML) {
			var currNode = nodeElement[curr][i];
			var valPoint = next;	//定义有效指针(判断是否含有子节点)
			while (currNode.hasChildNodes()) {
				currNode = currNode.getElementsByTagName(nodeTagName[valPoint])[0];
				valPoint++;
			}
			for (j=valPoint; j<total; j++)	//无效级连菜单不可见
				oField[j].style.display = "none";
			if (valPoint==next)	//无级连菜单则返回
				return;
			currNode = nodeElement[curr][i];
			nodeElement[next] = currNode.getElementsByTagName(nodeTagName[next]);
			clearSelOp(oField[next]);	//清空下级菜单选项
			createSelOp(oField[next],nodeElement[next],attribArray,1);	//创建下级菜单选项
			if (next+1<total) {	//创建其余有效级连动菜单默认项
				var dfltNode = nodeElement[next][0].getElementsByTagName(nodeTagName[next+1])[0];
				for (j=next+1; j<valPoint; j++) {
					clearSelOp(oField[j]);
					createSelOp(oField[j],dfltNode,attribArray,0);
					dfltNode = dfltNode.getElementsByTagName(nodeTagName[j+1])[0];
				}
			}
			break;
		}
	}
}
/*选择所在内容(编辑时所用)*/
function selectGDL(total,attribArray,str) {
	var strLength = str.split("|").length;
	//无效级连菜单不可见
	//3,name,中国|浙江省|台州市
	for (var i=strLength; i<total; i++)
	{
		oField[i].style.display = "none";
	}
	var strPart = new Array();
	for (var i=0; i<strLength; i++) 
	{
		strPart[i] = str.split("|")[i];
		for (var j=0; j<oField[i].options.length; j++)
			if (oField[i].options[j].innerHTML==strPart[i]) 
			{
				oField[i].options[0].setAttribute("selected",false);
				oField[i].options[j].setAttribute("selected",true);
				oField[i].options.selectedIndex = j;
				break;
			}
			
		//字段不匹配时返回错误
		if (j==oField[i].options.length) {
			alert("对不起，由于系统更新，造成地区数据不匹配，请您重新选择贵公司所在地区。造成不便，敬请谅解！");
			oField[i].focus();
			return false;
		}
		if (i+1<strLength) {
			nodeElement[i+1] = nodeElement[i][j].getElementsByTagName(nodeTagName[i+1]);
			createSelOp(oField[i+1],nodeElement[i+1],attribArray,1);
		}
		else return;
	}
}

/******二级行业类别函数******/
/*全局变量*/
var cateAttrib, cateNode;
/*初始化大行业类别*/
function initCate(xmlDoc,idArray,dfltCate) {
	cateAttrib = ["categoryName","categoryId"];
	cateNode = xmlDoc.getElementsByTagName("add");
	var oCate1 = document.getElementById(idArray[0]);
	var oCate2 = document.getElementById(idArray[1]);
	createSelOp(oCate1,null,cateAttrib,-1,"---请选择一个大行业类别---");
	for (var i=0; i<cateNode.length; i++)
		if (cateNode[i].getAttribute("parentId")=="0")
			createSelOp(oCate1,cateNode[i],cateAttrib,0);
	createSelOp(oCate2,null,cateAttrib,-1,"---请选择一个子行业类别---");
	if (dfltCate!=null)
		selectCate(xmlDoc,oCate1,oCate2,dfltCate);
	oCate1.style.width = "240px";
	oCate2.style.width = "240px";
	oCate1.onchange = function() {
		changeCate(oCate1,oCate2);
	}
}
/*变动子行业类别*/
function changeCate(oCate1, oCate2){
	clearSelOp(oCate2);
	createSelOp(oCate2,null,cateAttrib,-1,"---请选择一个子行业类别---");
	for (var i=0; i<cateNode.length; i++)
		if(cateNode[i].getAttribute("parentId") == oCate1.options[oCate1.options.selectedIndex].getAttribute("value"))
			createSelOp(oCate2,cateNode[i],cateAttrib,0);
	oCate2.style.width = "240px";
}
/*选择所在行业(编辑时所用)*/
function selectCate(xmlDoc,oCate1,oCate2,subId) {
	for (var i=0; i<cateNode.length; i++)
		if (cateNode[i].getAttribute("categoryId") == subId)
			var prtId = cateNode[i].getAttribute("parentId");
	for (var i=0; i<oCate1.options.length; i++)
		if (oCate1.options[i].value == prtId) {
			oCate1.options[0].setAttribute("selected",false);
			oCate1.options[i].setAttribute("selected",true);
			oCate1.options.selectedIndex = i;
		}
	for (var i=0; i<cateNode.length; i++)
		if(cateNode[i].getAttribute("parentId") == prtId)
			createSelOp(oCate2,cateNode[i],cateAttrib,0);
	for (var i=0; i<oCate2.options.length; i++)
		if (oCate2.options[i].value == subId) {
			oCate2.options[0].setAttribute("selected",false);
			oCate2.options[i].setAttribute("selected",true);
			oCate2.options.selectedIndex = i;
		}
}