b6eace725a05de77000620318e2112df05d4935e
[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                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
9                 JSAN.use('util.error'); this.error = new util.error();
10                 JSAN.use('util.network'); this.network = new util.network();
11         } catch(E) {
12                 dump('cat.z3950: ' + E + '\n');
13         }
14 }
15
16 cat.z3950.prototype = {
17
18         'creds_version' : 2,
19
20     'number_of_result_sets' : 0,
21
22     'result_set' : [],
23
24     'limit' : 10,
25
26         'init' : function( params ) {
27
28                 try {
29                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
30                         JSAN.use('util.widgets');
31
32                         var obj = this;
33
34             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
35
36                         obj.load_creds();
37
38                         JSAN.use('circ.util');
39                         var columns = circ.util.columns(
40                                 {
41                                         'tcn' : { 'hidden' : false },
42                                         'isbn' : { 'hidden' : false },
43                                         'title' : { 'hidden' : false, 'flex' : '1' },
44                                         'author' : { 'hidden' : false },
45                                         'edition' : { 'hidden' : false },
46                                         'pubdate' : { 'hidden' : false },
47                                         'publisher' : { 'hidden' : false },
48                                         'service' : { 'hidden' : false }
49                                 }
50             );
51
52                         JSAN.use('util.list'); obj.list = new util.list('results');
53                         obj.list.init(
54                                 {
55                                         'columns' : columns,
56                                         'map_row_to_columns' : circ.util.std_map_row_to_columns(),
57                                         'on_select' : function(ev) {
58                                                 try {
59                                                         JSAN.use('util.functional');
60                                                         var sel = obj.list.retrieve_selection();
61                                                         document.getElementById('clip_button').disabled = sel.length < 1;
62                                                         var list = util.functional.map_list(
63                                                                 sel,
64                                                                 function(o) { return o.getAttribute('retrieve_id'); }
65                                                         );
66                                                         obj.error.sdump('D_TRACE','cat/z3950: selection list = ' + js2JSON(list) );
67                                                         obj.controller.view.marc_import.disabled = false;
68                                                         obj.controller.view.marc_import.setAttribute('retrieve_id',list[0]);
69                                                         obj.controller.view.marc_import_overlay.disabled = false;
70                                                         obj.controller.view.marc_import_overlay.setAttribute('retrieve_id',list[0]);
71                                                         obj.controller.view.marc_view.disabled = false;
72                                                         obj.controller.view.marc_view.setAttribute('retrieve_id',list[0]);
73                                                 } catch(E) {
74                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_list_init.list_construction_error'),E);
75                                                 }
76                                         },
77                                 }
78                         );
79
80                         JSAN.use('util.controller'); obj.controller = new util.controller();
81                         obj.controller.init(
82                                 {
83                                         control_map : {
84                                                 'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
85                                                 'sel_clip' : [
86                                                         ['command'],
87                                                         function() { obj.list.clipboard(); }
88                                                 ],
89                                                 'cmd_export' : [
90                                                         ['command'],
91                                                         function() {
92                                                                 obj.list.dump_csv_to_clipboard();
93                                                         }
94                                                 ],
95                                                 'cmd_broken' : [
96                                                         ['command'],
97                                                         function() { alert('Not Yet Implemented'); }
98                                                 ],
99                                                 'result_message' : [['render'],function(e){return function(){};}],
100                                                 'clear' : [
101                                                         ['command'],
102                                                         function() {
103                                                                 obj.clear();
104                                                         }
105                                                 ],
106                                                 'save_creds' : [
107                                                         ['command'],
108                                                         function() {
109                                                                 obj.save_creds();
110                                 setTimeout( function() { obj.focus(); }, 0 );
111                                                         }
112                                                 ],
113                                                 'marc_view' : [
114                                                         ['command'],
115                                                         function(ev) {
116                                                                 try {
117                                                                         var n = obj.controller.view.marc_view;
118                                                                         if (n.getAttribute('toggle') == '1') {
119                                                                                 document.getElementById('deck').selectedIndex = 0;
120                                                                                 n.setAttribute('toggle','0');
121                                                                                 n.setAttribute('label','MARC View');
122                                                                                 document.getElementById('results').focus();
123                                                                         } else {
124                                                                                 document.getElementById('deck').selectedIndex = 1;
125                                                                                 n.setAttribute('toggle','1');
126                                                                                 n.setAttribute('label', $("catStrings").getString('staff.cat.z3950.obj_controller_init.deck_label'));
127                                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
128                                                                                 var f = get_contentWindow(document.getElementById('marc_frame'));
129                                         var retrieve_id = n.getAttribute('retrieve_id');
130                                         var result_idx = retrieve_id.split('-')[0];
131                                         var record_idx = retrieve_id.split('-')[1];
132                                                                                 f.xulG = { 'marcxml' : obj.result_set[result_idx].records[ record_idx ].marcxml };
133                                                                                 f.my_init();
134                                                                                 f.document.body.firstChild.focus();
135                                                                         }
136                                                                 } catch(E) {
137                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_view_error'),E);
138                                                                 }
139                                                         },
140                                                 ],
141                                                 'marc_import' : [
142                                                         ['command'],
143                                                         function() {
144                                 try {
145                                     var retrieve_id = obj.controller.view.marc_import.getAttribute('retrieve_id');
146                                     var result_idx = retrieve_id.split('-')[0];
147                                     var record_idx = retrieve_id.split('-')[1];
148                                     obj.spawn_marc_editor( 
149                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
150                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
151                                     );
152                                 } catch(E) {
153                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_import_error'),E);
154                                 }
155                                                         },
156                                                 ],
157                                                 'marc_import_overlay' : [ 
158                                                         ['command'],
159                                                         function() {
160                                                                 try {
161                                     var retrieve_id = obj.controller.view.marc_import_overlay.getAttribute('retrieve_id');
162                                     var result_idx = retrieve_id.split('-')[0];
163                                     var record_idx = retrieve_id.split('-')[1];
164                                     obj.spawn_marc_editor_for_overlay( 
165                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
166                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
167                                     );
168                                                                 } catch(E) {
169                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_import_overlay_error'),E);
170                                                                 }
171                                                         },
172                                                 ],
173                         'z3950_deck' : [ ['render'], function(e) { return function() { e.selectedIndex = 0; }; } ],
174                                                 'search' : [
175                                                         ['command'],
176                                                         function() {
177                                 obj.controller.view.z3950_deck.selectedIndex = 1;
178                                                                 obj.initial_search();
179                                                         },
180                                                 ],
181                                                 'search_form' : [
182                                                         ['command'],
183                                                         function() {
184                                 obj.controller.view.z3950_deck.selectedIndex = 0;
185                                                         },
186                                                 ],
187                                                 'results_page' : [
188                                                         ['command'],
189                                                         function() {
190                                 obj.controller.view.z3950_deck.selectedIndex = 1;
191                                                         },
192                                                 ],
193                                                 'page_next' : [
194                                                         ['command'],
195                                                         function() {
196                                                                 obj.page_next();
197                                                         },
198                                                 ],
199                                                 'service_rows' : [
200                                                         ['render'],
201                                                         function(e) {
202                                                                 return function() {
203                                                                         try {
204
205                                                                                 function handle_switch(node) {
206                                             try {
207                                                 obj.active_services = [];
208                                                 var snl = document.getElementsByAttribute('mytype','service_class');
209                                                 for (var i = 0; i < snl.length; i++) {
210                                                     var n = snl[i];
211                                                     if (n.nodeName == 'checkbox') {
212                                                         if (n.checked) obj.active_services.push( n.getAttribute('service') );
213                                                     }
214                                                 }
215                                                 var nl = document.getElementsByAttribute('mytype','search_class');
216                                                 for (var i = 0; i < nl.length; i++) { nl[i].disabled = true; }
217                                                 var attrs = {};
218                                                 for (var j = 0; j < obj.active_services.length; j++) {
219                                                     if (obj.services[obj.active_services[j]]) for (var i in obj.services[obj.active_services[j]].attrs) {
220                                                         var attr = obj.services[obj.active_services[j]].attrs[i];
221                                                         if (! attrs[i]) {
222                                                             attrs[i] = { 'labels' : {} };
223                                                         }
224                                                         if (attr.label) {
225                                                             attrs[i].labels[ attr.label ] = true;
226                                                         } else if (document.getElementById('commonStrings').testString('staff.z39_50.search_class.' + i)) {
227                                                             attrs[i].labels[ document.getElementById('commonStrings').getString('staff.z39_50.search_class.' + i) ] = true;
228                                                         } else if (attr.name) {
229                                                             attrs[i].labels[ attr.name ] = true;
230                                                         } else {
231                                                             attrs[i].labels[ i ] = true;
232                                                         }
233
234                                                     }
235                                                     
236                                                 }
237
238                                                 function set_label(x,attr) {
239                                                     var labels = [];
240                                                     for (var j in attrs[attr].labels) {
241                                                         labels.push(j);
242                                                     }
243                                                     if (labels.length > 0) {
244                                                         x.setAttribute('value',labels[0]);
245                                                         x.setAttribute('tooltiptext',labels.join(','));
246                                                         if (labels.length > 1) x.setAttribute('class','multiple_labels');
247                                                     }
248                                                 }
249
250                                                 for (var i in attrs) {
251                                                     var x = document.getElementById(i + '_input');
252                                                     if (x) {
253                                                         x.disabled = false;
254                                                         var y = document.getElementById(i + '_label',i);
255                                                         if (y) set_label(y,i);
256                                                     } else {
257                                                         var rows = document.getElementById('query_inputs');
258                                                         var row = document.createElement('row'); rows.appendChild(row);
259                                                         var label = document.createElement('label');
260                                                         label.setAttribute('id',i+'_label');
261                                                         label.setAttribute('control',i+'_input');
262                                                         label.setAttribute('search_class',i);
263                                                         label.setAttribute('style','-moz-user-focus: ignore');
264                                                         row.appendChild(label);
265                                                         set_label(label,i);
266                                                         label.addEventListener('click',function(ev){
267                                                                 var a = ev.target.getAttribute('search_class');
268                                                                 if (a) obj.default_attr = a;
269                                                             },false
270                                                         );
271                                                         var tb = document.createElement('textbox');
272                                                         tb.setAttribute('id',i+'_input');
273                                                         tb.setAttribute('mytype','search_class');
274                                                         tb.setAttribute('search_class',i);
275                                                         row.appendChild(tb);
276                                                         tb.addEventListener('keypress',function(ev) { return obj.handle_enter(ev); },false);
277                                                     }
278                                                 }
279                                             } catch(E) {
280                                                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.search_fields_error'),E);
281                                             }
282                                                                                 }
283
284                                         document.getElementById('native-evergreen-catalog_service').addEventListener('command',handle_switch,false);
285
286                                                                                 var robj = obj.network.simple_request(
287                                                                                         'RETRIEVE_Z3950_SERVICES',
288                                                                                         [ ses() ]
289                                                                                 );
290                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
291                                                                                 obj.services = robj;
292                                         var x = document.getElementById('service_rows');
293                                                                                 for (var i in robj) {
294                                             var r = document.createElement('row'); x.appendChild(r);
295                                             var cb = document.createElement('checkbox'); 
296                                                 if (robj[i].label) {
297                                                     cb.setAttribute('label',robj[i].label);
298                                                 } else if (robj[i].name) {
299                                                     cb.setAttribute('label',robj[i].name);
300                                                 } else {
301                                                     cb.setAttribute('label',i);
302                                                 }
303                                                 cb.setAttribute('tooltiptext',i + ' : ' + robj[i].db + '@' + robj[i].host + ':' + robj[i].port); 
304                                                 cb.setAttribute('mytype','service_class'); cb.setAttribute('service',i);
305                                                 cb.setAttribute('id',i+'_service'); r.appendChild(cb);
306                                                 cb.addEventListener('command',handle_switch,false);
307                                             var username = document.createElement('textbox'); username.setAttribute('id',i+'_username'); 
308                                             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);
309                                             r.appendChild(username);
310                                             if (typeof robj[i].auth != 'undefined') username.hidden = ! get_bool( robj[i].auth );
311                                             var password = document.createElement('textbox'); password.setAttribute('id',i+'_password'); 
312                                             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);
313                                             password.setAttribute('type','password'); r.appendChild(password);
314                                             if (typeof robj[i].auth != 'undefined') password.hidden = ! get_bool( robj[i].auth );
315                                         }
316                                         obj.services[ 'native-evergreen-catalog' ] = { 'attrs' : { 'author' : {}, 'title' : {} } };
317                                         setTimeout(
318                                                                                         function() { 
319                                                 if (obj.creds.hosts[ obj.data.server_unadorned ]) {
320                                                     for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
321                                                         var x = document.getElementById(obj.creds.hosts[ obj.data.server_unadorned ].default_services[i]+'_service');
322                                                         if (x) x.checked = true;
323                                                     }
324                                                 } else if (obj.creds.default_service) {
325                                                     var x = document.getElementById(obj.creds.default_service+'_service');
326                                                     if (x) x.checked = true;
327                                                 }
328                                                 handle_switch();
329                                                                                         },0
330                                                                                 );
331                                                                         } catch(E) {
332                                                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.z39_service_error'),E);
333                                                                         }
334                                                                 }
335                                                         }
336                                                 ],
337                                         }
338                                 }
339                         );
340
341                         obj.controller.render();
342
343             setTimeout( function() { obj.focus(); }, 0 );
344
345                 } catch(E) {
346                         this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
347                 }
348         },
349
350         'focus' : function() {
351                 var obj = this;
352         var focus_me; var or_focus_me;
353         for (var i = 0; i < obj.active_services.length; i++) {
354             if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ]) {
355                         var x = obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ].default_attr;
356                 if (x) { focus_me = x; break; }
357             }
358             if (ob.services[ obj.active_services[i] ]) for (var i in obj.services[ obj.active_services[i] ].attr) { or_focus_me = i; }
359         }
360         if (! focus_me) focus_me = or_focus_me;
361                 var xx = document.getElementById(focus_me+'_input'); if (xx) xx.focus();
362         },
363
364         'clear' : function() {
365                 var obj = this;
366                 var nl = document.getElementsByAttribute('mytype','search_class');
367                 for (var i = 0; i < nl.length; i++) { nl[i].value = ''; nl[i].setAttribute('value',''); }
368                 //obj.focus(obj.controller.view.service_menu.value);
369         },
370
371         'search_params' : {},
372
373         'initial_search' : function() {
374                 try {
375                         var obj = this;
376             obj.result_set = []; obj.number_of_result_sets = 0;
377                         JSAN.use('util.widgets');
378                         util.widgets.remove_children( obj.controller.view.result_message );
379                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
380             if (obj.active_services.length < 1) {
381                             x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.no_search_selection')));
382                 return;
383             }
384                         x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.searching')));
385                         obj.search_params = {}; obj.list.clear();
386                         obj.controller.view.page_next.disabled = true;
387
388                         obj.search_params.service = []; 
389                         obj.search_params.username = [];
390                         obj.search_params.password = [];
391             for (var i = 0; i < obj.active_services.length; i++) {
392                 obj.search_params.service.push( obj.active_services[i] );
393                 obj.search_params.username.push( document.getElementById( obj.active_services[i]+'_username' ).value );
394                 obj.search_params.password.push( document.getElementById( obj.active_services[i]+'_password' ).value );
395             }
396                         obj.search_params.limit = Math.ceil( obj.limit / obj.active_services.length );
397                         obj.search_params.offset = 0;
398
399                         obj.search_params.search = {};
400                         var nl = document.getElementsByAttribute('mytype','search_class');
401                         var count = 0;
402                         for (var i = 0; i < nl.length; i++) {
403                                 if (nl[i].disabled) continue;
404                                 if (nl[i].value == '') continue;
405                                 count++;
406                                 obj.search_params.search[ nl[i].getAttribute('search_class') ] = nl[i].value;
407                         }
408                         if (count>0) {
409                                 obj.search();
410                         } else {
411                                 util.widgets.remove_children( obj.controller.view.result_message );
412                         }
413                 } catch(E) {
414                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.initial_search.failed_search'),E);
415                 }
416         },
417
418         'page_next' : function() {
419                 try {
420                         var obj = this;
421                         JSAN.use('util.widgets');
422                         util.widgets.remove_children( obj.controller.view.result_message );
423                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
424                         x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.page_next.more_results')));
425                         obj.search_params.offset += obj.search_params.limit;
426                         obj.search();
427                 } catch(E) {
428                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.page_next.subsequent_search_error'),E);
429                 }
430         },
431
432         'search' : function() {
433                 try {
434                         var obj = this;
435                         var method;
436                         if (typeof obj.search_params.query == 'undefined') {
437                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_SEARCH';
438                         } else {
439                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_RAW_SEARCH';
440                         }
441                         obj.network.simple_request(
442                                 method,
443                                 [ ses(), obj.search_params ],
444                                 function(req) {
445                                         obj.handle_results(req.getResultObject())
446                                 }
447                         );
448                         document.getElementById('deck').selectedIndex = 0;
449                 } catch(E) {
450                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.search.search_error'),E);
451                 }
452         },
453
454         'handle_results' : function(results) {
455                 var obj = this;
456                 try {
457                         JSAN.use('util.widgets');
458                         util.widgets.remove_children( obj.controller.view.result_message ); var x;
459                         if (results == null) {
460                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
461                                 x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.handle_results.null_server_error')));
462                                 return;
463                         }
464                         if (typeof results.ilsevent != 'undefined') {
465                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
466                                 x.appendChild( document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.server_error', [results.textcode, results.desc])));
467                                 return;
468                         }
469             if (typeof results.length == 'undefined') results = [ results ];
470             for (var i = 0; i < results.length; i++) {
471                 if (results[i].query) {
472                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
473                     x.appendChild( document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.raw_query', [results[i].query])));
474                 }
475                 if (results[i].count) {
476                     if (results[i].records) {
477                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
478                         var showing = obj.search_params.offset + results[i].records.length; 
479                         x.appendChild(
480                             document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.showing_results',
481                                                                 [(showing > results[i].count ? results[i].count : showing), results[i].count, results[i].service]))
482                         );
483                     }
484                     if (obj.search_params.offset + obj.search_params.limit <= results[i].count) {
485                         obj.controller.view.page_next.disabled = false;
486                     }
487                 } else {
488                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
489                         x.appendChild(
490                             document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.num_of_results', [(results[i].count ? results[i].count : 0)])));
491                 }
492                 if (results[i].records) {
493                     obj.result_set[ ++obj.number_of_result_sets ] = results[i];
494                     obj.controller.view.marc_import.disabled = true;
495                     obj.controller.view.marc_import_overlay.disabled = true;
496                     var x = obj.controller.view.marc_view;
497                     if (x.getAttribute('toggle') == '0') x.disabled = true;
498                     for (var j = 0; j < obj.result_set[ obj.number_of_result_sets ].records.length; j++) {
499                         var f;
500                         var n = obj.list.append(
501                             {
502                                 'retrieve_id' : String( obj.number_of_result_sets ) + '-' + String( j ),
503                                 'row' : {
504                                     'my' : {
505                                         'mvr' : function(a){return a;}(obj.result_set[ obj.number_of_result_sets ].records[j].mvr),
506                                         'service' : results[i].service
507                                     }
508                                 }
509                             }
510                         );
511                         if (!f) { n.my_node.parentNode.focus(); f = n; } 
512                     }
513                 } else {
514                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
515                     x.appendChild(
516                         document.createTextNode($("catStrings").getString('staff.cat.z3950.handle_results.result_error'))
517                     );
518                 }
519             }
520                 } catch(E) {
521                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.handle_results.search_result_error'),E);
522                 }
523         },
524
525         'replace_tab_with_opac' : function(doc_id) {
526                 var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
527                 var content_params = { 
528                         'session' : ses(),
529                         'authtime' : ses('authtime'),
530                         'opac_url' : opac_url,
531                 };
532                 xulG.set_tab(
533                         xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
534                         {'tab_name': $("catStrings").getString('staff.cat.z3950.replace_tab_with_opac.tab_name')}, 
535                         content_params
536                 );
537         },
538
539         'spawn_marc_editor' : function(my_marcxml,biblio_source) {
540                 var obj = this;
541                 xulG.new_tab(
542                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
543                         { 'tab_name' : 'MARC Editor' }, 
544                         { 
545                                 'record' : { 'marc' : my_marcxml },
546                                 'save' : {
547                                         'label' : 'Import Record',
548                                         'func' : function (new_marcxml) {
549                                                 try {
550                                                         var r = obj.network.simple_request('MARC_XML_RECORD_IMPORT', [ ses(), new_marcxml, biblio_source ]);
551                                                         if (typeof r.ilsevent != 'undefined') {
552                                                                 switch(Number(r.ilsevent)) {
553                                                                         case 1704 /* TCN_EXISTS */ :
554                                                                                 var msg = $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor.same_tcn', [r.payload.tcn]);
555                                                                                 var title = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.title');
556                                                                                 var btn1 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.btn1_overlay');
557                                                                                 var btn2 = typeof r.payload.new_tcn == 'undefined' ? null : $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor.btn2_import', [r.payload.new_tcn]);
558                                                                                 if (btn2) {
559                                                                                         obj.data.init({'via':'stash'});
560                                                                                         var robj = obj.network.simple_request(
561                                                                                                 'PERM_CHECK',[
562                                                                                                         ses(),
563                                                                                                         obj.data.list.au[0].id(),
564                                                                                                         obj.data.list.au[0].ws_ou(),
565                                                                                                         [ 'ALLOW_ALT_TCN' ]
566                                                                                                 ]
567                                                                                         );
568                                                                                         if (typeof robj.ilsevent != 'undefined') {
569                                                                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.permission_error'),E);
570                                                                                         }
571                                                                                         if (robj.length != 0) btn2 = null;
572                                                                                 }
573                                                                                 var btn3 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.btn3_cancel_import');
574                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,btn3,$("catStrings").getString('staff.cat.z3950.spawn_marc_editor.confirm_action'));
575                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
576                                                                                 switch(p) {
577                                                                                         case 0:
578                                                                                                 var r3 = obj.network.simple_request('MARC_XML_RECORD_UPDATE', [ ses(), r.payload.dup_record, new_marcxml, biblio_source ]);
579                                                                                                 if (typeof r3.ilsevent != 'undefined') {
580                                                                                                         throw(r3);
581                                                                                                 } else {
582                                                                                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_overlay'));
583                                                                                                         obj.replace_tab_with_opac(r3.id());
584                                                                                                 }
585                                                                                         break;
586                                                                                         case 1:
587                                                                                                 var r2 = obj.network.request(
588                                                                                                         api.MARC_XML_RECORD_IMPORT.app,
589                                                                                                         api.MARC_XML_RECORD_IMPORT.method + '.override',
590                                                                                                         [ ses(), new_marcxml, biblio_source ]
591                                                                                                 );
592                                                                                                 if (typeof r2.ilsevent != 'undefined') {
593                                                                                                         throw(r2);
594                                                                                                 } else {
595                                                                                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_import_with_new_tcn'));
596                                                                                                         obj.replace_tab_with_opac(r2.id());
597                                                                                                 }
598                                                                                         break;
599                                                                                         case 2:
600                                                                                         default:
601                                                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.import_cancelled'));
602                                                                                         break;
603                                                                                 }
604                                                                         break;
605                                                                         default:
606                                                                                 throw(r);
607                                                                         break;
608                                                                 }
609                                                         } else {
610                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_import'));
611                                                                 obj.replace_tab_with_opac(r.id());
612                                                         }
613                                                 } catch(E) {
614                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.import_error'),E);
615                                                 }
616                                         }
617                                 }
618                         } 
619                 );
620         },
621
622         'confirm_overlay' : function(record_ids) {
623                 var obj = this; // JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
624                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
625                 var top_xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" >';
626                 top_xml += '<description>'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.description')+'</description>';
627                 top_xml += '<hbox><button id="lead" disabled="false" label="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.lead.label')+'" name="fancy_submit"';
628                 top_xml += ' accesskey="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.lead.accesskey')+'"/>';
629                 top_xml += ' <button label="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.cancel.label')+'" accesskey="'+
630                                                 $("catStrings").getString('staff.cat.z3950.confirm_overlay.cancel.accesskey')+'" name="fancy_cancel"/></hbox></vbox>';
631
632                 var xml = '<form xmlns="http://www.w3.org/1999/xhtml">';
633                 xml += '<table width="100%"><tr valign="top">';
634                 for (var i = 0; i < record_ids.length; i++) {
635                         xml += '<td nowrap="nowrap"><iframe src="' + urls.XUL_BIB_BRIEF; 
636                         xml += '?docid=' + record_ids[i] + '"/></td>';
637                 }
638                 xml += '</tr><tr valign="top">';
639                 for (var i = 0; i < record_ids.length; i++) {
640                         html = obj.network.simple_request('MARC_HTML_RETRIEVE',[ record_ids[i] ]);
641                         xml += '<td nowrap="nowrap"><iframe style="min-height: 1000px; min-width: 300px;" flex="1" src="data:text/html,' + window.escape(html) + '"/></td>';
642                 }
643                 xml += '</tr></table></form>';
644                 // data.temp_merge_top = top_xml; data.stash('temp_merge_top');
645                 // data.temp_merge_mid = xml; data.stash('temp_merge_mid');
646                 JSAN.use('util.window'); var win = new util.window();
647                 var fancy_prompt_data = win.open(
648                         urls.XUL_FANCY_PROMPT,
649                         // + '?xml_in_stash=temp_merge_mid'
650                         // + '&top_xml_in_stash=temp_merge_top'
651                         // + '&title=' + window.escape('Record Overlay'),
652                         'fancy_prompt', 'chrome,resizable,modal,width=700,height=500',
653                         { 'top_xml' : top_xml, 'xml' : xml, 'title' : $("catStrings").getString('staff.cat.z3950.confirm_overlay.title') }
654                 );
655                 //data.stash_retrieve();
656                 if (fancy_prompt_data.fancy_status == 'incomplete') { alert($("catStrings").getString('staff.cat.z3950.confirm_overlay.aborted')); return false; }
657                 return true;
658         },
659
660         'spawn_marc_editor_for_overlay' : function(my_marcxml,biblio_source) {
661                 var obj = this;
662                 obj.data.init({'via':'stash'});
663                 if (!obj.data.marked_record) {
664                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.try_again'));
665                         return;
666                 }
667
668                 xulG.new_tab(
669                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
670                         { 'tab_name' : $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.tab_name') },
671                         { 
672                                 'record' : { 'marc' : my_marcxml },
673                                 'save' : {
674                                         'label' : $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.overlay_record_label'),
675                                         'func' : function (new_marcxml) {
676                                                 try {
677                                                         if (! obj.confirm_overlay( [ obj.data.marked_record ] ) ) { return; }
678                                                         var r = obj.network.simple_request('MARC_XML_RECORD_REPLACE', [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]);
679                                                         if (typeof r.ilsevent != 'undefined') {
680                                                                 switch(Number(r.ilsevent)) {
681                                                                         case 1704 /* TCN_EXISTS */ :
682                                                                                 var msg = $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor_for_overlay.same_tcn', [r.payload.tcn]);
683                                                                                 var title = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.import_collision');
684                                                                                 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]);
685                                                                                 if (btn1) {
686                                                                                         var robj = obj.network.simple_request(
687                                                                                                 'PERM_CHECK',[
688                                                                                                         ses(),
689                                                                                                         obj.data.list.au[0].id(),
690                                                                                                         obj.data.list.au[0].ws_ou(),
691                                                                                                         [ 'ALLOW_ALT_TCN' ]
692                                                                                                 ]
693                                                                                         );
694                                                                                         if (typeof robj.ilsevent != 'undefined') {
695                                                                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.permission_error'),E);
696                                                                                         }
697                                                                                         if (robj.length != 0) btn1 = null;
698                                                                                 }
699                                                                                 var btn2 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.btn2_cancel');
700                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,null, $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.confirm_action'));
701                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
702                                                                                 switch(p) {
703                                                                                         case 0:
704                                                                                                 var r2 = obj.network.request(
705                                                                                                         api.MARC_XML_RECORD_REPLACE.app,
706                                                                                                         api.MARC_XML_RECORD_REPLACE.method + '.override',
707                                                                                                         [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]
708                                                                                                 );
709                                                                                                 if (typeof r2.ilsevent != 'undefined') {
710                                                                                                         throw(r2);
711                                                                                                 } else {
712                                                                                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.successful_overlay_with_new_TCN'));
713                                                                                                         obj.replace_tab_with_opac(r2.id());
714                                                                                                 }
715                                                                                         break;
716                                                                                         case 1:
717                                                                                         default:
718                                                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.cancelled_overlay'));
719                                                                                         break;
720                                                                                 }
721                                                                         break;
722                                                                         default:
723                                                                                 throw(r);
724                                                                         break;
725                                                                 }
726                                                         } else {
727                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.success_overlay'));
728                                                                 obj.replace_tab_with_opac(r.id());
729                                                         }
730                                                 } catch(E) {
731                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.overlay_error'),E);
732                                                 }
733                                         }
734                                 }
735                         } 
736                 );
737         },
738
739
740         'load_creds' : function() {
741                 var obj = this;
742                 try {
743                         obj.creds = { 'version' : g.save_version, 'services' : {}, 'hosts' : {} };
744                         /*
745                                 {
746                                         'version' : xx,
747                                         'default_service' : xx,
748                                         'services' : {
749
750                                                 'xx' : {
751                                                         'username' : xx,
752                                                         'password' : xx,
753                                                         'default_attr' : xx,
754                                                 },
755
756                                                 'xx' : {
757                                                         'username' : xx,
758                                                         'password' : xx,
759                                                         'default_attr' : xx,
760                                                 },
761                                         },
762                     // new in version 2
763                     'hosts' : {
764                         'xxxx' : {
765                             'default_services' : [ xx, ... ],
766                             'services' : {
767
768                                 'xx' : {
769                                     'username' : xx,
770                                     'password' : xx,
771                                     'default_attr' : xx,
772                                 },
773
774                                 'xx' : {
775                                     'username' : xx,
776                                     'password' : xx,
777                                     'default_attr' : xx,
778                                 },
779                             },
780                         }
781                     }
782                                 }
783                         */
784                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
785                         JSAN.use('util.file'); var file = new util.file('z3950_store');
786                         if (file._file.exists()) {
787                                 var creds = file.get_object(); file.close();
788                                 if (typeof creds.version != 'undefined') {
789                                         if (creds.version >= obj.creds_version) {  /* so apparently, this guy is assuming that future versions will be backwards compatible */
790                         if (typeof creds.hosts == 'undefined') creds.hosts = {};
791                                                 obj.creds = creds;
792                                         }
793                                 }
794                         }
795                 } catch(E) {
796                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.load_creds.z3950_cred_error'),E);
797                 }
798         },
799
800         'save_creds' : function () {
801                 try {
802                         var obj = this;
803             if (typeof obj.creds.hosts == 'undefined') obj.creds.hosts = {};
804             if (typeof obj.creds.hosts[ obj.data.server_unadorned ] == 'undefined') obj.creds.hosts[ obj.data.server_unadorned ] = { 'services' : {} };
805             obj.creds.hosts[ obj.data.server_unadorned ].default_services = obj.active_services;
806             for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
807                             var service = obj.creds.hosts[ obj.data.server_unadorned ].default_services[i];
808                         if (typeof obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] == 'undefined') {
809                     obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] = {}
810                         }
811                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].username = document.getElementById(service + '_username').value;
812                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].password = document.getElementById(service + '_password').value;
813                         if (obj.default_attr) {
814                                 obj.creds.hosts[ obj.data.server_unadorned ].services[service].default_attr = obj.default_attr;
815                         }
816             }
817                         obj.creds.version = obj.creds_version;
818                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
819                         JSAN.use('util.file'); var file = new util.file('z3950_store');
820                         file.set_object(obj.creds);
821                         file.close();
822                 } catch(E) {
823                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.save_creds.z3950_cred_error'),E);
824                 }
825         },
826
827         'handle_enter' : function(ev) {
828                 var obj = this;
829                 if (ev.target.tagName != 'textbox') return;
830                 if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.controller.view.z3950_deck.selectedIndex = 1; obj.initial_search(); }, 0);
831         },
832 }
833
834 dump('exiting cat.z3950.js\n');