Lazy Circ (AKA partial barcode lookup)
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / cat / opac.js
1 var docid; var marc_html; var top_pane; var bottom_pane; var opac_frame; var opac_url;
2
3 var marc_view_reset = true;
4 var marc_edit_reset = true;
5 var copy_browser_reset = true;
6 var manage_parts_reset = true;
7 var manage_multi_home_reset = true;
8 var hold_browser_reset = true;
9 var serctrl_view_reset = true;
10
11 function $(id) { return document.getElementById(id); }
12
13 function my_init() {
14     try {
15         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
16         if (typeof JSAN == 'undefined') { throw(document.getElementById('offlineStrings').getString('common.jsan.missing')); }
17         JSAN.errorLevel = "die"; // none, warn, or die
18         JSAN.addRepository('..');
19         JSAN.use('util.error'); g.error = new util.error();
20         g.error.sdump('D_TRACE','my_init() for cat/opac.xul');
21
22         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
23         XML_HTTP_SERVER = g.data.server_unadorned;
24
25         // Pull in local customizations
26         var r = new XMLHttpRequest();
27         r.open("GET", xulG.url_prefix('/xul/server/skin/custom.js'), false);
28         r.send(null);
29         if (r.status == 200) {
30             dump('Evaluating /xul/server/skin/custom.js\n');
31             eval( r.responseText );
32         }
33
34         window.help_context_set_locally = true;
35
36         JSAN.use('util.network'); g.network = new util.network();
37
38         g.cgi = new CGI();
39         try { authtime = g.cgi.param('authtime') || xulG.authtime; } catch(E) { g.error.sdump('D_ERROR',E); }
40         try { docid = g.cgi.param('docid') || xulG.docid; } catch(E) { g.error.sdump('D_ERROR',E); }
41         try { opac_url = g.cgi.param('opac_url') || xulG.opac_url; } catch(E) { g.error.sdump('D_ERROR',E); }
42         try { g.view_override = g.cgi.param('default_view') || xulG.default_view; } catch(E) { g.error.sdump('D_ERROR',E); }
43
44         JSAN.use('util.deck');
45         top_pane = new util.deck('top_pane');
46         bottom_pane = new util.deck('bottom_pane');
47
48         set_opac();
49
50     } catch(E) {
51         var err_msg = document.getElementById("offlineStrings").getFormattedString("common.exception", ["cat/opac.xul", E]);
52         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
53         alert(err_msg);
54     }
55 }
56
57 function default_focus() {
58     opac_wrapper_set_help_context(); 
59 }
60
61 function opac_wrapper_set_help_context() {
62     try {
63         dump('Entering opac.js, opac_wrapper_set_help_context\n');
64         var cw = bottom_pane.get_contentWindow(); 
65         if (cw && typeof cw['location'] != 'undefined') {
66             if (typeof cw.help_context_set_locally == 'undefined') {
67                 var help_params = {
68                     'protocol' : cw.location.protocol,
69                     'hostname' : cw.location.hostname,
70                     'port' : cw.location.port,
71                     'pathname' : cw.location.pathname,
72                     'src' : ''
73                 };
74                 xulG.set_help_context(help_params);
75             } else {
76                 dump('\tcw.help_context_set_locally = ' + cw.help_context_set_locally + '\n');
77                 if (typeof cw.default_focus == 'function') {
78                     cw.default_focus();
79                 }
80             }
81         } else {
82             dump('opac.js: problem in opac_wrapper_set_help_context(): bottom_pane = ' + bottom_pane + ' cw = ' + cw + '\n');
83             dump('\tcw.location = ' + cw.location + '\n');
84         }
85     } catch(E) {
86         // We can expect some errors here if this called before the DOM is ready.  Easiest to just trap and ignore
87         dump('Error in opac.js, opac_wrapper_set_help_context(): ' + E + '\n');
88     }
89 }
90
91 function set_brief_view() {
92     var url = xulG.url_prefix( urls.XUL_BIB_BRIEF ) + '?docid=' + window.escape(docid); 
93     dump('spawning ' + url + '\n');
94
95     var content_params = {
96         'set_tab_name' : function(n) {
97             if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
98                 try { window.xulG.set_tab_name(document.getElementById('offlineStrings').getFormattedString("cat.bib_record", [n])); } catch(E) { alert(E); }
99             } else {
100                 dump('no set_tab_name\n');
101             }
102         }
103     };
104
105     ["url_prefix", "new_tab", "set_tab", "close_tab", "new_patron_tab",
106         "set_patron_tab", "volume_item_creator", "get_new_session",
107         "holdings_maintenance_tab", "open_chrome_window", "url_prefix",
108         "network_meter", "page_meter", "set_statusbar", "set_help_context",
109         "get_barcode"
110     ].forEach(function(k) { content_params[k] = xulG[k]; });
111
112     top_pane.set_iframe( 
113         url,
114         {},
115         content_params
116     );
117 }
118
119 function set_marc_view() {
120     g.view = 'marc_view';
121     if (marc_view_reset) {
122         bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_MARC_VIEW ) + '?docid=' + window.escape(docid),{},xulG);
123         marc_view_reset = false;
124     } else {
125         bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_MARC_VIEW ) + '?docid=' + window.escape(docid),{},xulG);
126     }
127     opac_wrapper_set_help_context(); 
128     bottom_pane.get_contentWindow().addEventListener('load',opac_wrapper_set_help_context,false);
129 }
130
131 function set_marc_edit() {
132     g.view = 'marc_edit';
133     var a =    xulG.url_prefix( urls.XUL_MARC_EDIT );
134     var b =    {};
135     var c =    {
136             'marc_control_number_identifier' : g.data.hash.aous['cat.marc_control_number_identifier'] || 'Set cat.marc_control_number_identifier in Library Settings',
137             'record' : { 'url' : '/opac/extras/supercat/retrieve/marcxml/record/' + docid, "id": docid, "rtype": "bre" },
138             'fast_add_item' : function(doc_id,cn_label,cp_barcode) {
139                 try {
140                     var cat = { util: {} }; /* FIXME: kludge since we can't load remote JSAN libraries into chrome */
141                     cat.util.spawn_copy_editor = function(params) {
142                         try {
143                             if (!params.copy_ids && !params.copies) return;
144                             if (params.copy_ids && params.copy_ids.length == 0) return;
145                             if (params.copies && params.copies.length == 0) return;
146                             if (params.copy_ids) params.copy_ids = js2JSON(params.copy_ids); // legacy
147                             if (!params.caller_handles_update) params.handle_update = 1; // legacy
148
149                             var obj = {};
150                             JSAN.use('util.network'); obj.network = new util.network();
151                             JSAN.use('util.error'); obj.error = new util.error();
152                         
153                             var title = '';
154                             if (params.copy_ids && params.copy_ids.length > 1 && params.edit == 1)
155                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.batch_edit');
156                             else if(params.copies && params.copies.length > 1 && params.edit == 1)
157                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.batch_view');
158                             else if(params.copy_ids && params.copy_ids.length == 1)
159                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.edit');
160                             else
161                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.view');
162
163                             JSAN.use('util.window'); var win = new util.window();
164                             var my_xulG = win.open(
165                                 (urls.XUL_COPY_EDITOR),
166                                 title,
167                                 'chrome,modal,resizable',
168                                 params
169                             );
170                             if (!my_xulG.copies && params.edit) {
171                             } else {
172                                 return my_xulG.copies;
173                             }
174                             return [];
175                         } catch(E) {
176                             JSAN.use('util.error'); var error = new util.error();
177                             error.standard_unexpected_error_alert('Error in chrome/content/cat/opac.js, cat.util.spawn_copy_editor',E);
178                         }
179                     }
180                     cat.util.fast_item_add = function(doc_id,cn_label,cp_barcode) {
181                         var error;
182                         try {
183
184                             JSAN.use('util.error'); error = new util.error();
185                             JSAN.use('util.network'); var network = new util.network();
186
187                             var acn_blob = network.simple_request(
188                                 'FM_ACN_FIND_OR_CREATE',
189                                 [ ses(), cn_label, doc_id, ses('ws_ou') ]
190                             );
191
192                             if (typeof acn_blob.ilsevent != 'undefined') {
193                                 error.standard_unexpected_error_alert('Error in chrome/content/cat/opac.js, cat.util.fast_item_add', acn_blob);
194                                 return;
195                             }
196
197                             // Get the default copy status; default to available if unset, per 1.6
198                             var fast_ccs = g.data.hash.aous['cat.default_copy_status_fast'] || 0;
199
200                             var copy_obj = new acp();
201                             copy_obj.id( -1 );
202                             copy_obj.isnew('1');
203                             copy_obj.barcode( cp_barcode );
204                             copy_obj.call_number( acn_blob.acn_id );
205                             copy_obj.circ_lib( ses('ws_ou') );
206                             /* FIXME -- use constants */
207                             copy_obj.deposit(0);
208                             copy_obj.price(0);
209                             copy_obj.deposit_amount(0);
210                             copy_obj.fine_level(2); // Normal
211                             copy_obj.loan_duration(2); // Normal
212                             copy_obj.location(1); // Stacks
213                             copy_obj.status(fast_ccs);
214                             copy_obj.circulate(get_db_true());
215                             copy_obj.holdable(get_db_true());
216                             copy_obj.opac_visible(get_db_true());
217                             copy_obj.ref(get_db_false());
218
219                             JSAN.use('util.window'); var win = new util.window();
220                             return cat.util.spawn_copy_editor( { 'handle_update' : 1, 'edit' : 1, 'docid' : doc_id, 'copies' : [ copy_obj ] });
221
222                         } catch(E) {
223                             if (error) error.standard_unexpected_error_alert('Error in chrome/content/cat/opac.js, cat.util.fast_item_add #2',E); else alert('FIXME: ' + E);
224                         }
225                     }
226                     return cat.util.fast_item_add(doc_id,cn_label,cp_barcode);
227                 } catch(E) {
228                     alert('Error in chrome/content/cat/opac.js, set_marc_edit, fast_item_add: ' + E);
229                 }
230             },
231             'save' : {
232                 'label' : document.getElementById('offlineStrings').getString('cat.save_record'),
233                 'func' : function (new_marcxml) {
234                     try {
235                         var r = g.network.simple_request('MARC_XML_RECORD_UPDATE', [ ses(), docid, new_marcxml ]);
236                         marc_view_reset = true;
237                         copy_browser_reset = true;
238                         hold_browser_reset = true;
239                         if (typeof r.ilsevent != 'undefined') {
240                             throw(r);
241                         } else {
242                             return {
243                                 'id' : r.id(),
244                                 'oncomplete' : function() {}
245                             };
246                         }
247                     } catch(E) {
248                             g.error.standard_unexpected_error_alert(document.getElementById('offlineStrings').getString("cat.save.failure"), E);
249                     }
250                 }
251             },
252             'lock_tab' : xulG.lock_tab,
253             'unlock_tab' : xulG.unlock_tab
254         };
255     if (marc_edit_reset) {
256         bottom_pane.reset_iframe( a,b,c );
257         marc_edit_reset = false;
258     } else {
259         bottom_pane.set_iframe( a,b,c );
260     }
261     opac_wrapper_set_help_context(); 
262     bottom_pane.get_contentWindow().addEventListener('load',opac_wrapper_set_help_context,false);
263 }
264
265 function set_copy_browser() {
266     g.view = 'copy_browser';
267     if (copy_browser_reset) {
268         bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_COPY_VOLUME_BROWSE ) + '?docid=' + window.escape(docid),{},xulG);
269         copy_browser_reset =false;
270     } else {
271         bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_COPY_VOLUME_BROWSE ) + '?docid=' + window.escape(docid),{},xulG);
272     }
273     opac_wrapper_set_help_context(); 
274     bottom_pane.get_contentWindow().addEventListener('load',opac_wrapper_set_help_context,false);
275 }
276
277 function set_hold_browser() {
278     g.view = 'hold_browser';
279     if (hold_browser_reset) {
280         bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_HOLDS_BROWSER ) + '?docid=' + window.escape(docid),{},xulG);
281         hold_browser_reset = false;
282     } else {
283         bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_HOLDS_BROWSER ) + '?docid=' + window.escape(docid),{},xulG);
284     }
285     opac_wrapper_set_help_context(); 
286     bottom_pane.get_contentWindow().addEventListener('load',opac_wrapper_set_help_context,false);
287 }
288
289
290 function open_acq_orders() {
291     try {
292         var content_params = {
293             "session": ses(),
294             "authtime": ses("authtime"),
295             "no_xulG": false,
296             "show_nav_buttons": true,
297             "show_print_button": false
298         };
299
300         ["url_prefix", "new_tab", "set_tab", "close_tab", "new_patron_tab",
301             "set_patron_tab", "volume_item_creator", "get_new_session",
302             "holdings_maintenance_tab", "set_tab_name", "open_chrome_window",
303             "url_prefix", "network_meter", "page_meter", "set_statusbar",
304             "set_help_context", "get_barcode"
305         ].forEach(function(k) { content_params[k] = xulG[k]; });
306
307         var loc = urls.XUL_BROWSER + "?url=" + window.escape(
308             xulG.url_prefix("/eg/acq/lineitem/related/") +
309             docid + "?target=bib"
310         );
311         xulG.new_tab(
312             loc, {
313                 "tab_name": $("offlineStrings").getString(
314                     "staff.cat.opac.related_items"
315                 ),
316                 "browser": false
317             }, content_params
318         );
319     } catch (E) {
320         g.error.sdump("D_ERROR", E);
321     }
322 }
323
324 function open_alt_serial_mgmt() {
325     try {
326         var content_params = {
327             "session": ses(),
328             "authtime": ses("authtime"),
329             "show_nav_buttons": true,
330             "no_xulG": false,
331             "show_print_button": false
332         };
333
334         ["url_prefix", "new_tab", "set_tab", "close_tab", "new_patron_tab",
335             "set_patron_tab", "volume_item_creator", "get_new_session",
336             "holdings_maintenance_tab", "set_tab_name", "open_chrome_window",
337             "url_prefix", "network_meter", "page_meter", "set_statusbar",
338             "set_help_context", "get_barcode"
339         ].forEach(function(k) { content_params[k] = xulG[k]; });
340
341         var loc = urls.XUL_BROWSER + "?url=" + window.escape(
342             xulG.url_prefix("/eg/serial/list_subscription?record_entry=") +
343             docid
344         );
345         xulG.new_tab(
346             loc, {
347                 "tab_name": $("offlineStrings").getString(
348                     "staff.cat.opac.serial_alt_mgmt"
349                 ),
350                 "browser": false
351             }, content_params
352         );
353     } catch (E) {
354         g.error.sdump("D_ERROR", E);
355     }
356 }
357
358 function set_opac() {
359     g.view = 'opac';
360     try {
361         var content_params = { 
362             'show_nav_buttons' : true,
363             'show_print_button' : true,
364             'passthru_content_params' : { 
365                 'authtoken' : ses(), 
366                 'authtime' : ses('authtime'),
367                 'window_open' : function(a,b,c) {
368                     try {
369                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
370                         return window.open(a,b,c);
371                     } catch(E) {
372                         g.error.standard_unexpected_error_alert('window_open',E);
373                     }
374                 },
375                 'get_barcode' : xulG.get_barcode
376             },
377             'on_url_load' : function(f) {
378                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
379                 var win;
380                 try {
381                     if (typeof f.contentWindow.wrappedJSObject.attachEvt != 'undefined') {
382                         win = f.contentWindow.wrappedJSObject;
383                     } else {
384                         win = f.contentWindow;
385                     }
386                 } catch(E) {
387                     win = f.contentWindow;
388                 }
389                 win.attachEvt("rdetail", "recordRetrieved",
390                     function(id){
391                         try {
392                             if (docid == id) return;
393                             docid = id;
394                             refresh_display(id);
395                         } catch(E) {
396                             g.error.standard_unexpected_error_alert('rdetail -> recordRetrieved',E);
397                         }
398                     }
399                 );
400                 
401                 g.f_record_start = null; g.f_record_prev = null; g.f_record_next = null; g.f_record_end = null;
402                 $('record_start').disabled = true; $('record_next').disabled = true;
403                 $('record_prev').disabled = true; $('record_end').disabled = true;
404                 $('record_pos').setAttribute('value','');
405
406                 win.attachEvt("rdetail", "nextPrevDrawn",
407                     function(rIndex,rCount){
408                         $('record_pos').setAttribute('value', document.getElementById('offlineStrings').getFormattedString('cat.record.counter', [(1+rIndex), rCount ? rCount : 1]));
409                         if (win.rdetailNext) {
410                             g.f_record_next = function() { 
411                                 g.view_override = g.view; 
412                                 win.rdetailNext(); 
413                             }
414                             $('record_next').disabled = false;
415                         }
416                         if (win.rdetailPrev) {
417                             g.f_record_prev = function() { 
418                                 g.view_override = g.view; 
419                                 win.rdetailPrev(); 
420                             }
421                             $('record_prev').disabled = false;
422                         }
423                         if (win.rdetailStart) {
424                             g.f_record_start = function() { 
425                                 g.view_override = g.view; 
426                                 win.rdetailStart(); 
427                             }
428                             $('record_start').disabled = false;
429                         }
430                         if (win.rdetailEnd) {
431                             g.f_record_end = function() { 
432                                 g.view_override = g.view; 
433                                 win.rdetailEnd(); 
434                             }
435                             $('record_end').disabled = false;
436                         }
437                     }
438                 );
439
440                 $('mfhd_add').setAttribute('oncommand','create_mfhd()');
441                 var mfhd_edit_menu = $('mfhd_edit');
442                 var mfhd_delete_menu = $('mfhd_delete');
443
444                 // clear menus on subsequent loads
445                 if (mfhd_edit_menu.firstChild) {
446                     mfhd_edit_menu.removeChild(mfhd_edit_menu.firstChild);
447                     mfhd_delete_menu.removeChild(mfhd_delete_menu.firstChild);
448                 }
449
450                 mfhd_edit_menu.disabled = true;
451                 mfhd_delete_menu.disabled = true;
452
453                 win.attachEvt("rdetail", "MFHDDrawn",
454                     function() {
455                         if (win.mfhdDetails && win.mfhdDetails.length > 0) {
456                             g.mfhd = {};
457                             g.mfhd.details = win.mfhdDetails;
458                             mfhd_edit_menu.disabled = false;
459                             mfhd_delete_menu.disabled = false;
460                             for (var i = 0; i < win.mfhdDetails.length; i++) {
461                                 var mfhd_details = win.mfhdDetails[i];
462                                 var num = mfhd_details.entryNum;
463                                 num++;
464                                 var label = mfhd_details.label + ' (' + num + ')';
465                                 var item = mfhd_edit_menu.appendItem(label);
466                                 item.setAttribute('oncommand','open_mfhd_editor('+mfhd_details.id+')');
467                                 item = mfhd_delete_menu.appendItem(label);
468                                 item.setAttribute('oncommand','delete_mfhd('+mfhd_details.id+')');
469                             }
470                         }
471                     }
472                 );
473             },
474             'url_prefix' : xulG.url_prefix,
475         };
476         content_params.new_tab = xulG.new_tab;
477         content_params.set_tab = xulG.set_tab;
478         content_params.close_tab = xulG.close_tab;
479         content_params.lock_tab = xulG.lock_tab;
480         content_params.unlock_tab = xulG.unlock_tab;
481         content_params.inspect_tab = xulG.inspect_tab;
482         content_params.new_patron_tab = xulG.new_patron_tab;
483         content_params.set_patron_tab = xulG.set_patron_tab;
484         content_params.volume_item_creator = xulG.volume_item_creator;
485         content_params.get_new_session = xulG.get_new_session;
486         content_params.holdings_maintenance_tab = xulG.holdings_maintenance_tab;
487         content_params.set_tab_name = xulG.set_tab_name;
488         content_params.open_chrome_window = xulG.open_chrome_window;
489         content_params.url_prefix = xulG.url_prefix;
490         content_params.network_meter = xulG.network_meter;
491         content_params.page_meter = xulG.page_meter;
492         content_params.set_statusbar = xulG.set_statusbar;
493         content_params.set_help_context = xulG.set_help_context;
494         content_params.get_barcode = xulG.get_barcode;
495
496         if (opac_url) { content_params.url = opac_url; } else { content_params.url = xulG.url_prefix( urls.browser ); }
497         browser_frame = bottom_pane.set_iframe( xulG.url_prefix(urls.XUL_BROWSER) + '?name=Catalog', {}, content_params);
498         /* // Remember to use the REMOTE_BROWSER if we ever try to move this to remote xul again
499         browser_frame = bottom_pane.set_iframe( xulG.url_prefix(urls.XUL_REMOTE_BROWSER) + '?name=Catalog', {}, content_params);
500         */
501     } catch(E) {
502         g.error.sdump('D_ERROR','set_opac: ' + E);
503     }
504     opac_wrapper_set_help_context(); 
505     bottom_pane.get_contentWindow().addEventListener('load',opac_wrapper_set_help_context,false);
506 }
507
508 function set_serctrl_view() {
509     g.view = 'serctrl_view';
510     if (serctrl_view_reset) {
511         bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_SERIAL_SERCTRL_MAIN ) + '?docid=' + window.escape(docid), {}, xulG);
512         serctrl_view_reset =false;
513     } else {
514         bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_SERIAL_SERCTRL_MAIN ) + '?docid=' + window.escape(docid), {}, xulG);
515     }
516 }
517
518 function create_mfhd() {
519     try {
520         JSAN.use('util.window'); var win = new util.window();
521         var select_aou_window = win.open(
522             xulG.url_prefix(urls.XUL_SERIAL_SELECT_AOU),
523             '_blank',
524             'chrome,resizable,modal,centerscreen',
525             {'server_unadorned' : g.data.server_unadorned}
526         );
527         if (!select_aou_window.create_mfhd_aou) {
528             return;
529         }
530         var r = g.network.simple_request(
531                 'MFHD_XML_RECORD_CREATE',
532                 [ ses(), 1, select_aou_window.create_mfhd_aou, docid ]
533             );
534         if (typeof r.ilsevent != 'undefined') {
535             throw(r);
536         }
537         alert("MFHD record created."); //TODO: better success message
538         //TODO: refresh opac display
539     } catch(E) {
540         g.error.standard_unexpected_error_alert("Create MFHD failed", E); //TODO: better error handling
541     }
542 }
543
544 function delete_mfhd(sre_id) {
545     if (g.error.yns_alert(
546         document.getElementById('offlineStrings').getFormattedString('serial.delete_record.confirm', [sre_id]),
547         document.getElementById('offlineStrings').getString('cat.opac.delete_record'),
548         document.getElementById('offlineStrings').getString('cat.opac.delete'),
549         document.getElementById('offlineStrings').getString('cat.opac.cancel'),
550         null,
551         document.getElementById('offlineStrings').getString('cat.opac.record_deleted.confirm')) == 0) {
552         var robj = g.network.request(
553                 'open-ils.permacrud',
554                 'open-ils.permacrud.delete.sre',
555                 [ses(),sre_id]);
556         if (typeof robj.ilsevent != 'undefined') {
557             alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_deleted.error',  [docid, robj.textcode, robj.desc]) + '\n');
558         } else {
559             alert(document.getElementById('offlineStrings').getString('cat.opac.record_deleted'));
560             //TODO: refresh opac display
561         }
562     }
563 }
564
565 function open_mfhd_editor(sre_id) {
566     try {
567         var r = g.network.simple_request(
568                 'FM_SRE_RETRIEVE',
569                 [ ses(), sre_id ]
570               );
571         if (typeof r.ilsevent != 'undefined') {
572             throw(r);
573         }
574         open_marc_editor(r, 'MFHD');
575     } catch(E) {
576         g.error.standard_unexpected_error_alert("Create MFHD failed", E); //TODO: better error handling
577     }
578 }
579
580 function open_marc_editor(rec, label) {
581     win = window.open( xulG.url_prefix('/xul/server/cat/marcedit.xul') );
582
583     win.xulG = {
584         record : {marc : rec.marc()},
585         save : {
586             label: 'Save ' + label,
587             func: function(xmlString) {  // TODO: switch to pcrud, or define an sre update method in Serial.pm?
588                 var method = 'open-ils.permacrud.update.' + rec.classname;
589                 rec.marc(xmlString);
590                 g.network.request(
591                     'open-ils.permacrud', method,
592                     [ses(), rec]
593                 );
594             }
595         }
596     };
597 }
598
599 function bib_in_new_tab() {
600     try {
601         var url = browser_frame.contentWindow.g.browser.controller.view.browser_browser.contentWindow.wrappedJSObject.location.href;
602         var content_params = { 'session' : ses(), 'authtime' : ses('authtime'), 'opac_url' : url };
603         content_params.url_prefix = xulG.url_prefix;
604         content_params.new_tab = xulG.new_tab;
605         content_params.set_tab = xulG.set_tab;
606         content_params.close_tab = xulG.close_tab;
607         content_params.lock_tab = xulG.lock_tab;
608         content_params.unlock_tab = xulG.unlock_tab;
609         content_params.inspect_tab = xulG.inspect_tab;
610         content_params.new_patron_tab = xulG.new_patron_tab;
611         content_params.set_patron_tab = xulG.set_patron_tab;
612         content_params.volume_item_creator = xulG.volume_item_creator;
613         content_params.get_new_session = xulG.get_new_session;
614         content_params.holdings_maintenance_tab = xulG.holdings_maintenance_tab;
615         content_params.set_tab_name = xulG.set_tab_name;
616         content_params.open_chrome_window = xulG.open_chrome_window;
617         content_params.url_prefix = xulG.url_prefix;
618         content_params.network_meter = xulG.network_meter;
619         content_params.page_meter = xulG.page_meter;
620         content_params.set_statusbar = xulG.set_statusbar;
621         content_params.set_help_context = xulG.set_help_context;
622         content_params.get_barcode = xulG.get_barcode;
623
624         xulG.new_tab(xulG.url_prefix(urls.XUL_OPAC_WRAPPER), {}, content_params);
625     } catch(E) {
626         g.error.sdump('D_ERROR',E);
627     }
628 }
629
630 function batch_receive_in_new_tab() {
631     try {
632         var content_params = {"session": ses(), "authtime": ses("authtime")};
633
634         ["url_prefix", "new_tab", "set_tab", "close_tab", "new_patron_tab",
635             "set_patron_tab", "volume_item_creator", "get_new_session",
636             "holdings_maintenance_tab", "set_tab_name", "open_chrome_window",
637             "url_prefix", "network_meter", "page_meter", "set_statusbar",
638             "set_help_context", "get_barcode"
639         ].forEach(function(k) { content_params[k] = xulG[k]; });
640
641         xulG.new_tab(
642             xulG.url_prefix(urls.XUL_SERIAL_BATCH_RECEIVE) +
643                 "?docid=" + window.escape(docid), {
644                 "tab_name": $("offlineStrings").getString(
645                     "menu.cmd_serial_batch_receive.tab"
646                 )
647             }, content_params
648         );
649     } catch (E) {
650         g.error.sdump("D_ERROR", E);
651     }
652 }
653
654 function remove_me() {
655     var url = xulG.url_prefix( urls.XUL_BIB_BRIEF ) + '?docid=' + window.escape(docid);
656     dump('removing ' + url + '\n');
657     try { top_pane.remove_iframe( url ); } catch(E) { dump(E + '\n'); }
658     $('nav').setAttribute('hidden','true');
659 }
660
661 function add_to_bucket() {
662     JSAN.use('util.window'); var win = new util.window();
663     win.open(
664         xulG.url_prefix(urls.XUL_RECORD_BUCKETS_QUICK),
665         '_blank',
666         'chrome,resizable,modal,centerscreen',
667         {
668             record_ids: [ docid ]
669         }
670     );
671 }
672
673 function mark_for_overlay() {
674     g.data.marked_record = docid;
675     g.data.stash('marked_record');
676     var robj = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative',[docid]);
677     if (typeof robj.ilsevent == 'undefined') {
678         g.data.marked_record_mvr = robj;
679     } else {
680         g.data.marked_record_mvr = null;
681         g.error.standard_unexpected_error_alert('in mark_for_overlay',robj);
682     }
683     g.data.stash('marked_record_mvr');
684     if (g.data.marked_record_mvr) {
685         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_marked_for_overlay.tcn.alert',[ g.data.marked_record_mvr.tcn() ]));
686         xulG.set_statusbar(
687             1,
688             $("offlineStrings").getFormattedString('staff.cat.z3950.marked_record_for_overlay_indicator.tcn.label',[g.data.marked_record_mvr.tcn()]),
689             $("offlineStrings").getFormattedString('staff.cat.z3950.marked_record_for_overlay_indicator.record_id.label',[g.data.marked_record]),
690             gen_statusbar_click_handler('marked_record')
691         );
692     } else {
693         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_marked_for_overlay.record_id.alert',[ g.data.marked_record  ]));
694         xulG.set_statusbar(
695             1,
696             $("offlineStrings").getFormattedString('staff.cat.z3950.marked_record_for_overlay_indicator.record_id.label',[g.data.marked_record]),
697             '',
698             gen_statusbar_click_handler('marked_record')
699         );
700     }
701 }
702
703 function mark_for_hold_transfer() {
704     g.data.marked_record_for_hold_transfer = docid;
705     g.data.stash('marked_record_for_hold_transfer');
706     var robj = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative',[docid]);
707     if (typeof robj.ilsevent == 'undefined') {
708         g.data.marked_record_for_hold_transfer_mvr = robj;
709     } else {
710         g.data.marked_record_for_hold_transfer_mvr = null;
711         g.error.standard_unexpected_error_alert('in mark_for_hold_transfer',robj);
712     }
713     g.data.stash('marked_record_for_hold_transfer_mvr');
714     if (g.data.marked_record_mvr) {
715         var m = $("offlineStrings").getFormattedString('staff.cat.opac.marked_record_for_hold_transfer_indicator.tcn.label',[g.data.marked_record_for_hold_transfer_mvr.tcn()]);
716         alert(m);
717         xulG.set_statusbar(
718             3,
719             m,
720             '',
721             gen_statusbar_click_handler('marked_record_for_hold_transfer')
722         );
723     } else {
724         var m = $("offlineStrings").getFormattedString('staff.cat.opac.marked_record_for_hold_transfer_indicator.record_id.label',[g.data.marked_record_for_hold_transfer]);
725         alert(m);
726         xulG.set_statusbar(
727             3,
728             m,
729             '',
730             gen_statusbar_click_handler('marked_record_for_hold_transfer')
731         );
732     }
733 }
734
735 function transfer_title_holds() {
736     g.data.stash_retrieve();
737     var target = g.data.marked_record_for_hold_transfer;
738     if (!target) {
739         var m = $("offlineStrings").getString('staff.cat.opac.title_for_hold_transfer.destination_needed.label');
740         alert(m);
741         return;
742     }
743     var robj = g.network.simple_request('TRANSFER_TITLE_HOLDS',[ ses(), target, [ docid ] ]);
744     if (robj == 1) {
745         var m = $("offlineStrings").getString('staff.cat.opac.title_for_hold_transfer.success.label');
746         alert(m);
747     } else {
748         var m = $("offlineStrings").getString('staff.cat.opac.title_for_hold_transfer.failure.label');
749         alert(m);
750     }
751     hold_browser_reset = true;
752     if (g.view == 'hold_browser') { set_hold_browser(); };
753 }
754
755 function delete_record() {
756     if (g.error.yns_alert(
757         document.getElementById('offlineStrings').getFormattedString('cat.opac.delete_record.confirm', [docid]),
758         document.getElementById('offlineStrings').getString('cat.opac.delete_record'),
759         document.getElementById('offlineStrings').getString('cat.opac.delete'),
760         document.getElementById('offlineStrings').getString('cat.opac.cancel'),
761         null,
762         document.getElementById('offlineStrings').getString('cat.opac.record_deleted.confirm')) == 0) {
763         var robj = g.network.simple_request('FM_BRE_DELETE',[ses(),docid]);
764         if (typeof robj.ilsevent != 'undefined') {
765             alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_deleted.error',  [docid, robj.textcode, robj.desc]) + '\n');
766         } else {
767             alert(document.getElementById('offlineStrings').getString('cat.opac.record_deleted'));
768             refresh_display(docid);
769         }
770     }
771 }
772
773 function undelete_record() {
774     if (g.error.yns_alert(
775         document.getElementById('offlineStrings').getFormattedString('cat.opac.undelete_record.confirm', [docid]),
776         document.getElementById('offlineStrings').getString('cat.opac.undelete_record'),
777         document.getElementById('offlineStrings').getString('cat.opac.undelete'),
778         document.getElementById('offlineStrings').getString('cat.opac.cancel'),
779         null,
780         document.getElementById('offlineStrings').getString('cat.opac.record_undeleted.confirm')) == 0) {
781
782         var robj = g.network.simple_request('FM_BRE_UNDELETE',[ses(),docid]);
783         if (typeof robj.ilsevent != 'undefined') {
784             alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_undeleted.error',  [docid, robj.textcode, robj.desc]) + '\n');
785         } else {
786             alert(document.getElementById('offlineStrings').getString('cat.opac.record_undeleted'));
787             refresh_display(docid);
788         }
789     }
790 }
791
792 function refresh_display(id) {
793     try { 
794         marc_view_reset = true;
795         marc_edit_reset = true;
796         copy_browser_reset = true;
797         hold_browser_reset = true;
798         manage_parts_reset = true;
799         manage_multi_home_reset = true;
800         serctrl_view_reset = true;
801         while(top_pane.node.lastChild) top_pane.node.removeChild( top_pane.node.lastChild );
802         var children = bottom_pane.node.childNodes;
803         for (var i = 0; i < children.length; i++) {
804             if (children[i] != browser_frame) bottom_pane.node.removeChild(children[i]);
805         }
806
807         set_brief_view();
808         $('nav').setAttribute('hidden','false');
809         var settings = g.network.simple_request(
810             'FM_AUS_RETRIEVE',
811             [ ses(), g.data.list.au[0].id() ]
812         );
813         var view = settings['staff_client.catalog.record_view.default'];
814         if (g.view_override) {
815             view = g.view_override;
816             g.view_override = null;
817         }
818         switch(view) {
819             case 'marc_view' : set_marc_view(); break;
820             case 'marc_edit' : set_marc_edit(); break;
821             case 'copy_browser' : set_copy_browser(); break;
822             case 'hold_browser' : set_hold_browser(); break;
823             case 'serctrl_view' : set_serctrl_view(); break;
824             case 'opac' :
825             default: set_opac(); break;
826         }
827         opac_wrapper_set_help_context(); 
828     } catch(E) {
829         g.error.standard_unexpected_error_alert('in refresh_display',E);
830     }
831 }
832
833 function set_default() {
834     var robj = g.network.simple_request(
835         'FM_AUS_UPDATE',
836         [ ses(), g.data.list.au[0].id(), { 'staff_client.catalog.record_view.default' : g.view } ]
837     )
838     if (typeof robj.ilsevent != 'undefined') {
839         if (robj.ilsevent != 0) g.error.standard_unexpected_error_alert(document.getElementById('offlineStrings').getString('cat.preference.error'), robj);
840     }
841 }
842
843 function add_volumes() {
844     try {
845         var edit = 0;
846         try {
847             edit = g.network.request(
848                 api.PERM_MULTI_ORG_CHECK.app,
849                 api.PERM_MULTI_ORG_CHECK.method,
850                 [ 
851                     ses(), 
852                     ses('staff_id'), 
853                     [ ses('ws_ou') ],
854                     [ 'CREATE_VOLUME', 'CREATE_COPY' ]
855                 ]
856             ).length == 0 ? 1 : 0;
857         } catch(E) {
858             g.error.sdump('D_ERROR','batch permission check: ' + E);
859         }
860
861         if (edit==0) {
862             alert(document.getElementById('offlineStrings').getString('staff.circ.copy_status.add_volumes.perm_failure'));
863             return; // no read-only view for this interface
864         }
865
866         var title = document.getElementById('offlineStrings').getFormattedString('staff.circ.copy_status.add_volumes.title', [docid]);
867
868         var url;
869         var unified_interface = String( g.data.hash.aous['ui.unified_volume_copy_editor'] ) == 'true';
870         if (unified_interface) {
871             var horizontal_interface = String( g.data.hash.aous['ui.cat.volume_copy_editor.horizontal'] ) == 'true';
872             url = window.xulG.url_prefix( horizontal_interface ? urls.XUL_VOLUME_COPY_CREATOR_HORIZONTAL : urls.XUL_VOLUME_COPY_CREATOR );
873         } else {
874             url = window.xulG.url_prefix( urls.XUL_VOLUME_COPY_CREATOR_ORIGINAL );
875         }
876
877         var w = xulG.new_tab(
878             url,
879             { 'tab_name' : title },
880             { 'doc_id' : docid, 'ou_ids' : [ ses('ws_ou') ] }
881         );
882     } catch(E) {
883         alert('Error in chrome/content/cat/opac.js, add_volumes(): ' + E);
884     }
885 }
886
887 function manage_parts() {
888     try {
889         g.view = 'manage_parts';
890         var loc = urls.XUL_BROWSER + "?url=" + window.escape(
891             window.xulG.url_prefix(urls.CONIFY_MANAGE_PARTS) + '?r=' + docid
892         );
893         if (manage_parts_reset) {
894             bottom_pane.reset_iframe( loc,{},xulG);
895             manage_parts_reset =false;
896         } else {
897             bottom_pane.set_iframe( loc,{},xulG);
898         }
899         opac_wrapper_set_help_context();
900         bottom_pane.get_contentWindow().addEventListener('load',opac_wrapper_set_help_context,false);
901     } catch(E) {
902         alert('Error in chrome/content/cat/opac.js, manage_parts(): ' + E);
903     }
904 }
905
906 function manage_multi_home_items() {
907     try {
908         g.view = 'manage_multi_home';
909         var loc = window.xulG.url_prefix(urls.MANAGE_MULTI_HOME_ITEMS);
910         if (manage_multi_home_reset) {
911             bottom_pane.reset_iframe( loc,{},{'docid':docid,'no_bib_summary':true,'url_prefix':xulG.url_prefix,'new_tab':xulG.new_tab});
912             manage_multi_home_reset =false;
913         } else {
914             bottom_pane.set_iframe( loc,{},{'docid':docid,'no_bib_summary':true,'url_prefix':xulG.url_prefix,'new_tab':xulG.new_tab});
915         }
916         opac_wrapper_set_help_context();
917         bottom_pane.get_contentWindow().addEventListener('load',opac_wrapper_set_help_context,false);
918     } catch(E) {
919         alert('Error in chrome/content/cat/opac.js, manage_multi_home_items(): ' + E);
920     }
921 }
922
923 function mark_for_multi_home() {
924     g.data.marked_multi_home_record = docid;
925     g.data.stash('marked_multi_home_record');
926     var robj = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative',[docid]);
927     if (typeof robj.ilsevent == 'undefined') {
928         g.data.marked_multi_home_record_mvr = robj;
929     } else {
930         g.data.marked_multi_home_record_mvr = null;
931         g.error.standard_unexpected_error_alert('in mark_for_multi_home',robj);
932     }
933     g.data.stash('marked_multi_home_record_mvr');
934
935     if (g.data.marked_multi_home_record_mvr) {
936         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_marked_for_multi_home.tcn.alert',[ g.data.marked_multi_home_record_mvr.tcn() ]));
937         xulG.set_statusbar(
938             2,
939             $("offlineStrings").getFormattedString('staff.cat.copy_browser.marked_record_for_multi_home_indicator.tcn.label',[g.data.marked_multi_home_record_mvr.tcn()]),
940             $("offlineStrings").getFormattedString('staff.cat.copy_browser.marked_record_for_multi_home_indicator.record_id.label',[g.data.marked_multi_home_record]),
941             gen_statusbar_click_handler('marked_multi_home_record')
942         );
943     } else {
944         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_marked_for_multi_home.record_id.alert',[ g.data.marked_multi_home_record ]));
945         xulG.set_statusbar(
946             2,
947             $("offlineStrings").getFormattedString('staff.cat.copy_browser.marked_record_for_multi_home_indicator.record_id.label',[g.data.marked_multi_home_record]),
948             '',
949             gen_statusbar_click_handler('marked_multi_home_record')
950         );
951     }
952 }
953
954 function gen_statusbar_click_handler(data_key) {
955     return function (ev) {
956
957         if (! g.data[data_key]) {
958             return;
959         }
960
961         if (ev.button == 0 /* left click, spawn opac */) {
962             var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + g.data[data_key];
963             var content_params = {
964                 'session' : ses(),
965                 'authtime' : ses('authtime'),
966                 'opac_url' : opac_url,
967             };
968             xulG.new_tab(
969                 xulG.url_prefix(urls.XUL_OPAC_WRAPPER),
970                 {'tab_name':'Retrieving title...'},
971                 content_params
972             );
973         }
974
975         if (ev.button == 2 /* right click, remove mark */) {
976             if ( window.confirm( document.getElementById('offlineStrings').getString('cat.opac.clear_statusbar') ) ) {
977                 g.data[data_key] = null;
978                 g.data.stash(data_key);
979                 ev.target.setAttribute('label','');
980                 if (ev.target.hasAttribute('tooltiptext')) {
981                     ev.target.removeAttribute('tooltiptext');
982                 }
983             }
984         }
985     }
986 }
987
988