/**************************************************************************************************************
	stefano guagnini
	1/2/2002
 **************************************************************************************************************
 
	Object DDLManager 
	-----------------
 
 **************************************************************************************************************/

function _DDLManager()
{
	this.Clear			= _DDL_Clear;
	this.Add			= _DDL_Add;
	this.Select			= _DDL_Select;
	this.GetSelection	= _DDL_GetSelection;
	this.Length			= _DDL_Length;
	this.GetItem		= _DDL_GetItem;
	this.Remove			= _DDL_Remove;
	this.Move			= _DDL_Move;
	this.Copy			= _DDL_Copy;
	this.Sort			= _DDL_Sort;
	this.GetAttribute	= _DDL_GetAttribute;
}

//*************************************************************************************************************

function _DDL_Clear( pObj )
{
	while ( pObj.length )
		pObj.remove(0);
}

//*************************************************************************************************************

function _DDL_Length(pObj)
{
	return pObj.length;
}

//*************************************************************************************************************

function _DDL_Add( pObj, value, text /* = null, attrName0, attrValue0, ..., attrNameN, attrValueN,*/ )
{
	if ( typeof(value) == "object" )
	{
		//	insert array of values with text=value
		_DDL_Clear( pObj );
		for ( k=0; k<value.length; k++ )
		{
			var el = document.createElement("OPTION");
			el.text = el.value= value[k];
			pObj.options.add(el);
		}
	}	
	else
	{
		//	insert a new single item...
		var el = document.createElement("OPTION");
		el.text = text;
		el.value= value;
		for ( var i=3; i < arguments.length; i+=2 )
			el.setAttribute( arguments[i], arguments[i+1] );
		pObj.options.add(el);
		return el;
	}
}


//*************************************************************************************************************

function _DDL_Remove( pObj, szAttribute /*="value"*/, pSearch )
{
	if ( arguments.length == 2 )
	{
		pSearch = szAttribute;
		szAttribute = "value";
	}
	
	if ( typeof(pSearch) == "string" ) 
		pSearch = pSearch.toLowerCase();
	
	for ( var i=0; i < pObj.length; i++ )
	{
		var attr = pObj[i].getAttribute(szAttribute);
		if ( typeof(attr) == "string" )
			attr = attr.toLowerCase();

		if ( attr == pSearch )
		{
			pObj[i] = null;
			return true;
		}
	}
	return false;
}

//*************************************************************************************************************

function _DDL_Select( pObj, szAttribute /*="value"*/, pSearch )
{
	if ( arguments.length == 2 )
	{
		pSearch = szAttribute;
		szAttribute = "value";
	}

	if ( typeof(pSearch) != "object" )
	{
		var ret = -1;
		//	single value selection

		if ( typeof(pSearch) == "string" ) 
			pSearch = pSearch.toLowerCase();

		for ( var i=0; i < pObj.length; i++ )
		{
			var attr = pObj[i].getAttribute(szAttribute);
			if ( typeof(attr) == "string" )
				attr = attr.toLowerCase();

			if ( attr == pSearch )
			{
				if ( InputManager.IsInfo(pObj) )
				{

					InputManager.MakeOptional(pObj);
					pObj.selectedIndex = i;
					InputManager.MakeInfo(pObj);
				}
				else pObj.selectedIndex = i;

				if ( pObj.multiple )
					pObj[i].selected = true;
				
				ret++;
			}
		}
		return ret;
	}
	else
	{
		//	multiple selection
		for ( var i=0; i < pObj.length; i++ )
			for ( var j=0; j < pSearch.length; j++ )
				if ( pSearch[j] == pObj[i].getAttribute(szAttribute) )
					pObj[i].selected = true;
	}
}


//*************************************************************************************************************


function _DDL_GetSelection( pObj, szAttribute /* = "value" */ )
{
	if ( szAttribute == null ) 
		szAttribute = "value";
		
	return (pObj.selectedIndex >= 0) ? pObj[pObj.selectedIndex].getAttribute(szAttribute) : "";
}


//*************************************************************************************************************


function _DDL_GetItem( pObj, szAttribute /* = "value" */, szSearch )
{
	if ( arguments.length == 2 )
	{
		szSearch = szAttribute;
		szAttribute = "value";
	}
	for ( var i=0; i < pObj.length; i++ )
		if ( pObj[i].getAttribute(szAttribute) == szSearch )
			return i;
	return null;
}


//*************************************************************************************************************


function _DDL_GetAttribute( pObj, szSearchAttr, szSearchStr, szRetAttr /* = "value" */ )
{
	if ( szRetAttr == null )
		szRetAttr = "value";
	var i = _DDL_GetItem(pObj, szSearchAttr, szSearchStr );
	return i == null ? null : pObj[i].getAttribute(szRetAttr);
}


//*************************************************************************************************************

function _DDL_Move( pSrc, pDest, bAll )
{
	if ( bAll )
	{
		for ( var k=0; k<pSrc.length; )
		{
			if ( pSrc[k].className != "disabledoption" )
			{
				var el = document.createElement("OPTION");
				el = pSrc[k];
				pSrc[k] = null;
				pDest.options.add(el);
			}
			else k++;
		}
	}
	else if ( pSrc.multiple )
	{
		for ( var k=0; k<pSrc.length; )
		{
			if ( pSrc[k].selected && pSrc[k].className != "disabledoption" )
			{
				var el = document.createElement("OPTION");
				el = pSrc[k];
				pSrc[k] = null;
				pDest.options.add(el);
			}
			else k++;
		}
	}	
	else if ( pSrc.selectedIndex >= 0 && pSrc[pSrc.selectedIndex].className != "disabledoption" )
	{
		var el = document.createElement("OPTION");
		el = pSrc[pSrc.selectedIndex];
		pSrc[pSrc.selectedIndex] = null;
		pDest.options.add(el);
	}
}

//*************************************************************************************************************

function _DDL_Copy( pSrc, pDest )
{
	DDLManager.Clear(pDest);
	for( var q=0; q < pSrc.length; q++ )
	{
		var el = document.createElement("OPTION");
		el.text = pSrc[q].text;
		el.value = pSrc[q].value;
		pDest.options.add(el);
	}
}

//*************************************************************************************************************

function _DDL_Sort( pObj, bAscending /* = true */ )
{
	if ( bAscending == null ) 
		bAscending = true;
	
	if ( bAscending )
	{
		for (var i = 0; i < pObj.options.length-1; i++)
			for (var j = i+1; j < pObj.options.length; j++)
				if (pObj.options(i).text > pObj.options(j).text)
					pObj.children(i).swapNode(pObj.children(j));
	}
	else
	{
		for (var i = 0; i < pObj.options.length-1; i++)
			for (var j = pObj.options.length-1; j > i; j--)
				if (pObj.options(i).text < pObj.options(j).text)
					pObj.children(i).swapNode(pObj.children(j));
	}					

}


//*************************************************************************************************************

var DDLManager = new _DDLManager();

//*************************************************************************************************************
