// EditInPlace.js must be included prior to this script for it to work

// Global array to hold all of the inline edit objects. The key is the
// name of the input element and the value is the EditInPlace object
var editElements = new Array();




/*
 * Creates a text field for inline editing
 *
 * @param	Object;	The object to create the edit field in
 * @param	String;	The name to give the new input element
 * @param	Mixed;	The value to set to the new element
 * @param	Number;	The width of the new element
 * @return	Bool;	TRUE if object was created, NULL on error,
 *						else FALSE
 */
function createInlineText( oElement, sName, mValue, nSize )
{
	if( !isObject(oElement) || !isString(sName) )
		return null;
		
	if( nSize < 1 )
		nSize = 100;
	
	
	// Make sure that we only create an object once, so changes and the such are
	// saved
	if( !isObject(editElements[sName]) )
	{	
		editElements[sName] = new EditInPlace( oElement );
		
		editElements[sName].width = nSize;
		editElements[sName].lines = 1;
		editElements[sName].type = 'text';
		editElements[sName].inputName = sName;
		
		return true;
		
	}
	
	return false;
	
}


/*
 * Creates a textarea field for inline editing
 *
 * @param	Object;	The object to create the edit field in
 * @param	String;	The name to give the new input element
 * @param	Mixed;	The value to set to the new element
 * @param	Number;	The width of the new element
 * @param	Number; The number of rows to display
 * @return	Bool;	TRUE if object was created, NULL on error,
 *						else FALSE
 */
function createInlineTextArea( oElement, sName, mValue, nSize, nRows )
{
  	// We can use 'createInlineText' for most of this, then modify the
	// type and number of lines
	if( createInlineText( oElement, sName, mValue, nSize ) )
	{
		editElements[sName].type = 'textarea';
		editElements[sName].lines = nRows;

		return true;
	}
	
	return false;
}

/*
 * Creates a drop-down list for inline editing
 *
 * @param	Object;	The object to create the edit field in
 * @param	Array;	An array containing the items to create the
*				list from, with both [VALUE] and [TEXT] attributes
 * @param	String;	The name to give the new input element
 * @param	Mixed;	The value to have selected
 * @param	String;	The text of the option selected
 * @return	Bool;	TRUE if object was created, NULL on error,
 *						else FALSE
 */
function createInlineSelect( oElement, aOptionList, sName, mValue, sText )
{
	if( !isObject(oElement) || !isString(sName) )
		return null;
	
	if( ( !mValue || mValue.length < 1 ) && sText )
	{
		// Find which value was selected based on the text
		for( sub in aOptionList )
		{
			if( sText == aOptionList[sub]['TEXT'] )
				mValue = aOptionList[sub]['VALUE'];
		}		
	}
		
	// Make sure that we only create an object once, so changes and the such are
	// saved
	if( !isObject(editElements[sName]) )
	{	
		editElements[sName] = new EditInPlace( oElement );
		
		//new Option(text, value, defaultSelected, selected)
		//editElements[sName].width = nSize;
		//editElements[sName].lines = 1;
		editElements[sName].type = 'select';
		editElements[sName].inputName = sName;
		editElements[sName].value = mValue;
		
		i = 0;
		opts = new Array();
		
		for( sub in aOptionList )
		{
			opts[i] = new Option( aOptionList[sub]['TEXT'], aOptionList[sub]['VALUE'], '', ( aOptionList[sub]['VALUE'] == mValue ) );
			i++;
		}
		
		editElements[sName].options = opts;
		
		return true;
		
	}
	
	return false;
}
	
