/*
** Dynamic text manipulation routines
*/

/*
 * Sets the given text on the specified element
 *
 * element: The element or id of node to add text to
 * text: The text to add
 */
function awpxSetText(element, text)
{
    var el = awpxGetElement(element);
    if(!text)
    {
        text = "";
    }
    el.innerHTML = text;
}

/*
 * Sets the text from the given element to the
 * specified element.  This can be used to propagate text
 * from a form field, such as a text box, to a static
 * element.
 */
function awpxSetTextFromFormField(element, formField)
{
    awpxSetText(element, awpxGetElement(formField).value);
}

/*
 * Sets the text in the target element depending on a 
 * selection from a SELECT tag.  This first looks for the
 * value in the passed text array by indexing by the value.
 * If not found, then it tries by indexing by the label.
 * If that doesn't work, then it indexes by option index.
 * If multiple options are selected, then the given separator
 * is used.
 */
function awpxSetTextFromSelect(element, select, textArray, separator)
{
    var selectEl = awpxGetElement(select);
    var text = "";
    for(var i=0; i<selectEl.options.length; i++)
    {
        if(selectEl.options[i].selected)
        {
            var newText = textArray[selectEl.options[i].value];
            if(!newText)
            {
                newText = textArray[selectEl.options[i].text];
            }
            if(!newText)
            {
                newText = textArray[i];
            }
            if(newText)
            {
                if(separator && text)
                {
                    text += separator;
                }
                text += newText;
            }
        }
    }
    awpxSetText(element, text);
}
