Don't disable the Create Volume/Item button when building the data structure from...
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / volume_copy_creator.js
1 const g_max_copies_that_can_be_added_at_a_time_per_volume = 999;
2 const rel_vert_pos_volume_count = 1;
3 const rel_vert_pos_call_number_classification = 2;
4 const rel_vert_pos_call_number_prefix = 3;
5 const rel_vert_pos_call_number = 4;
6 const rel_vert_pos_call_number_suffix = 5;
7 const rel_vert_pos_copy_count = 6;
8 const rel_vert_pos_barcode = 7;
9 const rel_vert_pos_part = 8;
10 const update_timer = 1000;
11 var g = {};
12 g.use_defaults = true;
13 g.acn_map = {}; // store retrieved acn objects here by id
14
15 function my_init() {
16     try {
17
18         /***********************************************************************************************************/
19         /* Initial setup */
20
21         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
22         if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
23         JSAN.errorLevel = "die"; // none, warn, or die
24         JSAN.addRepository('/xul/server/');
25         JSAN.use('util.error'); g.error = new util.error();
26         g.error.sdump('D_TRACE','my_init() for cat/volume_copy_creator.xul');
27
28         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
29         JSAN.use('util.widgets'); JSAN.use('util.functional'); JSAN.use('util.fm_utils');
30
31         JSAN.use('util.network'); g.network = new util.network();
32
33         g.refresh = xul_param('onrefresh');
34
35         if (xulG.unified_interface) {
36             $('non_unified_buttons').hidden = true;
37             xulG.reset_batch_menus = function() {
38                 $('batch_class_menulist').value = false;
39                 util.widgets.dispatch('command',$('batch_class_menulist'));
40                 $('batch_prefix_menulist').value = false;
41                 util.widgets.dispatch('command',$('batch_prefix_menulist'));
42                 $('batch_suffix_menulist').value = false;
43                 util.widgets.dispatch('command',$('batch_suffix_menulist'));
44             }
45             xulG.apply_template_to_batch = function(id,value) {
46                 if (!isNaN(Number(value))) {
47                     $(id).value = value;
48                     util.widgets.dispatch('command',$(id));
49                 }
50                 setTimeout(
51                     function() {
52                         // TODO:  Only apply batch to columns that haven't been adjusted manually?
53                         util.widgets.dispatch('command',$('batch_button'));
54                     },0
55                 );
56             }
57         } else {
58             $('Create').hidden = true;
59         }
60
61         /***********************************************************************************************************/
62         /* Am I adding just copies or copies and volumes?  Or am I rebarcoding existing copies? */
63
64         // g.copy_shortcut = { ou_id : { callnumber_composite_key : callnumber_id, callnumber_label : callnumber_id, ... }, ... }
65         g.copy_shortcut = xul_param('copy_shortcut');
66         // g.existing_copies = [ copy1, copy2, ... ]
67         g.existing_copies = xul_param('existing_copies') || [];
68
69         function set_attr(id,attr,msgcat_key) {
70             var x = $(id);
71             if (x) {
72                 x.setAttribute(
73                     attr,
74                     $('catStrings').getString(msgcat_key)
75                 );
76             }
77         }
78         if (g.existing_copies.length > 0) {
79             set_attr('EditThenCreate','label','staff.cat.volume_copy_creator.edit_then_rebarcode.btn.label');
80             set_attr('EditThenCreate','accesskey','staff.cat.volume_copy_creator.edit_then_rebarcode.btn.accesskey');
81             set_attr('CreateWithDefaults','label','staff.cat.volume_copy_creator.rebarcode.btn.label');
82             set_attr('CreateWithDefaults','accesskey','staff.cat.volume_copy_creator.rebarcode.btn.accesskey');
83             set_attr('Create','label','staff.cat.volume_copy_creator.rebarcode.btn.label');
84             set_attr('Create','accesskey','staff.cat.volume_copy_creator.rebarcode.btn.accesskey');
85         } else {
86             set_attr('EditThenCreate','label','staff.cat.volume_copy_creator.edit_then_create.btn.label');
87             set_attr('EditThenCreate','accesskey','staff.cat.volume_copy_creator.edit_then_create.btn.accesskey');
88             set_attr('CreateWithDefaults','label','staff.cat.volume_copy_creator.create_with_defaults.btn.label');
89             set_attr('CreateWithDefaults','accesskey','staff.cat.volume_copy_creator.create_with_defaults.btn.accesskey');
90             set_attr('Create','label','staff.cat.volume_copy_creator.create.btn.label');
91             set_attr('Create','accesskey','staff.cat.volume_copy_creator.create.btn.accesskey');
92         }
93
94         //g.error.sdump('D_ERROR','location.href = ' + location.href + '\n\ncopy_short cut = ' + g.copy_shortcut + '\n\nou_ids = ' + xul_param('ou_ids'));
95
96         var ou_ids = xul_param('ou_ids',{'concat' : true}) || [];
97
98         // Get the default callnumber classification scheme from OU settings
99         dojo.require('fieldmapper.OrgUtils');
100         //fieldmapper.aou.fetchOrgSettingDefault(ses('ws_ou'), 'cat.default_classification_scheme');
101         g.label_class = g.data.hash.aous['cat.default_classification_scheme'];
102
103         // Assign a default value if none was returned
104         if (!g.label_class) {
105             g.label_class = g.data.list.acnc[0].id();
106         }
107
108         /***********************************************************************************************************/
109         /* If we're passed existing_copies, rig up a copy_shortcut object to leverage existing code for rendering the volume labels, etc.
110          * Also make a lookup object for existing copies keyed on org id and callnumber composite key, and another keyed on copy id. */
111
112         // g.org_label_existing_copy_map = { ou_id : { callnumber_composite_key : [ copy1, copy2, ... ] }, ... }
113         g.org_label_existing_copy_map = {};
114         // g.id_copy_map = { acp_id : acp, ... }
115         g.id_copy_map = {};
116         for (var i = 0; i < g.existing_copies.length; i++) {
117             if (! g.copy_shortcut) { g.copy_shortcut = {}; }
118             var copy = g.existing_copies[i];
119             g.id_copy_map[ copy.id() ] = copy;
120             var call_number = copy.call_number();
121             g.doc_id = call_number.record();
122             if (!g.copy_shortcut[ call_number.owning_lib() ]) {
123                 ou_ids.push( call_number.owning_lib() );
124                 g.copy_shortcut[ call_number.owning_lib() ] = {};
125                 g.org_label_existing_copy_map[ call_number.owning_lib() ] = {};
126             }
127             var acnc_id = call_number.label_class() ?
128                 ( typeof call_number.label_class() == 'object' ? call_number.label_class().id() : call_number.label_class() )
129                 : g.label_class;
130             var acnp_id = typeof call_number.prefix() == 'object' ? call_number.prefix().id() : call_number.prefix();
131             var acns_id = typeof call_number.suffix() == 'object' ? call_number.suffix().id() : call_number.suffix();
132             var callnumber_composite_key = acnc_id + ':' + acnp_id + ':' + call_number.label() + ':' + acns_id;
133             g.copy_shortcut[ call_number.owning_lib() ][ callnumber_composite_key ] = call_number.id();
134             if (! g.org_label_existing_copy_map[ call_number.owning_lib() ][ callnumber_composite_key ]) {
135                 g.org_label_existing_copy_map[ call_number.owning_lib() ][ callnumber_composite_key ] = [];
136             }
137             g.org_label_existing_copy_map[ call_number.owning_lib() ][ callnumber_composite_key ].push( copy );
138         }
139
140         /***********************************************************************************************************/
141         /* What record am I dealing with?  */
142
143         g.doc_id = g.doc_id || xul_param('doc_id');
144         if (! g.doc_id) {
145             alert('Error in volume_copy_creator.js, g.doc_id not valid');
146             window.close(); return;
147         }
148         var sb = document.getElementById('summary_box'); while(sb.firstChild) sb.removeChild(sb.lastChild);
149         var summary = document.createElement('iframe'); sb.appendChild(summary);
150         summary.setAttribute('src',urls.XUL_BIB_BRIEF);
151         summary.setAttribute('flex','1');
152         get_contentWindow(summary).xulG = { 'docid' : g.doc_id };
153
154         /***********************************************************************************************************/
155         /* Setup pcrud and fetch the monographic parts for this bib */
156
157         dojo.require('openils.PermaCrud');
158         g.pcrud = new openils.PermaCrud({'authtoken':ses()});
159         g.parts = g.pcrud.search('bmp',{'record':g.doc_id},{'order_by': { 'bmp' : 'label_sortkey' } });
160         g.parts_hash = util.functional.convert_object_list_to_hash( g.parts );
161
162         /***********************************************************************************************************/
163         /* For the batch drop downs */
164
165         g.list_classes();
166         g.list_callnumbers(g.doc_id, g.label_class);
167         g.render_batch_button();
168
169         /***********************************************************************************************************/
170         /* render the orgs and volumes/input */
171
172         var rows = document.getElementById('rows');
173
174         g.ou_ids = [];
175         for (var i = 0; i < ou_ids.length; i++) {
176             try {
177                 var org = g.data.hash.aou[ ou_ids[i] ];
178                 if ( get_bool( g.data.hash.aout[ org.ou_type() ].can_have_vols() ) ) {
179                     var row = document.createElement('row'); rows.appendChild(row); row.setAttribute('ou_id',ou_ids[i]);
180                     g.render_library_label(row,ou_ids[i]);
181                     g.render_volume_count_entry( row, ou_ids[i] );
182                     g.ou_ids.push( ou_ids[i] );
183                 }
184             } catch(E) {
185                 g.error.sdump('D_ERROR',E);
186             }
187         }
188         g.common_ancestor_ou_ids = util.fm_utils.find_common_aou_ancestors( g.ou_ids ).reverse();
189
190         /***********************************************************************************************************/
191         /* For the remainder batch drop downs */
192
193         g.list_prefixes();
194         g.list_suffixes();
195
196         /************/
197
198         g.load_prefs();
199
200         if (g.existing_copies.length > 0) {
201             g.gather_copies_soon();
202         }
203
204         try {
205             $('main').parentNode.scrollLeft = 9999;
206         } catch(E) {
207             dump('Error in volume_copy_creator.js, my_init(), trying to auto-scroll to the far right: ' + E + '\n');
208         }
209
210     } catch(E) {
211         var err_msg = $("commonStrings").getFormattedString('common.exception', ['cat/volume_copy_creator.js', E]);
212         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
213         alert(err_msg);
214     }
215 }
216
217 g.render_library_label = function(row,ou_id) {
218     dump('g.render_library_label(row='+row+',ou_id='+ou_id+')\n');
219     var label = document.createElement('label'); row.appendChild(label);
220     label.setAttribute('ou_id',ou_id);
221     label.setAttribute('value',g.data.hash.aou[ ou_id ].shortname());
222 }
223
224 g.render_volume_count_entry = function(row,ou_id) {
225     dump('g.render_volume_count_entry(row='+row+',ou_id='+ou_id+')\n');
226     var hb = document.createElement('vbox'); row.appendChild(hb);
227     var tb = document.createElement('textbox'); hb.appendChild(tb);
228     if (g.use_defaults) {
229         tb.value = 1; // default to 1 volume per org
230         tb.select();
231     }
232     tb.setAttribute('ou_id',ou_id); tb.setAttribute('size','3'); tb.setAttribute('cols','3');
233     tb.setAttribute('rel_vert_pos',rel_vert_pos_volume_count);
234     if ( (!g.copy_shortcut) && (!g.last_focus) ) { tb.focus(); g.last_focus = tb; }
235     var node;
236     function render_copy_count_entry(ev) {
237         dump('\t\trender_copy_count_entry()\n');
238         if (ev.target.disabled) return;
239         if (! isNaN( Number( ev.target.value) ) ) {
240             if ( Number( ev.target.value ) > g_max_copies_that_can_be_added_at_a_time_per_volume ) {
241                 g.error.yns_alert($("catStrings").getFormattedString('staff.cat.volume_copy_creator.render_volume_count_entry.message', [g_max_copies_that_can_be_added_at_a_time_per_volume]),
242                     $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.title'),
243                     $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.ok_label'),null,null,'');
244                 return;
245             }
246             if (node) { row.removeChild(node); node = null; }
247             node = g.render_callnumber_copy_count_entry(row,ou_id,ev.target.value);
248         }
249     }
250     util.widgets.apply_vertical_tab_on_enter_handler(
251         tb,
252         function() { render_copy_count_entry({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); },
253         g.delay_gather_copies_soon
254     );
255     tb.addEventListener( 'change', render_copy_count_entry, false);
256     tb.addEventListener( 'change', g.gather_copies_soon, false);
257     tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
258     setTimeout(
259         function() {
260             try {
261                 if (g.copy_shortcut) {
262                     dump('\t\tg.render_volume_count_entry, using g.copy_shortcut\n');
263                     JSAN.use('util.functional');
264                     tb.value = util.functional.map_object_to_list(
265                         g.copy_shortcut[ou_id],
266                         function(o,i) {
267                             return g.copy_shortcut[ou_id][i];
268                         }
269                     ).length;
270                     dump('\t\tnumber of volumes = ' + tb.value + '\n');
271                     render_copy_count_entry({'target':tb});
272                     tb.disabled = true;
273                 } else if (tb.value) {
274                     dump('\t\tg.render_volume_count_entry, number of volumes = ' + tb.value + '\n');
275                     // since we're now supplying a default
276                     render_copy_count_entry({'target':tb});
277                     setTimeout(
278                         function() {
279                             util.widgets.vertical_tab(tb);
280                         }, 0
281                     );
282                 }
283             } catch(E) {
284                 alert(E);
285             }
286         }, 0
287     );
288 }
289
290 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
291     dump('g.render_call_number_copy_count_entry(row='+row+',ou_id='+ou_id+',count='+count+')\n');
292     var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
293     grid.setAttribute('flex','1');
294     grid.setAttribute('ou_id',ou_id);
295     var rows = grid.lastChild;
296     var r = document.createElement('row'); rows.appendChild( r );
297     var x = document.createElement('label'); r.appendChild(x);
298         x.setAttribute('value', $("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.classification'));
299         x.setAttribute('style','font-weight: bold');
300     x = document.createElement('label'); r.appendChild(x);
301         x.setAttribute('value', $("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.prefix'));
302         x.setAttribute('style','font-weight: bold');
303     x = document.createElement('label'); r.appendChild(x);
304         x.setAttribute('value', $("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.call_nums'));
305         x.setAttribute('style','font-weight: bold');
306     x = document.createElement('label'); r.appendChild(x);
307         x.setAttribute('value', $("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.suffix'));
308         x.setAttribute('style','font-weight: bold');
309     x = document.createElement('label'); r.appendChild(x);
310         x.setAttribute('value',$("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.num_of_copies'));
311         x.setAttribute('style','font-weight: bold');
312     x = document.createElement('label'); r.appendChild(x);
313         x.setAttribute('value',$("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.barcodes_and_parts'));
314         x.setAttribute('style','font-weight: bold');
315
316     function handle_change_precipitating_barcode_rendering(
317         callnumber_composite_key,
318         number_of_copies_column_textbox,
319         barcode_column_box
320     ) {
321         dump('handle_change_precipitating_barcode_rendering('+callnumber_composite_key+',number_of_copies = '+number_of_copies_column_textbox.value+','+ barcode_column_box + ')\n');
322
323         if (isNaN( Number( number_of_copies_column_textbox.value ) )) {
324             dump('1:handle_change_precipitating_barcode_rendering early return\n');
325             return;
326         }
327         if ( Number( number_of_copies_column_textbox.value ) > g_max_copies_that_can_be_added_at_a_time_per_volume ) {
328             g.error.yns_alert($("catStrings").getFormattedString('staff.cat.volume_copy_creator.render_volume_count_entry.message', [g_max_copies_that_can_be_added_at_a_time_per_volume]),
329                 $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.title'),
330                 $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.ok_label'),null,null,'');
331             return;
332         }
333
334         while (barcode_column_box.childNodes.length > Number(number_of_copies_column_textbox.value)) {
335             barcode_column_box.removeChild( barcode_column_box.lastChild );
336         }
337         g.render_barcode_entry(
338             barcode_column_box,
339             callnumber_composite_key,
340             Number(number_of_copies_column_textbox.value),
341             ou_id
342         );
343
344         if (! xulG.unified_interface) {
345             document.getElementById("EditThenCreate").disabled = false;
346             document.getElementById("CreateWithDefaults").disabled = false;
347         } else {
348             document.getElementById("Create").disabled = false;
349         }
350     }
351
352     function handle_change_to_callnumber_data(ev) {
353         dump('handle_change_to_callnumber_data\n');
354         var _call_number_column_textbox = ev.target;
355         var _call_number_column_box = _call_number_column_textbox.parentNode;
356
357         var _classification_column_box = _call_number_column_box.previousSibling.previousSibling; /* two over to the left */
358         var _classification_column_menulist = _classification_column_box.firstChild;
359
360         var _prefix_column_box = _call_number_column_box.previousSibling; /* one over to the left */
361         var _prefix_column_menulist = _prefix_column_box.firstChild;
362
363         var _suffix_column_box = _call_number_column_box.nextSibling; /* one over to the right */
364         var _suffix_column_menulist = _suffix_column_box.firstChild;
365
366         var _number_of_copies_column_box = _call_number_column_box.nextSibling.nextSibling; /* two over to the right */
367         var _number_of_copies_column_textbox = _number_of_copies_column_box.firstChild;
368
369         var _barcode_column_box = _number_of_copies_column_box.nextSibling;
370
371         var acn_label = _call_number_column_textbox.value;
372         var acnc_id = _classification_column_menulist.value;
373         var acnp_id = _prefix_column_menulist.value;
374         var acns_id = _suffix_column_menulist.value;
375         var callnumber_composite_key = acnc_id + ':' + acnp_id + ':' + acn_label + ':' + acns_id;
376         dump('\tcomposite_key = ' + callnumber_composite_key + '\n');
377
378         _call_number_column_textbox.setAttribute('callkey',callnumber_composite_key);
379         _call_number_column_textbox.setAttribute('acnc_id',acnc_id);
380         _call_number_column_textbox.setAttribute('acnp_id',acnp_id);
381         _call_number_column_textbox.setAttribute('acns_id',acns_id);
382
383         handle_change_precipitating_barcode_rendering(
384             callnumber_composite_key,
385             _number_of_copies_column_textbox,
386             _barcode_column_box
387         );
388     }
389
390     function handle_change_number_of_copies_column_textbox(ev) {
391         dump('handle_change_number_of_copies_column_textbox\n');
392         var _number_of_copies_column_textbox = ev.target;
393         var _number_of_copies_column_box = _number_of_copies_column_textbox.parentNode;
394         var _call_number_column_box = _number_of_copies_column_box.previousSibling.previousSibling; /* two over */
395         var _call_number_column_textbox = _call_number_column_box.firstChild;
396         handle_change_to_callnumber_data({'target':_call_number_column_textbox}); // let this guy do the work
397     }
398
399     for (var i = 0; i < count; i++) {
400         var r = document.createElement('row'); rows.appendChild(r);
401
402             /**** CLASSIFICATION COLUMN ****/
403             var classification_column_box = document.createElement('vbox');
404             r.appendChild(classification_column_box);
405
406             /**** PREFIX COLUMN ****/
407             var prefix_column_box = document.createElement('vbox');
408             r.appendChild(prefix_column_box);
409
410             /**** CALLNUMBER COLUMN ****/
411             var call_number_column_box = document.createElement('vbox');
412             r.appendChild(call_number_column_box);
413                 var call_number_column_textbox = document.createElement('textbox');
414                 call_number_column_box.appendChild(call_number_column_textbox);
415                     if (g.use_defaults && $('marc_cn').firstChild) {
416                         // default to first real value from batch callnumber menu
417                         var menupopup = $('marc_cn').firstChild.firstChild;
418                         if (menupopup.childNodes.length > 1) {
419                             call_number_column_textbox.value = menupopup.childNodes[1].getAttribute('label');
420                             call_number_column_textbox.select();
421                         }
422                     }
423                     call_number_column_textbox.setAttribute('rel_vert_pos',rel_vert_pos_call_number);
424                     call_number_column_textbox.setAttribute('ou_id',ou_id);
425                     util.widgets.apply_vertical_tab_on_enter_handler(
426                         call_number_column_textbox,
427                         function() {
428                             handle_change_to_callnumber_data({'target':call_number_column_textbox});
429                             setTimeout(
430                                 function(){
431                                     util.widgets.vertical_tab(call_number_column_textbox);
432                                 },0
433                             );
434                         },
435                         g.delay_gather_copies_soon
436                     );
437                     call_number_column_textbox.addEventListener( 'change', handle_change_to_callnumber_data, false);
438                     call_number_column_textbox.addEventListener( 'change', g.gather_copies_soon, false);
439                     call_number_column_textbox.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
440
441                     /**** CLASSIFICATION COLUMN revisited ****/
442                     var classification_column_menulist = g.render_class_menu(call_number_column_textbox);
443                     classification_column_menulist.addEventListener(
444                         'command',
445                         function() {
446                             handle_change_to_callnumber_data({'target':call_number_column_textbox});
447                         }
448                         ,false
449                     );
450                     classification_column_box.appendChild(classification_column_menulist);
451
452                     /**** PREFIX COLUMN revisited ****/
453                     var prefix_column_menulist = g.render_prefix_menu(call_number_column_textbox);
454                     prefix_column_menulist.addEventListener(
455                         'command',
456                         function() {
457                             handle_change_to_callnumber_data({'target':call_number_column_textbox});
458                         }
459                         ,false
460                     );
461
462                     prefix_column_box.appendChild(prefix_column_menulist);
463
464             /**** SUFFIX COLUMN ****/
465             var suffix_column_box = document.createElement('vbox');
466             r.appendChild(suffix_column_box);
467                 var suffix_column_menulist = g.render_suffix_menu(call_number_column_textbox);
468                 suffix_column_menulist.addEventListener(
469                     'command',
470                     function() {
471                         handle_change_to_callnumber_data({'target':call_number_column_textbox});
472                     }
473                     ,false
474                 );
475                 suffix_column_box.appendChild(suffix_column_menulist);
476
477             /**** NUMBER OF COPIES COLUMN ****/
478             var number_of_copies_column_box = document.createElement('vbox');
479             r.appendChild(number_of_copies_column_box);
480                 var number_of_copies_column_textbox = document.createElement('textbox');
481                 number_of_copies_column_box.appendChild(number_of_copies_column_textbox);
482                     if (g.use_defaults) {
483                         // default to one copy per call number
484                         number_of_copies_column_textbox.value = 1;
485                         number_of_copies_column_textbox.select();
486                     }
487                     number_of_copies_column_textbox.setAttribute('size','3'); number_of_copies_column_textbox.setAttribute('cols','3');
488                     number_of_copies_column_textbox.setAttribute('rel_vert_pos',rel_vert_pos_copy_count);
489                     number_of_copies_column_textbox.setAttribute('ou_id',ou_id);
490                     util.widgets.apply_vertical_tab_on_enter_handler(
491                         number_of_copies_column_textbox,
492                         function() {
493                             handle_change_number_of_copies_column_textbox({'target':number_of_copies_column_textbox});
494                             setTimeout(
495                                 function(){
496                                     util.widgets.vertical_tab(number_of_copies_column_textbox);
497                                 },0
498                             );
499                         },
500                         g.delay_gather_copies_soon
501                     );
502                     number_of_copies_column_textbox.addEventListener( 'change', handle_change_number_of_copies_column_textbox, false);
503                     number_of_copies_column_textbox.addEventListener( 'change', g.gather_copies_soon, false);
504                     number_of_copies_column_textbox.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
505                     if ( !g.last_focus ) { number_of_copies_column_textbox.focus(); g.last_focus = number_of_copies_column_textbox; }
506
507             /**** BARCODE COLUMN ****/
508             var barcode_column_box = document.createElement('vbox');
509             r.appendChild(barcode_column_box);
510
511         setTimeout(
512             function(idx,call_number_column_textbox,number_of_copies_column_textbox){
513                 return function() {
514                     try {
515                         JSAN.use('util.functional');
516                         if (g.copy_shortcut) {
517                             dump('\t\tg.render_call_number_copy_count_entry() using g.copy_shortcut\n');
518                             var callnumber_composite_key = util.functional.map_object_to_list(
519                                 g.copy_shortcut[ou_id],
520                                 function(o,i) {
521                                     return i;
522                                 }
523                             )[idx];
524                             dump('\tcallnumber_composite_key = ' + callnumber_composite_key + '\n');
525                             if (g.org_label_existing_copy_map[ou_id]) {
526                                 var num_of_copies = g.org_label_existing_copy_map[ou_id][callnumber_composite_key].length;
527                                 if (num_of_copies>0) {
528                                     number_of_copies_column_textbox.value = num_of_copies;
529                                     number_of_copies_column_textbox.disabled = true;
530                                 }
531                             }
532                             var acn_label = callnumber_composite_key.split(/:/).slice(2,-1).join(':');
533                             var acnc_id = callnumber_composite_key.split(/:/)[0];
534                             var acnp_id = callnumber_composite_key.split(/:/)[1];
535                             var acns_id = callnumber_composite_key.split(/:/).slice(-1)[0];
536                             call_number_column_textbox.value = acn_label;
537                             classification_column_menulist.value = acnc_id;
538                             prefix_column_menulist.value = acnp_id;
539                             suffix_column_menulist.value = acns_id;
540                             dump('\tacn_label = ' + acn_label + ' acnc_id = ' + acnc_id + ' acnp_id = ' + acnp_id + ' acns_id = ' + acns_id + '\n');
541                             handle_change_to_callnumber_data({'target':call_number_column_textbox});
542                         } else {
543                             dump('\t\tg.render_call_number_copy_count_entry() using defaults\n');
544
545                             // if we're providing defaults, keep on rendering
546                             if (call_number_column_textbox.value) {
547                                 util.widgets.dispatch('change',call_number_column_textbox);
548                             }
549                             if (number_of_copies_column_textbox.value) {
550                                 util.widgets.dispatch('change',number_of_copies_column_textbox);
551                             }
552                         }
553                     } catch(E) {
554                         alert(E);
555                     }
556                 }
557             }(i,call_number_column_textbox,number_of_copies_column_textbox),0
558         );
559     }
560
561     return grid;
562 }
563
564 g.render_part_menu = function(barcode_tb) {
565     var hbox = document.createElement('hbox');
566     var menulist = document.createElement('menulist');
567         menulist.setAttribute('editable','true');
568         hbox.appendChild(menulist);
569     var button = document.createElement('button');
570         button.setAttribute('label',$('catStrings').getString('staff.cat.volume_copy_creator.create_part.btn.label'));
571         button.hidden = true;
572         hbox.appendChild(button);
573
574     var menupopup = document.createElement('menupopup');
575         menulist.appendChild(menupopup);
576         g.render_part_menuitems(menupopup);
577
578     button.addEventListener(
579         'command',
580         function(ev) {
581             var new_part = new bmp();
582                 new_part.isnew(1);
583                 new_part.label(menulist.value);
584                 new_part.record(g.doc_id);
585             g.pcrud.create(new_part, {
586                 "oncomplete": function (r, objs) {
587                     var db_part = objs[0];
588                     if (!db_part) { return; }
589                     g.parts.push( db_part );
590                     g.parts_hash[ db_part.id() ] = db_part;
591                     g.render_part_menuitems(menupopup);
592                     if (menulist.selectedItem) {
593                         barcode_tb.setAttribute('bmp_id',menulist.selectedItem.value);
594                         button.hidden = true;
595                     }
596                     g.gather_copies_soon();
597                 }
598             });
599         },
600         false
601     );
602
603     menulist.addEventListener(
604         'change',
605         function(ev) {
606             if (! ev.target.selectedItem) {
607                 button.hidden = false;
608             }
609         },
610         false
611     );
612     menulist.addEventListener('change',g.gather_copies_soon,false);
613     menulist.addEventListener(
614         'command',
615         function(ev) {
616             barcode_tb.setAttribute('bmp_id',menulist.selectedItem.value);
617             button.hidden = true;
618         },
619         false
620     );
621     menulist.addEventListener('command',g.gather_copies_soon,false);
622
623     return hbox;
624 }
625
626 g.render_part_menuitems = function(menupopup) {
627     util.widgets.remove_children(menupopup);
628     var menuitem = document.createElement('menuitem');
629     menuitem.setAttribute('label','');
630     menuitem.setAttribute('value','');
631     menupopup.appendChild(menuitem);
632     for (var i = 0; i < g.parts.length; i++) {
633         var menuitem = document.createElement('menuitem');
634         menuitem.setAttribute('label',g.parts[i].label());
635         menuitem.setAttribute('value',g.parts[i].id());
636         menupopup.appendChild(menuitem);
637     }
638
639 }
640
641 g.render_barcode_entry = function(node,callnumber_composite_key,count,ou_id) {
642     try {
643         dump('g.render_barcode_entry(node,'+callnumber_composite_key+',count='+count+',ou_id='+ou_id+'\n');
644         function ready_to_create(ev) {
645             if (! xulG.unified_interface) {
646                 document.getElementById("EditThenCreate").disabled = false;
647                 document.getElementById("CreateWithDefaults").disabled = false;
648             } else {
649                 document.getElementById("Create").disabled = false;
650             }
651         }
652
653         JSAN.use('util.barcode');
654
655         for (var i = 0; i < count; i++) {
656             var tb_part_box;
657             var tb;
658             var part_menu;
659             var set_handlers = false;
660             if (typeof node.childNodes[i] == 'undefined') {
661                 tb_part_box = document.createElement('hbox');
662                 node.appendChild(tb_part_box);
663                 tb = document.createElement('textbox');
664                 tb_part_box.appendChild(tb);
665                 part_menu = g.render_part_menu(tb);
666                 tb_part_box.appendChild(part_menu);
667                 set_handlers = true;
668             } else {
669                 tb_part_box = node.childNodes[i];
670                 tb = tb_part_box.firstChild;
671                 part_menu = tb_part_box.lastChild;
672             }
673             tb.setAttribute('ou_id',ou_id);
674             tb.setAttribute('callkey',callnumber_composite_key);
675             tb.setAttribute('rel_vert_pos',rel_vert_pos_barcode);
676             part_menu.firstChild.setAttribute('rel_vert_pos',rel_vert_pos_part);
677             if (!tb.value && g.org_label_existing_copy_map[ ou_id ]) {
678                 tb.value = g.org_label_existing_copy_map[ ou_id ][ callnumber_composite_key ][i].barcode();
679                 tb.setAttribute('acp_id', g.org_label_existing_copy_map[ ou_id ][ callnumber_composite_key ][i].id());
680                 var temp_parts = g.org_label_existing_copy_map[ ou_id ][ callnumber_composite_key ][i].parts();
681                 temp_parts = util.functional.filter_list(
682                     temp_parts,
683                     function(p) {
684                         return p.record() == g.doc_id; // filter out foreign parts
685                     }
686                 );
687                 if (temp_parts.length > 0) {
688                     tb.setAttribute('bmp_id',temp_parts[0].id());
689                     part_menu.firstChild.value = g.parts_hash[ temp_parts[0].id() ].label();
690                 }
691                 tb.select();
692                 if (! g.first_focus) { g.first_focus = tb; }
693             }
694             if (g.use_defaults && ! g.first_focus) {
695                 g.first_focus = tb;
696                 tb.focus();
697             }
698             if (set_handlers) {
699                 util.widgets.apply_vertical_tab_on_enter_handler(
700                     tb,
701                     function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); },
702                     g.delay_gather_copies_soon
703                 );
704                 util.widgets.apply_vertical_tab_on_enter_handler(
705                     part_menu.firstChild,
706                     function() { setTimeout(function(){util.widgets.vertical_tab(part_menu.firstChild);},0); },
707                     g.delay_gather_copies_soon
708                 );
709                 tb.addEventListener('change', function(ev) {
710                     var barcode = String( ev.target.value ).replace(/\s/g,'');
711                     if (barcode != ev.target.value) ev.target.value = barcode;
712                     if ($('check_barcodes').checked && ! util.barcode.check(barcode) ) {
713                         g.error.yns_alert($("catStrings").getFormattedString('staff.cat.volume_copy_creator.render_barcode_entry.alert_message', [barcode]),
714                             $("catStrings").getString('staff.cat.volume_copy_creator.render_barcode_entry.alert_title'),
715                             $("catStrings").getString('staff.cat.volume_copy_creator.render_barcode_entry.alert_ok_button'),null,null,
716                             $("catStrings").getString('staff.cat.volume_copy_creator.render_barcode_entry.alert_confirm'));
717                         setTimeout( function() { ev.target.select(); ev.target.focus(); }, 0);
718                     }
719                 }, false);
720                 tb.addEventListener('change', g.gather_copies_soon, false);
721                 tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
722             }
723         }
724
725         g.gather_copies_soon();
726         setTimeout( function() { if (g.first_focus) { g.first_focus.focus(); } }, 0 );
727
728     } catch(E) {
729         g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
730     }
731 }
732
733 g.generate_barcodes = function() {
734     try {
735         var nodes = document.getElementsByAttribute('rel_vert_pos',rel_vert_pos_barcode);
736         if (nodes.length < 1) { return; }
737         var first_barcode = nodes[0].value;
738
739         if (! first_barcode) { return; }
740
741         var barcodes = g.network.simple_request(
742             'AUTOGENERATE_BARCODES',
743             [
744                 ses(),
745                 first_barcode,
746                 nodes.length - 1,
747                 $('check_barcodes').checked ? {} : { "checkdigit" : false }
748             ]
749         );
750
751         if (typeof barcodes.ilsevent != 'undefined') {
752             throw(barcodes);
753         }
754
755         for (var i = 0; i < barcodes.length; i++) {
756             nodes[i+1].value = barcodes[i];
757             nodes[i+1].select();
758             util.widgets.dispatch('change',nodes[i+1]);
759         }
760
761         setTimeout(
762             function() {
763                 g.gather_copies_soon();
764             },0
765         );
766
767     } catch(E) {
768         g.error.sdump('D_ERROR','g.generate_barcodes: ' + E);
769     }
770 }
771
772 g.delay_gather_copies_soon = function() {
773     if (xulG.unified_interface) {
774         dump('g.delay_gather_copies_soon()\n');
775         g.gather_copies_soon();
776     }
777 }
778
779 g.gather_copies_soon = function(ev) {
780     try {
781         if (!xulG.unified_interface) { return; }
782         dump('g.gather_copies_soon()\n');
783         if (g.update_copy_editor_timeoutID) {
784             clearTimeout(g.update_copy_editor_timeoutID);
785         }
786         // This function is expensive when it comes to keeping the UI responsive, so let's give it a delay
787         // that quick entry of consecutive fields can override
788         g.update_copy_editor_timeoutID = setTimeout(
789             function() {
790                 try {
791                     g.gather_copies();
792                     xulG.refresh_copy_editor();
793                 } catch(E) {
794                     dump('Error in volume_copy_editor.js with g.gather_copies_soon setTimeout func(): ' + E + '\n');
795                 }
796             }, update_timer
797         );
798     } catch(E) {
799         alert('Error in volume_copy_creator.js, g.gather_copies_soon(): ' + E);
800     }
801 }
802
803 g.new_acp_id = -1;
804 g.new_acn_id = -1;
805
806 g.gather_copies = function() {
807     dump('g.gather_copies()\n');
808     try {
809         var nl = document.getElementsByTagName('textbox');
810
811         g.volumes_scaffold = {};
812         /*
813             g.volumes_scaffold = {
814                 '#ou_id' : {
815                     '#class_id:#prefix_id:#callnumber label:#suffix_id' : {
816                         'callnumber_data' : {
817                             'acn_id' : '#callnumber id',
818                             'acn_label' : '#callnumber label',
819                             'acnc_id' : '#classification_id',
820                             'acnp_id' : '#prefix_id',
821                             'acns_id' : '#suffix_id'
822                         },
823                         'barcode_data' :
824                             [
825                                 {
826                                     'barcode' : '#barcode',
827                                     'acp_id' : '#copy_id',
828                                     'bmp_id' : '#part_id'
829                                 }, ...
830                             ]
831                     }
832                 }, ...
833             }
834         */
835
836         var barcodes = [];
837         var v_count = 0;
838         for (var i = 0; i < nl.length; i++) {
839             if ( nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_barcode ) barcodes.push( nl[i] );
840             if ( nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_call_number )  {
841                 v_count++;
842                 var ou_id = nl[i].getAttribute('ou_id');
843                 var acn_id = nl[i].getAttribute('acn_id');
844                 if (!acn_id) {
845                     acn_id = g.new_acn_id--;
846                     nl[i].setAttribute('acn_id',acn_id);
847                 }
848                 var acnc_id = nl[i].getAttribute('acnc_id') || g.label_class;
849                 var acnp_id = nl[i].getAttribute('acnp_id') || -1;
850                 var acns_id = nl[i].getAttribute('acns_id') || -1;
851                 var callnumber = nl[i].value;
852                 if (typeof g.volumes_scaffold[ou_id] == 'undefined') {
853                     g.volumes_scaffold[ou_id] = {}
854                 }
855                 var composite_key = acnc_id + ':' + acnp_id + ':' + callnumber + ':' + acns_id;
856                 if (typeof g.volumes_scaffold[ou_id][composite_key] == 'undefined') {
857                     g.volumes_scaffold[ou_id][composite_key] = {
858                         //'node' : nl[i],
859                         'callnumber_data' : {
860                             'acn_id' : acn_id,
861                             'acn_label' : callnumber,
862                             'acnc_id' : acnc_id,
863                             'acnp_id' : acnp_id,
864                             'acns_id' : acns_id
865                         },
866                         'barcode_data' : []
867                     }
868                     dump('fleshing volumes scaffold with ou_id = ' + ou_id + ' composite_key = ' + composite_key + ' acn_id = ' + acn_id + '\n');
869                 }
870             }
871         };
872         dump('volume_copy_creator: processed ' + nl.length + ' textbox nodes, consisting of ' + barcodes.length + ' barcodes and ' + v_count + 'volumes\n');
873         dump('volume scaffold = ' + js2JSON(g.volumes_scaffold) + '\n');
874
875         for (var i = 0; i < barcodes.length; i++) {
876             var acp_id = barcodes[i].getAttribute('acp_id') || g.new_acp_id--;
877             var ou_id = barcodes[i].getAttribute('ou_id');
878             var callnumber_composite_key = barcodes[i].getAttribute('callkey');
879             var barcode = barcodes[i].value;
880             var bmp_id = barcodes[i].getAttribute('bmp_id');
881
882             dump('placing ' + barcode + ' for ou = ' + ou_id + ' into composite_key bin ' + callnumber_composite_key + '\n');
883
884             if (typeof g.volumes_scaffold[ou_id] == 'undefined') {
885                 dump('1: I want to remove this soon, so alert me if it is getting used, ou_id = ' + ou_id + '\n');
886                 g.volumes_scaffold[ou_id] = {}
887             }
888             if (typeof g.volumes_scaffold[ou_id][callnumber_composite_key] == 'undefined') {
889                 dump('2: when does this happen, and why? ou_id = ' + ou_id + ' callnumber_composite_key = ' + callnumber_composite_key + '\n');
890                 // one way this can happen, race condition between this function and editing a widget
891                 g.volumes_scaffold[ou_id][callnumber_composite_key] = {
892                     'callnumber_data' : {
893                         // not ideal, but hey...
894                         'acn_label' : callnumber_composite_key.split(/:/).slice(2,-1).join(':'),
895                         'acnc_id' : callnumber_composite_key.split(/:/)[0],
896                         'acnp_id' : callnumber_composite_key.split(/:/)[1],
897                         'acns_id' : callnumber_composite_key.split(/:/).slice(-1)[0]
898                     },
899                     'barcode_data' : []
900                 }
901             }
902
903             if (barcode != '') {
904                 g.volumes_scaffold[ou_id][callnumber_composite_key].barcode_data.push(
905                     {
906                         'barcode' : barcode,
907                         'acp_id' : acp_id,
908                         'bmp_id' : bmp_id
909                     }
910                 );
911             }
912         }
913
914         var volumes = [];
915         var copies = [];
916         var volume_data = {};
917
918         // Get the default copy status; default to "In Process" if unset, per 1.6
919         var normal_ccs = g.data.hash.aous['cat.default_copy_status_normal'] || 5;
920
921         function new_copy(acp_id,ou_id,acn_id,barcode) {
922             var copy = new acp();
923             copy.id( acp_id );
924             copy.isnew('1');
925             copy.barcode( barcode );
926             copy.call_number( acn_id );
927             copy.circ_lib(ou_id);
928             /* FIXME -- use constants */
929             copy.deposit(0);
930             copy.price(0);
931             copy.deposit_amount(0);
932             copy.fine_level(2); // Normal
933             copy.loan_duration(2); // Normal
934             copy.location(1); // Stacks
935             copy.status(normal_ccs);
936             copy.circulate(get_db_true());
937             copy.holdable(get_db_true());
938             copy.opac_visible(get_db_true());
939             copy.ref(get_db_false());
940             return copy;
941         }
942
943         for (var ou_id in g.volumes_scaffold) {
944             for (var composite_key in g.volumes_scaffold[ou_id]) {
945                 for (var i = 0; i < g.volumes_scaffold[ou_id][composite_key].barcode_data.length; i++) {
946                     var barcode = g.volumes_scaffold[ou_id][composite_key].barcode_data[i].barcode;
947                     var acp_id = g.volumes_scaffold[ou_id][composite_key].barcode_data[i].acp_id;
948                     var bmp_id = g.volumes_scaffold[ou_id][composite_key].barcode_data[i].bmp_id;
949                     var acn_id = g.volumes_scaffold[ou_id][composite_key].callnumber_data.acn_id;
950                     dump('gather_copies(): barcode = ' + barcode + ' acp_id = ' + acp_id + ' bmp_id = ' + bmp_id + ' acn_id = ' + acn_id + ' composite_key = ' + composite_key + '\n');
951                     var copy = g.id_copy_map[ acp_id ];
952                     if (!copy) {
953                         copy = new_copy(acp_id,ou_id,acn_id,barcode);
954                         g.id_copy_map[ acp_id ] = copy;
955                     } else {
956                         copy.ischanged( get_db_true() );
957                     }
958                     copy.barcode( barcode );
959                     copy.call_number( acn_id );
960                     var temp_parts = util.functional.filter_list(
961                         copy.parts() || [],
962                         function(p) {
963                             return (p.record() != g.doc_id); // filter out parts for this bib
964                         }
965                     );
966                     if (bmp_id) {
967                         temp_parts.push( g.parts_hash[ bmp_id ] );
968                     }
969                     copy.parts( temp_parts );
970                     copies.push( copy );
971                 }
972             }
973         }
974
975         xulG.copies = copies;
976         return copies;
977
978     } catch(E) {
979         alert('Error in volume_copy_creator.js, g.gather_copies():' + E);
980     }
981 }
982
983 g.vivicate_update_volumes = function() {
984     try {
985         var volumes = [];
986         for (var ou_id in g.volumes_scaffold) {
987             for (var composite_key in g.volumes_scaffold[ou_id]) {
988
989                 var callnumber_data = g.volumes_scaffold[ou_id][composite_key].callnumber_data;
990                 var acn_id = callnumber_data.acn_id;
991                 var acnp_id = callnumber_data.acnp_id;
992                 var acns_id = callnumber_data.acns_id;
993                 var acnc_id = callnumber_data.acnc_id;
994
995                 if (acn_id < 0) {
996
997                     var acn_blob = g.network.simple_request(
998                         'FM_ACN_FIND_OR_CREATE',
999                         [ ses(), callnumber_data.acn_label, g.doc_id, ou_id, acnp_id, acns_id, acnc_id ]
1000                     );
1001                     dump('FM_ACN_FIND_OR_CREATE: label = ' + callnumber_data.acn_label
1002                         + ' doc = ' + g.doc_id + ' ou = ' + ou_id + ' acnp = ' + acnp_id + ' acns = ' + acns_id + ' acnc = ' + acnc_id + '\n');
1003
1004                     if (typeof acn_blob.ilsevent != 'undefined') {
1005                         alert('Error in g.vivicate_update_volumes, acn_id = ' + acn_id + ' acn_blob = ' + js2JSON(acn_blob));
1006                         continue;
1007                     }
1008
1009                     acn_id = acn_blob.acn_id;
1010
1011                     if (typeof g.acn_map[ acn_id ] == 'undefined') {
1012                         var temp_acn = g.network.simple_request(
1013                             'FM_ACN_RETRIEVE.authoritative',
1014                             [ acn_id ]
1015                         );
1016                         if (typeof temp_acn.ilsevent != 'undefined') {
1017                             alert('Error in g.vivicate_update_volumes, acn_id = ' + acn_id + ' temp_acn = ' + js2JSON(temp_acn));
1018                             continue;
1019                         }
1020                         g.acn_map[ acn_id ] = temp_acn;
1021                         if (callnumber_data.acn_id < 0) {
1022                             g.acn_map[ callnumber_data.acn_id ] = temp_acn;
1023                         }
1024                     }
1025
1026                 }
1027 /*
1028                 var my_acn = g.acn_map[ acn_id ];
1029
1030                 var node = g.volumes_scaffold[ou_id][composite_key].node;
1031                 var class_menulist = node.parentNode.previousSibling.previousSibling.firstChild;
1032                 var prefix_menulist = node.parentNode.previousSibling.firstChild;
1033                 var suffix_menulist = node.parentNode.nextSibling.firstChild;
1034
1035                 if ( String(class_menulist.value) != String(my_acn.label_class()) {
1036                     my_acn.label_class( class_menulist.value );
1037                     my_acn.ischanged( get_db_true() );
1038                 }
1039                 if ( String(prefix_menulist.value) != String(my_acn.prefix()) {
1040                     my_acn.prefix( prefix_menulist.value );
1041                     my_acn.ischanged( get_db_true() );
1042                 }
1043                 if ( String(suffix_menulist.value) != String(my_acn.suffix()) {
1044                     my_acn.suffix( suffix_menulist.value );
1045                     my_acn.ischanged( get_db_true() );
1046                 }
1047
1048                 if (get_bool( my_acn.ischanged() )) {
1049                     volumes.push( my_acn );
1050                 }
1051 */
1052             }
1053         }
1054         if (volumes.length > 0) {
1055             if (typeof xul_param('update_volume') == 'function') {
1056                 xul_param('update_volume')(volumes);
1057             } else {
1058                  var r = g.network.simple_request(
1059                     'FM_ACN_TREE_UPDATE',
1060                     [ ses(),volumes, false, { 'auto_merge_vols' : false } ]
1061                 );
1062                 if (typeof r.ilsevent != 'undefined') {
1063                     alert('error with volume update: ' + js2JSON(r));
1064                 }
1065             }
1066         }
1067     } catch(E) {
1068         alert('Error in volume_copy_creator.js, vivicate_volumes(): ' + E);
1069     }
1070 }
1071
1072 g.stash_and_close = function(param) {
1073
1074     try {
1075
1076         if (g.update_copy_editor_timeoutID) {
1077             clearTimeout(g.update_copy_editor_timeoutID);
1078         }
1079
1080         var copies;
1081         if (xulG.unified_interface) {
1082             g.gather_copies();
1083             xulG.refresh_copy_editor();
1084             copies = xulG.copies;
1085         } else {
1086             copies = g.gather_copies();
1087             copies = blob.copies;
1088         }
1089
1090         var dont_close = false;
1091
1092         g.vivicate_update_volumes();
1093         for (var i = 0; i < copies.length; i++) {
1094             var acn_id = copies[i].call_number();
1095             if (typeof g.acn_map[acn_id] != 'undefined') {
1096                 // handle vivicated-callnumbers
1097                 copies[i].call_number( g.acn_map[acn_id].id() );
1098             } else {
1099                 alert('error in stash and close, acn_id = ' + acn_id);
1100             }
1101         }
1102
1103         if (copies.length > 0) {
1104             if (param == 'edit') {
1105                 JSAN.use('cat.util');
1106                 copies = cat.util.spawn_copy_editor( { 'edit' : true, 'docid' : g.doc_id, 'copies' : copies, 'caller_handles_update' : true });
1107             }
1108             if (typeof xul_param('update_copy') == 'function') {
1109                 xul_param('update_copy')(copies);
1110             } else {
1111                  var r = g.network.simple_request(
1112                     'FM_ACP_FLESHED_BATCH_UPDATE',
1113                     [ ses(),copies, true ]
1114                 );
1115                 if (typeof r.ilsevent != 'undefined') {
1116                     alert('error with copy update:' + js2JSON(r));
1117                 }
1118             }
1119             try {
1120                 //case 1706 /* ITEM_BARCODE_EXISTS */ :
1121                 if (copies && copies.length > 0 && $('print_labels').checked) {
1122                     JSAN.use('util.functional');
1123                     dont_close = true;
1124                     xulG.set_tab(
1125                         urls.XUL_SPINE_LABEL,
1126                         { 'tab_name' : $("catStrings").getString('staff.cat.util.spine_editor.tab_name') },
1127                         {
1128                             'barcodes' : util.functional.map_list( copies, function(o){return o.barcode();})
1129                         }
1130                     );
1131                 }
1132             } catch(E) {
1133                 alert('2: Error in volume_copy_creator.js with g.stash_and_close(): ' + E);
1134             }
1135         }
1136
1137         try { if (typeof window.refresh == 'function') { window.refresh(); } } catch(E) { dump(E+'\n'); }
1138         try { if (typeof g.refresh == 'function') { g.refresh(); } } catch(E) { dump(E+'\n'); }
1139
1140         if (typeof xulG.unlock_copy_editor == 'function') {
1141             xulG.unlock_copy_editor();
1142         }
1143
1144         if (! dont_close) { xulG.close_tab(); }
1145
1146     } catch(E) {
1147         alert('3: Error in volume_copy_creator.js with g.stash_and_close(): ' + E);
1148     }
1149 }
1150
1151 g.load_prefs = function() {
1152     try {
1153         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
1154         JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
1155         if (file._file.exists()) {
1156             var prefs = file.get_object(); file.close();
1157             if (prefs.check_barcodes) {
1158                 if ( prefs.check_barcodes == 'false' ) {
1159                     $('check_barcodes').checked = false;
1160                 } else {
1161                     $('check_barcodes').checked = prefs.check_barcodes;
1162                 }
1163             } else {
1164                 $('check_barcodes').checked = false;
1165             }
1166             if (prefs.print_labels) {
1167                 if ( prefs.print_labels == 'false' ) {
1168                     $('print_labels').checked = false;
1169                 } else {
1170                     $('print_labels').checked = prefs.print_labels;
1171                 }
1172             } else {
1173                 $('print_labels').checked = false;
1174             }
1175
1176         }
1177     } catch(E) {
1178         alert('Error in volume_copy_creator.js with g.load_prefs(): ' + E);
1179     }
1180 }
1181
1182 g.save_prefs = function () {
1183     try {
1184         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
1185         JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
1186         file.set_object(
1187             {
1188                 'check_barcodes' : $('check_barcodes').checked,
1189                 'print_labels' : $('print_labels').checked,
1190             }
1191         );
1192         file.close();
1193     } catch(E) {
1194         alert('Error in volume_copy_creator.js with g.save_prefs(): ' + E);
1195     }
1196 }
1197
1198 g.render_class_menu = function(call_number_tb) {
1199     var ml = util.widgets.make_menulist(
1200         util.functional.map_list(
1201             g.data.list.acnc,
1202             function(o) {
1203                 return [ o.name(), o.id() ];
1204             }
1205         )
1206     );
1207     ml.setAttribute('rel_vert_pos',rel_vert_pos_call_number_classification);
1208     ml.addEventListener(
1209         'command',
1210         function() {
1211             call_number_tb.setAttribute('acnc_id',ml.value);
1212         },
1213         false
1214     );
1215     return ml;
1216 }
1217
1218 g.render_prefix_menu = function(call_number_tb) {
1219     var ou_id = call_number_tb.getAttribute('ou_id');
1220     var org = g.data.hash.aou[ ou_id ];
1221     var menulist = document.createElement('menulist');
1222         var menupopup = document.createElement('menupopup');
1223         menulist.appendChild(menupopup);
1224         var org_list = []; // order from top of consortium to owning lib
1225         while(org) {
1226             org_list.unshift(org.id());
1227             org = org.parent_ou();
1228             if (org && typeof org != 'object') {
1229                 org = g.data.hash.aou[ org ];
1230             }
1231         }
1232         for (var i = 0; i < org_list.length; i++) {
1233             g.render_prefix_menu_items(menupopup,org_list[i]);
1234         }
1235
1236     menulist.setAttribute('rel_vert_pos',rel_vert_pos_call_number_prefix);
1237     menulist.addEventListener(
1238         'command',
1239         function() {
1240             call_number_tb.setAttribute('acnp_id',menulist.value);
1241         },
1242         false
1243     );
1244     return menulist;
1245 }
1246
1247 g.render_prefix_menu_items = function(menupopup,ou_id) {
1248     if (typeof g.data.list['acnp_for_lib_'+ou_id] == 'undefined') {
1249         g.data.list['acnp_for_lib_'+ou_id] = g.network.simple_request(
1250             'FM_ACNP_RETRIEVE_VIA_PCRUD',
1251             [ ses(), {"owning_lib":{"=":ou_id}}, {"order_by":{"acnp":"label_sortkey"}} ]
1252         );
1253         g.data.stash('list');
1254     }
1255     for (var i = 0; i < g.data.list['acnp_for_lib_'+ou_id].length; i++) {
1256         var my_acnp = g.data.list['acnp_for_lib_'+ou_id][i];
1257         var menuitem = document.createElement('menuitem');
1258         menupopup.appendChild(menuitem);
1259             menuitem.setAttribute(
1260                 'label',
1261                 my_acnp.id() == -1 ? '' :
1262                 $('catStrings').getFormattedString(
1263                     'staff.cat.volume_copy_creator.call_number_prefix.menuitem_label',
1264                     [
1265                         my_acnp.label(),
1266                         g.data.hash.aou[ ou_id ].shortname()
1267                     ]
1268                 )
1269             );
1270             menuitem.setAttribute('value',my_acnp.id());
1271     }
1272 }
1273
1274 g.render_suffix_menu = function(call_number_tb) {
1275     var ou_id = call_number_tb.getAttribute('ou_id');
1276     var org = g.data.hash.aou[ ou_id ];
1277     var menulist = document.createElement('menulist');
1278         var menupopup = document.createElement('menupopup');
1279         menulist.appendChild(menupopup);
1280         var org_list = []; // order from top of consortium to owning lib
1281         while(org) {
1282             org_list.unshift(org.id());
1283             org = org.parent_ou();
1284             if (org && typeof org != 'object') {
1285                 org = g.data.hash.aou[ org ];
1286             }
1287         }
1288         for (var i = 0; i < org_list.length; i++) {
1289             g.render_suffix_menu_items(menupopup,org_list[i]);
1290         }
1291
1292     menulist.setAttribute('rel_vert_pos',rel_vert_pos_call_number_suffix);
1293     menulist.addEventListener(
1294         'command',
1295         function() {
1296             call_number_tb.setAttribute('acns_id',menulist.value);
1297         },
1298         false
1299     );
1300     return menulist;
1301 }
1302
1303 g.render_suffix_menu_items = function(menupopup,ou_id) {
1304     if (typeof g.data.list['acns_for_lib_'+ou_id] == 'undefined') {
1305         g.data.list['acns_for_lib_'+ou_id] = g.network.simple_request(
1306             'FM_ACNS_RETRIEVE_VIA_PCRUD',
1307             [ ses(), {"owning_lib":{"=":ou_id}}, {"order_by":{"acns":"label_sortkey"}} ]
1308         );
1309         g.data.stash('list');
1310     }
1311     for (var i = 0; i < g.data.list['acns_for_lib_'+ou_id].length; i++) {
1312         var my_acns = g.data.list['acns_for_lib_'+ou_id][i];
1313         var menuitem = document.createElement('menuitem');
1314         menupopup.appendChild(menuitem);
1315             menuitem.setAttribute(
1316                 'label',
1317                 my_acns.id() == -1 ? '' :
1318                 $('catStrings').getFormattedString(
1319                     'staff.cat.volume_copy_creator.call_number_suffix.menuitem_label',
1320                     [
1321                         my_acns.label(),
1322                         g.data.hash.aou[ ou_id ].shortname()
1323                     ]
1324                 )
1325             );
1326             menuitem.setAttribute('value',my_acns.id());
1327     }
1328 }
1329
1330
1331 g.list_callnumbers = function(doc_id, label_class) {
1332     var cn_blob;
1333     try {
1334         cn_blob = g.network.simple_request('BLOB_MARC_CALLNUMBERS_RETRIEVE',[g.doc_id, label_class]);
1335     } catch(E) {
1336         cn_blob = [];
1337     }
1338     var hbox = document.getElementById('marc_cn');
1339     var ml = util.widgets.make_menulist(
1340         [
1341             [ '', '' ]
1342         ].concat(
1343             util.functional.map_list(
1344                 cn_blob,
1345                 function(o) {
1346                     for (var i in o) {
1347                         return [ o[i], i ];
1348                     }
1349                 }
1350             )
1351         )
1352     ); hbox.appendChild(ml);
1353     ml.setAttribute('editable','true');
1354     ml.setAttribute('width', '200');
1355     ml.setAttribute('id', 'marc_cn_menulist');
1356 }
1357
1358 g.list_classes = function() {
1359     var hbox = $('batch_class');
1360     var ml = util.widgets.make_menulist(
1361         [
1362             [ '<No Change>', false ]
1363         ].concat(
1364             util.functional.map_list(
1365                 g.data.list.acnc,
1366                 function(o) {
1367                     return [ o.name(), o.id() ];
1368                 }
1369             )
1370         )
1371     ); hbox.appendChild(ml);
1372     ml.setAttribute('id','batch_class_menulist');
1373     ml.addEventListener(
1374         'command',
1375         function() {
1376             if (!isNaN(Number(ml.value))) {
1377                 addCSSClass(hbox,'copy_editor_field_changed');
1378                 if (xulG.unified_interface) {
1379                     xulG.notify_of_templatable_field_change('batch_class_menulist',ml.value);
1380                 }
1381             } else {
1382                 removeCSSClass(hbox,'copy_editor_field_changed');
1383             }
1384         },
1385         false
1386     );
1387 }
1388
1389 g.list_prefixes = function() {
1390     var hbox = $('batch_prefix');
1391     var ml = util.widgets.make_menulist(
1392         [
1393             [ '<No Change>', false ]
1394         ]
1395     ); hbox.appendChild(ml);
1396     for (var i = 0; i < g.common_ancestor_ou_ids.length; i++) {
1397         g.render_prefix_menu_items(ml.firstChild,g.common_ancestor_ou_ids[i]);
1398     }
1399     ml.setAttribute('id','batch_prefix_menulist');
1400     ml.addEventListener(
1401         'command',
1402         function() {
1403             if (!isNaN(Number(ml.value))) {
1404                 addCSSClass(hbox,'copy_editor_field_changed');
1405                 if (xulG.unified_interface) {
1406                     xulG.notify_of_templatable_field_change('batch_prefix_menulist',ml.value);
1407                 }
1408             } else {
1409                 removeCSSClass(hbox,'copy_editor_field_changed');
1410             }
1411         },
1412         false
1413     );
1414 }
1415
1416 g.list_suffixes = function() {
1417     var hbox = $('batch_suffix');
1418     var ml = util.widgets.make_menulist(
1419         [
1420             [ '<No Change>', false ]
1421         ]
1422     ); hbox.appendChild(ml);
1423     for (var i = 0; i < g.common_ancestor_ou_ids.length; i++) {
1424         g.render_suffix_menu_items(ml.firstChild,g.common_ancestor_ou_ids[i]);
1425     }
1426     ml.setAttribute('id','batch_suffix_menulist');
1427     ml.addEventListener(
1428         'command',
1429         function() {
1430             if (!isNaN(Number(ml.value))) {
1431                 addCSSClass(hbox,'copy_editor_field_changed');
1432                 if (xulG.unified_interface) {
1433                     xulG.notify_of_templatable_field_change('batch_suffix_menulist',ml.value);
1434                 }
1435             } else {
1436                 removeCSSClass(hbox,'copy_editor_field_changed');
1437             }
1438         },
1439         false
1440     );
1441 }
1442
1443 g.render_batch_button = function() {
1444     var hbox = $('batch_button_box');
1445     var btn = document.createElement('button');
1446     btn.setAttribute('id','batch_button');
1447     btn.setAttribute('label',$('catStrings').getString('staff.cat.volume_copy_creator.my_init.btn.label'));
1448     btn.setAttribute('accesskey',$('catStrings').getString('staff.cat.volume_copy_creator.my_init.btn.accesskey'));
1449     btn.setAttribute('image','/xul/server/skin/media/images/down_arrow.gif');
1450     hbox.appendChild(btn);
1451     btn.addEventListener(
1452         'command',
1453         function() {
1454             var nl = document.getElementsByTagName('textbox');
1455             for (var i = 0; i < nl.length; i++) {
1456                 /* label */
1457                 if (nl[i].getAttribute('rel_vert_pos')==rel_vert_pos_call_number && !nl[i].disabled) {
1458                     var label =  $('marc_cn').firstChild.value;
1459                     if (label != '') {
1460                         nl[i].value = label;
1461                         util.widgets.dispatch('change',nl[i]);
1462                     }
1463                 }
1464             }
1465             nl = document.getElementsByTagName('menulist');
1466             for (var i = 0; i < nl.length; i++) {
1467                 /* classification */
1468                 if (nl[i].getAttribute('rel_vert_pos')==rel_vert_pos_call_number_classification && !nl[i].disabled) {
1469                     var value =  $('batch_class_menulist').value;
1470                     if (!isNaN( Number(value) )) {
1471                         nl[i].value = value;
1472                         util.widgets.dispatch('command',nl[i]);
1473                     }
1474                 }
1475                 /* prefix */
1476                 if (nl[i].getAttribute('rel_vert_pos')==rel_vert_pos_call_number_prefix && !nl[i].disabled) {
1477                     var value =  $('batch_prefix_menulist').value;
1478                     if (!isNaN( Number(value) )) {
1479                         nl[i].value = value;
1480                         util.widgets.dispatch('command',nl[i]);
1481                     }
1482                 }
1483                 /* suffix */
1484                 if (nl[i].getAttribute('rel_vert_pos')==rel_vert_pos_call_number_suffix && !nl[i].disabled) {
1485                     var value =  $('batch_suffix_menulist').value;
1486                     if (!isNaN( Number(value) )) {
1487                         nl[i].value = value;
1488                         util.widgets.dispatch('command',nl[i]);
1489                     }
1490                 }
1491             }
1492             setTimeout(
1493                 function() {
1494                     g.gather_copies_soon();
1495                 },0
1496             );
1497             if (g.last_focus) setTimeout( function() { g.last_focus.focus(); }, 0 );
1498         },
1499         false
1500     );
1501 }