/******* 无限级类别下拉框处理 v1.0 by simon *******/

/******* 类别对象定义 类别id，类别父id，类别名称，类别深度 ********/
function myTypeObj(typeId, typePid, typeName, typeDepth)
{
	this.typeId = typeId;
	this.typePid = typePid;
	this.typeName = typeName;
	this.typeDepth = typeDepth;
}

/******* 重定义页面控件名称 引自 prototype1.4.1.js ********/
function $(){
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}

/******* 清除Options ********/
function selectClearOption(obj)
{
	var len = $(obj).length;
	var i;
	for(i=1; i<len; i++)
		$(obj).remove(len-i);
}

/******* 生成select 该处默认下拉框名称以sel_Type开头,
		selNum指下拉框名称编号最大值，即最大深度maxDepth，
		objName指存放div名称 
********/
function selectCreate(selNum, objName)
{
	var strTmp = "";
	var i;
	for(i=1; i <= selNum; i++)
//		strTmp += "<select id=sel_Type"+ i.toString() +" onChange=\"selectOnChange(this,"+ i +")\"><option value=>请选择</option></select>";
		strTmp += "<div style='margin:0 1px; float:left;'><font id=fnt_Sel"+ i.toString() +"></font><select id=sel_Type"+ i.toString() +" onChange=\"selectOnChange(this,"+ i +")\"><option value=>请选择</option></select></div>";
	strTmp += "<input type=hidden id=hid_SelType>";
	$(objName).innerHTML = strTmp;
}

/******* 初始化下拉列表 typePid类别父id,typeDepth本类别所属深度********/
function selectInit(typePid,typeDepth)
{
	var i;
	for(i=0; i < myObjAry.length; i++)
	{
		if(myObjAry[i].typePid == typePid && myObjAry[i].typeDepth == typeDepth)
		{
			var objOption = document.createElement("OPTION");
			$("sel_Type"+ typeDepth.toString()).options.add(objOption);
			objOption.innerText = myObjAry[i].typeName;
			objOption.value = myObjAry[i].typeId;
		}
	}
}

/******* 下拉框onChange事件 *******/
function selectOnChange(obj,typeDepth)
{
	// set this value to hid_SelType's value
	$("hid_SelType").value = obj.value;
	if((typeDepth+1) > maxDepth)
		return;
	// cleare all next select
	var i;
	for(i=(typeDepth+1); i<=maxDepth; i++)
		selectClearOption("sel_Type"+ i.toString())
	// init next select
	selectInit(obj.value.toString(),(typeDepth+1).toString());
}


/******* 通过 typeId 值获得类别对象 *******/
function getTypeObj(typeId)
{
	for(var i=0; i<myObjAry.length; i++)
	{
		if(typeId == myObjAry[i].typeId)
			return myObjAry[i];
	}
}

/******* 通过 typeId 值获得类别对象树型显示类别 
		 typeid是myObjAry的typeId值
		 breakkChar是间隔字符串值 
		 depth 返回几级类别 0，1，2 ..... -1或不写参数表示不限几级
*******/
function getTreeTypeName(typeId,breakChar,depth)
{
	if(breakChar == null) 
		breakChar = '';
	var tmpStr = "";
	var tmpDepth = 0;
	var tmpObj = new myTypeObj;
	tmpObj = getTypeObj(typeId);
	while(tmpObj != null)
	{
		if(depth != null && depth != -1)
		{
			tmpDepth++;
			if(tmpDepth > depth)
				break;
		}
		typeId = tmpObj.typePid;
		tmpStr = tmpObj.typeName + breakChar + tmpStr;
		tmpObj = getTypeObj(typeId);
		
	}
	var idx = tmpStr.lastIndexOf(breakChar);
	if(idx != -1)
		tmpStr = tmpStr.substring(0,idx);
	return tmpStr;
}


/******* 通过 typeId 值获得类别对象 *******/
function getTypeObj(typeId)
{
	for(var i=0; i<myObjAry.length; i++)
	{
		if(typeId == myObjAry[i].typeId)
			return myObjAry[i];
	}
}

/******* 通过 typeId 值获得类别对象树型显示类别 
		 typeid是myObjAry的typeId值
		 breakkChar是间隔字符串值 
		 depth 返回几级类别 0，1，2 ..... -1或不写参数表示不限几级
*******/
function getTreeTypeName(typeId,breakChar,depth)
{
	if(breakChar == null) 
		breakChar = '';
	var tmpStr = "";
	var tmpDepth = 0;
	var tmpObj = new myTypeObj;
	tmpObj = getTypeObj(typeId);
	while(tmpObj != null)
	{
		if(depth != null && depth != -1)
		{
			tmpDepth++;
			if(tmpDepth > depth)
				break;
		}
		typeId = tmpObj.typePid;
		tmpStr = tmpObj.typeName + breakChar + tmpStr;
		tmpObj = getTypeObj(typeId);
		
	}
	var idx = tmpStr.lastIndexOf(breakChar);
	if(idx != -1)
		tmpStr = tmpStr.substring(0,idx);
	return tmpStr;
}

/******* 通过 typeId 值获得类别对象树型显示类别
		 typeid是myObjAry的typeId值
		 例如 "<a href=aaa.aspx?id={typeId}>{typename}</a>"
		 breakkChar是间隔字符串值 
		 depth 返回几级类别 0，1，2 ..... -1或不写参数表示不限几级
		 longStr 是替换用字符串值,必须有{typeId}及{typeName}在字符串中 
*******/
function getTreeTypeNameStr(typeId,breakChar,depth,longStr)
{
	if(breakChar == null) 
		breakChar = '';
	var tmpStr = '';
	var tmpLongStr;
	var tmpDepth = 0;
	var tmpObj = new myTypeObj;
	tmpObj = getTypeObj(typeId);
	while(tmpObj != null)
	{
		if(depth != null && depth != -1)
		{
			tmpDepth++;
			if(tmpDepth > depth)
				break;
		}
		tmpLongStr = longStr.replace('{typeId}',tmpObj.typeId);
		tmpLongStr = tmpLongStr.replace('{typeName}',tmpObj.typeName);
		tmpStr = tmpLongStr + breakChar + tmpStr;
		typeId = tmpObj.typePid;
		tmpObj = getTypeObj(typeId);
	}
	var idx = tmpStr.lastIndexOf(breakChar);
	if(idx != -1)
		tmpStr = tmpStr.substring(0,idx);
	return tmpStr;
}

/******* 通过 typeId 逆向初始化下拉框 20061111*******/
function selectedReInit(typeId)
{
	$("hid_SelType").value = typeId;
	var myPid = "";
	var myDepth = "";
	var tmpObj = new myTypeObj;
	tmpObj = getTypeObj(typeId);
	// 初始化下一级
	if(tmpObj != null)
		if((tmpObj.typeDepth + 1) <= maxDepth)
			selectInit(typeId, (parseInt(tmpObj.typeDepth) + 1));
	// 逆向初始化
	while(tmpObj != null)
	{
		myPid = tmpObj.typePid;
		myDepth = tmpObj.typeDepth;
		typeId = tmpObj.typeId;
		if(myDepth != 1)
			selectInit(myPid, myDepth);
		$("sel_Type"+ myDepth).value = typeId;
		tmpObj = getTypeObj(myPid);
	}
}