 /*
  * 文件描述：关于list列表框的一些工具方法
  * 使用本功能时form不能有reset，否则会有异常
  *
  * 主要方法：
  *          1， moveUp(oSelect,isToTop) －－－－－－－－－－－－ 向上移动一个list列表框的选中项目，
  *                                                                可以支持多选移动，可以设置是否移动到顶层
  *          2， moveDown(oSelect,isToBottom)－－－－－－－－－－ 向下移动一个list列表框的选中项目，
  *                                                                可以支持多选移动，可以设置是否移动到底层
  *          3， moveSelected(oSourceSel,oTargetSel) －－－－－－ 在两个列表框之间转移数据
  *          4， moveAll(oSourceSel,oTargetSel)－－－－－－－－－ 转移两个列表框之间的全部数据
  *          5， deleteSelectItem(oSelect) －－－－－－－－－－－ 删除所选的项目
  *          6， orderItemValue(oSelect) －－－－－－－－－－－ 根据顺序对项目的值排序
  *
  *CHHD架构SELECT移动公用方法
  */

 /**
  * 使选中的项目上移
  *
  * oSelect: 源列表框
  * isToTop: 是否移至选择项到顶端，其它依次下移，
  *          true为移动到顶端，false反之，默认为false
  */
 function moveUp(oSelect,isToTop)
 {
     //默认状态不是移动到顶端
     if(isToTop == null)
         var isToTop = false;

     //如果是多选------------------------------------------------------------------
     if(oSelect.multiple)
     {
         for(var selIndex=0; selIndex<oSelect.options.length; selIndex++)
         {
             //如果设置了移动到顶端标志
             if(isToTop)
             {
                 if(oSelect.options[selIndex].selected)
                 {
                     var transferIndex = selIndex;
                     while(transferIndex > 0 && !oSelect.options[transferIndex - 1].selected)
                     {
                         replaceOpt(oSelect.options[transferIndex], oSelect.options[transferIndex - 1]);
                         transferIndex --;
                     }
                 }
             }
             //没有设置移动到顶端标志
             else
             {
                 if(oSelect.options[selIndex].selected)
                 {
                     if(selIndex > 0)
                     {
                         if(!oSelect.options[selIndex - 1].selected){
                             replaceOpt(oSelect.options[selIndex], oSelect.options[selIndex - 1]);
                         }
                     }
                 }
             }
         }
     }
     //如果是单选--------------------------------------------------------------------
     else
     {
         var selIndex = oSelect.selectedIndex;
         if(selIndex <= 0)
             return;
         //如果设置了移动到顶端标志
         if(isToTop)
         {
             while(selIndex > 0)
             {
                 replaceOpt(oSelect.options[selIndex], oSelect.options[selIndex - 1]);
                 selIndex --;
             }
         }
         //没有设置移动到顶端标志
         else
             replaceOpt(oSelect.options[selIndex], oSelect.options[selIndex - 1]);
     }
 }

/**
  * 使选中的项目下移
  *
  * oSelect: 源列表框
  * isToTop: 是否移至选择项到底端，其它依次上移，
  *          true为移动到底端，false反之，默认为false
  */
 function moveDown(oSelect,isToBottom)
 {
     //默认状态不是移动到顶端
     if(isToBottom == null)
         var isToBottom = false;

     var selLength = oSelect.options.length - 1;

     //如果是多选------------------------------------------------------------------
     if(oSelect.multiple)
     {
         for(var selIndex=oSelect.options.length - 1; selIndex>= 0; selIndex--)
         {
             //如果设置了移动到顶端标志
             if(isToBottom)
             {
                 if(oSelect.options[selIndex].selected)
                 {
                     var transferIndex = selIndex;
                     while(transferIndex < selLength && !oSelect.options[transferIndex + 1].selected)
                     {
                         replaceOpt(oSelect.options[transferIndex], oSelect.options[transferIndex + 1]);
                         transferIndex ++;
                     }
                 }
             }
             //没有设置移动到顶端标志
             else
             {
                 if(oSelect.options[selIndex].selected)
                 {
                     if(selIndex < selLength)
                     {
                         if(!oSelect.options[selIndex + 1].selected)
             								 replaceOpt(oSelect.options[selIndex], oSelect.options[selIndex + 1]);
                     }
                 }
             }
         }
     }
     //如果是单选--------------------------------------------------------------------
     else
     {
         var selIndex = oSelect.selectedIndex;
         if(selIndex >= selLength - 1)
             return;
         //如果设置了移动到顶端标志
         if(isToBottom)
         {
             while(selIndex < selLength - 1)
             {
             		 replaceOpt(oSelect.options[selIndex], oSelect.options[selIndex + 1]);
                 selIndex ++;
             }
         }
         //没有设置移动到顶端标志
         else
             replaceOpt(oSelect.options[selIndex], oSelect.options[selIndex + 1]);
     }
 }
 
 //前移
 function moveLeft(oSelect){
 	 //如果是多选------------------------------------------------------------------
     if(oSelect.multiple)
     {
         for(var selIndex=oSelect.options.length - 1; selIndex>= 0; selIndex--)
         {
             if(oSelect.options[selIndex].selected)
             {
	             var ct = countChar(oSelect.options[selIndex].text);	             
                 if(ct >= 4)
                 {
                 	var tmpStr = oSelect.options[selIndex].text;
                 	for(i=0; i<4; i++){
                 		tmpStr = tmpStr.replace(/^-/, "");
                 	}
                 	oSelect.options[selIndex].text = tmpStr;
                 }
                 oSelect.options[selIndex].selected = true;
             }
         }
     }
     //如果是单选--------------------------------------------------------------------
     else
     {
         var selIndex = oSelect.selectedIndex;
         var ct = countChar(oSelect.options[selIndex].text);	             
         if(ct >= 4)
         {
         	var tmpStr = oSelect.options[selIndex].text;
         	for(i=0; i<ct-4; i++){
         		tmpStr = tmpStr.replace(/^-/, "");
         	}
         	oSelect.options[selIndex].text = tmpStr;
         }
         oSelect.options[selIndex].selected = true;
     }
 }
 
 //后移
 //judge 是否使第一项不能后移
 function moveRight(oSelect, judge){
 	 //如果是多选------------------------------------------------------------------
     if(oSelect.multiple)
     {
         for(var selIndex=oSelect.options.length - 1; selIndex>= 0; selIndex--)
         {
             if(oSelect.options[selIndex].selected)
             {
             	var tmpStr = oSelect.options[selIndex].text;
             	oSelect.options[selIndex].text = padLeft(tmpStr, "-", 4);
         		oSelect.options[selIndex].selected = true;
             }
         }
     }
     //如果是单选--------------------------------------------------------------------
     else
     {
		var selIndex = oSelect.selectedIndex;
     	var tmpStr = oSelect.options[selIndex].text;
     	oSelect.options[selIndex].text = padLeft(tmpStr, "-", 4);
		oSelect.options[selIndex].selected = true;
     }
 }
 

/**
  *插入
  *只有同时选中两个option时，才能执行该操作
  *mode 向上插入或者向下插入
  *     up 为向上插入， down为向下插入
  */
 function insertSelect(oSelect, mode){
     if(getSelectNum(oSelect) != 2){
     		alert("请选择两个对象");
     		return;     
     }
     if(!checkNull(mode)){
     		alert("无法获取到插入模式");
     		return;
     }	
     var SelIndex = new Array();
     var SelObj = new Array();
     var AryIndex = 0;
     for(i=0; i<oSelect.options.length; i++){
		 		if(oSelect.options(i).selected){
					SelIndex[AryIndex] = i;
					SelObj[AryIndex] = oSelect.options(i);
					AryIndex++;
				}
		 }
		 if(mode == "up"){
		 		oSelect.remove(SelIndex[1]);
		 		oSelect.add(SelObj[1], SelIndex[0]+1);
		 }else{
		 		oSelect.remove(SelIndex[0]);
		 		oSelect.add(SelObj[0], SelIndex[1]);
		 }
 }

/**
  * 移动select的部分内容,必须存在value，此函数以value为标准进行移动
  *
  * oSourceSel: 源列表框对象
  * oTargetSel: 目的列表框对象
  * toAdd: 是否添加到列表中
  * toDel: 是否删除源列表中的相
  */
 function moveSelected(oSourceSel,oTargetSel,toAdd,toDel)
 {

   if(oSourceSel.options.length <= 0){
   	 alert("请选中要操作的数据");
   	 return;
   }else{
   	 var flag = false;
   	 for(var l=0;l<oSourceSel.options.length;l++){
   	 	 if(oSourceSel.options[l].selected){
   	 	 	flag = true;
   	 	 }
   	 }
   	 if(flag == false){
   	 	alert("请选中要操作的数据");
   	 	return;
   	 }
   }
 	 if(!toDel){
	     if(!compareOptions(oSourceSel,oTargetSel)){
	     	return;
	     }
     }
     
     //建立存储value和text的缓存数组
     var arrSelValue = new Array();
     var arrSelText = new Array();
     //此数组存贮选中的options，以value来对应
     var arrValueTextRelation = new Array();
     var index = 0;//用来辅助建立缓存数组

     //存储源列表框中所有的数据到缓存中，并建立value和选中option的对应关系
     for(var i=0; i<oSourceSel.options.length; i++)
     {
         if(oSourceSel.options[i].selected)
         {
             //存储
             arrSelValue[index] = oSourceSel.options[i].value;
             arrSelText[index] = oSourceSel.options[i].text;
             //建立value和选中option的对应关系
             arrValueTextRelation[arrSelValue[index]] = oSourceSel.options[i];
             index ++;
         }
     }
	arrSelValue.sort();
     //添加缓存的数据到目的列表框中，并删除源列表框中的对应项
     for(var i=0; i<arrSelText.length; i++)
     {
     	 if(toAdd){
	         //添加
	         var oOption = document.createElement("option");
	         oOption.text = arrSelText[i];
	         oOption.value = arrSelValue[i];
	         oTargetSel.add(oOption);
         }
         if(toDel){
	         //删除源列表框中的对应项
	         oSourceSel.removeChild(arrValueTextRelation[arrSelValue[i]]);
         }
     }
 }

/**
  * 移动select的整块内容
  *
  * oSourceSel: 源列表框对象
  * oTargetSel: 目的列表框对象
  * toAdd: 是否添加到列表中
  * toDel: 是否删除源列表中的相
  */
 function moveAll(oSourceSel,oTargetSel,toAdd,toDel)
 {
     //建立存储value和text的缓存数组
     var arrSelValue = new Array();
     var arrSelText = new Array();

     //存储所有源列表框数据到缓存数组
     for(var i=0; i<oSourceSel.options.length; i++)
     {
         arrSelValue[i] = oSourceSel.options[i].value;
         arrSelText[i] = oSourceSel.options[i].text;
     }
	 if(toAdd){
	     //将缓存数组的数据添加到目的select中
	     for(var i=0; i<arrSelText.length; i++)
	     {
	         var oOption = document.createElement("option");
	         oOption.text = arrSelText[i];
	         oOption.value = arrSelValue[i];
	         oTargetSel.add(oOption);
	     }
     }
	 if(toDel){
	     //清空源列表框数据，完成移动
	     oSourceSel.innerHTML = "";
     }
 }

/**
  * 删除选定项目
  *
  * oSelect: 源列表框对象
  */
 function deleteSelectItem(oSelect)
 {
     for(var i=0; i<oSelect.options.length; i++)
     {
         if(i>=0 && i<=oSelect.options.length-1 && oSelect.options[i].selected)
         {
             oSelect.options[i] = null;
             i --;
         }
     }
 }
 
 /**
  * 根据顺序对项目的值排序
  * value加上- ：根据-的数量确定当前节点的层次
  */
function orderSelectItem(obj){
	if(obj!=null && obj.options!=null && obj.options.length!=0){
  		for (oppos=0; oppos<obj.options.length; oppos++){
      		obj.options[oppos].value = padLeft(obj.options[oppos].value, 
      			"-", countChar(obj.options[oppos].text));
      	}
  	}
}

//替换OPTION对象
function replaceOpt(selectOpt, replaceOpt){
		var occc = selectOpt.text;
		var occl = selectOpt.value;
		var occ = replaceOpt.text;
		var ocl = replaceOpt.value;
			selectOpt.text = occ;
			selectOpt.value = ocl;
			replaceOpt.text = occc;
			replaceOpt.value = occl;
			replaceOpt.selected=true;
			selectOpt.selected=false;
}
