function boxAction(box, action)
{
	var val = "";
	if (box)
	{
		if (! box[0])
		{
			if (action == "selected")
			{
				return box.checked;
			} else if (action == "value")
			{
				if (box.checked)
					val = box.value;
			} else if (action == "toggle")
			{
				box.checked = ! box.checked;
			}
		} else
		{
			for (var i=0; i<box.length; i++)
			{
				if (action == "selected")
				{
					if (box[i].checked)
						return box[i].checked;
				} else if (action == "value")
				{
					if (box[i].checked)
					{
						if (box[i].type == "radio")
						{
							val = box[i].value;
						} else if (box[i].type == "checkbox")
						{
							if (val != "")
								val = val + ",";	
							val = val + box[i].value;
						}
					}
				} else if (action == "toggle")
				{
					box[i].checked = ! box[i].checked;
				}
			}
		}
	}

    if (action == "selected")
        return false;
	else
        return val;
}

function checkboxSelected(chk)
{
	return boxAction(chk, "selected");
}

function getRadioValue(radio)
{
	return boxAction(radio, "value");
}
	
function getCheckBoxValue(chk)
{
	return boxAction(chk, "value");
}

function getCheckBoxRelativeValue(input, chkBox)
{
	var val = "";
	if (chkBox)
	{
		if (! chkBox[0])
		{
			if (chkBox.checked)
				val = input.value;
		} else
		{
			for (var i=0; i<chkBox.length; i++)
			{
				if (chkBox[i].checked)
				{
					if(val!="")
					{
					  val = val + ",";	
					}
					val = val + input[i].value;
					
				}
			}
		}
	}
	return val;
}

//
function toggleCheckBox(chk)
{		
    boxAction(chk, "toggle");
}

function toggleCheck(thisField,thisValue) {
	           checkSet = eval(thisField);			
	           if (checkSet)
                   {
			if(!checkSet[0]){
				checkSet.checked = !(checkSet.checked);
			}else{
				for (i=0;i<checkSet.length;i++) {
					if (checkSet[i].value == thisValue)
						checkSet[i].checked = !(checkSet[i].checked);
				}
			}
		    }
}

function toggleRadio(thisField,thisValue) {
	        radioSet = eval(thisField);
		if (radioSet){
			if(!radioSet[0]){
				radioSet.checked = !radioSet.checked;
			}else{
				for (i=0;i<radioSet.length;i++) {
					if (radioSet[i].value == thisValue)
						radioSet[i].checked = true;
				}
			}
		}
}

function getAllOptionValue(select)
{
	var val = "";
	var options = select.options;
	for (var i=0; i<options.length; i++)
	{   
		if (val != "")
			val = val + ",";	
		val = val + options[i].value;
	}
	return val;
}

function getSelectedOptionValue(select)
{
	var val = "";
	var options = select.options;
	for (var i=0; i<options.length; i++)
	{   
		if (options[i].selected)
		{
			if (val != "")
				val = val + ",";	
			val = val + options[i].value;
		}
	}
	return val;
}

function hasOption(select, op)
{ 
	for (var i=0; i<select.length; i++ )
    {
		if (select.options[i].value == op.value)
            return true;
    }
    
	return false;
}

function clearSelectStatus(select)
{
    //CLEAR
    for (var i=0; i<select.length; i++)
        select.options[j].selected = false;
}

function AppendSelectedOption(srcSelect, destSelect)
{ 
	for (var i=0; i<srcSelect.length; i++)
	{   
		if (srcSelect.options[i].selected)
		{  
			var op = srcSelect.options[i];
			if (hasOption(destSelect, op))
			   window.alert("此选项已选择。");
			else
			   destSelect.options[destSelect.length]= new Option(op.text, op.value);
		 }
	 }
              
     clearSelectStatus(srcSelect);
}

function removeSelectedOption(select)
{
	var options = select.options;
	for (var i=options.length-1; i>=0; i--)
	{   
		if (options[i].selected)
		{  
			options[i] = null;
		}
	}
}


/* 返回被选中元素个数，适用于checkbox和radio */
function getCheckedNum(nameOfCheck)
{
	var theNum=0;
	var theCheckInputs=document.getElementsByName(nameOfCheck);
	for (var i=0;i<theCheckInputs.length;i++)
	{
		if(theCheckInputs[i].checked) theNum++;
	}
	return theNum;
}

/* 返回被选中元素单个值，适用于checkbox和radio */
function getCheckedValue(nameOfCheck)
{
  var theValue="";
  var theCheckInputs=document.getElementsByName(nameOfCheck);
  for (var i=0;i<theCheckInputs.length;i++)
  {
 	if(theCheckInputs[i].checked){
	  if(theValue!="")
		 theValue += ",";
	  theValue += theCheckInputs[i].value;
	}
  }
  return theValue;
}