/**********************************************
	赵华zhaohua_cn@263.net,如果有问题请与我讨论
	多层树型结构的select对象，交互表单最终读取伴随隐含变量
	两个左右连动的父子select对象--即2层结构交互表单最终读取右边的儿子select对象
**********************************************/
//多层树型结构的select对象-填充到同一个select对象
function ChangeOption(Oselect,myArray){
//Oselect-填充到select对象,myArray伴随数组
var i,j,k,mytext,fid,nCount;
var LastID;

nCount=myArray.length;
i=Oselect.selectedIndex;
LastID=Oselect.options[i].value;
mytext=Oselect.options[i].text;
fid=0;
if(mytext=="返回上级..."){
	fid=GetParent(Oselect.options[i+1].value,myArray);
}
/*先判断是树支还是树叶*/
/*是树支则进入下级*/
/*j是第一个孩子的数组下标*/
j=HaveSon(LastID,myArray);
if(j>-1){
	Oselect.length=0;
	var k=0;
	if(LastID!="0")	{
		Oselect.options[k]=new Option("返回上级...",GetParent(LastID,myArray));
		k++;
	}

	for(i=j;i<nCount;i++){
		if(myArray[i].ParentID==LastID){
			Oselect.options[k]=new Option(myArray[i].LabelDesc,myArray[i].LabelID);
			if(Oselect.options[k].value==fid){
				Oselect.selectedIndex=k;
			}
			k++;
		}
	}
}
}

//供ChangeOption函数调用
function HaveSon(LastID,myArray)
{
/*先判断是树支还是树叶,返回第一个孩子的数组下标,如果是树叶则返回-1*/
var i,nCount;
nCount=myArray.length;
for(i=0;(i<nCount)&&(myArray[i].ParentID!=LastID);i++);

if(i<nCount-1||(i==nCount-1&&myArray[nCount-1].ParentID==LastID))
{
	vRet=i;
}
else
{
	vRet=-1;
}
return vRet;
}

//供ChangeOption函数调用
function GetParent(son,myArray)
{
var i,nCount;
nCount=myArray.length;

for(i=0;i<nCount&&myArray[i].LabelID!=son;i++);

return myArray[i].ParentID;
}

//填充select对象，选中第一个项目
function initselect(Oselect,myArray)
{
var i,k=0,nCount;
nCount=myArray.length;
Oselect.length=0;

for(i=0;i<nCount;i++)
{
	if(myArray[i].ParentID=="0"){
	Oselect.options[k]=new Option(myArray[i].LabelDesc,myArray[i].LabelID);
	k++;
	}
}
Oselect.selectedIndex=0;
}

//多层树型结构的select对象-根据代码son填充到同一个select对象
function FillSelect2(Oselect,myArray,son){
if(son==""||son=="0"){
	initselect(Oselect,myArray);
}
else{
var i,k=0,nCount,pid,nlen;

nCount=myArray.length;
pid=GetParent(son,myArray);
Oselect.length=0;
if(pid!="0"){
	Oselect.options[k]=new Option("返回上级...",GetParent(pid,myArray));
	k++;
}
for(i=0;i<nCount;i++)
{
	if(myArray[i].ParentID==pid){
		Oselect.options[k]=new Option(myArray[i].LabelDesc,myArray[i].LabelID);
		if(Oselect.options[k].value==son){
			Oselect.selectedIndex=k;
		}
		k++;
	}
}}

}

//两个select对象，左边备选项，右边选中项，将sourceselect对象的当前项填充到destselect对象的末尾，并且更新伴随隐含变量myhidden
function select_add_hidden(sourceselect,destselect,myhidden){
	select_add(sourceselect,destselect);
	myhidden.value=refresh_hidden(destselect);
}

//两个select对象，左边备选项，右边选中项，将destselect对象的当前项（没有当前项时把末尾项作为当前项）删除，并且更新伴随隐含变量myhidden
function select_delete_hidden(destselect,myhidden){
	select_delete(destselect);
	myhidden.value=refresh_hidden(destselect);
}

//两个select对象，左边备选项，右边选中项，将destselect对象清除,并且更新伴随隐含变量myhidden
function select_clear_hidden(destselect,myhidden){
	select_clear(destselect);
	myhidden.value="";
}

//供select_add_hidden调用
function select_add(sourceselect,destselect){
var i=sourceselect.selectedIndex;
maxlength=10;
if(i<0){//没有当前项时选择第一项
	i=0;
	sourceselect.selectedIndex=i;
}
if(sourceselect.options[i].text!="返回上级..."){
for(var j=0;j<destselect.length;j++){
	if(destselect.options[j].value==sourceselect.options[i].value){
		break;
	}
}
if(j==destselect.length){
	if(destselect.length<maxlength){
		destselect.options[destselect.length]=new Option(sourceselect.options[i].text,sourceselect.options[i].value);
	}else{
		alert("最多允许选择"+maxlength+"个项目！");
	}
}
else{
	alert("此项目已经加入!");
}
if(i<sourceselect.length-1){
	i++;
	sourceselect.selectedIndex=i;
}
}
}
//供select_delete_hidden调用
function select_delete(destselect){
	var i=0;
	if(destselect.length>0){
		i=destselect.selectedIndex;
		if(i<0){//没有当前项时选择最后一项
			i=destselect.length-1;
		}
		destselect.options.remove(i);
	}
	if(i>destselect.length-1){
		i=destselect.length-1;
	}
	destselect.selectedIndex=i;
}

//供select_clear_hidden调用
function select_clear(destselect){
	destselect.length=0;
}

//供select_add_hidden,select_delete_hidden,select_clear_hidden调用
function refresh_hidden(destselect){
	var retstr="";
	for(i=0;i<destselect.length;i++){
		retstr+=destselect.options[i].value+",";
	}
	if(retstr!=""){
		retstr=retstr.substr(0,retstr.length-1);
	}
	return retstr;
}

//两个select对象，左边备选项，右边选中项，将sourceselect对象的当前项填充到destselect对象的末尾
//在修改界面反填destselect对象，并且反填伴随隐含变量myhidden
function initdestselect(destselect,initstr,arraysource,myhidden){
	myhidden.value=initstr;
	var temparray=initstr.split(",");
	for(var j=0;j<temparray.length;j++){
		for(var i=0;i<arraysource.length;i++){
			if(temparray[j]==arraysource[i].LabelID){
				var oOption = document.createElement("OPTION");
				oOption.text=arraysource[i].LabelDesc;
				oOption.value=arraysource[i].LabelID;
				destselect.add(oOption);
				oOption.empty;
				break;
			}
		}
	}
}

//两个select对象，左边备选项，右边选中项，将sourceselect对象的当前项填充到destselect对象的末尾
//在修改界面反填destselect对象，并且反填伴随隐含变量myhidden
//与initdestselect不同这个函数不用事先的数组arraysource而是用左边的sourceselect填充destselect,
function initdestselect2(sourceselect,destselect,initstr,myhidden){
	myhidden.value=initstr;
	var temparray=initstr.split(",");
	for(var j=0;j<temparray.length;j++){
		for(var i=0;i<sourceselect.length;i++){
			if(temparray[j]==sourceselect.options[i].value){
				var oOption = document.createElement("OPTION");
				oOption.text=sourceselect.options[i].text;
				oOption.value=sourceselect.options[i].value;
				destselect.add(oOption);
				oOption.empty;
				break;
			}
		}
	}
}

//两个左右连动的父子select对象，左边是存放父亲，右边存放与左边父亲对应的儿子
//在左边父亲onChange事件里调用此函数，更新右边的儿子
function ChangeSubOption(Oselect,myArray,LastID){
	var nCount=myArray.length;
	Oselect.length=0;
	var k=0;
	k=0;
	for(var i=0;i<nCount;i++){
		if(myArray[i].ParentID==LastID){
			Oselect.options[k]=new Option(myArray[i].LabelDesc,myArray[i].LabelID);
			k++;
		}
	}
	Oselect.selectedIndex=0;
}

//两个左右连动的父子select对象，左边是存放父亲，右边存放与左边父亲对应的儿子
//在添加和修改时反填父子select对象
//要求sonid的前fatherlength位是fatherid--即儿子代码的前缀是父亲代码
function fill2select(myArray,Oselectfather,Oselectson,sonid,fatherlength){
if(sonid==""){
	initselect(Oselectfather,myArray);
	ChangeSubOption(Oselectson,myArray,Oselectfather.value);
}else{
	var fatherid=sonid.substr(0,fatherlength);
	var i,k=0,nCount=myArray.length;
	var initselected=0;
	Oselectfather.length=0;
	for(i=0;i<nCount;i++){
		if(myArray[i].ParentID=="0"){
			Oselectfather.options[k]=new Option(myArray[i].LabelDesc,myArray[i].LabelID);
			if(myArray[i].LabelID==fatherid){
				initselected=k;
			}
			k++;
		}
	}
	Oselectfather.selectedIndex=initselected;
	
	initselected=0;
	k=0;
	for(i=0;i<nCount;i++){
		if(myArray[i].ParentID==fatherid){
			Oselectson.options[k]=new Option(myArray[i].LabelDesc,myArray[i].LabelID);
			if(myArray[i].LabelID==sonid){
				initselected=k;
			}
			k++;
		}		
	}
	Oselectson.selectedIndex=initselected;
}}

function ChangeOption2(selectleft,selectright,myhidden,myArray){
//Oselect-填充到select对象,myArray伴随数组
var i,j,k,mytext,fid,nCount;
var LastID;

nCount=myArray.length;
i=selectleft.selectedIndex;
LastID=selectleft.options[i].value;
mytext=selectleft.options[i].text;
fid=0;
if(mytext=="返回上级..."){
	fid=GetParent(selectleft.options[i+1].value,myArray);
}
/*先判断是树支还是树叶*/
/*是树支则进入下级*/
/*j是第一个孩子的数组下标*/
j=HaveSon(LastID,myArray);
if(j>-1){
	selectleft.length=0;
	var k=0;
	if(LastID!="0")	{
		selectleft.options[k]=new Option("返回上级...",GetParent(LastID,myArray));
		k++;
	}

	for(i=j;i<nCount;i++){
		if(myArray[i].ParentID==LastID){
			selectleft.options[k]=new Option(myArray[i].LabelDesc,myArray[i].LabelID);
			/*if(selectleft.options[k].value==fid){
				selectleft.selectedIndex=k;
			}*/
			k++;
		}
	}
	}else{
	select_add_hidden(selectleft,selectright,myhidden);
}
}

function ChangeOption3(Oselect,myArray,Otext){
	ChangeOption(Oselect,myArray);
	//Otext.value=Oselect.options[Oselect.selectedIndex].text;
}

//从2个用逗号分开的串填充select对象
function fillselectfromstr(s1,s2,Oselect){
	var mycode=s1.split(",");
	var mytext=s2.split(",");
	Oselect.length=0;
	var nCount=mycode.length+0;
	//alert("nCount="+nCount);
	for(i=0;i<nCount;i++){
			Oselect.options[i]=new Option(mytext[i],mycode[i]);
	}
}