function SelectHelper()
{
	
}

/**
 *	是否已经选择
 */
SelectHelper.prototype.isSelected = function(formID)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		return ( selectObj.selectedIndex != -1 )
	}
	else
	{
		return false;
	}
}

/**
 *	得到选中的Option对象
 */
SelectHelper.prototype.getSelectedOptionObj = function(formID)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		return selectObj.options[selectObj.selectedIndex];
	}
	else
	{
		return null;
	}
}

/**
 *	得到选中的option的value
 */
SelectHelper.prototype.getSelectedValue = function(formID)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		return selectObj.options[selectObj.selectedIndex].value;
	}
	else
	{
		return "";
	}
}

/**
 *	设置不选中任何值
 */
SelectHelper.prototype.setSelectNoneByID = function(formID)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		selectObj.options.selectedIndex = -1;
	}
}

/**	
 *	设置不选中任何值(多个Select)
 */
SelectHelper.prototype.setSelectNoneByIDs = function(formIDs)
{
	try
	{
		for (var i=0;i<formIDs.length;i++)
		{
			setSelectNoneByID(formIDs[i]); 
		} 
	}
	catch(e)
	{
		alert("在执行setSelectNoneByIDs时，遇到如下错误：\n" + e.description);
	}
}

/**	
 *	移除options
 */
SelectHelper.prototype.removeOptions = function(formID,remainIndex)
{
	remainIndex = (null == remainIndex) ? 0 : remainIndex;
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		var optionsLength  = selectObj.options.length;
		for (var i = optionsLength - 1; i >= remainIndex; i--) 
		{
			selectObj.options[i] = null;
		}
	}
}

/**	
 *	根据value移除option
 */
SelectHelper.prototype.removeOptionByValue = function(formID,value)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		var optionsLength  = selectObj.options.length;
		for (var i = optionsLength - 1; i >= remainIndex; i--) 
		{
			if (selectObj.options[i].value == value){
				selectObj.options[i] = null;
			}
		}
	}
}

/**
 *	把多选列表中选中的option去掉
 */
SelectHelper.prototype.removeSelectedOptions = function(formID)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		var optionsLength  = selectObj.options.length;
		for (var i = selectObj.options.length - 1 ; i >= 0 ; i--) 
		{
			if (selectObj.options[i].selected)
			{
				selectObj.options[i] = null;
			}
		}
	}
}

/**	
 *	跟据value选中对象
 */
SelectHelper.prototype.setSelectedByValue = function(formID,value)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		var selectOptionsObj = selectObj.options;
		for (var i=0;i<selectOptionsObj.length;i++){
			if (selectOptionsObj[i].value == value) 
			{
				selectOptionsObj[i].selected = true;
			}
		}
	}
}

SelectHelper.prototype.setSelectedByAttribute = function(formID,value,attributeName)
{
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj)
	{
		var selectOptionsObj = selectObj.options;
		for (var i=0;i<selectOptionsObj.length;i++){
			if (eval("selectOptionsObj[i]." + attributeName + " == value")) 
			{
				selectOptionsObj[i].selected = true;
			}
		}
	}
}

/**
 *	双列表选择,把A选择的列表移动至B
 */
SelectHelper.prototype.moveSelectedOptionFromAToB = function(formA,formB,removeFun,isOrder)
{
	var formAObj = this.getSelectObj(formA);
	var formBObj = this.getSelectObj(formB);
	for ( var m = 0 ; m < formAObj.options.length ; m++)
	{
		if (formAObj.options[m].selected)
		{
			if (!this.isExistOption(formB,formAObj.options[m].value,"value")){
				var oOption = document.createElement("option");
				oOption.value = formAObj.options[m].value;
				oOption.text  = formAObj.options[m].text;
				if (isOrder != null && isOrder){
					for (var k=0;k<formAObj.options[m].attributes.length;k++){
						if (formAObj.options[m].attributes[k].specified){
							if (formAObj.options[m].attributes[k].nodeName != "value"){
								oOption.setAttribute(formAObj.options[m].attributes[k].nodeName,formAObj.options[m].attributes[k].nodeValue);
							}
						}
					}
				}
				formBObj.options.add(oOption);
				if (isOrder != null && isOrder){
					for (var j=0;j<formBObj.options.length ; j++){
						if (parseInt(formAObj.options[m].orderIndex) <= parseInt(formBObj.options[j].orderIndex)){
							for (var l=j;l < formBObj.options.length ; l++){
								this.exchangeOption(formBObj.options[l],formBObj.options[formBObj.options.length - 1]);
							} 
							break;
						}
					}
				}
			}
		}
	}
	for (var i = formAObj.options.length - 1 ; i >= 0 ; i--) 
	{
		if (formAObj.options[i].selected)
		{
			if (removeFun != null){
				if (eval(removeFun + "(formAObj.options[i])")){
					formAObj.options[i] = null;
				}
			}else{
				formAObj.options[i] = null;
			}
		}
	}
}

/**
 * 交换option
 * 为了提升效率，写死attribute
 */
SelectHelper.prototype.exchangeOption = function(optionA,optionB){
	var text  = optionA.text;
	optionA.text = optionB.text;
	optionB.text = text;
	var value = optionA.value;
	optionA.setAttribute("value",optionB.value);
	optionB.setAttribute("value",value);
	try{
		var tabid = optionA.tabid;
		optionA.setAttribute("tabid",optionB.tabid);
		optionB.setAttribute("tabid",tabid);
		var parenttabid = optionA.parenttabid;
		optionA.setAttribute("parenttabid",optionB.parenttabid);
		optionB.setAttribute("parenttabid",parenttabid);
		var oIndex = optionA.orderIndex;
		optionA.setAttribute("orderIndex",optionB.orderIndex);
		optionB.setAttribute("orderIndex",oIndex);
	}catch(e){}
	
	/*	
	var optionAAttributeNames = new Array();
	//先从optionA开始找
	for (var k=0;k<optionA.attributes.length;k++){
		if (optionA.attributes[k].specified){
			//两个option都有的
			if (eval("null != optionB." + optionB.attributes[k].nodeName)){
				var attributeValue = optionA.attributes[k].nodeValue;
				optionA.setAttribute(optionA.attributes[k].nodeName,optionB.getAttribute(optionA.attributes[k].nodeName));
				optionB.setAttribute(optionA.attributes[k].nodeName,attributeValue);
			}else{
				optionB.setAttribute(optionA.attributes[k].nodeName,optionA.attributes[k].nodeValue);
				optionAAttributeNames.push(optionA.attributes[k].nodeName);
				optionA.setAttribute(optionA.attributes[k].nodeName,null);
			}
		}
	}
	//再从optionA开始找
	for (var k=0;k<optionB.attributes.length;k++){
		if (optionB.attributes[k].specified){
			//不是两个option都有的
			if (!eval("null != optionA." + optionB.attributes[k].nodeName)){
				//是否是optionsA copy来的
				var isCopyFromA = false;
				for (var i=0;i<optionAAttributeNames.length;i++){
					if (optionAAttributeNames[i] == optionB.attributes[k].nodeName){
						isCopyFromA = true;
					}
				}
				//如果不是optionA拿过来的
				if ( !isCopyFromA ){
					optionA.setAttribute(optionB.attributes[k].nodeName,optionB.attributes[k].nodeValue);
					optionB.setAttribute(optionB.attributes[k].nodeName,null)
				}
			}
		}
	}
	*/
}

SelectHelper.prototype.copyOption = function(copyTo,beCopy){
	copyTo.value = beCopy.value;
	copyTo.text  = beCopy.text;
	for (var k=0;k<beCopy.attributes.length;k++){
		if (beCopy.attributes[k].specified){
			if (beCopy.nodeName != "value"){
				copyTo.setAttribute(beCopy.attributes[k].nodeName,beCopy.attributes[k].nodeValue);
			}
		}
	}
}

/**
 * 为select的每个option创建一个order序列
 */
SelectHelper.prototype.createOrder = function(formID){
	
	var selectObj = this.getSelectObj(formID);
	for ( var i = 0 ; i < selectObj.options.length ; i++)
	{
		selectObj.options[i].setAttribute("orderIndex",i);
	}
}

/**
 * 为selectA的每个option创建一个order序列，跟据dependSelect创建
 */
SelectHelper.prototype.createOrderBySelect = function(needCreateFormID,dependFormID){
	var needCreateObj = this.getSelectObj(needCreateFormID);
	var dependFormObj = this.getSelectObj(dependFormID);
	for ( var i = 0 ; i < needCreateObj.options.length ; i++)
	{
		var needOption = needCreateObj.options[i];
		for (var m=0;m<dependFormObj.options.length;m++){
			if (needOption.value == dependFormObj.options[m].value){
				if (dependFormObj.options[m].orderIndex != null){
					needCreateObj.options[i].setAttribute("orderIndex",dependFormObj.options[m].orderIndex);
				}
			}
		}
		if (needOption.orderIndex == null){
			alert("selectHelper在执行createOrderBySelect时出错，系统在depend的列表中找不到相应的数据！");
			return false;
		}
	}
	return true;
}

/**
 * 添加option
 * 若有其它attribute需要添加，请传入listAttributeObj
 * listAttributeObj为一个array(),其中每个array元素为一个Object，每个Object有两个属性 name和value
 */
SelectHelper.prototype.addOption = function(formID,value,text,listAttributeObj)
{
	var selectObj = this.getSelectObj(formID);
	var oOption   = document.createElement("option");
	oOption.value = value;
	oOption.text  = text;
	if (listAttributeObj != null){
		for (var i=0;i<listAttributeObj.length;i++){
			var attributeObj = listAttributeObj[i];
			oOption.setAttribute(attributeObj.name,attributeObj.value);
		}
	}
	selectObj.options.add(oOption);
}

SelectHelper.prototype.selectAllOptions = function(formID)
{
	var selectObj = this.getSelectObj(formID);
	for (var i = 0; i < selectObj.options.length ; i ++ )
	{
		selectObj.options[i].selected = true;
	}
}

/**
 *	得到选择的多个对象,
 *	return: Ojbect array() , in Array , one Object is format : object.value object.text
 */
SelectHelper.prototype.getSelectObjs = function(formID)
{
	var selectedRolesObj = this.getSelectObj(formID);
	var returnObj        = new Array();
	for (var i = 0 ; i < selectedRolesObj.options.length ; i++)
	{
		var selectedOption   = new Object();
		selectedOption.value = selectedRolesObj.options[i].value;
		selectedOption.text  = selectedRolesObj.options[i].text;	
		for (var m=0;m<selectedRolesObj.options[i].attributes.length;m++){
			if (selectedRolesObj.options[i].attributes[m].specified){
				if (selectedRolesObj.options[i].attributes[m].nodeName != "value"){
					eval("selectedOption." + selectedRolesObj.options[i].attributes[m].nodeName + "=" + selectedRolesObj.options[i].attributes[m].nodeValue);
				}
			}
		}
		returnObj[i] = selectedOption;
	}	
	return returnObj;
}

/**
 *	把needMoveID列表中,存在于dependID列表中的值的option去掉
 */
SelectHelper.prototype.removeOptionsDependOnSelect = function(needMoveID,dependID,conditionFun)
{
	var needMoveObj = this.getSelectObj(needMoveID);
	var dependObj   = this.getSelectObj(dependID);
	for (var i = needMoveObj.options.length - 1 ; i >= 0 ; i--) 
	{
		for(var m = dependObj.options.length - 1 ; m >= 0 ; m--)
		{
			if (needMoveObj.options[i].value == dependObj.options[m].value)
			{
				if (conditionFun != null){
					if (eval(conditionFun + "(needMoveObj.options[i],dependObj.options[m])")){
						needMoveObj.options[i] = null;
					}
				}else{
					needMoveObj.options[i] = null;
				}
				break;
			}
		}
	}
}

/**	
 *	得到Select对象
 */
SelectHelper.prototype.getSelectObj = function(formID)
{
	var selectObj = document.getElementById(formID);
	if ( null != selectObj )
	{
		return selectObj;
	}
	else
	{
		alert("找不到你需要设置的 " + formID + " 列表框！");
		return null;				
	}
}
/**
 * 在select的所有option中，是否存在attributeName的值为value的option
 */
SelectHelper.prototype.isExistOption = function(formID,value,attributeName){
	var isExist = false;
	var selectObj = this.getSelectObj(formID);
	if (null != selectObj )
	{
		for (var i = 0 ; i < selectObj.options.length ; i++)
		{
			var optionObj 	 = selectObj.options[i];	
			var temp 		 = optionObj.getAttribute(attributeName);
			if (temp != null){
				if (temp == value){
					isExist = true;
					break;
				}
			}else{
				alert("selectHelper的isExistOptionValue找不到你所传入的attrbuteName");
			}
		}
	}
	return isExist;
}

var selectHelper = new SelectHelper();
