Forms = {};


/**
 * Enhance all forms in all ways.
 */
Forms.enhance = function() {
    Forms.enhanceSelectOthers();
};


/**
 * Enhance all "select other" form widgets on the page.
 */
Forms.enhanceSelectOthers = function() {
    $('form .selectotherchoice .inputs').each(function() {Forms.enhanceSelectOther(this);});
};


/**
 * Enhance a single "select other" form widget.
 */
Forms.enhanceSelectOther = function(e) {

    // Find the interesting elements.
    var select = $('select', e);
    var otherInput = $('input', e);

    // Look up the "other" option value, it should be unique.
    var selectOptions = select.get(0).options;
    var otherValue = selectOptions[selectOptions.length-1].value;

    // Set the disabled state of the input now.
    otherInput.attr('disabled', select.get(0).value != otherValue);

    // Install a 'change' event handler on the select.
    select.change(function() {
            otherInput.attr('disabled', this.value != otherValue);
            });
};


// Enhance forms once the document is ready.
$(Forms.enhance);

