/**
 * Defines a 'class' which manages option selections for
 * a set of product groups within a paoduct pack.  The
 * options for each group are managed by an OptionState
 * object
 */

function PackState() {
}

/** A stack of product options objects to manage each group individually */
PackState.prototype.groups = [];

/** A stack of products which exist in the group but are not controlled by a Group Controller */
PackState.prototype.staticProducts = [];

/** When submitting all forms in a group, this is assumed to be the name of the quantity drop-down */
PackState.prototype.qtyFldName = 'quantity';

/** Convenience: Return a reference to the form element which contains control */
PackState.prototype.getEnclosingForm = function (control) {
    if (control.tagName.toUpperCase() == 'FORM') {
        return control;
    }
    else if (control.parentNode) {
        return this.getEnclosingForm(control.parentNode);
    }
    else {
        return false;
    }
}
/**
 * Called to add group controllers.  Calls to this method are typically 
 * hard-coded in to the HTML source by the templating engine on the server
 */
PackState.prototype.addGroupController = function (locator, opt_keys) {
    if (form = document.getElementById(locator)) {
        opt = new OptionState(opt_keys);
        this.groups.push([form, opt]);
        return opt;
    }
}

/**
 * Called to add instance_id references for products which only have
 * one option, and therefore are not controlled by a GroupGontroller
 */
PackState.prototype.addStaticGroupMember = function (pid) {
    this.staticProducts.push([document.getElementById(pid), pid]);
}


/** Convenience, get the selected quantity in form */
PackState.prototype.getQuantity = function (form) {
    var selects = form.getElementsByTagName('select');
    for (var i = 0; i < selects.length; i++) {
        select = selects.item(i);
        if (select.name == this.qtyFldName) {
            return select.options[select.selectedIndex].value;
        }
    }
    return -1;
}


/** Convenience: return the group controller for Form form, or false */
PackState.prototype.getGroupController = function (form) {
    for (var i = 0; i < this.groups.length; i++) {
        if (this.groups[i][0] == form) {
            return this.groups[i][1];
        }
    }
    return false;
}

/**
 * Delegate to the checkAllOptionsSelected method on the group controller
 * associated with Form form
 */
PackState.prototype.checkAllOptionsSelected = function (form) {
    GC = this.getGroupController(form);
    if (GC) {
        return GC.checkAllOptionsSelected(form);
    }
}

/** Another delegate method. */
PackState.prototype.checkSelectedOptionCombination = function (node_url, fld, select) {
    form = this.getEnclosingForm(select);
    GC = this.getGroupController(form);
    if (GC) {
        return GC.checkSelectedOptionCombination(node_url, fld, select);
    }
}

/** Submit all items in all forms all at once! */
PackState.prototype.submitPack = function (button) {
    var ids = [];
    //First, ask each of the Group controllers if they're happy
    all = this.groups.concat(this.staticProducts);
    for (var i = 0; i < all.length; i++) {
        if ((i < this.groups.length) && (! all[i][1].isReadyToPost())) {
            alert("Please complete all product options");
            return false;
        }
        if (i < this.groups.length) {
            ids.push([all[i][1].targetProduct, this.getQuantity(all[i][0])]);
        }
        else {
            ids.push([all[i][1], this.getQuantity(all[i][0])]);
        }
    }
    //Now we've got a list of items and quantities, build a form with all this in and
    //submit the sukka!
    form = document.createElement("form");
    form.method = "POST";
    //Have a look at what action was set on the button
    action = "addtobasket";
    if (button.name.toLowerCase().indexOf("addtobaskethere") != -1) {
        action = "addtobaskethere";
    }
    else if (button.name.toLowerCase().indexOf("addtoemptybasket") != -1) {
        action = "addtoemptybasket";
    }
    else if (button.name.toLowerCase().indexOf("addtoemptybaskethere") != -1) {
        action = "addtoemptybaskethere";
    }
    if (window.location.toString().toLowerCase().indexOf(action) == -1) {
        form.action = window.location + ":" + action;
    }
    else {
        form.action = window.location;
    }
    for (var i = 0; i < ids.length; i++) {
        hf = document.createElement('input');
        hf.type = "hidden";
        hf.name = "multiple_id[" + ids[i][0] + "]";
        hf.value = ids[i][1];
        form.appendChild(hf);
    }
    //Get a handle on the body element
    var body = document.getElementsByTagName('body').item(0);
    body.appendChild(form);
    form.submit();
}

