dojo.require('dojo.date.locale');
dojo.require('dojo.date.stamp');
dojo.require('dijit.form.Button');
dojo.require('dijit.form.TextBox');
dojo.require('dijit.form.FilteringSelect');
dojo.require('dijit.form.Textarea');
dojo.require('dijit.Tooltip');
dojo.require('dijit.ProgressBar');
dojo.require('openils.acq.Lineitem');
dojo.require('openils.acq.PO');
dojo.require('openils.acq.Picklist');
dojo.require('openils.widget.AutoFieldWidget');
dojo.require('dojo.data.ItemFileReadStore');
dojo.require('openils.widget.ProgressDialog');
dojo.require('openils.PermaCrud');

dojo.requireLocalization('openils.acq', 'acq');
var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
const XUL_OPAC_WRAPPER = 'chrome://open_ils_staff_client/content/cat/opac.xul';
var li_exportable_attrs = ["issn", "isbn", "upc"];

var fundLabelFormat = [
    '<span class="fund_${0}">${1} (${2})</span>', 'id', 'code', 'year'
];
var fundSearchFormat = ['${0} (${1})', 'code', 'year'];

function nodeByName(name, context) {
    return dojo.query('[name='+name+']', context)[0];
}


var liDetailBatchFields = ['fund', 'owning_lib', 'location', 'collection_code', 'circ_modifier', 'cn_label'];
var liDetailFields = liDetailBatchFields.concat(['barcode', 'note']);
var fundStyles = {
    "stop": "color: #c00; font-weight: bold;",
    "warning": "color: #c93;"
};

function AcqLiTable() {

    var self = this;
    this.liCache = {};
    this.plCache = {};
    this.poCache = {};
    this.relCache = {};
    this.haveFundClass = {}
    this.fundBalanceState = {};
    this.realDfaCache = {};
    this.virtDfaCounts = {};
    this.virtDfaId = -1;
    this.dfeOffset = 0;
    this.claimEligibleLidByLi = {};
    this.claimEligibleLid = {};
    this.toggleState = false;
    this.tbody = dojo.byId('acq-lit-tbody');
    this.selectors = [];
    this.noteAcks = {};
    this.authtoken = openils.User.authtoken;
    this.pcrud = new openils.PermaCrud();
    this.rowTemplate = this.tbody.removeChild(dojo.byId('acq-lit-row'));
    this.copyTbody = dojo.byId('acq-lit-li-details-tbody');
    this.copyRow = this.copyTbody.removeChild(dojo.byId('acq-lit-li-details-row'));
    this.copyBatchRow = dojo.byId('acq-lit-li-details-batch-row');
    this.copyBatchWidgets = {};
    this.liNotesTbody = dojo.byId('acq-lit-notes-tbody');
    this.liNotesRow = this.liNotesTbody.removeChild(dojo.byId('acq-lit-notes-row'));
    this.realCopiesTbody = dojo.byId('acq-lit-real-copies-tbody');
    this.realCopiesRow = this.realCopiesTbody.removeChild(dojo.byId('acq-lit-real-copies-row'));
    this._copy_fields_for_acqdf = ['owning_lib', 'location'];
    this.skipInitialEligibilityCheck = false;
    this.invoiceLinkDialogManager = new InvoiceLinkDialogManager("li");
    this.claimDialog = new ClaimDialogManager(
        liClaimDialog, finalClaimDialog, this.claimEligibleLidByLi,
        function(li) {    /* callback that fires when claims are made */
            self.fetchClaimInfo(li.id(), /* force update */ true);
        }
    );

    dojo.connect(acqLitLiActionsSelector, 'onChange', 
        function() { 
            self.applySelectedLiAction(this.attr('value')) 
            acqLitLiActionsSelector.attr('value', '_');
        });

    acqLitCreatePoSubmit.onClick = function() {
        if (self._confirmPoPrepaySituation()) {
            acqLitPoCreateDialog.hide();
            self._createPO(acqLitPoCreateDialog.getValues());
        } else {
            return false;
        }
    }

    acqLitSavePlButton.onClick = function() {
        acqLitSavePlDialog.hide();
        self._savePl(acqLitSavePlDialog.getValues());
    }

    acqLitCancelLiStateButton.onClick = function() {
        acqLitChangeLiStateDialog.hide();
    }
    acqLitSaveLiStateButton.onClick = function() {
        acqLitChangeLiStateDialog.hide();
        self._updateLiState(acqLitChangeLiStateDialog.getValues(), acqLitChangeLiStateDialog.attr('state'));
    }


    dojo.byId('acq-lit-select-toggle').onclick = function(){self.toggleSelect()};
    dojo.byId('acq-lit-info-back-button').onclick = function(){self.show('list')};
    dojo.byId('acq-lit-copies-back-button').onclick = function(){self.show('list')};
    dojo.byId('acq-lit-notes-back-button').onclick = function(){self.show('list')};
    dojo.byId('acq-lit-real-copies-back-button').onclick = function(){self.show('list')};

    this.reset = function(keep_selectors) {
        while(self.tbody.childNodes[0])
            self.tbody.removeChild(self.tbody.childNodes[0]);
        self.noteAcks = {};
        self.relCache = {};

        if (!keep_selectors)
            self.selectors = [];
    };
    
    this.setNext = function(handler) {
        var link = dojo.byId('acq-lit-next');
        if(handler) {
            dojo.style(link, 'visibility', 'visible');
            link.onclick = handler;
        } else {
            dojo.style(link, 'visibility', 'hidden');
        }
    };

    this.setPrev = function(handler) {
        var link = dojo.byId('acq-lit-prev');
        if(handler) {
            dojo.style(link, 'visibility', 'visible'); 
            link.onclick = handler; 
        } else {
            dojo.style(link, 'visibility', 'hidden');
        }
    };

    this.show = function(div) {
        openils.Util.hide('acq-lit-table-div');
        openils.Util.hide('acq-lit-info-div');
        openils.Util.hide('acq-lit-li-details');
        openils.Util.hide('acq-lit-notes-div');
        openils.Util.hide('acq-lit-real-copies-div');
        switch(div) {
            case 'list':
                openils.Util.show('acq-lit-table-div');
                break;
            case 'info':
                openils.Util.show('acq-lit-info-div');
                break;
            case 'copies':
                openils.Util.show('acq-lit-li-details');
                break;
            case 'real-copies':
                openils.Util.show('acq-lit-real-copies-div');
                break;
            case 'notes':
                openils.Util.show('acq-lit-notes-div');
                break;
            default:
                if(div) 
                    openils.Util.show(div);
        }
    }

    this.hide = function() {
        this.show(null);
    }

    this.toggleSelect = function() {
        if(self.toggleState) 
            dojo.forEach(self.selectors, function(i){i.checked = false});
        else 
            dojo.forEach(self.selectors, function(i){i.checked = true});
        self.toggleState = !self.toggleState;
    };


    /** @param all If true, assume all are selected */
    this.getSelected = function(all) {
        var selected = [];
        var indices = {};   /* use to uniqify. needed in paging situations. */
        dojo.forEach(self.selectors, 
            function(i) { 
                if(i.checked || all)
                    indices[i.parentNode.parentNode.getAttribute('li')] = true;
            }
        );
        return openils.Util.objectProperties(indices).map(
            function(liId) { return self.liCache[liId]; }
        );
    };

    this.setRowAttr = function(td, liWrapper, field, type) {
        var val = liWrapper.findAttr(field, type || 'lineitem_marc_attr_definition') || '';
        td.appendChild(document.createTextNode(val));
    };

    this.setClaimPolicyControl = function(li, row) {
        if (!self.claimPolicyPicker) {
            self.claimPolicyPicker = true; /* prevents a race condition */
            new openils.widget.AutoFieldWidget({
                "parentNode": "acq-lit-li-claim-policy",
                "fmClass": "acqclp",
                "selfReference": true,
                "dijitArgs": {"required": true}
            }).build(function(w) { self.claimPolicyPicker = w; });
        }

        if (!row) row = this._findLiRow(li);

        var actViewPolicy = nodeByName("action_view_claim_policy", row);
        if (li.claim_policy())
            actViewPolicy.innerHTML = localeStrings.CHANGE_CLAIM_POLICY;

        if (!actViewPolicy.onclick) {
            actViewPolicy.onclick = function() {
                if (li.claim_policy())
                    self.claimPolicyPicker.attr("value", li.claim_policy());
                liClaimPolicyDialog.show();
                liClaimPolicySave.onClick = function() {
                    self.changeClaimPolicy(
                        [li], self.claimPolicyPicker.attr("value"),
                        function() {
                            self.setClaimPolicyControl(li, row);
                            liClaimPolicyDialog.hide();
                        }
                    );
                }
            };
        }
    };

    this.fetchClaimInfo = function(liId, force, callback, row) {
        this._fetchLineitem(
            liId, function(full) {
                self.liCache[full.id()] = full;
                self.checkClaimEligibility(full, callback, row);
            }, force
        );
    }

    /**
     * Inserts a single lineitem into the growing table of lineitems
     * @param {Object} li The lineitem object to insert
     */
    this.addLineitem = function(li, skip_final_placement) {
        this.liCache[li.id()] = li;

        // sort the lineitem notes on edit_time
        if(!li.lineitem_notes()) li.lineitem_notes([]);

        var liWrapper = new openils.acq.Lineitem({lineitem:li});
        var row = self.rowTemplate.cloneNode(true);
        row.setAttribute('li', li.id());
        var tds = dojo.query('[attr]', row);
        dojo.forEach(tds, function(td) {self.setRowAttr(td, liWrapper, td.getAttribute('attr'), td.getAttribute('attr_type'));});
        dojo.query('[name=source_label]', row)[0].appendChild(document.createTextNode(li.source_label()));

        var identifier =
            liWrapper.findAttr("isbn", "lineitem_marc_attr_definition") ||
            liWrapper.findAttr("upc", "lineitem_marc_attr_definition");

        // XXX media prefix for added content
        if (identifier) {
            nodeByName("jacket", row).setAttribute(
                "src", "/opac/extras/ac/jacket/small/" + identifier
            );
        }

        nodeByName("liid", row).innerHTML += li.id();

        if(li.eg_bib_id()) {
            openils.Util.show(nodeByName('catalog', row), 'inline');
            nodeByName("catalog_link", row).onclick = this.generateMakeRecTab(li.eg_bib_id());
        } else {
            openils.Util.show(nodeByName('link_to_catalog', row), 'inline');
            nodeByName("link_to_catalog_link", row).onclick = function() { self.drawBibFinder(li) };
        }

        nodeByName("worksheet_link", row).href =
            oilsBasePath + "/acq/lineitem/worksheet/" + li.id();

        dojo.query('[attr=title]', row)[0].onclick = function() {self.drawInfo(li.id())};
        dojo.query('[name=copieslink]', row)[0].onclick = function() {self.drawCopies(li.id())};
        dojo.query('[name=noteslink]', row)[0].onclick = function() {self.drawLiNotes(li)};

        if (!this.skipInitialEligibilityCheck)
            this.fetchClaimInfo(li.id(), false, null, row);

        this.updateLiNotesCount(li, row);

        this.setClaimPolicyControl(li, row);

        // show which PO this lineitem is a member of
        if(li.purchase_order() && !this.isPO) {
            var po = 
                this.poCache[li.purchase_order()] =
                this.poCache[li.purchase_order()] ||
                fieldmapper.standardRequest(
                    ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
                    {params: [
                        this.authtoken, li.purchase_order(), {
                            "flesh_price_summary": true,
                            "flesh_lineitem_count": true
                        }
                    ]});
            if(po && !this.isMeta) {
                openils.Util.show(nodeByName('po', row), 'inline');
                var link = nodeByName('po_link', row);
                link.setAttribute('href', oilsBasePath + '/acq/po/view/' + li.purchase_order());
                link.innerHTML += po.name();
            }
        }

        // show which picklist this lineitem is a member of
        if(li.picklist() && (this.isPO || this.isMeta || this.isUni)) {
            var pl = 
                this.plCache[li.picklist()] = 
                this.plCache[li.picklist()] || 
                fieldmapper.standardRequest(
                    ['open-ils.acq', 'open-ils.acq.picklist.retrieve'],
                    {params: [this.authtoken, li.picklist()]});
            if (pl) {
                if (pl.name() == "") {
                    openils.Util.show(nodeByName("bib_origin", row), "inline");

                } else {

                    openils.Util.show(nodeByName('pl', row), 'inline');
                    var link = nodeByName('pl_link', row);
                    link.setAttribute('href', oilsBasePath + '/acq/picklist/view/' + li.picklist());
                    link.innerHTML += pl.name();
                }
            }
        }

        var countNode = nodeByName('count', row);
        var count = li.item_count() || 0;
        if (typeof(this._copy_count_cb) == "function") {
            this._copy_count_cb(li.id(), count);
        }
        countNode.innerHTML = count;
        countNode.id = 'acq-lit-copy-count-label-' + li.id();

        // lineitem price
        var priceInput = dojo.query('[name=price]', row)[0];
        priceInput.value = li.estimated_unit_price() || '';
        priceInput.onchange = function() { self.updateLiPrice(priceInput, li) };

        // show either "mark received" or "unreceive" as appropriate
        this.updateLiState(li, row);

        if (!skip_final_placement) {
            self.tbody.appendChild(row);
            self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
        } else {
            return row;
        }
    };

    this._liCountClaims = function(li) {
        var total = 0;
        for (var i = 0; i < li.lineitem_details().length; i++)
            total += li.lineitem_details()[i].claims().length;
        return total;
    };

    this._findLiRow = function(li) {
        return dojo.query('tr[li="' + li.id() + '"]', "acq-lit-tbody")[0];
    };

    this.reconsiderClaimControl = function(li, row) {
        var option = nodeByName("action_manage_claims", row);
        var eligible = this.claimEligibleLidByLi[li.id()].length;
        var count = this._liCountClaims(li);
        if (!row) row = this._findLiRow(li);

        option.disabled = !(count || eligible);
        option.innerHTML =
            dojo.string.substitute(localeStrings.NUM_CLAIMS_EXISTING, [count]);
        option.onclick = function() { self.claimDialog.show(li); };
    };

    this.clearEligibility = function(li) {
        this.claimEligibleLidByLi[li.id()] = [];

        if (li.lineitem_details()) {
            li.lineitem_details().forEach(
                function(lid) { delete self.claimEligibleLid[lid.id()]; }
            );
        }

        if (this.copyCache) {
            var to_del = [];
            for (var k in this.copyCache) {
                if (this.copyCache[k].lineitem() == li.id())
                    to_del.push(k);
            }
            to_del.forEach(
                function(k) { delete self.claimEligibleLid[k]; }
            );
        }
    };

    this.checkClaimEligibility = function(li, callback, row) {
        this.clearEligibility(li);
        fieldmapper.standardRequest(
            ["open-ils.acq", "open-ils.acq.claim.eligible.lineitem_detail"], {
                "params": [openils.User.authtoken, {"lineitem": li.id()}],
                "async": true,
                "onresponse": function(r) {
                    if (r = openils.Util.readResponse(r)) {
                        self.claimEligibleLidByLi[li.id()].push(
                            r.lineitem_detail()
                        );
                        self.claimEligibleLid[r.lineitem_detail()] = true;
                    }
                },
                "oncomplete": function() {
                    self.reconsiderClaimControl(li, row);
                    if (typeof(callback) == "function")
                        callback();
                }
            }
        );
    };

    this.updateLiNotesCount = function(li, row) {
        if (!row) row = this._findLiRow(li);

        var has_notes = (li.lineitem_notes().filter(
                function(o) { return Boolean (o.alert_text()); }
            ).length > 0);

        /* U+2691 is the code point for a filled-in flag character */
        nodeByName("notes_alert_flag", row).innerHTML =
             has_notes ? "&#x2691;" : "";
        nodeByName("noteslink", row).style.fontStyle =
            has_notes ? "italic" : "normal";
        nodeByName("notes_count", row).innerHTML = li.lineitem_notes().length;
    };

    /* XXX NOT related to _updateLiState(). rethink */
    this.updateLiState = function(li, row) {
        if (!row) row = this._findLiRow(li);

        var actReceive = nodeByName("action_mark_recv", row);
        var actUnRecv = nodeByName("action_mark_unrecv", row);
        var actUpdateBarcodes = nodeByName("action_update_barcodes", row);
        var actHoldingsMaint = nodeByName("action_holdings_maint", row);

        var actNewInvoice = nodeByName('action_new_invoice', row);
        var actLinkInvoice = nodeByName('action_link_invoice', row);
        var actViewInvoice = nodeByName('action_view_invoice', row);

        nodeByName('action_view_history', row).onclick = 
            function() { location.href = oilsBasePath + '/acq/lineitem/history/' + li.id(); };

        var state_cell = nodeByName("li_state", row);

        if (li.state() == "cancelled") {
            if (typeof li.cancel_reason() == "object") {
                var holds_state = dojo.create(
                    "span", {
                        "style": "border-bottom: 1px dashed #000;",
                        "innerHTML": li.state()
                    }, state_cell, "only"
                );
                new dijit.Tooltip(
                    {
                        "label": "<em>" + li.cancel_reason().label() +
                            "</em><br />" + li.cancel_reason().description(),
                        "connectId": [holds_state]
                    }, dojo.create("span", null, state_cell, "last")
                );
            } else {
                state_cell.innerHTML = li.state(); // TODO i18n state labels
            }
        } else {
            state_cell.innerHTML = li.state(); // TODO i18n state labels
        }


        /* handle row coloring for based on LI state */
        openils.Util.removeCSSClass(row, /^oils-acq-li-state-/);
        openils.Util.addCSSClass(row, "oils-acq-li-state-" + li.state());

        /* handle links that appear/disappear based on whether LI is received */
        if (this.isPO) {
            var self = this;

            actNewInvoice.onclick = function() {
                location.href = oilsBasePath + '/acq/invoice/view?create=1&attach_li=' + li.id();
                nodeByName("action_none", row).selected = true;
            };
            actLinkInvoice.onclick = function() {
                self.invoiceLinkDialogManager.target = li;
                acqLitLinkInvoiceDialog.show();
                nodeByName("action_none", row).selected = true;
            };
            actViewInvoice.onclick = function() {
                location.href = oilsBasePath +
                    "/acq/search/unified?so=" +
                    base64Encode({"jub":[{"id": li.id()}]}) +
                    "&rt=invoice";
                nodeByName("action_none", row).selected = true;
            };

            actNewInvoice.disabled = false;
            actLinkInvoice.disabled = false;
            actViewInvoice.disabled = false;

            switch(li.state()) {
                case "on-order":
                    actReceive.disabled = false;
                    actReceive.onclick = function() {
                        if (self.checkLiAlerts(li.id()))
                            self.issueReceive(li);
                        nodeByName("action_none", row).selected = true;
                    };
                    return;

                case "received":
                    actUnRecv.disabled = false;
                    actUnRecv.onclick = function() {
                        if (confirm(localeStrings.UNRECEIVE_LI))
                            self.issueReceive(li, /* rollback */ true);
                        nodeByName("action_none", row).selected = true;
                    };
                    // TODO we should allow editing before receipt, in which case the
                    // test should be "if 1 or more real (acp) copies exist
                    actUpdateBarcodes.disabled = false;
                    actUpdateBarcodes.onclick = function() {
                        self.showRealCopyEditUI(li);
                        nodeByName("action_none", row).selected = true;
                    }
                    actHoldingsMaint.disabled = false;
                    actHoldingsMaint.onclick = self.generateMakeRecTab( li.eg_bib_id(), 'copy_browser', row );

                    return;
            }
        }
    };


    this._setAlertStore = function() {
        acqLitAlertAlertText.store = new dojo.data.ItemFileReadStore(
            {
                "data": acqliat.toStoreData(
                    this.pcrud.search(
                        "acqliat", {"id": {"!=": null}}
                    )
                )
            }
        );
        acqLitAlertAlertText.setValue(); /* make the store "live" */
        acqLitAlertAlertText._store_ready = true;
    };

    /**
     * Draws and shows the lineitem notes pane
     */
    this.drawLiNotes = function(li) {
        var self = this;

        if (!acqLitAlertAlertText._store_ready)
            this._setAlertStore();

        li.lineitem_notes(
            li.lineitem_notes().sort(
                function(a, b) { 
                    if(a.edit_time() < b.edit_time()) return 1;
                    return -1;
                }
            )
        );

        while(this.liNotesTbody.childNodes[0])
            this.liNotesTbody.removeChild(this.liNotesTbody.childNodes[0]);
        this.show('notes');

        acqLitCreateNoteSubmit.onClick = function() {
            var value = acqLitCreateNoteText.attr('value');
            if(!value) return;
            var note = new fieldmapper.acqlin();
            note.isnew(true);
            note.vendor_public(
                Boolean(acqLitCreateNoteVendorPublic.attr('checked'))
            );
            note.value(value);
            note.lineitem(li.id());

            self.updateLiNotes(li, note);
            acqLitCreateNoteVendorPublic.attr("checked", false);
            acqLitCreateNoteText.attr("value", "");
        }

        acqLitCreateAlertSubmit.onClick = function() {
            if (!acqLitAlertAlertText.item) {
                alert(localeStrings.ALERT_UNSELECTED);
                return;
            }

            var alert_text = new fieldmapper.acqliat().fromStoreItem(
                acqLitAlertAlertText.item
            );
            var value = acqLitAlertNoteValue.attr("value") || "";

            var note = new fieldmapper.acqlin();
            note.isnew(true);
            note.lineitem(li.id());
            note.value(value);
            note.alert_text(alert_text);

            self.updateLiNotes(li, note);
        }

        dojo.forEach(li.lineitem_notes(), function(note) { self.addLiNote(li, note) });
    }

    /**
     * Draws a single lineitem note in the notes pane
     */
    this.addLiNote = function(li, note) {
        if(note.isdeleted()) return;
        var self = this;
        var row = self.liNotesRow.cloneNode(true);
        nodeByName("value", row).innerHTML = note.value();
        var alert_node = nodeByName("alert_code", row);
        if (note.alert_text()) {
            alert_node.innerHTML = note.alert_text().code();
            if (note.alert_text().description()) {
                new dijit.Tooltip(
                    {
                        "connectId": [alert_node],
                        "label": note.alert_text().description()
                    }, dojo.create("span", null, alert_node, "after")
                );
            }
        }

        if (openils.Util.isTrue(note.vendor_public()))
            nodeByName("vendor_public", row).innerHTML =
                localeStrings.VENDOR_PUBLIC;

        nodeByName("delete", row).onclick = function() {
            note.isdeleted(true);
            self.liNotesTbody.removeChild(row);
            self.updateLiNotes(li);
        };

        if(note.edit_time()) {
            nodeByName("edit_time", row).innerHTML =
                dojo.date.locale.format(
                    dojo.date.stamp.fromISOString(note.edit_time()), 
                    {formatLength:'short'});
        }

        self.liNotesTbody.appendChild(row);
    }

    /**
     * Updates any new/changed/deleted notes on the server
     */
    this.updateLiNotes = function(li, newNote) {

        var notes;
        if(newNote) {
            notes = [newNote];
        } else {
            notes = li.lineitem_notes().filter(
                function(note) {
                    if(note.ischanged() || note.isnew() || note.isdeleted())
                        return note;
                }
            );
        }

        if(notes.length == 0) return;
        progressDialog.show();

        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.lineitem_note.cud.batch'],
            {   async : true,
                params : [this.authtoken, notes],
                onresponse : function(r) {
                    var resp = openils.Util.readResponse(r);

                    if(resp.complete) {

                        if(!newNote) {
                            // remove the old changed notes
                            var list = [];
                            dojo.forEach(li.lineitem_notes(), 
                                function(note) {
                                    if(!(note.ischanged() || note.isnew() || note.isdeleted()))
                                        list.push(note);
                                }
                            );
                            li.lineitem_notes(list);
                        }

                        progressDialog.hide();
                        self.updateLiNotesCount(li);
                        self.drawLiNotes(li);
                        return;
                    }

                    progressDialog.update(resp);
                    var newnote = resp.note;

                    if(!newnote.isdeleted()) {
                        newnote.isnew(false);
                        newnote.ischanged(false);
                        li.lineitem_notes().push(newnote);
                    }
                },
            }
        );
    }

    this.updateLiPrice = function(input, li) {

        var price = input.value;
        if(Number(price) == Number(li.estimated_unit_price())) return;

        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.lineitem.price.set'],
            {   async : false, // redundant w/ timeout
                timeout : 10,
                params : [this.authtoken, li.id(), price],
                oncomplete : function(r) {
                    openils.Util.readResponse(r);
                    li.estimated_unit_price(price); // update local copy
                }
            }
        );
    }

    this.removeLineitem = function(liId) {
        this.tbody.removeChild(dojo.query('[li='+liId+']', this.tbody)[0]);
        delete this.liCache[liId];
        //selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
    }

    this.drawInfo = function(liId) {
        if (!this._isRelatedViewer) {
            var d = dojo.byId("acq-lit-info-related");
            if (!this.relCache[liId]) {
                fieldmapper.standardRequest(
                    [
                        "open-ils.acq",
                        "open-ils.acq.lineitems_for_bib.by_lineitem_id.count"
                    ], {
                        "async": true,
                        "params": [openils.User.authtoken, liId],
                        "onresponse": function(r) {
                            self.relCache[liId] = openils.Util.readResponse(r);
                            nodeByName("related_number", d).innerHTML =
                                self.relCache[liId];
                            openils.Util[
                                self.relCache[liId] >1 ? "show" : "hide"
                            ](d);
                        }
                    }
                );
            } else {
                nodeByName("related_number", d).innerHTML = this.relCache[liId];
                openils.Util[this.relCache[liId] > 1 ? "show" : "hide"](d);
            }
        }

        this.show('info');
        openils.acq.Lineitem.fetchAttrDefs(
            function() { 
                self._fetchLineitem(liId, function(li){self._drawInfo(li);}); 
            } 
        );
    };

    this._fetchLineitem = function(liId, handler, force) {

        var li = this.liCache[liId];
        if(li && li.marc() && li.lineitem_details() && !force)
            return handler(li);
        
        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
            {   async: true,

                params: [self.authtoken, liId, {
                    flesh_attrs: true,
                    flesh_cancel_reason: true,
                    flesh_li_details: true,
                    flesh_notes: true,
                    flesh_fund_debit: true }],

                oncomplete: function(r) {
                    var li = openils.Util.readResponse(r);
                    self.liCache[liId] = li;
                    handler(li)
                }
            }
        );
    };

    this._drawInfo = function(li) {

        acqLitEditOrderMarc.onClick = function() { self.editOrderMarc(li); }

        if(li.eg_bib_id()) {
            openils.Util.hide('acq-lit-marc-order-record-label');
            openils.Util.hide(acqLitEditOrderMarc.domNode);
            openils.Util.show('acq-lit-marc-real-record-label');
        } else {
            openils.Util.show('acq-lit-marc-order-record-label');
            openils.Util.show(acqLitEditOrderMarc.domNode);
            openils.Util.hide('acq-lit-marc-real-record-label');
        }

        this.drawMarcHTML(li);
        this.infoTbody = dojo.byId('acq-lit-info-tbody');

        if(!this.infoRow)
            this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
        while(this.infoTbody.childNodes[0])
            this.infoTbody.removeChild(this.infoTbody.childNodes[0]);

        for(var i = 0; i < li.attributes().length; i++) {
            var attr = li.attributes()[i];
            var row = this.infoRow.cloneNode(true);

            var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
            var name = openils.acq.Lineitem.attrDefs[type].filter(
                function(a) {
                    return (a.code() == attr.attr_name());
                }
            ).pop().description();

            dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
            dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
            this.infoTbody.appendChild(row);
        }

        if (!this._isRelatedViewer) {
            nodeByName("rel_link", dojo.byId("acq-lit-info-related")).href =
                oilsBasePath + "/acq/lineitem/related/" + li.id();
        }

    };

    this.generateMakeRecTab = function(bib_id,default_view, row) {
        return function() {
            xulG.new_tab(
                XUL_OPAC_WRAPPER,
                {tab_name: localeStrings.XUL_RECORD_DETAIL_PAGE, browser:false},
                {
                    no_xulG : false, 
                    show_nav_buttons : true, 
                    show_print_button : true, 
                    opac_url : xulG.url_prefix(xulG.urls.opac_rdetail + '?r=' + bib_id),
                    default_view : default_view
                }
            );

            if(row) nodeByName("action_none", row).selected = true;
        }
    };

    this.drawMarcHTML = function(li) {
        var params = [null, true, li.marc()];
        if(li.eg_bib_id()) 
            params = [li.eg_bib_id(), true];

        fieldmapper.standardRequest(
            ['open-ils.search', 'open-ils.search.biblio.record.html'],
            {   async: true,
                params: params,
                oncomplete: function(r) {
                    dojo.byId('acq-lit-marc-div').innerHTML = 
                        openils.Util.readResponse(r);
                }
            }
        );
    }

    this.drawCopies = function(liId, force_fetch) {
        if (typeof force_fetch == "undefined")
            force_fetch = false;

        this.show('copies');
        var self = this;
        this.copyCache = {};
        this.copyWidgetCache = {};
        this.oldCopyWidgetCache = {};
        this.virtDfaCounts = {};
        this.realDfaCache = {};
        this.dfeOffset = 0;

        acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
        acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
        acqLitCopyCountInput.attr('value', '0');

        while(this.copyTbody.childNodes[0])
            this.copyTbody.removeChild(this.copyTbody.childNodes[0]);

        this._drawBatchCopyWidgets();

        this._drawDistribApplied(liId);

        this._fetchDistribFormulas(
            function() {
                openils.acq.Lineitem.fetchAttrDefs(
                    function() { 
                        self._fetchLineitem(liId, function(li){self._drawCopies(li);}, force_fetch); 
                    } 
                );
            }
        );
    };

    this._saveDistribAppliedTemplates = function() {
        if (!this._appliedDistribTemplate) {
            this._appliedDistribTemplate =
                dojo.byId("acq-lit-distrib-applied-tbody").
                    removeChild(dojo.byId("acq-lit-distrib-applied-row"));
            dojo.attr(this._appliedDistribTemplate, "id");
        }
    };

    this._drawDistribApplied = function(liId) {
        /* Build this table while hidden to prevent rendering artifacts */
        openils.Util.hide("acq-lit-distrib-applied-tbody");

        this._saveDistribAppliedTemplates();

        /* Remove any rows in the table from previous populations */
        dojo.query("tr[formula]", "acq-lit-distrib-applied-tbody").
            forEach(dojo.destroy);

        /* Unregister all dijits previously created (for some reason this isn't
         * covered by the above destroy calls). */
        dijit.registry.forEach(
            function(w) { if (/^dfa-/.test(w.id)) w.destroyRecursive(); }
        );

        /* Populate the table with our liId */
        var total = 0;
        fieldmapper.standardRequest(
            ["open-ils.acq",
            "open-ils.acq.distribution_formula_application.ranged.retrieve"],
            {
                "async": true,
                "params": [self.authtoken, liId],
                "onresponse": function(r) {
                    var dfa = openils.Util.readResponse(r);
                    if (dfa) {
                        total++;
                        self.realDfaCache[dfa.id()] = dfa;
                        self._drawDistribAppliedUnit(dfa);
                    }
                },
                "oncomplete": function() {
                    /* Reveal built table */
                    if (total) {
                        openils.Util.show(
                            "acq-lit-distrib-applied-tbody", "table-row-group"
                        );
                    }
                }
            }
        );
    };

    this._drawDistribAppliedUnit = function(dfa) {
        var new_row = false;
        var row = dojo.query(
            'tr[formula="' + dfa.formula().id() + '"]',
            "acq-lit-distrib-applied-tbody"
        )[0];

        if (!row) {
            new_row = true;
            row = dojo.clone(this._appliedDistribTemplate);
            dojo.attr(row, "formula", dfa.formula().id());
            dojo.query("th", row)[0].innerHTML = dfa.formula().name();
        }

        var td = dojo.query("td", row)[0];

        dojo.create("span", {"id": "dfa-button-" + dfa.id()}, td, "last");
        dojo.create("span", {"id": "dfa-tip-" + dfa.id()}, td, "last");

        if (new_row)
            dojo.place(row, "acq-lit-distrib-applied-tbody", "last");

        new dijit.form.Button(
            {
                "onClick": function() {
                    if (confirm(localeStrings.EXPLAIN_DFA_MGMT))
                        self.deleteDfa(dfa);
                },
                "label": "X",
                /* XXX I /cannot/ make the following work in as a CSS class
                 * for some reason. So frustrating... */
                "style": function(id) {
                     return (id > 0 ?
                        "font-weight: bold; color: #c00;" :
                        "color: #666;");
                     }(dfa.id()) + "margin: 0 6px;display: inline;"
            }, "dfa-button-" + dfa.id()
        );
        new dijit.Tooltip(
            {
                "connectId": ["dfa-button-" + dfa.id()],
                "label": dojo.string.substitute(
                    localeStrings.DFA_TIP, dfa.id() > 0 ? [
                        openils.User.formalName(dfa.creator()),
                        dojo.date.locale.format(
                            dojo.date.stamp.fromISOString(dfa.create_time()),
                            {"formatLength":"short"}
                        )
                    ] : [localeStrings.ITS_YOU, localeStrings.JUST_NOW]
                )
            }, "dfa-tip-" + dfa.id()
        );
    }

    this.deleteDfa = function(dfa) {
        if (dfa.id() > 0) { /* real */
            this.pcrud.eliminate(
                dfa, {
                    "async": true,
                    "oncomplete": function() {
                        self._removeDistribApplied(dfa.id());
                        delete self.realDfaCache[dfa.id()];
                    }
                }
            );
        } else { /* virtual */
            if (--(this.virtDfaCounts[dfa.formula().id()]) < 0)
            this.virtDfaCounts[dfa.formula().id()] = 0;
            /* hasn't been saved yet, so no need to do anything server side */
            this._removeDistribApplied(dfa.id());
        }

    };

    this._removeDistribApplied = function(dfaId) {
        var re = new RegExp("^dfa-\\w+-" + String(dfaId));
        dijit.registry.forEach(
            function(w) { if (re.test(w.id)) w.destroyRecursive(); }
        );
        this._removeDistribAppliedEmptyRows();
    };

    this._removeAllDistribAppliedVirtual = function() {
        /* Unregister dijits */
        dijit.registry.forEach(
            function(w) { if (/^dfa-\w+--/.test(w.id)) w.destroyRecursive(); }
        );
        this._removeDistribAppliedEmptyRows();
    };

    this._removeDistribAppliedEmptyRows = function() {
        /* Remove any rows with no DFA at all */
        dojo.query("tr[formula] td", "acq-lit-distrib-applied-tbody").forEach(
            function(o) {
                if (o.childNodes.length < 1) dojo.destroy(o.parentNode);
            }
        );
    };

    /**
     * Insert a new row into the distribution formula selection form
     */
    this._addDistribFormulaRow = function() {
        var self = this;

        if (!self.distribForms) {
            // no formulas, hide the form
            openils.Util.hide('acq-lit-distrib-formula-tbody');
            return;
        }

        if(!this.distribFormulaTemplate) 
            this.distribFormulaTemplate = 
                dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));

        var row = this.distribFormulaTemplate.cloneNode(true);
        dojo.place(row, "acq-lit-distrib-formula-tbody", "only");

        this.dfSelector = new dijit.form.FilteringSelect(
            {"labelAttr": "dynLabel", "labelType": "html"},
            nodeByName("selector", row)
        );
        this._updateFormulaStore();
        this.dfSelector.fetchProperties =
            {"sort": [{"attribute": "use_count", "descending": true}]};

        var apply = new dijit.form.Button(
            {"label": localeStrings.APPLY},
            nodeByName('set_button', row)
        ); 

        var reset = new dijit.form.Button(
            {"label": localeStrings.RESET_FORMULAE, "disabled": true},
            nodeByName("reset_button", row)  
        );

        dojo.connect(apply, 'onClick', 
            function() {
                var form_id = self.dfSelector.attr("value");
                if(!form_id) return;
                self._applyDistribFormula(form_id);
                reset.attr("disabled", false);
            }
        );

        dojo.connect(reset, 'onClick', 
            function() {
                self.restoreCopyFieldsBeforeDF();
                self.virtDfaCounts = {};
                self.virtDfaId = -1;
                self.dfeOffset = 0;
                self._updateFormulaStore();
                self._removeAllDistribAppliedVirtual();
                reset.attr("disabled", "true");
            }
        );

    };

    /**
     * Applies a distrib formula to the current set of copies
     */
    this._applyDistribFormula = function(formula) {
        if(!formula) return;

        formula = this.distribForms.filter(
            function(form) { return form.id() == formula; }
        )[0];

        var copyRows = dojo.query('tr', self.copyTbody);

        if (this.dfeOffset >= copyRows.length) {
            alert(localeStrings.OUT_OF_COPIES);
            return;
        }

        var entries_applied = 0;
        for(
            var rowIndex = this.dfeOffset;
            rowIndex < copyRows.length;
            rowIndex++
        ) {
            
            var row = copyRows[rowIndex];
            var copy_id = row.getAttribute('copy_id');
            var copyWidgets = this.copyWidgetCache[copy_id];
            var entryIndex = this.dfeOffset;
            var entry = null;

            // find the correct entry for the current row
            dojo.forEach(formula.entries(), 
                function(e) {
                    if(!entry) {
                        entryIndex += e.item_count();
                        if(entryIndex > rowIndex)
                            entry = e;
                    }
                }
            );

            if(entry) {
                
                //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" + 
                //  entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
    
                entries_applied++;
                this.saveCopyFieldsBeforeDF(copy_id);
                this._copy_fields_for_acqdf.forEach(
                    function(field) {
                        if(entry[field]()) {
                            copyWidgets[field].attr('value', (entry[field]()));
                        }
                    }
                );
            }
        }

        if (entries_applied) {
            this.virtDfaCounts[formula.id()] =
                ++(this.virtDfaCounts[formula.id()]) || 1;
            this._updateFormulaStore();
            this._drawDistribAppliedUnit(
                function(df) {
                    var dfa = new acqdfa();
                    dfa.formula(df); dfa.id(self.virtDfaId--); return dfa;
                }(formula)
            );
            this.dfeOffset += entries_applied;
        };
    };

    /**
     * This function updates the DF store for the dropdown so that use_counts
     * can reflect DF applications from this session before they're saved
     * server-side.
     */
    this._updateFormulaStore = function() {
        this.dfSelector.store = new dojo.data.ItemFileReadStore(
            {
                "data": self._labelFormulasWithCounts(
                    acqdf.toStoreData(self.distribForms)
                )
            }
        );
    };

    this.saveCopyFieldsBeforeDF = function(copy_id) {
        var self = this;
        if (!this.oldCopyWidgetCache[copy_id]) {
            var copyWidgets = this.copyWidgetCache[copy_id];

            this.oldCopyWidgetCache[copy_id] = {};
            this._copy_fields_for_acqdf.forEach(
                function(f) {
                    self.oldCopyWidgetCache[copy_id][f] =
                        copyWidgets[f].attr("value");
                }
            );
        }
    };

    this.restoreCopyFieldsBeforeDF = function() {
        var self = this;
        for (var copy_id in this.oldCopyWidgetCache) {
            this._copy_fields_for_acqdf.forEach(
                function(f) {
                    self.copyWidgetCache[copy_id][f].attr(
                        "value", self.oldCopyWidgetCache[copy_id][f]
                    );
                }
            );
        }
    };

    this._labelFormulasWithCounts = function(store_data) {
        for (var key in store_data.items) {
            var obj = store_data.items[key];
            obj.use_count = Number(obj.use_count); /* needed for sorting */

            if (this.virtDfaCounts[obj.id])
                obj.use_count = obj.use_count + Number(this.virtDfaCounts[obj.id]);

            obj.dynLabel = "<span class='acq-lit-distrib-form-use-count'>[" +
                obj.use_count + "]</span>&nbsp; " + obj.name;
        }
        return store_data;
    };

    /**
     * This method formerly would not refetch the DF formulas if they'd been
     * loaded already, but now it always re-fetches, since use_count changes.
     */
    this._fetchDistribFormulas = function(onload) {
        fieldmapper.standardRequest(
            ["open-ils.acq",
                "open-ils.acq.distribution_formula.ranged.retrieve.atomic"],
            {
                "async": true,
                "params": [openils.User.authtoken],
                "oncomplete": function(r) {
                    self.distribForms = openils.Util.readResponse(r);
                    if(!self.distribForms || self.distribForms.length == 0) {
                        self.distribForms = [];
                    }
                    self._addDistribFormulaRow();
                    onload();
                }
            }
        );
    }

    this._drawBatchCopyWidgets = function() {
        var row = this.copyBatchRow;
        dojo.forEach(liDetailBatchFields, 
            function(field) {
                if(self.copyBatchRowDrawn) {
                    self.copyBatchWidgets[field].attr('value', null);
                } else {
                    var widget = new openils.widget.AutoFieldWidget({
                        fmField : field,
                        fmClass : 'acqlid',
                        labelFormat : (field == 'fund') ? fundLabelFormat : null,
                        searchFormat : (field == 'fund') ? fundSearchFormat : null,
                        searchFilter : (field == 'fund') ? {"active": "t"} : null,
                        parentNode : dojo.query('[name='+field+']', row)[0],
                        orgLimitPerms : ['CREATE_PICKLIST'],
                        dijitArgs : {
                            "required": false,
                            "labelType": (field == "fund") ? "html" : null
                        },
                        noCache: (field == "fund"),
                        forceSync : true
                    });
                    widget.build(
                        function(w, ww) {
                            if (field == "fund" && w.store)
                                self._ensureCSSFundClasses(w.store);
                            self.copyBatchWidgets[field] = w;
                        }
                    );
                    if (field == "fund") {
                        dojo.connect(
                            widget.widget, "onChange", function(val) {
                                self._updateFundSelectorStyle(widget, val);
                            }
                        );
                    }
                }
            }
        );
        this.copyBatchRowDrawn = true;
    };

    this.batchCopyUpdate = function() {
        var self = this;
        for(var k in this.copyWidgetCache) {
            var cache = this.copyWidgetCache[k];
            dojo.forEach(liDetailBatchFields, function(f) {
                var newval = self.copyBatchWidgets[f].attr('value');
                if(newval) cache[f].attr('value', newval);
            });
        }
    };

    this._drawCopies = function(li) {
        var self = this;

        // this button sets the total number of copies for a given lineitem
        acqLitAddCopyCount.onClick = function() { 
            var count = acqLitCopyCountInput.attr('value');

            // add new rows
            while(self.copyCount() < count)
                self.addCopy(li); 
            
            // delete rows if necessary
            var diff = self.copyCount() - count;
            if(diff > 0) {
                var rows = dojo.query('tr', self.copyTbody).reverse().slice(0, diff);
                if(confirm(dojo.string.substitute(localeStrings.DELETE_LI_COPIES_CONFIRM, [diff]))) {
                    dojo.forEach(rows, function(row) {self.deleteCopy(row); });
                } else {
                    acqLitCopyCountInput.attr('value', self.copyCount()+'');
                }
            }
        }


        if(li.lineitem_details().length > 0) {
            dojo.forEach(li.lineitem_details(),
                function(copy) {
                    self.addCopy(li, copy);
                }
            );
        } else {
            self.addCopy(li);
        }
    };

    this.copyCount = function() {
        var count = 0;
        for(var id in this.copyCache) {
            if(!this.copyCache[id].isdeleted())
                count++;
        }
        return count;
    }

    this.virtCopyId = -1;
    this.addCopy = function(li, copy) {
        var row = this.copyRow.cloneNode(true);
        this.copyTbody.appendChild(row);
        var self = this;

        if(!copy) {
            copy = new fieldmapper.acqlid();
            copy.isnew(true);
            copy.id(this.virtCopyId--);
            copy.lineitem(li.id());
        }

        this.copyCache[copy.id()] = copy;
        row.setAttribute('copy_id', copy.id());
        self.copyWidgetCache[copy.id()] = {};

        acqLitCopyCountInput.attr('value', self.copyCount()+'');

        dojo.forEach(liDetailFields,
            function(field) {
                var searchFilter;
                if (field == "fund") {
                    searchFilter = (copy.fund() ?
                        {"-or": {"active": "t", "id": copy.fund()}} :
                        {"active" : "t"});
                } else {
                    searchFilter = null;
                }

                var readOnly = false;
                
                // TODO: Add support for changing the owning_lib after real copies have been made.  
                // owning_lib is order data as much as its item data
                if(copy.eg_copy_id() && ['owning_lib', 'location', 'circ_modifier', 'cn_label', 'barcode'].indexOf(field) >= 0) {
                    readOnly = true;
                }

                // TODO: add support for changing the fund after debits have been created
                // Note: invoicing allows the change
                if(copy.fund_debit() && field == 'fund') {
                    readOnly = true;
                }

                var widget = new openils.widget.AutoFieldWidget({
                    fmObject : copy,
                    fmField : field,
                    labelFormat : (field == 'fund') ? fundLabelFormat : null,
                    searchFormat : (field == 'fund') ? fundSearchFormat : null,
                    dijitArgs: {"labelType": (field == 'fund') ? "html" : null},
                    searchFilter : searchFilter,
                    noCache: (field == "fund"),
                    fmClass : 'acqlid',
                    parentNode : dojo.query('[name='+field+']', row)[0],
                    orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
                    readOnly : readOnly,
                });

                widget.build(
                    // make sure we capture the value from any async widgets
                    function(w, ww) { 

                        if (field == "fund" && w.store)
                            self._ensureCSSFundClasses(w.store);

                        if(!readOnly) 
                            copy[field](ww.getFormattedValue()) 

                        self.copyWidgetCache[copy.id()][field] = w;

                        dojo.connect(w, 'onChange', 
                            function(val) { 
                                if (field == "fund")
                                    self._updateFundSelectorStyle(widget, val);

                                if (!readOnly && (copy.isnew() || val != copy[field]())) {
                                    // prevent setting ischanged() automatically on widget load for existing copies
                                    copy[field](widget.getFormattedValue()) 
                                    copy.ischanged(true);
                                }
                            }
                        );
                    }
                );
            }
        );

        this.updateLidState(copy, row);
    };

    this._ensureCSSFundClass = function(id) {
        if (!this.fundStyleSheet) {
            dojo.create(
                "style", {"type": "text/css"},
                document.getElementsByTagName("head")[0], "last"
            );
            this.fundStyleSheet = document.styleSheets[
                document.styleSheets.length - 1
            ];
        }

        var cn = "fund_" + id;
        if (!this.haveFundClass[cn]) {
            fieldmapper.standardRequest(
                ["open-ils.acq", "open-ils.acq.fund.check_balance_percentages"],
                {
                    "params": [openils.User.authtoken, id],
                    "async": true,
                    "oncomplete": function(r) {
                        r = openils.Util.readResponse(r);
                        self.fundBalanceState[id] = r;
                        var style = "";
                        if (r[0] /* stop */)
                            style = fundStyles.stop;
                        else if (r[1] /* warning */)
                            style = fundStyles.warning;
                        self.fundStyleSheet.insertRule(
                            "." + cn + " { " + style + " }",
                            self.fundStyleSheet.cssRules.length
                        );
                        self.haveFundClass[cn] = true;
                    }
                }
            );
        }
    };

    this._ensureCSSFundClasses = function(store) {
        store.fetch({
            "query": {"id": "*"},
            "onItem": function(o) { self._ensureCSSFundClass(o.id[0]); }
        });
    };

    this._updateFundSelectorStyle = function(widget, fund_id) {
        openils.Util.removeCSSClass(widget.widget.domNode, /fund_\d+/);
        openils.Util.addCSSClass(widget.widget.domNode, "fund_" + fund_id);
    };

    this.updateLidState = function(copy, row) {
        if (typeof(row) == "undefined") {
            row = dojo.query(
                'tr[copy_id="' + copy.id() + '"]', this.copyTbody
            )[0];
        }

        var self = this;
        var recv_link = nodeByName("receive", row);
        var unrecv_link = nodeByName("unreceive", row);
        var del_link = nodeByName("delete", row);
        var cxl_link = nodeByName("cancel", row);
        var claim_link = nodeByName("claim", row);
        var cxl_reason_link = nodeByName("cancel_reason", row);

        if (copy.cancel_reason()) {
            openils.Util.hide(del_link.parentNode);
            openils.Util.hide(recv_link);
            openils.Util.hide(unrecv_link);
            openils.Util.hide(cxl_link);
            openils.Util.hide(claim_link);

            /* XXX the following may leak memory in a long lived table: dijits may not get destroyed... not positive. revisit. */
            var holds_reason = dojo.create(
                "span", {
                    "style": "border-bottom: 1px dashed #000;",
                    "innerHTML": "Cancelled" /* XXX [sic] and i18n */
                }, cxl_reason_link, "only"
            );
            new dijit.Tooltip(
                {
                    "label": "<em>" + copy.cancel_reason().label() +
                        "</em><br />" + copy.cancel_reason().description(),
                    "connectId": [holds_reason]
                }, dojo.create("span", null, cxl_reason_link, "last")
            );
            openils.Util.show(cxl_reason_link, "inline");
        } else if (this.isPO) {
            /* Only using this in one place so far, but may want it for better
             * decisions on when to display certain controls. */
            var li_state = this.liCache[copy.lineitem()].state();

            openils.Util.hide(del_link.parentNode);
            openils.Util.hide(cxl_reason_link);

            /* Avoid showing (un)receive links, cancel links, for virt copies */
            if (copy.id() > 0) {
                if (copy.recv_time()) {
                    openils.Util.hide(cxl_link);
                    openils.Util.hide(recv_link);
                    openils.Util.hide(claim_link);

                    openils.Util.show(unrecv_link, "inline");
                    unrecv_link.onclick = function() {
                        if (confirm(localeStrings.UNRECEIVE_LID))
                            self.issueReceive(copy, /* rollback */ true);
                    };
                } else {
                    openils.Util.hide(unrecv_link);

                    if (this.claimEligibleLid[copy.id()]) {
                        openils.Util.show(claim_link, "inline");
                        claim_link.onclick = function() {
                            self.claimDialog.show(
                                self.liCache[copy.lineitem()], copy.id()
                            );
                        };
                    } else {
                        openils.Util.hide(claim_link);
                    }

                    openils.Util[li_state == "on-order" ? "show" : "hide"](
                        recv_link, "inline"
                    );
                    openils.Util.show(cxl_link, "inline");
                    recv_link.onclick = function() {
                        if (self.checkLiAlerts(copy.lineitem()))
                            self.issueReceive(copy);
                    };
                    cxl_link.onclick = function() {
                        self.cancelLid(copy.id());
                    };
                }
            } else {
                openils.Util.hide(cxl_link);
                openils.Util.hide(unrecv_link);
                openils.Util.hide(recv_link);
                openils.Util.hide(claim_link);
            }
        } else {
            openils.Util.hide(unrecv_link);
            openils.Util.hide(recv_link);
            openils.Util.hide(cxl_reason_link);
            openils.Util.hide(claim_link);

            del_link.onclick = function() { self.deleteCopy(row) };
            openils.Util.show(del_link.parentNode);
        }
    }

    this.cancelLid = function(lid_id) {
        lidCancelDialog._lid_id = lid_id;
        openils.Util.show(lidCancelDialog.domNode.parentNode);
        lidCancelDialog.show();
        if (!lidCancelDialog._prepared) {
            var widget = new openils.widget.AutoFieldWidget({
                "fmField": "cancel_reason",
                "fmClass": "acqlid",
                "parentNode": dojo.byId("acq-lit-lid-cancel-reason"),
                "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
                "forceSync": true
            });
            widget.build(
                function(w, ww) {
                    acqLidCancelButton.onClick = function() {
                        if (w.attr("value")) {
                            if (confirm(localeStrings.LID_CANCEL_CONFIRM)) {
                                self._cancelLid(
                                    lidCancelDialog._lid_id,
                                    w.attr("value")
                                );
                            }
                            lidCancelDialog.hide();
                        }
                    };
                    lidCancelDialog._prepared = true;
                }
            );
        }
    };

    this._cancelLid = function(lid_id, reason) {
        fieldmapper.standardRequest(
            ["open-ils.acq", "open-ils.acq.lineitem_detail.cancel"], {
                "params": [openils.User.authtoken, lid_id, reason],
                "async": true,
                "onresponse": function(r) {
                    if (r = openils.Util.readResponse(r)) {
                        if (r.lid) {
                            for (var id in r.lid) {
                                /* actually this should only iterate once */
                                self.copyCache[id].cancel_reason(
                                    r.lid[id].cancel_reason
                                );
                                self.updateLidState(self.copyCache[id]);
                            }
                        }
                    }
                }
            }
        );
    };

    this._confirmAlert = function(li, lin) {
        return confirm(
            dojo.string.substitute(
                localeStrings.CONFIRM_LI_ALERT, [
                    (new openils.acq.Lineitem({"lineitem": li})).findAttr(
                        "title", "lineitem_marc_attr_definition"
                    ),
                    lin.alert_text().code(),
                    lin.alert_text().description() || "",
                    lin.value()
                ]
            )
        );
    };

    this.checkLiAlerts = function(li_id) {
        var li = this.liCache[li_id];

        var alert_notes = li.lineitem_notes().filter(
            function(o) { return Boolean(o.alert_text()); }
        );

        /* this is _intentionally_ not done in a call to forEach() ... */
        for (var i = 0; i < alert_notes.length; i++) {
            if (this.noteAcks[alert_notes[i].id()])
                continue;
            else if (!this._confirmAlert(li, alert_notes[i]))
                return false;
            else
                this.noteAcks[alert_notes[i].id()] = true;
        }

        return true;
    };

    this.deleteCopy = function(row) {
        var copy = this.copyCache[row.getAttribute('copy_id')];
        copy.isdeleted(true);
        if(copy.isnew())
            delete this.copyCache[copy.id()];
        this.copyTbody.removeChild(row);
    }

    this._virtDfaCountsAsList = function() {
        var L = [];
        for (var key in this.virtDfaCounts) {
            for (var i = 0; i < this.virtDfaCounts[key]; i++)
                L.push(key);
        }
        return L;
    }

    this.confirmBreachedCopyFunds = function(copies) {
        var stop = 0, warning = 0;
        copies.forEach(
            function(o) {
                if (o.fund()) {
                    var state = self.fundBalanceState[o.fund()];
                    if (state[0] /* stop */)
                        stop++;
                    else if (state[1] /* warning */)
                        warning++;
                }
            }
        );

        if (stop) {
            return confirm(localeStrings.CONFIRM_FUNDS_AT_STOP);
        } else if (warning) {
            return confirm(localeStrings.CONFIRM_FUNDS_AT_WARNING);
        }
        return true;
    };

    this.saveCopyChanges = function(liId) {
        var self = this;
        var copies = [];


        var total = 0;
        for(var id in this.copyCache) {
            var c = this.copyCache[id];
            if(!c.isdeleted()) total++;
            if(c.isnew() || c.ischanged() || c.isdeleted()) {
                if(c.id() < 0) c.id(null);
                copies.push(c);
            }
        }


        dojo.byId('acq-lit-copy-count-label-' + liId).innerHTML = total;


        if (copies.length > 0) {
            if (!this.confirmBreachedCopyFunds(copies))
                return;

            if (typeof(this._copy_count_cb) == "function")
                this._copy_count_cb(liId, total);

            openils.Util.show("acq-lit-update-copies-progress");
            fieldmapper.standardRequest(
                ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
                {   async: true,
                    params: [openils.User.authtoken, copies],
                    onresponse: function(r) {
                        var res = openils.Util.readResponse(r);
                        litUpdateCopiesProgress.update(res);
                    },
                    oncomplete: function() {
                        self.drawCopies(liId, true /* force_fetch */);
                        openils.Util.hide("acq-lit-update-copies-progress");
                    }
                }
            );
        }

        var dfa_list = this._virtDfaCountsAsList();
        if (dfa_list.length > 0) {
            fieldmapper.standardRequest(
                ["open-ils.acq",
                "open-ils.acq.distribution_formula.record_application"],
                {
                    "async": true,
                    "params": [openils.User.authtoken, dfa_list, liId],
                    "onresponse": function(r) {
                        var res = openils.Util.readResponse(r);
                        if (res && res.length < dfa_list.length)
                            alert(localeStrings.DFA_NOT_ALL);
                    }
                }
            );
            this.virtDfaCounts = {};
        }
    }

    this._updateCreatePoPrepayCheckbox = function(prepay) {
        var prepay = openils.Util.isTrue(prepay);
        this._prepayRequiredByVendor = prepay;
        dijit.byId("acq-lit-po-prepay").attr("checked", prepay);
    };

    this._confirmPoPrepaySituation = function() {
        var want_prepay = dijit.byId("acq-lit-po-prepay").attr("checked");
        if (want_prepay != this._prepayRequiredByVendor) {
            return confirm(
                want_prepay ?
                    localeStrings.VENDOR_SAYS_PREPAY_NOT_NEEDED :
                    localeStrings.VENDOR_SAYS_PREPAY_NEEDED
            );
        } else {
            return true;
        }
    };

    this.applySelectedLiAction = function(action) {
        var self = this;
        switch(action) {

            case 'delete_selected':
                this._deleteLiList(self.getSelected());
                break;

            case 'create_order':
                this._loadPOSelect();
                acqLitPoCreateDialog.show();
                break;

            case 'save_picklist':
                this._loadPLSelect();
                acqLitSavePlDialog.show();
                break;

            case 'selector_ready':
            case 'order_ready':
                acqLitChangeLiStateDialog.attr('state', action.replace('_', '-'));
                acqLitChangeLiStateDialog.show();
                break;

            case 'print_po':
                this.printPO();
                break;

            case 'po_history':
                location.href = oilsBasePath + '/acq/po/history/' + this.isPO;
                break;

            case 'receive_po':
                this.receivePO();
                break;

            case 'rollback_receive_po':
                this.rollbackPoReceive();
                break;

            case 'create_assets':
                this.createAssets();
                break;

            case 'export_attr_list':
                this.chooseExportAttr();
                break;

            case 'batch_apply_funds':
                this.applyBatchLiFunds();
                break;

            case 'add_brief_record':
                if(this.isPO)
                    location.href = oilsBasePath + '/acq/picklist/brief_record?po=' + this.isPO;
                else
                    location.href = oilsBasePath + '/acq/picklist/brief_record?pl=' + this.isPL;

                break;

            case "cancel_lineitems":
                this.maybeCancelLineitems();
                break;

            case "change_claim_policy":
                var li_list = this.getSelected();
                this.claimPolicyPicker.attr("value", null);
                liClaimPolicyDialog.show();
                liClaimPolicySave.onClick = function() {
                    self.changeClaimPolicy(
                        li_list,
                        self.claimPolicyPicker.attr("value"),
                        function() {
                            li_list.forEach(
                                function(li) { self.setClaimPolicyControl(li); }
                            );
                            liClaimPolicyDialog.hide();
                        }
                    )
                };
                break;
        }
    };

    this.changeClaimPolicy = function(li_list, value, callback) {
        li_list.forEach(
            function(li) { li.claim_policy(value); }
        );
        fieldmapper.standardRequest(
            ["open-ils.acq", "open-ils.acq.lineitem.update"], {
                "params": [openils.User.authtoken, li_list],
                "async": true,
                "oncomplete": function(r) {
                    r = openils.Util.readResponse(r);
                    if (callback) callback(r);
                }
            }
        );
    };

    this.createAssets = function() {
        if(!this.isPO) return;
        if(!confirm(localeStrings.CREATE_PO_ASSETS_CONFIRM)) return;
        this.show('acq-lit-progress-numbers');
        var self = this;
        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.purchase_order.assets.create'],
            {   async: true,
                params: [this.authtoken, this.isPO],
                onresponse: function(r) {
                    var resp = openils.Util.readResponse(r);
                    self._updateProgressNumbers(resp, true);
                }
            }
        );
    }

    this.maybeCancelLineitems = function() {
        openils.Util.show("acq-lit-cancel-reason", "inline");
        if (!acqLitCancelLineitemsButton._prepared) {
            var widget = new openils.widget.AutoFieldWidget({
                "fmField": "cancel_reason",
                "fmClass": "jub",
                "parentNode": dojo.byId("acq-lit-cancel-reason-selector"),
                "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
                "forceSync": true
            });
            widget.build(
                function(w, ww) {
                    acqLitCancelLineitemsButton.onClick = function() {
                        if (w.attr("value")) {
                            if (confirm(localeStrings.LI_CANCEL_CONFIRM)) {
                                self._cancelLineitems(w.attr("value"));
                            }
                            openils.Util.hide("acq-lit-cancel-reason");
                        }
                    };
                    acqLitCancelLineitemsButton._prepared = true;
                }
            );
        }
    };

    this._cancelLineitems = function(reason) {
        var id_list = this.getSelected().map(function(o) { return o.id(); });
        fieldmapper.standardRequest(
            ["open-ils.acq", "open-ils.acq.lineitem.cancel.batch"], {
                "params": [openils.User.authtoken, id_list, reason],
                "async": true,
                "onresponse": function(r) {
                    if (r = openils.Util.readResponse(r)) {
                        if (r.li) {
                            for (var id in r.li) {
                                self.liCache[id].state(r.li[id].state);
                                self.liCache[id].cancel_reason(
                                    r.li[id].cancel_reason
                                );
                                self.updateLiState(self.liCache[id]);
                            }
                        }
                        if (r.lid && self.copyCache) {
                            for (var id in r.lid) {
                                if (self.copyCache[id]) {
                                    self.copyCache[id].cancel_reason(
                                        r.lid[id].cancel_reason
                                    );
                                    self.updateLidState(self.copyCache[id]);
                                }
                            }
                        }
                    }
                }
            }
        );
    };

    this.chooseExportAttr = function() {
        if (!acqLitExportAttrSelector._li_setup) {
            var self = this;
            acqLitExportAttrSelector.store = new dojo.data.ItemFileReadStore(
                {
                    "data": acqliad.toStoreData(
                        this.pcrud.search(
                            "acqliad", {"code": li_exportable_attrs}
                        )
                    )
                }
            );
            acqLitExportAttrSelector.setValue();
            acqLitExportAttrButton.onClick = function(){self.exportAttrList();};
            acqLitExportAttrSelector._li_setup = true;
        }
        openils.Util.show("acq-lit-export-attr-holder", "inline");
    };

    this.exportAttrList = function() {
        var attr_def = acqLitExportAttrSelector.item;
        var li_list = this.getSelected();
        var value_list = li_list.map(
            function(li) {
                return (new openils.acq.Lineitem({"lineitem": li})).findAttr(
                    attr_def.code, "lineitem_marc_attr_definition"
                );
            }
        ).filter(function(attr) { return Boolean(attr); });

        if (value_list.length > 0) {
            if (value_list.length < li_list.length) {
                if (!confirm(
                    dojo.string.substitute(
                        localeStrings.EXPORT_SHORT_LIST, [attr_def.description]
                    )
                )) {
                    return;
                }
            }
            try {
                openils.XUL.contentToFileSaveDialog(
                    value_list.join("\n"),
                    localeStrings.EXPORT_SAVE_DIALOG_TITLE
                );
            } catch (E) {
                alert(E);
            }
        } else {
            alert(dojo.string.substitute(
                localeStrings.EXPORT_EMPTY_LIST, [attr_def.description]
            ));
        }

        openils.Util.hide("acq-lit-export-attr-holder");
    };

    this.printPO = function() {
        if(!this.isPO) return;
        progressDialog.show(true);
        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.purchase_order.format'],
            {   async: true,
                params: [this.authtoken, this.isPO, 'html'],
                oncomplete: function(r) {
                    progressDialog.hide();
                    var evt = openils.Util.readResponse(r);
                    if(evt && evt.template_output()) {
                        openils.Util.printHtmlString(evt.template_output().data());
                    }
                }
            }
        );
    }


    this.receivePO = function() {
        if (!this.isPO) return;

        for (var id in this.liCache) {
            /* assumption: liCache reflects exactly the
             * set of LIs that belong to our PO */
            if (this.liCache[id].state() != "received" &&
                !this.checkLiAlerts(id)) return;
        }

        this.show('acq-lit-progress-numbers');
        var self = this;
        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.purchase_order.receive'],
            {   async: true,
                params: [this.authtoken, this.isPO],
                onresponse : function(r) {
                    var resp = openils.Util.readResponse(r);
                    self._updateProgressNumbers(resp, true);
                },
            }
        );
    }

    this.issueReceive = function(obj, rollback) {
        /* (For now) there shall be no marking LI or LIDs (un)received
         * except from the actual "view PO" interface. */
        if (!this.isPO) return;

        var part =
            {"jub": "lineitem", "acqlid": "lineitem_detail"}[obj.classname];
        var method =
            "open-ils.acq." + part + ".receive" + (rollback ? ".rollback" : "");

        progressDialog.show(true);
        fieldmapper.standardRequest(
            ["open-ils.acq", method], {
                "async": true,
                "params": [this.authtoken, obj.id()],
                "onresponse": function(r) {
                    if (r = openils.Util.readResponse(r)) {
                        self.fetchClaimInfo(
                            part == "lineitem" ? obj.id() : obj.lineitem(),
                            /* force */ true,
                            function() { self.handleReceive(r); }
                        );
                        progressDialog.hide();
                    }
                }
            }
        );
    };

    /**
     * Handles the responses from receive and rollback ML calls.
     */
    this.handleReceive = function(resp) {
        if (resp) {
            if (resp.li) {
                for (var li_id in resp.li) {
                    for (var key in resp.li[li_id])
                        self.liCache[li_id][key](resp.li[li_id][key]);
                    self.updateLiState(self.liCache[li_id]);
                }
            }
            if (resp.po) {
                if (typeof(self.poUpdateCallback) == "function")
                    self.poUpdateCallback(resp.po);
            }
            if (resp.lid) {
                for (var lid_id in resp.lid) {
                    for (var key in resp.lid[lid_id])
                        self.copyCache[lid_id][key](resp.lid[lid_id][key]);
                    self.updateLidState(self.copyCache[lid_id]);
                }
            }
        }
    };

    this.rollbackPoReceive = function() {
        if(!this.isPO) return;
        if(!confirm(localeStrings.ROLLBACK_PO_RECEIVE_CONFIRM)) return;
        this.show('acq-lit-progress-numbers');
        var self = this;
        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.purchase_order.receive.rollback'],
            {   async: true,
                params: [this.authtoken, this.isPO],
                onresponse : function(r) {
                    var resp = openils.Util.readResponse(r);
                    self._updateProgressNumbers(resp, true);
                },
            }
        );
    }

    this._updateProgressNumbers = function(resp, reloadOnComplete) {
        if(!resp) return;
        dojo.byId('acq-pl-lit-li-processed').innerHTML = resp.li;
        dojo.byId('acq-pl-lit-lid-processed').innerHTML = resp.lid;
        dojo.byId('acq-pl-lit-debits-processed').innerHTML = resp.debits_accrued;
        dojo.byId('acq-pl-lit-bibs-processed').innerHTML = resp.bibs;
        dojo.byId('acq-pl-lit-indexed-processed').innerHTML = resp.indexed;
        dojo.byId('acq-pl-lit-copies-processed').innerHTML = resp.copies;
        if(resp.complete && reloadOnComplete) 
            location.href = location.href;
    }


    this._createPO = function(fields) {
        this.show('acq-lit-progress-numbers');
        var po = new fieldmapper.acqpo();
        po.provider(this.createPoProviderSelector.attr('value'));
        po.ordering_agency(this.createPoAgencySelector.attr('value'));
        po.prepayment_required(fields.prepayment_required[0] ? true : false);

        var selected = this.getSelected( (fields.create_from == 'all') );
        if(selected.length == 0) return;

        var max = selected.length * 3;

        var self = this;
        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.purchase_order.create'],
            {   async: true,
                params: [
                    openils.User.authtoken, 
                    po, 
                    {
                        lineitems : selected.map(function(li) { return li.id() }),
                        create_assets : fields.create_assets[0],
                    }
                ],

                onresponse : function(r) {
                    var resp = openils.Util.readResponse(r);
                    self._updateProgressNumbers(resp);
                    if(resp.complete) 
                        location.href = oilsBasePath + '/acq/po/view/' + resp.purchase_order.id();
                }
            }
        );
    }

    this.batchFundWidget = null;

    this.applyBatchLiFunds = function() {

        var liIds = this.getSelected().map(function(li) { return li.id(); });
        if(liIds.length == 0) return; // warn?

        var self = this;
        batchFundUpdateDialog.show();

        if(!this.batchFundWidget) {
            this.batchFundWidget = new openils.widget.AutoFieldWidget({
                fmClass : 'acqf',
                selfReference : true,
                labelFormat : fundLabelFormat,
                searchFormat : fundSearchFormat,
                searchFilter : {"active": "t"},
                parentNode : dojo.byId('acq-lit-batch-fund-selector'),
                orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
                dijitArgs : { "required": true, "labelType": "html" },
                forceSync : true
            });
            this.batchFundWidget.build();
        }

        dojo.connect(batchFundUpdateCancel, 'onClick', function() { batchFundUpdateDialog.hide(); });
        dojo.connect(batchFundUpdateSubmit, 'onClick', 
            function() { 

                // TODO: call .dry_run first to test thresholds
                fieldmapper.standardRequest(
                    ['open-ils.acq', 'open-ils.acq.lineitem.fund.update.batch'],
                    {
                        params : [
                            openils.User.authtoken, 
                            liIds,
                            self.batchFundWidget.widget.attr('value')
                        ],
                        oncomplete : function(r) {
                            var resp = openils.Util.readResponse(r);
                            if(resp) {
                                location.href = location.href;
                            }
                        }
                    }
                )
            }
        );
    }

    this._deleteLiList = function(list, idx) {
        if(idx == null) idx = 0;
        if(idx >= list.length) return;
        var liId = list[idx].id();
        fieldmapper.standardRequest(
            ['open-ils.acq', 'open-ils.acq.lineitem.delete'],
            {   async: true,
                params: [openils.User.authtoken, liId],
                oncomplete: function(r) {
                    self.removeLineitem(liId);
                    self._deleteLiList(list, ++idx);
                }
            }
        );
    }

    this.editOrderMarc = function(li) {

        /*  To run in Firefox directly, must set signed.applets.codebase_principal_support
            to true in about:config */

        if(!openils.XUL.enableXPConnect()) return;

        if(openils.XUL.isXUL()) {
            win = window.open('/xul/' + openils.XUL.buildId() + '/server/cat/marcedit.xul');
        } else {
            win = window.open('/xul/server/cat/marcedit.xul'); 
        }
        var self = this;
        win.xulG = {
            record : {marc : li.marc(), "rtype": "bre"},
            save : {
                label: 'Save Record', // XXX I18N
                func: function(xmlString) {
                    li.marc(xmlString);
                    fieldmapper.standardRequest(
                        ['open-ils.acq', 'open-ils.acq.lineitem.update'],
                        {   async: true,
                            params: [openils.User.authtoken, li],
                            oncomplete: function(r) {
                                openils.Util.readResponse(r);
                                win.close();
                                self.drawInfo(li.id())
                            }
                        }
                    );
                },
            }
        };
    }

    this._savePl = function(values) {
        var self = this;
        var selected = this.getSelected( (values.which == 'all') );
        openils.Util.show('acq-lit-generic-progress');

        if(values.new_name) {
            openils.acq.Picklist.create(
                {name: values.new_name}, 
                function(id) {
                    self._updateLiList(id, selected, 0, 
                        function(){
                            location.href = oilsBasePath + '/acq/picklist/view/' + id;
                        });
                }
            );
        } else if(values.existing_pl) {
            // update lineitems to use an existing picklist
            self._updateLiList(values.existing_pl, selected, 0, 
                function(){
                    location.href = oilsBasePath + '/acq/picklist/view/' + values.existing_pl;
                });
        }
    }

    this._updateLiState = function(values, state) {
        var self = this;
        var selected = this.getSelected( (values.which == 'all') );
        if(!selected.length) return;
        dojo.forEach(selected, function(li) {li.state(state);});
        self._updateLiList(null, selected, 0, 
            // TODO consider inline updates for efficiency
            function() { location.href = location.href }
        );
    }

    this._updateLiList = function(pl, list, idx, oncomplete) {
        if(idx >= list.length) return oncomplete();
        var li = list[idx];
        if(pl != null) li.picklist(pl);
        litGenericProgress.update({maximum: list.length, progress: idx});
        new openils.acq.Lineitem({lineitem:li}).update(
            function(r) {
                self._updateLiList(pl, list, ++idx, oncomplete);
            }
        );
    }

    this._loadPOSelect = function() {
        if (!this.createPoProviderSelector) {
            var widget = new openils.widget.AutoFieldWidget({
                "fmField": "provider",
                "fmClass": "acqpo",
                "searchFilter": {"active": "t"},
                "parentNode": dojo.byId("acq-lit-po-provider"),
                "dijitArgs": {
                    "onChange": function() {
                        if (this.item) {
                            self._updateCreatePoPrepayCheckbox(
                                this.item.prepayment_required
                            );
                        }
                    }
                }
            });
            widget.build(function(w) { self.createPoProviderSelector = w; });
        }

        if (!this.createPoAgencySelector) {
            var widget = new openils.widget.AutoFieldWidget({
                "fmField": "ordering_agency",
                "fmClass": "acqpo",
                "parentNode": dojo.byId("acq-lit-po-agency"),
                "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
            });
            widget.build(function(w) { self.createPoAgencySelector = w; });
        }
    };

    this._loadPLSelect = function(preSel) {
        if(this._plSelectLoaded) return;
        var plList = [];
        function handleResponse(r) {
            plList.push(r.recv().content());
        }
        var method = 'open-ils.acq.picklist.user.retrieve';
        fieldmapper.standardRequest(
            ['open-ils.acq', method],
            {   async: true,
                params: [this.authtoken],
                onresponse: handleResponse,
                oncomplete: function() {
                    self._plSelectLoaded = true;
                    acqLitAddExistingSelect.store = 
                        new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(plList)});

                    acqLitAddExistingSelect.setValue(preSel);
                }
            }
        );
    }

    this.showRealCopyEditUI = function(li) {
        copyList = [];
        var self = this;
        this.volCache = {};

        this._fetchLineitem(li.id(), 
            function(fullLi) {
                li = self.liCache[li.id()] = fullLi;

                self.pcrud.search(
                    'acp', {
                        id : li.lineitem_details().map(
                            function(item) { return item.eg_copy_id() }
                        )
                    }, {
                        async : true,
                        oncomplete : function(r) {
                            try {
                                var r_list = openils.Util.readResponse( r );
                                for (var i = 0; i < r_list.length; i++) {
                                    var copy = r_list[i];
                                    var volId = copy.call_number();
                                    var volume = self.volCache[volId];
                                    if(!volume) {
                                        volume = self.volCache[volId] = self.pcrud.retrieve('acn', volId);
                                    }
                                    copy.call_number(volume);
                                    copyList.push(copy);
                                }
                                if (xulG) {
                                    // If we need to, we can pass in an update_copy function to handle the update instead of volume_item_creator
                                    xulG.volume_item_creator( { 'existing_copies' : copyList } );
                                }
                            } catch(E) {
                                alert('error in oncomplete: ' + E);
                            }
                        }
                    }
                );
            }
        );
    },

    this.drawBibFinder = function(li) {

        var query = '';
        var liWrapper = new openils.acq.Lineitem({lineitem:li});

        dojo.forEach(
            ['isbn', 'upc', 'issn', 'title', 'author'],
            function(field) {
                var val = liWrapper.findAttr(field, 'lineitem_marc_attr_definition');
                if(val) {
                    if(field == 'title' || field == 'author') {
                        query += field +':' + val + ' ';
                    } else {
                        query += 'identifier|' + field + ':' + val + ' ';
                    }
                }
            }
        );

        win = window.open(
            oilsBasePath + '/acq/lineitem/findbib?query=' + escape(query),
            '', 'resizable,scrollbars=1');

        win.window.recordFound = function(bibId) { 
            win.close();

            var attrs = li.attributes();
            li.attributes(null);
            li.eg_bib_id(bibId);

            fieldmapper.standardRequest(
                ["open-ils.acq", "open-ils.acq.lineitem.update"], 
                {
                    "params": [openils.User.authtoken, li],
                    "async": true,
                    "oncomplete": function(r) {
                        if(openils.Util.readResponse(r)) {
                            location.href = location.href;
                        }
                    }
                }
            );
        }
    }
}

