// ('transportFrameId', 'scriptURL' 'group', 'animal')

function selectChain(frameId, scriptURL, selects)
{
    this.frame        = document.getElementById(arguments[0]);
    this.URL          = arguments[1];
    this.lastSelect   = null;
    this.selectsById  = Array();
    this.lastSelect   = null;
    this.firstSelect  = null;
    this.query        = '';

    for (var i = 2; i < arguments.length; i++) {
        var selectId = arguments[i];
        var s = document.getElementById(selectId);
        if (!s) {
            alert("Missing select field: " + selectId);
        } else {
            s.chain    = this;
            s.onchange = this.handler;
            s.newValue = s.value;
            s.oldValue = s.value;
            s.next     = null;

            if (this.lastSelect) {
                this.lastSelect.next = s;
            }
            this.lastSelect = s;
            if (!this.firstSelect) this.firstSelect = s;
            this.selectsById[selectId] = s;
        }
    }
}

selectChain.prototype.addToFetchList = function (selectId, value)
{
    if (this.query) this.query += '&';
    this.query += selectId + '=' + value;
}

selectChain.prototype.fetchList = function ()
{
    if (this.query) {
        this.frame.src = this.URL + '?' + this.query;
        //alert(this.URL + '?' + this.query);
        this.query = '';
    }
}

selectChain.prototype.resetFirst = function ()
{
    this.addToFetchList(this.firstSelect.id, 0);
}

/**
 * Checks for select changes and calls for new lists
 */
selectChain.prototype.handler = function ()
{
    this.newValue = (this.value == '' ? null : this.value);
    this.chain.resetToTail(this.next);
    this.chain.fetch();
}

/**
 * Sets newValue to null to the end of chain starting after headName
 */
selectChain.prototype.resetToTail = function(s)
{
    if (s) {
        s.newValue = null;
        s.chain.resetToTail(s.next);
    }
}

/**
 * Selects select item
 * @param string select name
 * @param string item id
 */
selectChain.prototype.set = function(selectId, value)
{
    var s = this.selectsById[selectId];

    if (!s) {
        alert("Missing select " + selectId);
        return false;
    }

    for (i = s.length - 1; i >= 0;  i--) {
        if (s.options[i].value == value) break;
    }
    s.selectedIndex = i;
    s.newValue = (value == '' ? null : value);
}

selectChain.prototype.fetch = function()
{
    var sid;
    var s;

    for (sid in this.selectsById) {
        s = this.selectsById[sid];
        if (s.newValue != s.oldValue && s.next) {
            this.addToFetchList(s.next.id, s.newValue);
        }
    }
    this.fetchList();
}

/**
 *  Populates select with given values list.
 *  @param string selectName
 *  @param string selectValue
 *  @param string selectLabel
 *  ...
 */
selectChain.prototype.setSelectList = function (selectId)
{
    // remove old
    var s = this.selectsById[selectId];
    if (!s) {
        alert("Missing select selectId");
        return;
    }

    for (var i = s.length - 1; i >= 1; i--) { // leave default value
        s.options[i] = null;
    }

    // populate new
    var optionValue, optionLabel;
    for (var i = 1; i < arguments.length; i = i + 2) {
        optionValue = arguments[i];
        optionLabel = arguments[i+1];
        //alert (s.id + ' ' + s.newValue + ' ' + optionValue);
        s.options[s.length] = new Option(optionLabel, optionValue, false, (s.newValue == optionValue));
    }
}

