/**
 * BrochureRequest javascript file
 * @version $Revision: 1.1.2.1 $
 */

window.mediaOptions = new Object();

/**
 * Initializes infoday form
 * Appends two select boxes
 * @return void
 */
function initBrochureRequestForm()
{
    var el = document.getElementById('fldmedium_id');
    var dd, option_other;
    if (el) {
        // change id to 'parent_id'
        el.setAttribute('name', 'fldparent_medium_id');
        el.setAttribute('onchange', 'refreshBrochureRequestForm');

        dd = el;
        while (dd.tagName.toString().toLowerCase() != 'dd') dd = dd.parentNode;

        // add 'medium' form element:
        medium_dt = document.createElement('dt');
        medium_label = document.createElement('label');
        medium_label.appendChild(document.createTextNode('\u00A0')); // non-breaking space
        medium_dt.appendChild(medium_label);
        medium_select = document.createElement('select');
        medium_select.setAttribute('name', 'fldmedium_id');
        medium_select.setAttribute('id', 'fldchildmedium_id');
        medium_dd = document.createElement('dd');
        medium_dd.appendChild(medium_select);

        dd.parentNode.insertBefore(medium_dd, dd.nextSibling);
        dd.parentNode.insertBefore(medium_dt, dd.nextSibling);

        // move last item to child box
        option_other = document.createElement('option');
        option_other.setAttribute('value', '');
        option_other.appendChild(document.createTextNode(el.options[el.options.length - 1].text));
        //el.options[el.options.length - 1] = null;
        medium_select.appendChild(option_other);

        // process parent/child options
        var lastparent = 0;
        var i = 0;
        while (i < (el.options.length - 1)) {
            var option = el.options[i];
            if ((option.getAttribute('disabled') == 'disabled') || (option.className == 'parent')) {
                // it's a parent option
                lastparent = parseInt(option.value);
                option.removeAttribute('disabled');
                i++;
            } else {
                // it's a child option, append to array and delete it
                if (!window.mediaOptions[lastparent]) window.mediaOptions[lastparent] = new Object();
                window.mediaOptions[lastparent][option.value] = option.text;
                el.options[i] = null;
            }
        }

        el.setAttribute('onchange', 'refreshBrochureRequestForm');
        el.onchange = refreshBrochureRequestForm;
        refreshBrochureRequestForm(el);

    }

}

/**
 * Refreshes the form, updates options etc
 * @param DOMNode The select node that had its selection changed
 * @return void
 */
function refreshBrochureRequestForm()
{
    child = document.getElementById('fldchildmedium_id');
    if (child) {
        // remove all but the last
        while (child.options.length > 1) child.options[0] = null;

        el = document.getElementById('fldmedium_id');
        if (el) {

            parent_id = el.options[el.selectedIndex].value;

            if (window.mediaOptions[parent_id]) {
                child.removeAttribute('disabled');
                for (e in window.mediaOptions[parent_id]) {
                    option = document.createElement('option');
                    option.setAttribute('value', e);
                    option.appendChild(document.createTextNode(window.mediaOptions[parent_id][e]));
                    child.insertBefore(option, child.lastChild);
                }
                child.style.display = 'inline';
                child.setAttribute('name', 'fldmedium_id');
                el.setAttribute('name', 'fldparentmedium_id');
            } else {
                if (parent_id) {
                    // copy parent to child box
                    option = document.createElement('option');
                    option.setAttribute('value', parent_id);
                    option.appendChild(document.createTextNode(el.options[el.selectedIndex].text));
                    child.insertBefore(option, child.lastChild);
                }
                child.style.display = 'none';
                child.setAttribute('name', 'fldchildmedium_id');
                el.setAttribute('name', 'fldmedium_id');
            }
        }
        child.selectedIndex = 0;
    }
}
