LP#827442 Z39.50 will split the total cap between locations when multiple locations...
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / z3950.js
1 dump('entering cat.z3950.js\n');
2
3 function $(id) { return document.getElementById(id); }
4
5 if (typeof cat == 'undefined') cat = {};
6 cat.z3950 = function (params) {
7     try {
8         JSAN.use('util.error'); this.error = new util.error();
9         JSAN.use('util.network'); this.network = new util.network();
10     } catch(E) {
11         dump('cat.z3950: ' + E + '\n');
12     }
13 };
14
15 cat.z3950.prototype = {
16
17     'creds_version' : 2,
18
19     'number_of_result_sets' : 0,
20
21     'result_set' : [],
22
23     'results_buffer' : [],
24
25     'on_display' : [],        // Values transfered to display
26
27     'save_count' : [],        // Values available from data source
28
29     'limit' : 10,
30
31     'init' : function( params ) {
32
33         try {
34             JSAN.use('util.widgets');
35
36             var obj = this;
37
38             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
39
40             obj.load_creds();
41
42             buildStripTags();
43
44             JSAN.use('circ.util');
45             var columns = circ.util.columns(
46                 {
47                     'tcn' : { 'hidden' : false },
48                     'isbn' : { 'hidden' : false },
49                     'title' : { 'hidden' : false, 'flex' : '1' },
50                     'author' : { 'hidden' : false },
51                     'edition' : { 'hidden' : false },
52                     'pubdate' : { 'hidden' : false },
53                     'publisher' : { 'hidden' : false },
54                     'service' : { 'hidden' : false }
55                 }
56             );
57
58             JSAN.use('util.list'); obj.list = new util.list('results');
59             obj.list.init(
60                 {
61                     'columns' : columns,
62                     'on_select' : function(ev) {
63                         try {
64                             JSAN.use('util.functional');
65                             var sel = obj.list.retrieve_selection();
66                             document.getElementById('sel_clip').setAttribute('disabled', sel.length < 1);
67                             var list = util.functional.map_list(
68                                 sel,
69                                 function(o) {
70                                     if ( $('jacket_image') ) {
71                                         // A side-effect in this map function, mu hahaha
72                                         if (o.getAttribute('isbn')) {
73                                             $('jacket_image').setAttribute('src',urls.ac_jacket_large+o.getAttribute('isbn'));
74                                             $('jacket_image').setAttribute('tooltiptext',urls.ac_jacket_large+o.getAttribute('isbn'));
75                                         } else {
76                                             $('jacket_image').setAttribute('src','');
77                                             $('jacket_image').setAttribute('tooltiptext','');
78                                         }
79                                     }
80                                     if (o.getAttribute('service') == 'native-evergreen-catalog') {
81                                         $('mark_overlay_btn').disabled = false;
82                                         $('show_in_catalog_btn').disabled = false;
83                                         obj.controller.view.mark_overlay.setAttribute('doc_id',o.getAttribute('doc_id'));
84                                     } else {
85                                         $('mark_overlay_btn').disabled = true;
86                                         $('show_in_catalog_btn').disabled = true;
87                                     }
88                                     return o.getAttribute('retrieve_id');
89                                 }
90                             );
91                             obj.error.sdump('D_TRACE','cat/z3950: selection list = ' + js2JSON(list) );
92                             obj.controller.view.marc_import.disabled = false;
93                             obj.controller.view.marc_import.setAttribute('retrieve_id',list[0]);
94                             obj.data.init({'via':'stash'});
95                             if (obj.data.marked_record) {
96                                 obj.controller.view.marc_import_overlay.disabled = false;
97                             } else {
98                                 obj.controller.view.marc_import_overlay.disabled = true;
99                             }
100                             obj.controller.view.marc_import_overlay.setAttribute('retrieve_id',list[0]);
101                             obj.controller.view.marc_view_btn.disabled = false;
102                             obj.controller.view.marc_view_btn.setAttribute('retrieve_id',list[0]);
103                         } catch(E) {
104                             obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_list_init.list_construction_error'),E);
105                         }
106                     },
107                 }
108             );
109
110             JSAN.use('util.controller'); obj.controller = new util.controller();
111             obj.controller.init(
112                 {
113                     control_map : {
114                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
115                         'sel_clip' : [ ['command'], function() { obj.list.clipboard(); } ],
116                         'cmd_z3950_csv_to_clipboard' : [ ['command'], function() { obj.list.dump_csv_to_clipboard(); } ],
117                         'cmd_z3950_csv_to_printer' : [ ['command'], function() { obj.list.dump_csv_to_printer(); } ], 
118                         'cmd_z3950_csv_to_file' : [ ['command'], function() { obj.list.dump_csv_to_file( { 'defaultFileName' : 'z3950_results.txt' } ); } ],
119                         'cmd_broken' : [
120                             ['command'],
121                             function() { alert('Not Yet Implemented'); }
122                         ],
123                         'result_message' : [['render'],function(e){return function(){};}],
124                         'clear' : [
125                             ['command'],
126                             function() {
127                                 obj.clear();
128                             }
129                         ],
130                         'save_creds' : [
131                             ['command'],
132                             function() {
133                                 obj.save_creds();
134                                 setTimeout( function() { obj.focus(); }, 0 );
135                             }
136                         ],
137                         'marc_view_btn' : [
138                             ['render'],
139                             function(e) {
140                                 e.setAttribute('label', $("catStrings").getString('staff.cat.z3950.marc_view.label'));
141                                 e.setAttribute('accesskey', $("catStrings").getString('staff.cat.z3950.marc_view.accesskey'));
142                             }
143                         ],
144                         'marc_view' : [
145                             ['command'],
146                             function(ev) {
147                                 try {
148                                     var n = obj.controller.view.marc_view_btn;
149                                     if (n.getAttribute('toggle') == '1') {
150                                         document.getElementById('deck').selectedIndex = 0;
151                                         n.setAttribute('toggle','0');
152                                         n.setAttribute('label', $("catStrings").getString('staff.cat.z3950.marc_view.label'));
153                                         n.setAttribute('accesskey', $("catStrings").getString('staff.cat.z3950.marc_view.accesskey'));
154                                         document.getElementById('results').focus();
155                                     } else {
156                                         document.getElementById('deck').selectedIndex = 1;
157                                         n.setAttribute('toggle','1');
158                                         n.setAttribute('label', $("catStrings").getString('staff.cat.z3950.results_view.label'));
159                                         n.setAttribute('accesskey', $("catStrings").getString('staff.cat.z3950.results_view.accesskey'));
160                                         var f = get_contentWindow(document.getElementById('marc_frame'));
161                                         var retrieve_id = n.getAttribute('retrieve_id');
162                                         var result_idx = retrieve_id.split('-')[0];
163                                         var record_idx = retrieve_id.split('-')[1];
164                                         f.xulG = { 'marcxml' : obj.result_set[result_idx].records[ record_idx ].marcxml };
165                                         f.my_init();
166                                         f.document.body.firstChild.focus();
167                                     }
168                                 } catch(E) {
169                                     obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_view_error'),E);
170                                 }
171                             },
172                         ],
173                         'mark_overlay' : [
174                             ['command'],
175                             function() {
176                                 try {
177                                     var doc_id = obj.controller.view.mark_overlay.getAttribute('doc_id');
178                                     if (doc_id) {
179                                         cat.util.mark_for_overlay(doc_id);
180                                     }
181                                 } catch(E) {
182                                     alert('Error in z3950.js, mark_overlay: ' + E);
183                                 }
184                             }
185                         ],
186                         'show_in_catalog' : [
187                             ['command'],
188                             function() {
189                                 try {
190                                     var doc_id = obj.controller.view.mark_overlay.getAttribute('doc_id');
191                                     if (doc_id) {
192                                         var opac_url = xulG.url_prefix('opac_rdetail') + doc_id;
193                                         var content_params = { 
194                                             'session' : ses(),
195                                             'authtime' : ses('authtime'),
196                                             'opac_url' : opac_url,
197                                         };
198                                         xulG.new_tab(
199                                                      xulG.url_prefix('XUL_OPAC_WRAPPER'), 
200                                                      {'tab_name': $("catStrings").getString('staff.cat.z3950.replace_tab_with_opac.tab_name')}, 
201                                                      content_params
202                                                      );
203                                     }
204                                 } catch(E) {
205                                     alert('Error in z3950.js, show_in_catalog: ' + E);
206                                 }
207                             }
208                         ],
209                         'marc_import' : [
210                             ['command'],
211                             function() {
212                                 try {
213                                     var retrieve_id = obj.controller.view.marc_import.getAttribute('retrieve_id');
214                                     var result_idx = retrieve_id.split('-')[0];
215                                     var record_idx = retrieve_id.split('-')[1];
216                                     obj.spawn_marc_editor( 
217                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
218                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
219                                     );
220                                 } catch(E) {
221                                     obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_import_error'),E);
222                                 }
223                             },
224                         ],
225                         'marc_import_overlay' : [ 
226                             ['command'],
227                             function() {
228                                 try {
229                                     var retrieve_id = obj.controller.view.marc_import_overlay.getAttribute('retrieve_id');
230                                     var result_idx = retrieve_id.split('-')[0];
231                                     var record_idx = retrieve_id.split('-')[1];
232                                     obj.spawn_marc_editor_for_overlay( 
233                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
234                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
235                                     );
236                                 } catch(E) {
237                                     obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_import_overlay_error'),E);
238                                 }
239                             },
240                         ],
241                         'search' : [
242                             ['command'],
243                             function() {
244                                 obj.initial_search();
245                             },
246                         ],
247                         'raw_search' : [ 
248                             ['command'], 
249                             function() { 
250                                 var raw = window.prompt(
251                                     $("catStrings").getString('staff.cat.z3950.initial_search.raw_prompt.msg'),
252                                     $("catStrings").getString('staff.cat.z3950.initial_search.raw_prompt.default_value'),
253                                     $("catStrings").getString('staff.cat.z3950.initial_search.raw_prompt.title')
254                                 ); 
255                                 if (raw) obj.initial_raw_search(raw); 
256                             } 
257                         ], 
258                         'page_next' : [
259                             ['command'],
260                             function() {
261                                 obj.page_next();
262                             },
263                         ],
264                         'toggle_form_btn' : [
265                             ['render'],
266                             function(e) {
267                                 e.setAttribute('image',"/xul/server/skin/media/images/up_arrow.gif");
268                                 e.setAttribute('label',$("catStrings").getString('staff.cat.z3950.hide_top_pane.label'));
269                                 e.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.hide_top_pane.accesskey'));
270                             }
271                         ],
272                         'toggle_form' : [
273                             ['command'],
274                             function() {
275                                 var x = document.getElementById('top_pane');
276                                 document.getElementById('splitter_grippy2').doCommand();
277                                 var n = obj.controller.view.toggle_form_btn;
278                                 if (x.collapsed) {
279                                     n.setAttribute('image',"/xul/server/skin/media/images/down_arrow.gif");
280                                     n.setAttribute('label',$("catStrings").getString('staff.cat.z3950.unhide_top_pane.label'));
281                                     n.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.unhide_top_pane.accesskey'));
282                                 } else {
283                                     n.setAttribute('image',"/xul/server/skin/media/images/up_arrow.gif");
284                                     n.setAttribute('label',$("catStrings").getString('staff.cat.z3950.hide_top_pane.label'));
285                                     n.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.hide_top_pane.accesskey'));
286                                 }
287                             },
288                         ],
289                         'splitter_grippy2' : [
290                             ['click'],
291                             function() {
292                                 var x = document.getElementById('top_pane');
293                                 var n = obj.controller.view.toggle_form_btn;
294                                 if (x.collapsed) {
295                                     n.setAttribute('image',"/xul/server/skin/media/images/down_arrow.gif");
296                                     n.setAttribute('label',$("catStrings").getString('staff.cat.z3950.unhide_top_pane.label'));
297                                     n.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.unhide_top_pane.accesskey'));
298                                 } else {
299                                     n.setAttribute('image',"/xul/server/skin/media/images/up_arrow.gif");
300                                     n.setAttribute('label',$("catStrings").getString('staff.cat.z3950.hide_top_pane.label'));
301                                     n.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.hide_top_pane.accesskey'));
302                                 }
303                             }
304                         ],
305                         'service_rows' : [
306                             ['render'],
307                             function(e) {
308                                 return function() {
309                                     try {
310
311                                         function handle_switch(node) {
312                                             try {
313                                                 $('search').setAttribute('disabled','true'); $('raw_search').setAttribute('disabled','true');
314                                                 obj.active_services = [];
315                                                 var snl = document.getElementsByAttribute('mytype','service_class');
316                                                 for (var i = 0; i < snl.length; i++) {
317                                                     var n = snl[i];
318                                                     if (n.nodeName == 'checkbox') {
319                                                         if (n.checked) obj.active_services.push( n.getAttribute('service') );
320                                                     }
321                                                 }
322                                                 if (obj.active_services.length > 0) {
323                                                     $('search').setAttribute('disabled','false'); 
324                                                 }
325                                                 if (obj.active_services.length == 1) {
326                                                     if (obj.active_services[0] != 'native-evergreen-catalog') { 
327                                                         $('raw_search').setAttribute('disabled','false');
328                                                     }
329                                                 }
330                                                 var nl = document.getElementsByAttribute('mytype','search_class');
331                                                 for (var i = 0; i < nl.length; i++) { nl[i].disabled = true; }
332                                                 var attrs = {};
333                                                 for (var j = 0; j < obj.active_services.length; j++) {
334                                                     if (obj.services[obj.active_services[j]]) for (var i in obj.services[obj.active_services[j]].attrs) {
335                                                         var attr = obj.services[obj.active_services[j]].attrs[i];
336                                                         if (! attrs[i]) {
337                                                             attrs[i] = { 'labels' : {} };
338                                                         }
339                                                         if (attr.label) {
340                                                             attrs[i].labels[ attr.label ] = true;
341                                                         } else if (document.getElementById('commonStrings').testString('staff.z39_50.search_class.' + i)) {
342                                                             attrs[i].labels[ document.getElementById('commonStrings').getString('staff.z39_50.search_class.' + i) ] = true;
343                                                         } else if (attr.name) {
344                                                             attrs[i].labels[ attr.name ] = true;
345                                                         } else {
346                                                             attrs[i].labels[ i ] = true;
347                                                         }
348
349                                                     }
350                                                     
351                                                 }
352
353                                                 function set_label(x,attr) {
354                                                     var labels = [];
355                                                     for (var j in attrs[attr].labels) {
356                                                         labels.push(j);
357                                                     }
358                                                     if (labels.length > 0) {
359                                                         x.setAttribute('value',labels[0]);
360                                                         x.setAttribute('tooltiptext',labels.join(','));
361                                                         if (labels.length > 1) x.setAttribute('class','multiple_labels');
362                                                     }
363                                                 }
364
365                                                 for (var i in attrs) {
366                                                     var x = document.getElementById(i + '_input');
367                                                     if (x) {
368                                                         x.disabled = false;
369                                                         var y = document.getElementById(i + '_label',i);
370                                                         if (y) set_label(y,i);
371                                                     } else {
372                                                         var rows = document.getElementById('query_inputs');
373                                                         var row = document.createElement('row'); rows.appendChild(row);
374                                                         var label = document.createElement('label');
375                                                         label.setAttribute('id',i+'_label');
376                                                         label.setAttribute('control',i+'_input');
377                                                         label.setAttribute('search_class',i);
378                                                         label.setAttribute('style','-moz-user-focus: ignore');
379                                                         row.appendChild(label);
380                                                         set_label(label,i);
381                                                         label.addEventListener('click',function(ev){
382                                                                 var a = ev.target.getAttribute('search_class');
383                                                                 if (a) obj.default_attr = a;
384                                                             },false
385                                                         );
386                                                         var tb = document.createElement('textbox');
387                                                         tb.setAttribute('id',i+'_input');
388                                                         tb.setAttribute('mytype','search_class');
389                                                         tb.setAttribute('search_class',i);
390                                                         row.appendChild(tb);
391                                                         tb.addEventListener('keypress',function(ev) { return obj.handle_enter(ev); },false);
392                                                     }
393                                                 }
394                                             } catch(E) {
395                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.search_fields_error'),E);
396                                             }
397                                         }
398
399                                         document.getElementById('native-evergreen-catalog_service').addEventListener('command',handle_switch,false);
400
401                                         var robj = obj.network.simple_request(
402                                             'RETRIEVE_Z3950_SERVICES',
403                                             [ ses() ]
404                                         );
405                                         if (typeof robj.ilsevent != 'undefined') throw(robj);
406                                         obj.services = robj;
407                                         var x = document.getElementById('service_rows');
408                                         var services = new Array();
409                                         for (var i in obj.services) {
410                                             var label;
411                                             if (obj.services[i].label) {
412                                                 label = obj.services[i].label;
413                                             } else if (obj.services[i].name) {
414                                                 label = obj.services[i].name;
415                                             } else {
416                                                 label = i;
417                                             }
418                                             var j = [label, i];
419                                             services.push(j);
420                                         }
421                                         services.sort();
422                                         for (var j=0; j < services.length; j++) {
423                                             var i = services[j][1];
424                                             try {
425                                                 if (i == 'native-evergreen-catalog') continue;
426                                                 var r = document.createElement('row'); x.appendChild(r);
427                                                 var cb = document.createElement('checkbox'); 
428                                                     cb.setAttribute('label',services[j][0]);
429                                                     cb.setAttribute('tooltiptext',i + ' : ' + obj.services[i].db + '@' + obj.services[i].host + ':' + obj.services[i].port); 
430                                                     cb.setAttribute('mytype','service_class'); cb.setAttribute('service',i);
431                                                     cb.setAttribute('id',i+'_service'); r.appendChild(cb);
432                                                     cb.addEventListener('command',handle_switch,false);
433                                                 var username = document.createElement('textbox'); username.setAttribute('id',i+'_username'); 
434                                                 if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[i]) username.setAttribute('value',obj.creds.hosts[ obj.data.server_unadorned ].services[i].username);
435                                                 r.appendChild(username);
436                                                 if (typeof obj.services[i].auth != 'undefined') username.hidden = ! get_bool( obj.services[i].auth );
437                                                 var password = document.createElement('textbox'); password.setAttribute('id',i+'_password'); 
438                                                 if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[i]) password.setAttribute('value',obj.creds.hosts[ obj.data.server_unadorned ].services[i].password);
439                                                 password.setAttribute('type','password'); r.appendChild(password);
440                                                 if (typeof obj.services[i].auth != 'undefined') password.hidden = ! get_bool( obj.services[i].auth );
441                                             } catch(E) {
442                                                 alert(E);
443                                             }
444                                         }
445                                         //obj.services[ 'native-evergreen-catalog' ] = { 'attrs' : { 'author' : {}, 'title' : {} } };
446                                         setTimeout(
447                                             function() { 
448                                                 if (obj.creds.hosts[ obj.data.server_unadorned ]) {
449                                                     for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
450                                                         var x = document.getElementById(obj.creds.hosts[ obj.data.server_unadorned ].default_services[i]+'_service');
451                                                         if (x) x.checked = true;
452                                                     }
453                                                 } else if (obj.creds.default_service) {
454                                                     var x = document.getElementById(obj.creds.default_service+'_service');
455                                                     if (x) x.checked = true;
456                                                 }
457                                                 handle_switch();
458                                             },0
459                                         );
460                                     } catch(E) {
461                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.z39_service_error'),E);
462                                     }
463                                 }
464                             }
465                         ],
466                     }
467                 }
468             );
469
470             obj.controller.render();
471
472             setTimeout( function() { obj.focus(); }, 0 );
473
474             setInterval( 
475                 function() {
476                     obj.data.init({'via':'stash'});
477                     if (obj.data.marked_record) {
478                         var sel = obj.list.retrieve_selection();
479                         if (sel.length > 0) { obj.controller.view.marc_import_overlay.disabled = false; }
480                         if ($("overlay_tcn_indicator")) {
481                             if (obj.data.marked_record_mvr) {
482                                 $("overlay_tcn_indicator").setAttribute('value',$("catStrings").getFormattedString('staff.cat.z3950.marked_record_for_overlay_indicator.tcn.label',[obj.data.marked_record_mvr.tcn()]));
483                             } else {
484                                 $("overlay_tcn_indicator").setAttribute('value',$("catStrings").getFormattedString('staff.cat.z3950.marked_record_for_overlay_indicator.record_id.label',[obj.data.marked_record]));
485                             }
486                         }
487                     } else {
488                         obj.controller.view.marc_import_overlay.disabled = true;
489                         if ($("overlay_tcn_indicator")) {
490                             $("overlay_tcn_indicator").setAttribute('value',$("catStrings").getString('staff.cat.z3950.marked_record_for_overlay_indicator.no_record.label'));
491                         }
492                     }
493                 }, 2000
494             );
495
496         } catch(E) {
497             this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
498         }
499     },
500
501     'focus' : function() {
502         var obj = this;
503         var focus_me; var or_focus_me;
504         if (!obj.active_services) return;
505         for (var i = 0; i < obj.active_services.length; i++) {
506             if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ]) {
507                 var x = obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ].default_attr;
508                 if (x) { focus_me = x; break; }
509             }
510             if (obj.services[ obj.active_services[i] ]) for (var i in obj.services[ obj.active_services[i] ].attr) { or_focus_me = i; }
511         }
512         if (! focus_me) focus_me = or_focus_me;
513         var xx = document.getElementById(focus_me+'_input'); if (xx) xx.focus();
514     },
515
516     'clear' : function() {
517         var obj = this;
518         var nl = document.getElementsByAttribute('mytype','search_class');
519         for (var i = 0; i < nl.length; i++) { nl[i].value = ''; nl[i].setAttribute('value',''); }
520         obj.focus();
521     },
522
523     'search_params' : {},
524
525     'initial_search' : function() {
526         try {
527             var obj = this;
528             obj.result_set = []; 
529             obj.number_of_result_sets = 0;
530             obj.results_buffer = [];
531             obj.on_display = [];
532             obj.save_count = [];
533             JSAN.use('util.widgets');
534             util.widgets.remove_children( obj.controller.view.result_message );
535             var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
536             if (obj.active_services.length < 1) {
537                 x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.no_search_selection')));
538                 return;
539             }
540             x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.searching')));
541             obj.search_params = {}; obj.list.clear();
542             obj.controller.view.page_next.disabled = true;
543             obj.controller.view.cmd_z3950_csv_to_file.setAttribute('disabled','true');
544             obj.controller.view.cmd_z3950_csv_to_clipboard.setAttribute('disabled','true');
545             obj.controller.view.cmd_z3950_csv_to_printer.setAttribute('disabled','true');
546
547             obj.search_params.service_array = []; 
548             obj.search_params.username_array = [];
549             obj.search_params.password_array = [];
550             for (var i = 0; i < obj.active_services.length; i++) {
551                 obj.search_params.service_array.push( obj.active_services[i] );
552                 obj.search_params.username_array.push( document.getElementById( obj.active_services[i]+'_username' ).value );
553                 obj.search_params.password_array.push( document.getElementById( obj.active_services[i]+'_password' ).value );
554             }
555             obj.search_params.limit = obj.limit;
556             obj.search_params.offset = 0;
557
558             obj.search_params.search = {};
559             var nl = document.getElementsByAttribute('mytype','search_class');
560             var count = 0;
561             for (var i = 0; i < nl.length; i++) {
562                 if (nl[i].disabled) continue;
563                 if (nl[i].value == '') continue;
564                 count++;
565                 obj.search_params.search[ nl[i].getAttribute('search_class') ] = nl[i].value;
566             }
567             if (count>0) {
568                 obj.search();
569             } else {
570                 util.widgets.remove_children( obj.controller.view.result_message );
571             }
572         } catch(E) {
573             this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.initial_search.failed_search'),E);
574         }
575     },
576
577     'initial_raw_search' : function(raw) {
578         try {
579             var obj = this;
580             obj.result_set = []; 
581             obj.results_buffer = [];
582             obj.on_display = [];
583             obj.save_count = [];
584             obj.number_of_result_sets = 0;
585             JSAN.use('util.widgets');
586             util.widgets.remove_children( obj.controller.view.result_message );
587             var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
588             if (obj.active_services.length < 1) {
589                 x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.no_search_selection')));
590                 return;
591             }
592             if (obj.active_services.length > 1) {
593                 x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.too_many_selections')));
594                 return;
595             }
596             if (obj.active_services[0] == 'native-evergreen-catalog') {
597                 x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.raw_search_unsupported_for_native_catalog')));
598                 return;
599             }
600             x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.searching')));
601             obj.search_params = {}; obj.list.clear();
602             obj.controller.view.page_next.disabled = true;
603             obj.controller.view.cmd_z3950_csv_to_file.setAttribute('disabled','true');
604             obj.controller.view.cmd_z3950_csv_to_clipboard.setAttribute('disabled','true');
605             obj.controller.view.cmd_z3950_csv_to_printer.setAttribute('disabled','true');
606
607             obj.search_params.service_array = []; 
608             obj.search_params.username_array = [];
609             obj.search_params.password_array = [];
610             for (var i = 0; i < obj.active_services.length; i++) {
611                 obj.search_params.service_array.push( obj.active_services[i] );
612                 obj.search_params.username_array.push( document.getElementById( obj.active_services[i]+'_username' ).value );
613                 obj.search_params.password_array.push( document.getElementById( obj.active_services[i]+'_password' ).value );
614             }
615             obj.search_params.limit = obj.limit;
616             obj.search_params.offset = 0;
617
618             obj.search_params.query = raw;
619
620             obj.search();
621         } catch(E) {
622             this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.initial_search.failed_search'),E);
623         }
624     },
625
626     'page_next' : function() {
627         try {
628             var obj = this;
629             JSAN.use('util.widgets');
630             util.widgets.remove_children( obj.controller.view.result_message );
631             var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
632             x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.page_next.more_results')));
633             obj.search_params.offset += obj.search_params.limit;
634             obj.search();
635         } catch(E) {
636             this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.page_next.subsequent_search_error'),E);
637         }
638     },
639
640     'search' : function() {
641         try {
642             var obj = this;
643             var method;
644             if (typeof obj.search_params.query == 'undefined') {
645                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_SEARCH';
646                 obj.search_params.service = obj.search_params.service_array;
647                 obj.search_params.username = obj.search_params.username_array;
648                 obj.search_params.password = obj.search_params.password_array;
649             } else {
650                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_RAW_SEARCH';
651                 obj.search_params.service = obj.search_params.service_array[0];
652                 obj.search_params.username = obj.search_params.username_array[0];
653                 obj.search_params.password = obj.search_params.password_array[0];
654             }
655             obj.network.simple_request(
656                 method,
657                 [ ses(), obj.search_params ],
658                 function(req) {
659                     obj.handle_results(req.getResultObject())
660                 }
661             );
662             document.getElementById('deck').selectedIndex = 0;
663         } catch(E) {
664             this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.search.search_error'),E);
665         }
666     },
667
668     'handle_results' : function(results) {
669         var obj = this;
670         try {
671             JSAN.use('util.widgets');
672             util.widgets.remove_children( obj.controller.view.result_message ); var x;
673             if (results == null) {
674                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
675                 x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.handle_results.null_server_error')));
676                 return;
677             }
678             if (typeof results.ilsevent != 'undefined') {
679                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
680                 x.appendChild( document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.server_error', [results.textcode, results.desc])));
681                 return;
682             }
683             obj.controller.view.cmd_z3950_csv_to_file.setAttribute('disabled','false');
684             obj.controller.view.cmd_z3950_csv_to_clipboard.setAttribute('disabled','false');
685             obj.controller.view.cmd_z3950_csv_to_printer.setAttribute('disabled','false');
686             if (typeof results.length == 'undefined') results = [ results ];
687
688             var total_showing = 0;      // On screen
689             var total_count = 0;        // available if you want 'em
690             var tooltip_msg = '';       // Tooltip for message under "Results"
691             var set_tally = [];         // Keeps track of records per data source
692
693             /* Pass 1: Move results into FIFO buffers */
694             for (var i = 0; i < results.length; i++) {
695                 if (typeof obj.save_count[i] == 'undefined') {
696                        obj.save_count[i] = results[i].count;    // We'll need this later
697                 }
698                 if (typeof obj.results_buffer[i] == 'undefined') {
699                        obj.results_buffer[i] = new Array(); 
700                 }
701                 while (results[i].records.length) {
702                    obj.results_buffer[i].push(results[i].records.pop());
703                 }
704                
705                 /* Set up array that keeps track of # of recs in the buffer */
706                 var buff_info = {};
707                 buff_info.indx = i;
708                 buff_info.ele_count = obj.results_buffer[i].length;
709                 set_tally.push(buff_info);  
710             }
711
712             /* Pass 2: figure out optimal fill from buffers to result frame and move them */
713             var sorted_tally = set_tally.sort( function(e1, e2) {
714                                                    return e1.ele_count - e2.ele_count;
715                                                }
716                                              );
717
718             var available = obj.limit;               // Slots to fill in this batch
719             for (i = 0; i < sorted_tally.length; i++) {
720                 var use_count = Math.floor(available / (sorted_tally.length-i));
721                 var res_index = sorted_tally[i].indx;
722
723                 /* Don't use more records than we have */
724                 if (sorted_tally[i].ele_count < use_count) {
725                     use_count = sorted_tally[i].ele_count;
726                 }
727                
728                 results[res_index].count = use_count > 0 ?
729                                            obj.save_count[res_index] :
730                                            undefined;
731
732                 available = available - use_count;
733                 while (use_count) {
734                     results[res_index].records.push(
735                          obj.results_buffer[res_index].shift()
736                      );
737                     use_count--; 
738                 }
739             }
740              
741             /* Pass 3: Process the records from the results array */
742             for (i = 0; i < results.length; i++) {
743                 if (results[i].query) {
744                     tooltip_msg += $("catStrings").getFormattedString('staff.cat.z3950.handle_results.raw_query', [results[i].query]) + '\n';
745                 }
746                 if (typeof obj.on_display[i] == 'undefined') {
747                             obj.on_display[i] = 0;
748                 }
749                 if (results[i].count) {
750                     if (results[i].records) {
751                         obj.on_display[i] += results[i].records.length;
752                         total_showing += obj.on_display[i];
753                         total_count += results[i].count;
754                         tooltip_msg += $("catStrings").getFormattedString('staff.cat.z3950.handle_results.showing_results', [obj.on_display[i], results[i].count, results[i].service]) + '\n';
755                     }
756                 } else {
757                     total_showing += obj.on_display[i];
758                     total_count += obj.on_display[i];
759                     tooltip_msg += $("catStrings").getFormattedString('staff.cat.z3950.handle_results.showing_results', [obj.on_display[i], obj.on_display[i], results[i].service]) + '\n';
760
761                 }
762
763                 if (results[i].records) {
764                     obj.result_set[ ++obj.number_of_result_sets ] = results[i];
765                     obj.controller.view.marc_import.disabled = true;
766                     obj.controller.view.marc_import_overlay.disabled = true;
767                     var x = obj.controller.view.marc_view_btn;
768                     if (x.getAttribute('toggle') == '0') x.disabled = true;
769                     for (var j = 0; j < obj.result_set[ obj.number_of_result_sets ].records.length; j++) {
770                         var f;
771                         var n = obj.list.append(
772                             {
773                                 'retrieve_id' : String( obj.number_of_result_sets ) + '-' + String( j ),
774                                 'row' : {
775                                     'my' : {
776                                         'mvr' : function(a){
777                                             if (a.bibid) {
778                                                 // We have col definitions, etc.
779                                                 // expecting doc_id
780                                                 a.mvr.doc_id( a.bibid );
781                                             }
782                                             return a.mvr;
783                                         }(obj.result_set[ obj.number_of_result_sets ].records[j]),
784                                         'service' : results[i].service
785                                     }
786                                 }
787                             }
788                         );
789                         n.treeitem_node.setAttribute('isbn', function(a){return a;}(obj.result_set[ obj.number_of_result_sets ].records[j].mvr).isbn());
790                         n.treeitem_node.setAttribute(
791                             'service',
792                             function(a){return a;}(
793                                 results[i].service
794                             )
795                         );
796                         n.treeitem_node.setAttribute(
797                             'doc_id',
798                             function(a){return a;}(
799                                 (obj.result_set[ obj.number_of_result_sets ].records[j].mvr)
800                             ).doc_id()
801                         );
802
803                         if (!f) { n.treeitem_node.parentNode.focus(); f = n; } 
804                     }
805                 } else {
806                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
807                     x.appendChild(
808                         document.createTextNode($("catStrings").getString('staff.cat.z3950.handle_results.result_error'))
809                     );
810                 }
811             }
812
813             obj.controller.view.page_next.disabled = total_showing >= total_count;
814             if (total_showing) {
815                 x = document.createElement('description'); 
816                 x.setAttribute('crop','end');
817                 x.setAttribute('tooltiptext',tooltip_msg);
818                 obj.controller.view.result_message.appendChild(x);
819                 x.appendChild(
820                     document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.showing_total_results',
821                         [(total_showing > total_count ? total_count : total_showing), total_count]))
822                 );
823             } else {
824                 x = document.createElement('description'); 
825                 x.setAttribute('crop','end');
826                 x.setAttribute('tooltiptext',tooltip_msg);
827                 obj.controller.view.result_message.appendChild(x);
828                 x.appendChild(
829                     document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.num_of_results', [(total_count ? total_count : 0)]))
830                 );
831             }            
832
833         } catch(E) {
834             this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.handle_results.search_result_error'),E);
835         }
836     },
837
838     'replace_tab_with_opac' : function(doc_id) {
839         var opac_url = xulG.url_prefix('opac_rdetail') + doc_id;
840         var content_params = { 
841             'session' : ses(),
842             'authtime' : ses('authtime'),
843             'opac_url' : opac_url,
844         };
845         xulG.set_tab(
846             xulG.url_prefix('XUL_OPAC_WRAPPER'), 
847             {'tab_name': $("catStrings").getString('staff.cat.z3950.replace_tab_with_opac.tab_name')}, 
848             content_params
849         );
850     },
851
852     'spawn_marc_editor' : function(my_marcxml,biblio_source) {
853         var obj = this;
854
855         function save_marc (new_marcxml) {
856             try {
857
858                 // extract the import strip groups
859                 var strip_grps = dojo.query('[strip-fields-cbox]').filter(
860                     function(grp) { return grp.checked }).map(
861                     function(grp) { return grp.getAttribute('value') });
862
863                 var r = obj.network.simple_request(
864                     'MARC_XML_RECORD_IMPORT', [ 
865                         ses(), new_marcxml, biblio_source, 
866                         null, null, strip_grps 
867                     ]
868                 );
869
870                 if (typeof r.ilsevent != 'undefined') {
871                     switch(Number(r.ilsevent)) {
872                         case 1704 /* TCN_EXISTS */ :
873                             var msg = $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor.same_tcn', [r.payload.tcn]);
874                             var title = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.title');
875                             var btn1 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.btn1_overlay');
876                             var btn2 = typeof r.payload.new_tcn == 'undefined' ? null : $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor.btn2_import', [r.payload.new_tcn]);
877                             if (btn2) {
878                                 obj.data.init({'via':'stash'});
879                                 var robj = obj.network.simple_request(
880                                     'PERM_CHECK',[
881                                         ses(),
882                                         obj.data.list.au[0].id(),
883                                         obj.data.list.au[0].ws_ou(),
884                                         [ 'ALLOW_ALT_TCN' ]
885                                     ]
886                                 );
887                                 if (typeof robj.ilsevent != 'undefined') {
888                                     obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.permission_error'),E);
889                                 }
890                                 if (robj.length != 0) btn2 = null;
891                             }
892                             var btn3 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.btn3_cancel_import');
893                             var p = obj.error.yns_alert(msg,title,btn1,btn2,btn3,$("catStrings").getString('staff.cat.z3950.spawn_marc_editor.confirm_action'));
894                             obj.error.sdump('D_ERROR','option ' + p + 'chosen');
895                             switch(p) {
896                                 case 0:
897                                     var r3 = obj.network.simple_request('MARC_XML_RECORD_UPDATE', [ 
898                                         ses(), r.payload.dup_record, 
899                                         new_marcxml, biblio_source, null, strip_grps 
900                                     ]);
901                                     if (typeof r3.ilsevent != 'undefined') {
902                                         throw(r3);
903                                     } else {
904                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_overlay'));
905                                         return {
906                                             'id' : r3.id(),
907                                             'on_complete' : function() {
908                                                 try {
909                                                     obj.replace_tab_with_opac(r3.id());
910                                                 } catch(E) {
911                                                     alert(E);
912                                                 }
913                                             }
914                                         };
915                                     }
916                                 break;
917                                 case 1:
918                                     var r2 = obj.network.request(
919                                         api.MARC_XML_RECORD_IMPORT.app,
920                                         api.MARC_XML_RECORD_IMPORT.method + '.override',
921                                         [ ses(), new_marcxml, biblio_source, null, null, strip_grps ]
922                                     );
923                                     if (typeof r2.ilsevent != 'undefined') {
924                                         throw(r2);
925                                     } else {
926                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_import_with_new_tcn'));
927                                         return {
928                                             'id' : r2.id(),
929                                             'on_complete' : function() {
930                                                 try {
931                                                     obj.replace_tab_with_opac(r2.id());
932                                                 } catch(E) {
933                                                     alert(E);
934                                                 }
935                                             }
936                                         };
937                                     }
938                                 break;
939                                 case 2:
940                                 default:
941                                     alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.import_cancelled'));
942                                 break;
943                             }
944                         break;
945                         default:
946                             throw(r);
947                         break;
948                     }
949                 } else {
950                     alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_import'));
951                     return {
952                         'id' : r.id(),
953                         'on_complete' : function() {
954                             try {
955                                 obj.replace_tab_with_opac(r.id());
956                             } catch(E) {
957                                 alert(E);
958                             }
959                         }
960                     };
961                 }
962             } catch(E) {
963                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.import_error'),E);
964             }
965         };
966
967         if ( $('marc_editor').checked ) {
968             xulG.new_tab(
969                 xulG.url_prefix('XUL_MARC_EDIT'), 
970                 { 'tab_name' : 'MARC Editor' }, 
971                 { 
972                     'marc_control_number_identifier': obj.data.hash.aous['cat.marc_control_number_identifier'] || 'Set cat.marc_control_number_identifier in Library Settings',
973                     'record' : { 'marc' : my_marcxml, "rtype": "bre" },
974                     'fast_add_item' : function(doc_id,cn_label,cp_barcode) {
975                         try {
976                             JSAN.use('cat.util'); return cat.util.fast_item_add(doc_id,cn_label,cp_barcode);
977                         } catch(E) {
978                             alert(E);
979                         }
980                     },
981                     'save' : {
982                         'label' : $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.save_button_label'),
983                         'func' : save_marc
984                     },
985                     'lock_tab' : xulG.lock_tab,
986                     'unlock_tab' : xulG.unlock_tab
987                 } 
988             );
989         } else {
990             save_marc(my_marcxml);
991         }
992     },
993
994     'confirm_overlay' : function(record_ids) {
995         var obj = this; // JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
996         var top_xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" >';
997         top_xml += '<description>'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.description')+'</description>';
998         top_xml += '<hbox><button id="lead" disabled="false" label="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.lead.label')+'" name="fancy_submit"';
999         top_xml += ' accesskey="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.lead.accesskey')+'"/>';
1000         top_xml += ' <button label="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.cancel.label')+'" accesskey="'+
1001                         $("catStrings").getString('staff.cat.z3950.confirm_overlay.cancel.accesskey')+'" name="fancy_cancel"/></hbox></vbox>';
1002
1003         var xml = '<form xmlns="http://www.w3.org/1999/xhtml">';
1004         xml += '<table width="100%"><tr valign="top">';
1005         for (var i = 0; i < record_ids.length; i++) {
1006             xml += '<td nowrap="nowrap"><iframe src="' + urls.XUL_BIB_BRIEF; 
1007             xml += '?docid=' + record_ids[i] + '" oils_force_external="true"/></td>';
1008         }
1009         xml += '</tr><tr valign="top">';
1010         for (var i = 0; i < record_ids.length; i++) {
1011             xml += '<td nowrap="nowrap"><iframe style="min-height: 1000px; min-width: 300px;" flex="1" src="' + urls.XUL_MARC_VIEW + '?docid=' + record_ids[i] + ' " oils_force_external="true"/></td>';
1012         }
1013         xml += '</tr></table></form>';
1014         // data.temp_merge_top = top_xml; data.stash('temp_merge_top');
1015         // data.temp_merge_mid = xml; data.stash('temp_merge_mid');
1016         JSAN.use('util.window'); var win = new util.window();
1017         var fancy_prompt_data = win.open(
1018             urls.XUL_FANCY_PROMPT,
1019             'fancy_prompt', 'chrome,resizable,modal,width=700,height=500',
1020             { 'top_xml' : top_xml, 'xml' : xml, 'title' : $("catStrings").getString('staff.cat.z3950.confirm_overlay.title') }
1021         );
1022         //data.stash_retrieve();
1023         if (fancy_prompt_data.fancy_status == 'incomplete') { alert($("catStrings").getString('staff.cat.z3950.confirm_overlay.aborted')); return false; }
1024         return true;
1025     },
1026
1027     'spawn_marc_editor_for_overlay' : function(my_marcxml,biblio_source) {
1028         var obj = this;
1029         obj.data.init({'via':'stash'});
1030         if (!obj.data.marked_record) {
1031             alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.try_again'));
1032             return;
1033         }
1034
1035         function overlay_marc (new_marcxml) {
1036             try {
1037                 if (! obj.confirm_overlay( [ obj.data.marked_record ] ) ) { return; }
1038
1039                 var strip_grps = dojo.query('[strip-fields-cbox]').filter(
1040                     function(grp) { return grp.checked }).map(
1041                     function(grp) { return grp.getAttribute('value') });
1042
1043                 var r = obj.network.simple_request('MARC_XML_RECORD_REPLACE', 
1044                     [ ses(), obj.data.marked_record, new_marcxml, biblio_source, null, strip_grps ]);
1045
1046                 if (typeof r.ilsevent != 'undefined') {
1047                     switch(Number(r.ilsevent)) {
1048                         case 1704 /* TCN_EXISTS */ :
1049                             var msg = $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor_for_overlay.same_tcn', [r.payload.tcn]);
1050                             var title = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.import_collision');
1051                             var btn1 = typeof r.payload.new_tcn == 'undefined' ? null : $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor_for_overlay.btn1_overlay', [r.payload.new_tcn]);
1052                             if (btn1) {
1053                                 var robj = obj.network.simple_request(
1054                                     'PERM_CHECK',[
1055                                         ses(),
1056                                         obj.data.list.au[0].id(),
1057                                         obj.data.list.au[0].ws_ou(),
1058                                         [ 'ALLOW_ALT_TCN' ]
1059                                     ]
1060                                 );
1061                                 if (typeof robj.ilsevent != 'undefined') {
1062                                     obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.permission_error'),E);
1063                                 }
1064                                 if (robj.length != 0) btn1 = null;
1065                             }
1066                             var btn2 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.btn2_cancel');
1067                             var p = obj.error.yns_alert(msg,title,btn1,btn2,null, $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.confirm_action'));
1068                             obj.error.sdump('D_ERROR','option ' + p + 'chosen');
1069                             switch(p) {
1070                                 case 0:
1071                                     var r2 = obj.network.request(
1072                                         api.MARC_XML_RECORD_REPLACE.app,
1073                                         api.MARC_XML_RECORD_REPLACE.method + '.override',
1074                                         [ ses(), obj.data.marked_record, new_marcxml, 
1075                                             biblio_source, null, strip_grps ]
1076                                     );
1077                                     if (typeof r2.ilsevent != 'undefined') {
1078                                         throw(r2);
1079                                     } else {
1080                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.successful_overlay_with_new_TCN'));
1081                                         return {
1082                                             'id' : r2.id(),
1083                                             'on_complete' : function() {
1084                                                 try {
1085                                                     obj.replace_tab_with_opac(r2.id());
1086                                                 } catch(E) {
1087                                                     alert(E);
1088                                                 }
1089                                             }
1090                                         };
1091                                     }
1092                                 break;
1093                                 case 1:
1094                                 default:
1095                                     alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.cancelled_overlay'));
1096                                 break;
1097                             }
1098                         break;
1099                         default:
1100                             throw(r);
1101                         break;
1102                     }
1103                 } else {
1104                     alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.success_overlay'));
1105                     try {
1106                         obj.data.marked_record_mvr = null;
1107                         obj.data.marked_record = null;
1108                         obj.data.stash('marked_record');
1109                         obj.data.stash('marked_record_mvr');
1110                         obj.controller.view.marc_import_overlay.disabled = true;
1111                         if ($("overlay_tcn_indicator")) {
1112                             $("overlay_tcn_indicator").setAttribute('value',$("catStrings").getString('staff.cat.z3950.marked_record_for_overlay_indicator.no_record.label'));
1113                         }
1114                         xulG.set_statusbar(1, $("catStrings").getString('staff.cat.z3950.marked_record_for_overlay_indicator.no_record.label') );
1115                     } catch(E) {
1116                         dump('Error in z3950.js, post-overlay: ' + E + '\n');
1117                     }
1118                     return {
1119                         'id' : r.id(),
1120                         'on_complete' : function() {
1121                             try {
1122                                 obj.replace_tab_with_opac(r.id());
1123                             } catch(E) {
1124                                 alert(E);
1125                             }
1126                         }
1127                     };
1128                 }
1129             } catch(E) {
1130                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.overlay_error'),E);
1131             }
1132         }
1133
1134         if ( $('marc_editor').checked ) {
1135             xulG.new_tab(
1136                 xulG.url_prefix('XUL_MARC_EDIT'), 
1137                 { 'tab_name' : $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.tab_name') },
1138                 { 
1139                     'record' : { 'marc' : my_marcxml },
1140                     'fast_add_item' : function(doc_id,cn_label,cp_barcode) {
1141                         try {
1142                             JSAN.use('cat.util'); cat.util.fast_item_add(doc_id,cn_label,cp_barcode);
1143                         } catch(E) {
1144                             alert(E);
1145                         }
1146                     },
1147                     'save' : {
1148                         'label' : $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.overlay_record_label'),
1149                         'func' : overlay_marc
1150                     }
1151                 } 
1152             );
1153         } else {
1154             overlay_marc(my_marcxml);
1155         }
1156     },
1157
1158
1159     'load_creds' : function() {
1160         var obj = this;
1161         try {
1162             obj.creds = { 'version' : g.save_version, 'services' : {}, 'hosts' : {} };
1163             /*
1164                 {
1165                     'version' : xx,
1166                     'default_service' : xx,
1167                     'services' : {
1168
1169                         'xx' : {
1170                             'username' : xx,
1171                             'password' : xx,
1172                             'default_attr' : xx,
1173                         },
1174
1175                         'xx' : {
1176                             'username' : xx,
1177                             'password' : xx,
1178                             'default_attr' : xx,
1179                         },
1180                     },
1181                     // new in version 2
1182                     'hosts' : {
1183                         'xxxx' : {
1184                             'default_services' : [ xx, ... ],
1185                             'services' : {
1186
1187                                 'xx' : {
1188                                     'username' : xx,
1189                                     'password' : xx,
1190                                     'default_attr' : xx,
1191                                 },
1192
1193                                 'xx' : {
1194                                     'username' : xx,
1195                                     'password' : xx,
1196                                     'default_attr' : xx,
1197                                 },
1198                             },
1199                         }
1200                     }
1201                 }
1202             */
1203             JSAN.use('util.file'); var file = new util.file('z3950_store');
1204             if (file._file.exists()) {
1205                 var creds = file.get_object(); file.close();
1206                 if (typeof creds.version != 'undefined') {
1207                     if (creds.version >= obj.creds_version) {  /* so apparently, this guy is assuming that future versions will be backwards compatible */
1208                         if (typeof creds.hosts == 'undefined') creds.hosts = {};
1209                         obj.creds = creds;
1210                     }
1211                 }
1212             }
1213         } catch(E) {
1214             obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.load_creds.z3950_cred_error'),E);
1215         }
1216     },
1217
1218     'save_creds' : function () {
1219         try {
1220             var obj = this;
1221             if (typeof obj.creds.hosts == 'undefined') obj.creds.hosts = {};
1222             if (typeof obj.creds.hosts[ obj.data.server_unadorned ] == 'undefined') obj.creds.hosts[ obj.data.server_unadorned ] = { 'services' : {} };
1223             obj.creds.hosts[ obj.data.server_unadorned ].default_services = obj.active_services;
1224             for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
1225                 var service = obj.creds.hosts[ obj.data.server_unadorned ].default_services[i];
1226                 if (typeof obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] == 'undefined') {
1227                     obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] = {}
1228                 }
1229                 obj.creds.hosts[ obj.data.server_unadorned ].services[service].username = document.getElementById(service + '_username').value;
1230                 obj.creds.hosts[ obj.data.server_unadorned ].services[service].password = document.getElementById(service + '_password').value;
1231                 if (obj.default_attr) {
1232                     obj.creds.hosts[ obj.data.server_unadorned ].services[service].default_attr = obj.default_attr;
1233                 }
1234             }
1235             obj.creds.version = obj.creds_version;
1236             JSAN.use('util.file'); var file = new util.file('z3950_store');
1237             file.set_object(obj.creds);
1238             file.close();
1239         } catch(E) {
1240             obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.save_creds.z3950_cred_error'),E);
1241         }
1242     },
1243
1244     'handle_enter' : function(ev) {
1245         var obj = this;
1246         if (ev.target.tagName != 'textbox') return;
1247         if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.initial_search(); }, 0);
1248     },
1249 }
1250
1251 function buildStripTags() {
1252
1253     // give me every non-always-apply trash field 
1254     // group owned at my workstation or ancestor,
1255     var query = {   
1256         always_apply : 'f',                                                
1257         owner : {'in' : {
1258             select : {aou : [{
1259                 column : 'id',
1260                 transform : 'actor.org_unit_ancestors', 
1261                 result_field : 'id'
1262             }]},
1263             from : 'aou',
1264             where : {id : ses('ws_ou')}}
1265         }
1266     };
1267
1268     fieldmapper.standardRequest(
1269         ['open-ils.pcrud', 'open-ils.pcrud.search.vibtg.atomic'],
1270         {   async : true,
1271             params : [ses(), query, {order_by : {vibtg : ['label']}}],
1272             oncomplete : function(r) {
1273                 var resp = r.recv();
1274                 if (resp) buildStripTags2(resp.content());
1275             }
1276         }
1277     );
1278 }
1279
1280 function buildStripTags2(grps) {
1281     if (!grps || grps.length == 0) return;
1282
1283     $('strip-fields-hbox').className = '';
1284
1285     dojo.forEach(grps, function(grp) {
1286         var cbox = dojo.create('checkbox', {
1287             label : grp.label(), 
1288             value : grp.id(),
1289             'strip-fields-cbox' : 1
1290         });
1291         dojo.place(cbox, $('strip-fields-hbox'));
1292     });
1293 }
1294
1295
1296 dump('exiting cat.z3950.js\n');