/*jslint devel: false, browser: true, undef: true, unparam: true, sloppy: true, vars: true, white: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
/*global $ */

var SSApp = {};

// set the keyboard focus to the textField, being careful not to try if the textField isn't present
SSApp.selectTextField = function(textField)
{
    if (!textField || !$(textField)) {
        return;
    }
    var style = $(textField).getStyle('display');
    if (style === null || style === 'none') {
        return;
    }
    style = $(textField).getStyle('visibility');
    if (style === null || style === 'hidden') {
        return;
    }

    // note that we may still have trouble if the text element is hidden because its parent is hidden
    // ie will complain in this case

    // See bug 329
    textField.setAttribute('autocomplete', 'off');

    textField.focus();
    textField.select();
};

/**
   textAreaId is the name (id) of the textarea element.  This trims it down to maxLength chars
   to fit the limit we have set in the DB (typically 4096 chars).
*/
SSApp.trimTextArea = function(textAreaId, maxLength)
{
    var textAreas = document.getElementsByTagName('textarea');
    var i, textAreaValue;

    for (i = 0; i !== textAreas.length; i++) {
        if (textAreas[i].id === textAreaId) {
            textAreaValue = textAreas[i].value.trim();
            if (textAreaValue.length >= maxLength) {
                textAreaValue = textAreaValue.substring(0, maxLength - 1);
            }
            textAreas[i].value = textAreaValue;
        }
    }
};

