/* Reza Salimain 1388-11-27 */
/* EnhancedNews Scripts      */

/* this function get listbox and limit count. and set cssClass name of overflow options */
function HighlightOverflowItems( theSel, limit )
{
	if ( theSel == null )
		return;
	for( i = 0; i < limit && limit <= theSel.length; i++ )
	{
		theSel.options[i].className = "";
	}
	
	for( i = limit; i < theSel.length; i++ )
	{
		theSel.options[i].className = "overflow-item";
	}
}

/* this function moves an option up or down in a listbox */
/* direction can be "up" or "down"                                    */
function listbox_move(listbox, direction) 
{
	var selIndex = listbox.selectedIndex;

	if(-1 == selIndex) {
		alert("Please select an option to move.");
		return;
	}

	var increment = -1;
	if(direction == 'up')
		increment = -1;
	else
		increment = 1;

	if((selIndex + increment) < 0 ||
		(selIndex + increment) > (listbox.options.length-1)) {
		return;
	}

	var selValue = listbox.options[selIndex].value;
	var selText = listbox.options[selIndex].text;
	listbox.options[selIndex].value = listbox.options[selIndex + increment].value
	listbox.options[selIndex].text = listbox.options[selIndex + increment].text

	listbox.options[selIndex + increment].value = selValue;
	listbox.options[selIndex + increment].text = selText;

	listbox.selectedIndex = selIndex + increment;
}

/* this function get the listbox and return the Camma Seprated Values  */
function listItemsToCVS( theSel )
{
	var result = '';
	for(i=0; i < theSel.length; i++)
	{
		result = result + theSel.options[i].value;
		result = result + ',';
	}
	result = result.substring(0, result.length - 1 );
	return result;				
}
	
/* this fonction moves item(s) from theSelFrom listbox to theSelTo listbox */
function MoveTo(theSelFrom, theSelTo)
{
	var selIndex = theSelFrom.selectedIndex;
	var selectedItems = new Array();

	if (selIndex != -1) {
		for(i = theSelFrom.length - 1; i >= 0; i--)
		{
			if(theSelFrom.options[i].selected)
			{
				selectedItems[theSelFrom.options[i].value] = theSelFrom.options[i].text;
				theSelFrom.options[i] = null;
			}
		}
		if (theSelFrom.length > 0) {
		theSelFrom.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
		}
	}
	
	/* insert in the second select */
	if (theSelTo.selectedIndex == -1)
		theSelTo.selectedIndex = 0 ;

	var x;					
		
	if (theSelTo.length == 0) 
	{
		var index = 0;
		var newOpt1;
		for( x in selectedItems )
		{
			newOpt1 = new Option(selectedItems[x], x);
			theSelTo.options[index] = newOpt1;
			index++;
		}	
		theSelTo.selectedIndex = 0;						
	} 
	else if ( theSelTo.selectedIndex != -1 ) 
	{
		var selText = new Array();
		var selValues = new Array();
		var selIsSel = new Array();
		var newCount = -1;
		var newSelected = -1;
		var i;
		
		for( i = 0; i < theSelTo.length; i++ )
		{
			newCount++;
			if (newCount == theSelTo.selectedIndex)
			{
				for( x in selectedItems )
				{
					//alert(x);
					selText[newCount] = selectedItems[x];
					selValues[newCount] = x;
					selIsSel[newCount] = false;

					newCount++;
				}															
				newSelected = newCount;
			}
			selText[newCount] = theSelTo.options[i].text;
			selValues[newCount] = theSelTo.options[i].value;
			selIsSel[newCount] = theSelTo.options[i].selected;
		}
		
		for(i = 0; i <= newCount; i++ )
		{
			var newOpt = new Option(selText[i], selValues[i]);
			theSelTo.options[i] = newOpt;
			theSelTo.options[i].selected = selIsSel[i];
		}
		
		// reza salimian, 1388-11-27 ; afer moving, i select the first option of target
		theSelTo.selectedIndex = theSelTo.length > 0 ? 0 : -1;
	}
}	
