]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/marcedit.js
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / marcedit.js
1 /* vim: et:sw=4:ts=4:
2  *
3  * Copyright (C) 2004-2008  Georgia Public Library Service
4  * Copyright (C) 2008-2010  Equinox Software, Inc.
5  * Mike Rylander <miker@esilibrary.com> 
6  *
7  * Copyright (C) 2010 Dan Scott <dan@coffeecode.net>
8  * Copyright (C) 2010 Internationaal Instituut voor Sociale Geschiedenis <info@iisg.nl>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.  
19  *
20  */
21 // Pretty printing kills whitespace too, so disable it.
22 XML.prettyPrinting = false;
23 var xmlDeclaration = /^<\?xml version[^>]+?>/;
24
25 var serializer = new XMLSerializer();
26 var marcns = new Namespace("http://www.loc.gov/MARC21/slim");
27 var gw = new Namespace("http://opensrf.org/-/namespaces/gateway/v1");
28 var xulns = new Namespace("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
29 default xml namespace = marcns;
30
31 var tooltip_hash = {};
32 var current_focus;
33 var _record;
34 var _record_type;
35 var bib_data;
36
37 var xml_record;
38
39 var context_menus;
40 var tag_menu;
41 var p;
42 var auth_pages = {};
43 var show_auth_menu = false;
44
45 var _fixed_field_values = {}, _fixed_field_context_menus = {};
46 var _fixed_field_anonymous_func_counter = 0;
47
48 function $(id) { return document.getElementById(id); }
49
50 var acs; // AuthorityControlSet
51
52 function get_new_008() {
53     var orig008 = '                                        ';
54     var now = new Date();
55     var y = now.getUTCFullYear().toString().substr(2,2);
56     var m = now.getUTCMonth() + 1;
57     if (m < 10) m = '0' + m;
58     var d = now.getUTCDate();
59     if (d < 10) d = '0' + d;
60
61     if (xml_record.controlfield.(@tag == '008')) {
62         var field = xml_record.controlfield.(@tag == '008')[0];
63         orig008 = field.text();
64     }
65
66     /* lang code from 041a */
67     var lang = orig008.substr(35, 3);
68     if (xml_record.datafield.(@tag == '041')) {
69         var field = xml_record.datafield.(@tag == '041')[0];
70         if (field && field.subfield.(@code == 'a')) {
71             lang = field.subfield.(@code == 'a')[0];
72         }
73     }
74
75     /* country code from 044a */
76     var country = orig008.substr(15, 3);
77     if (xml_record.datafield.(@tag == '044')) {
78         var field = xml_record.datafield.(@tag == '044')[0];
79         if (field && field.subfield.(@code == 'a')) {
80             country = field.subfield.(@code == 'a')[0];
81         }
82     }
83     while (country.length < 3) country = country + ' ';
84     if (country.length > 3) country = country.substr(0,3);
85
86     /* date1 from 260c */
87     var date1 = now.getUTCFullYear().toString();
88     if (xml_record.datafield.(@tag == '260')) {
89         var field = xml_record.datafield.(@tag == '260')[0];
90         if (field && field.subfield.(@code == 'c')) {
91             var tmpd = field.subfield.(@code == 'c')[0].replace(/[^0-9]/g, '');
92             if (tmpd.match(/^\d\d\d\d/)) {
93                 date1 = tmpd.substr(0, 4);
94             }
95         }
96     }
97
98     var date2 = orig008.substr(11, 4);
99     var datetype = orig008.substr(6, 1);
100     var modded = orig008.substr(38, 1);
101     var catsrc = orig008.substr(39, 1);
102
103     return '' + y + m + d + datetype + date1 + date2 + country + '                 ' + lang + modded + catsrc;
104
105 }
106
107 function mangle_005() {
108     var now = new Date();
109     var y = now.getUTCFullYear();
110
111     var m = now.getUTCMonth() + 1;
112     if (m < 10) m = '0' + m;
113     
114     var d = now.getUTCDate();
115     if (d < 10) d = '0' + d;
116     
117     var H = now.getUTCHours();
118     if (H < 10) H = '0' + H;
119     
120     var M = now.getUTCMinutes();
121     if (M < 10) M = '0' + M;
122     
123     var S = now.getUTCSeconds();
124     if (S < 10) S = '0' + S;
125     
126
127     var stamp = '' + y + m + d + H + M + S + '.0';
128     createControlField('005',stamp);
129
130 }
131
132 function createControlField (tag,data) {
133     // first, remove the old field, if any;
134     for (var i in xml_record.controlfield.(@tag == tag)) delete xml_record.controlfield.(@tag == tag)[i];
135
136     var cf = <controlfield tag="" xmlns="http://www.loc.gov/MARC21/slim">{ data }</controlfield>;
137     cf.@tag = tag;
138
139     // then, find the right position and insert it
140     var done = 0;
141     var cfields = xml_record.controlfield;
142     var base = Number(tag.substring(2));
143     for (var i in cfields) {
144         var t = Number(cfields[i].@tag.toString().substring(2));
145         if (t > base) {
146             xml_record.insertChildBefore( cfields[i], cf );
147             done = 1
148             break;
149         }
150     }
151
152     if (!done) xml_record.insertChildBefore( xml_record.datafield[0], cf );
153
154     return cf;
155 }
156
157 function xml_escape_unicode ( str ) {
158     return str.replace(
159         /([\u0080-\ufffe])/g,
160         function (r,s) { return "&#x" + s.charCodeAt(0).toString(16) + ";"; }
161     );
162 }
163
164 function wrap_long_fields (node) {
165     var text_size = dojo.attr(node, 'size');
166     var hard_width = 100; 
167     if (text_size > hard_width) {
168         dojo.attr(node, 'multiline', 'true');
169         dojo.attr(node, 'cols', hard_width);
170         var text_rows = (text_size / hard_width) + 1;
171         dojo.attr(node, 'rows', text_rows);
172     }
173 }
174
175 function set_flat_editor (useFlatText) {
176
177     var xe = $('xul-editor');
178     var te = $('text-editor');
179
180     if (useFlatText) {
181         if (xe.hidden) { return; }
182         te.hidden = false;
183         xe.hidden = true;
184     } else {
185         if (te.hidden) { return; }
186         te.hidden = true;
187         xe.hidden = false;
188     }
189
190     if (te.hidden) {
191         // get the marcxml from the text box
192         var xml_string = new MARC.Record({
193             marcbreaker : $('text-editor-box').value,
194             delimiter : '$'
195         }).toXmlString();
196
197         // reset the xml record and rerender it
198         xml_record = new XML( xml_string );
199         if (xml_record..record[0]) xml_record = xml_record..record[0];
200         loadRecord();
201     } else {
202         var xml_string = xml_record.toXMLString();
203
204         // push the xml record into the textbox
205         var rec = new MARC.Record ({ delimiter : '$', marcxml : xml_string });
206         $('text-editor-box').value = rec.toBreaker();
207     }
208 }
209
210 function my_init() {
211     try {
212
213         if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
214         JSAN.errorLevel = "die"; // none, warn, or die
215         JSAN.addRepository('/xul/server/');
216
217         dojo.require('openils.AuthorityControlSet');
218         acs = new openils.AuthorityControlSet ();
219
220         // Fake xulG for standalone...
221         try {
222             window.xulG.record;
223         } catch (e) {
224             window.xulG = {};
225             window.xulG.record = {};
226             window.xulG.save = {};
227             window.xulG.marc_control_number_identifier = 'CONS';
228
229             window.xulG.save.label = $('catStrings').getString('staff.cat.marcedit.save.label');
230             window.xulG.save.func = function (r) { alert(r); }
231
232             var cgi = new CGI();
233             var _rid = cgi.param('record');
234             if (_rid) {
235                 window.xulG.record.id = _rid;
236                 window.xulG.record.url = '/opac/extras/supercat/retrieve/marcxml/record/' + _rid;
237             }
238         }
239
240         // End faking part...
241
242         /* Check for an explicitly passed record type
243          * This is not the same as the fixed-field record type; we can't trust
244          * the fixed fields when making modifications to the attributes for a
245          * given record (in particular, config.bib_source only applies for bib
246          * records, but an auth or MFHD record with the same ID and bad fixed
247          * fields could trample the config.bib_source value for the
248          * corresponding bib record if we're not careful.
249          *
250          * are = authority record
251          * sre = serial record (MFHD)
252          * bre = bibliographic record
253          */
254         if (!window.xulG.record.rtype) {
255             var cgi = new CGI();
256             window.xulG.record.rtype = cgi.param('rtype') || false;
257         }
258
259         document.getElementById('save-button').setAttribute('label', window.xulG.save.label);
260         /* Ugh. Sorry about the spaghetti. */
261         document.getElementById('save-button').setAttribute('oncommand',
262             'var to_save = function() { ' + /* begin to_save() */
263             'if ($("xul-editor").hidden) set_flat_editor(false); ' +
264             'mangle_005(); ' + 
265             'var xml_string = xml_escape_unicode( xml_record.toXMLString() ); ' + 
266             'save_attempt( xml_string ); ' +
267             'loadRecord(); ' +
268             '}; ' + /* end to_save() */
269
270             'if (typeof _owPCW == "object") { ' +
271             ' for (var k in _owPCW) { ' +
272             '  if (_owPCW[k].active) { ' +
273             '    try { _owPCW[k].apply(to_save); to_save.ran = true; } ' +
274             '    catch (E) { alert("_ow_PCW[" + k + "]: " + E); } ' +
275             '    break; ' +
276             '  }' +
277             ' }' +
278             '} ' +
279             'if (!to_save.ran) to_save();'
280         );
281
282         if (window.xulG.record.url) {
283             var req =  new XMLHttpRequest();
284             req.open('POST',window.xulG.record.url,false);
285             req.send(null);
286             window.xulG.record.marc = req.responseText.replace(xmlDeclaration, '');
287         }
288
289         xml_record = new XML( window.xulG.record.marc );
290         if (xml_record..record[0]) xml_record = xml_record..record[0];
291
292         // Get the tooltip xml all async like
293         req =  new XMLHttpRequest();
294
295         // Set a default locale in case preferences fail us
296         var locale = "en-US";
297
298         // Try to get the locale from our preferences
299         try {
300             const Cc = Components.classes;
301             const Ci = Components.interfaces;
302             locale = Cc["@mozilla.org/preferences-service;1"].
303                 getService(Ci.nsIPrefBranch).
304                 getCharPref("general.useragent.locale");
305         }
306         catch (e) { }
307
308         // TODO: We should send a HEAD request to check for the existence of the desired file
309         // then fall back to the default locale if preferred locale is not necessary;
310         // however, for now we have a simplistic check:
311         //
312         // we currently have translations for only three locales; in the absence of a
313         // valid locale, default to the almighty en-US
314         if (locale != 'en-US' && locale != 'fr-CA' && locale != 'fi-FI') {
315             locale = 'en-US';
316         }
317
318         // grab the right tooltip based on MARC type
319         var tooltip_doc = 'marcedit-tooltips.xml';
320         switch (window.xulG.record.rtype) {
321             case 'bre':
322                 tooltip_doc = 'marcedit-tooltips.xml';
323                 break; 
324             case 'are':
325                 tooltip_doc = 'marcedit-tooltips-authority.xml';
326                 locale = 'en-US'; // FIXME - note TODO above; at moment only en-US has this
327                 break; 
328             case 'sre':
329                 tooltip_doc = 'marcedit-tooltips-mfhd.xml';
330                 locale = 'en-US'; // FIXME - note TODO above; at moment only en-US has this
331                 break; 
332             default: 
333                 tooltip_doc = 'marcedit-tooltips.xml';
334         }
335
336         // Get the locale-specific tooltips
337         req.open('GET','/xul/server/locale/' + locale + '/' + tooltip_doc,true);
338
339         context_menus = createComplexXULElement('popupset');
340         document.documentElement.appendChild( context_menus );
341
342         tag_menu = createMenuPopup({position : 'after_start', id : 'tags_popup'});
343         context_menus.appendChild( tag_menu );
344
345         tag_menu.appendChild(
346             createMenuitem(
347                 { label : $('catStrings').getString('staff.cat.marcedit.add_row.label'),
348                   oncommand : 
349                     'var e = document.createEvent("KeyEvents");' +
350                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,13,0);' +
351                     'current_focus.inputField.dispatchEvent(e);'
352                  }
353             )
354         );
355
356         tag_menu.appendChild(
357             createMenuitem(
358                 { label : $('catStrings').getString('staff.cat.marcedit.insert_row.label'),
359                   oncommand : 
360                     'var e = document.createEvent("KeyEvents");' +
361                     'e.initKeyEvent("keypress",1,1,null,1,0,1,0,13,0);' +
362                     'current_focus.inputField.dispatchEvent(e);'
363                  }
364             )
365         );
366
367         tag_menu.appendChild(
368             createMenuitem(
369                 { label : $('catStrings').getString('staff.cat.marcedit.remove_row.label'),
370                   oncommand : 
371                     'var e = document.createEvent("KeyEvents");' +
372                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,46,0);' +
373                     'current_focus.inputField.dispatchEvent(e);'
374                 }
375             )
376         );
377
378         tag_menu.appendChild( createComplexXULElement( 'separator' ) );
379
380         tag_menu.appendChild(
381             createMenuitem(
382                 { label : $('catStrings').getString('staff.cat.marcedit.replace_006.label'),
383                   oncommand : 
384                     'var e = document.createEvent("KeyEvents");' +
385                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,117,0);' +
386                     'current_focus.inputField.dispatchEvent(e);'
387                  }
388             )
389         );
390
391         tag_menu.appendChild(
392             createMenuitem(
393                 { label : $('catStrings').getString('staff.cat.marcedit.replace_007.label'),
394                   oncommand : 
395                     'var e = document.createEvent("KeyEvents");' +
396                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,118,0);' +
397                     'current_focus.inputField.dispatchEvent(e);'
398                 }
399             )
400         );
401
402         tag_menu.appendChild(
403             createMenuitem(
404                 { label : $('catStrings').getString('staff.cat.marcedit.replace_008.label'),
405                   oncommand : 
406                     'var e = document.createEvent("KeyEvents");' +
407                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,119,0);' +
408                     'current_focus.inputField.dispatchEvent(e);'
409                 }
410             )
411         );
412
413         tag_menu.appendChild( createComplexXULElement( 'separator' ) );
414
415         p = createComplexXULElement('popupset');
416         document.documentElement.appendChild( p );
417
418         req.onreadystatechange = function () {
419             if (req.readyState == 4) {
420                 bib_data = new XML( req.responseText.replace(xmlDeclaration, '') );
421                 genToolTips();
422             }
423         }
424         req.send(null);
425
426         loadRecord();
427
428
429         if (! xulG.fast_add_item) {
430             document.getElementById('fastItemAdd_checkbox').hidden = true;
431         }
432         document.getElementById('fastItemAdd_textboxes').hidden = document.getElementById('fastItemAdd_checkbox').hidden || !document.getElementById('fastItemAdd_checkbox').checked;
433
434         // Only show bib sources for bib records that already exist in the database
435         if (xulG.record.rtype == 'bre' && xulG.record.id) {
436             dojo.require('openils.PermaCrud');
437             var authtoken = ses();
438             // Retrieve the current record attributes
439             var bib = new openils.PermaCrud({"authtoken": authtoken}).retrieve('bre', xulG.record.id);
440
441             // Remember the current bib source of the record
442             xulG.record.bre = bib;
443
444             buildBibSourceList(authtoken, xulG.record.id);
445         }
446
447         preparePhysCharWizardContext();
448         dojo.require('MARC.FixedFields');
449         dojo.require("openils.widget.PhysCharWizard");
450
451     } catch(E) {
452         alert('FIXME, MARC Editor, my_init: ' + E);
453     }
454 }
455
456
457 function createComplexHTMLElement (e, attrs, objects, text) {
458     var l = document.createElementNS('http://www.w3.org/1999/xhtml',e);
459
460     if (attrs) {
461         for (var i in attrs) l.setAttribute(i,attrs[i]);
462     }
463
464     if (objects) {
465         for ( var i in objects ) l.appendChild( objects[i] );
466     }
467
468     if (text) {
469         l.appendChild( document.createTextNode(text) )
470     }
471
472     return l;
473 }
474
475 function createComplexXULElement (e, attrs, objects) {
476     var l = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',e);
477
478     if (attrs) {
479         for (var i in attrs) {
480             if (typeof attrs[i] == 'function') {
481                 l.addEventListener( i, attrs[i], true );
482             } else {
483                 l.setAttribute(i,attrs[i]);
484             }
485         }
486     } 
487
488     if (objects) {
489         for ( var i in objects ) l.appendChild( objects[i] );
490     }
491
492     return l;
493 }
494
495 function createDescription (attrs) {
496     return createComplexXULElement('description', attrs, Array.prototype.slice.apply(arguments, [1]) );
497 }
498
499 function createTooltip (attrs) {
500     return createComplexXULElement('tooltip', attrs, Array.prototype.slice.apply(arguments, [1]) );
501 }
502
503 function createLabel (attrs) {
504     return createComplexXULElement('label', attrs, Array.prototype.slice.apply(arguments, [1]) );
505 }
506
507 function createVbox (attrs) {
508     return createComplexXULElement('vbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
509 }
510
511 function createHbox (attrs) {
512     return createComplexXULElement('hbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
513 }
514
515 function createRow (attrs) {
516     return createComplexXULElement('row', attrs, Array.prototype.slice.apply(arguments, [1]) );
517 }
518
519 function createTextbox (attrs) {
520     return createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
521 }
522
523 function createMenu (attrs) {
524     return createComplexXULElement('menu', attrs, Array.prototype.slice.apply(arguments, [1]) );
525 }
526
527 function createMenuPopup (attrs) {
528     return createComplexXULElement('menupopup', attrs, Array.prototype.slice.apply(arguments, [1]) );
529 }
530
531 function createPopup (attrs) {
532     return createComplexXULElement('popup', attrs, Array.prototype.slice.apply(arguments, [1]) );
533 }
534
535 function createMenuitem (attrs) {
536     return createComplexXULElement('menuitem', attrs, Array.prototype.slice.apply(arguments, [1]) );
537 }
538
539 function createCheckbox (attrs) {
540     return createComplexXULElement('checkbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
541 }
542
543 // Find the next textbox that we can use for a focus point
544 // For control fields, use the first editable text box
545 // For data fields, focus on the first subfield text box
546 function setFocusToNextTag (row, direction) {
547     var keep_looking = true;
548     while (keep_looking && (direction == 'up' ? row = row.previousSibling : row = row.nextSibling)) {
549         // Is it a datafield?
550         dojo.query('hbox', row).query('hbox').query('textbox').forEach(function(node, index, arr) {
551             node.focus();
552             keep_looking = false;
553         });
554
555         // No, it's a control field; use the first textbox
556         if (keep_looking) {
557             dojo.query('textbox', row).forEach(function(node, index, arr) {
558                 node.focus();
559                 keep_looking = false;
560             });
561         }
562     }
563
564     return true;
565 }
566
567 function set_lock_on_keypress(ev) {
568     try {
569         var tabs = window.parent.parent.document.getElementById('main_tabs');
570
571         if (tabs) {
572             var idx = tabs.selectedIndex;
573             var tab = tabs.childNodes[idx];
574         }
575         //dump('keypress: isChar = ' + ev.isChar + ' char = ' + ev.char + ' charCode = ' + ev.charCode + ' key = ' + ev.key + ' keyCode = ' + ev.keyCode + '\n');
576         if (! /* NOT */(
577                 ev.altKey
578                 || ev.ctrlKey
579                 || ev.metaKey
580                 || ev.shiftKey
581                 || ev.keyCode == ev.DOM_VK_F1
582                 || ev.keyCode == ev.DOM_VK_F2
583                 || ev.keyCode == ev.DOM_VK_F3
584                 || ev.keyCode == ev.DOM_VK_F4
585                 || ev.keyCode == ev.DOM_VK_F5
586                 || ev.keyCode == ev.DOM_VK_F6
587                 || ev.keyCode == ev.DOM_VK_F7
588                 || ev.keyCode == ev.DOM_VK_F8
589                 || ev.keyCode == ev.DOM_VK_F9
590                 || ev.keyCode == ev.DOM_VK_F10
591                 || ev.keyCode == ev.DOM_VK_F11
592                 || ev.keyCode == ev.DOM_VK_F12
593                 || ev.keyCode == ev.DOM_VK_F13
594                 || ev.keyCode == ev.DOM_VK_F14
595                 || ev.keyCode == ev.DOM_VK_F15
596                 || ev.keyCode == ev.DOM_VK_F16
597                 || ev.keyCode == ev.DOM_VK_F17
598                 || ev.keyCode == ev.DOM_VK_F18
599                 || ev.keyCode == ev.DOM_VK_F19
600                 || ev.keyCode == ev.DOM_VK_F20
601                 || ev.keyCode == ev.DOM_VK_F21
602                 || ev.keyCode == ev.DOM_VK_F22
603                 || ev.keyCode == ev.DOM_VK_F23
604                 || ev.keyCode == ev.DOM_VK_F24
605         )) {
606             var params = {};
607             if (tab) {
608                 params.allow_multiple_locks = tab.marc_edit_allow_multiple_locks;
609             }
610             oils_lock_page(params);
611         }
612     } catch(E) {
613         alert(E);
614     }
615 }
616
617 function createMARCTextbox (element,attrs) {
618     var box = createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [2]) );
619     box.addEventListener(
620         'keypress',
621         set_lock_on_keypress,
622         false
623     );
624     if ($('symbol-panel')) {
625         addSymbolTrigger(box);
626     }
627     box.onkeypress = function (event) {
628         var root_node;
629         var node = element;
630         while(node = node.parent()) {
631             root_node = node;
632         }
633
634         var row = event.target;
635         while (row.tagName != 'row') row = row.parentNode;
636
637         if (element.nodeKind() == 'attribute') element[0]=box.value;
638         else element.setChildren( box.value );
639
640         if (element.localName() != 'controlfield') {
641             if ((event.charCode == 100 || event.charCode == 105) && event.ctrlKey) { // ctrl+d or ctrl+i
642
643                 var index_sf, target, move_data;
644                 if (element.localName() == 'subfield') {
645                     index_sf = element;
646                     target = event.target.parentNode;
647
648                     var start = event.target.selectionStart;
649                     var end = event.target.selectionEnd - event.target.selectionStart ?
650                             event.target.selectionEnd :
651                             event.target.value.length;
652
653                     move_data = event.target.value.substring(start,end);
654                     event.target.value = event.target.value.substring(0,start) + event.target.value.substring(end);
655                     event.target.setAttribute('size', event.target.value.length + 2);
656     
657                     element.setChildren( event.target.value );
658
659                 } else if (element.localName() == 'code') {
660                     index_sf = element.parent();
661                     target = event.target.parentNode;
662                 } else if (element.localName() == 'tag' || element.localName() == 'ind1' || element.localName() == 'ind2') {
663                     index_sf = element.parent().children()[element.parent().children().length() - 1];
664                     target = event.target.parentNode.lastChild.lastChild;
665                 }
666
667                 var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{ move_data }</subfield>;
668
669                 index_sf.parent().insertChildAfter( index_sf, sf );
670
671                 var new_sf = marcSubfield(sf);
672
673                 if (target === target.parentNode.lastChild) {
674                     target.parentNode.appendChild( new_sf );
675                 } else {
676                     target.parentNode.insertBefore( new_sf, target.nextSibling );
677                 }
678
679                 new_sf.firstChild.nextSibling.focus();
680
681                 event.preventDefault();
682                 return false;
683
684             } else if (event.keyCode == 13 || event.keyCode == 77) {
685                 if (event.ctrlKey) { // ctrl+enter
686
687                     var index;
688                     if (element.localName() == 'subfield') index = element.parent();
689                     if (element.localName() == 'code') index = element.parent().parent();
690                     if (element.localName() == 'tag') index = element.parent();
691                     if (element.localName() == 'ind1') index = element.parent();
692                     if (element.localName() == 'ind2') index = element.parent();
693
694                     var df = <datafield tag="" ind1="" ind2="" xmlns="http://www.loc.gov/MARC21/slim"><subfield code="" /></datafield>;
695
696                     if (event.shiftKey) { // ctrl+shift+enter
697                         index.parent().insertChildBefore( index, df );
698                     } else {
699                         index.parent().insertChildAfter( index, df );
700                     }
701
702                     var new_df = marcDatafield(df);
703
704                     if (row.parentNode.lastChild === row) {
705                         row.parentNode.appendChild( new_df );
706                     } else {
707                         if (event.shiftKey) { // ctrl+shift+enter
708                             row.parentNode.insertBefore( new_df, row );
709                         } else {
710                             row.parentNode.insertBefore( new_df, row.nextSibling );
711                         }
712                     }
713
714                     new_df.firstChild.focus();
715
716                     event.preventDefault();
717                     return false;
718
719                 } else if (event.shiftKey) {
720                     if (row.previousSibling.className.match('marcDatafieldRow'))
721                         row.previousSibling.firstChild.focus();
722                 } else {
723                     row.nextSibling.firstChild.focus();
724                 }
725
726             } else if (event.keyCode == 38 || event.keyCode == 40) { // up-arrow or down-arrow
727                 if (event.ctrlKey) { // CTRL key: copy the field
728                     var index;
729                     if (element.localName() == 'subfield') index = element.parent();
730                     if (element.localName() == 'code') index = element.parent().parent();
731                     if (element.localName() == 'tag') index = element.parent();
732                     if (element.localName() == 'ind1') index = element.parent();
733                     if (element.localName() == 'ind2') index = element.parent();
734
735                     var copyField = index.copy();
736
737                     if (event.keyCode == 38) { // ctrl+up-arrow
738                         index.parent().insertChildBefore( index, copyField );
739                     } else {
740                         index.parent().insertChildAfter( index, copyField );
741                     }
742
743                     var new_df = marcDatafield(copyField);
744
745                     if (row.parentNode.lastChild === row) {
746                         row.parentNode.appendChild( new_df );
747                     } else {
748                         if (event.keyCode == 38) { // ctrl+up-arrow
749                             row.parentNode.insertBefore( new_df, row );
750                         } else { // ctrl+down-arrow
751                             row.parentNode.insertBefore( new_df, row.nextSibling );
752                         }
753                     }
754
755                     new_df.firstChild.focus();
756
757                     event.preventDefault();
758
759                     return false;
760                 } else {
761                     if (event.keyCode == 38) {
762                         return setFocusToNextTag(row, 'up');
763                     }
764                     if (event.keyCode == 40) {
765                         return setFocusToNextTag(row, 'down');
766                     }
767                     return false;
768                 }
769
770             } else if (event.keyCode == 46 && event.ctrlKey) { // ctrl+del
771
772                 var index;
773                 if (element.localName() == 'subfield') index = element.parent();
774                 if (element.localName() == 'code') index = element.parent().parent();
775                 if (element.localName() == 'tag') index = element.parent();
776                 if (element.localName() == 'ind1') index = element.parent();
777                 if (element.localName() == 'ind2') index = element.parent();
778
779                 for (var i in index.parent().children()) {
780                     if (index === index.parent().children()[i]) {
781                         delete index.parent().children()[i];
782                         break;
783                     }
784                 }
785
786                 row.previousSibling.firstChild.focus();
787                 row.parentNode.removeChild(row);
788
789                 event.preventDefault();
790                 return false;
791
792             } else if (event.keyCode == 46 && event.shiftKey) { // shift+del
793
794                 var index;
795                 if (element.localName() == 'subfield') index = element;
796                 if (element.localName() == 'code') index = element.parent();
797
798                 if (index) {
799                     for (var i in index.parent().children()) {
800                         if (index === index.parent().children()[i]) {
801                             delete index.parent().children()[i];
802                             break;
803                         }
804                     }
805
806                     if (event.target.parentNode === event.target.parentNode.parentNode.lastChild) {
807                         event.target.parentNode.previousSibling.lastChild.focus();
808                     } else {
809                         event.target.parentNode.nextSibling.firstChild.nextSibling.focus();
810                     }
811
812                     event.target.parentNode.parentNode.removeChild(event.target.parentNode);
813
814                     event.preventDefault();
815                     return false;
816                 }
817             } else if (event.keyCode == 117 && event.ctrlKey) { // ctrl + F6
818                 box = null;
819                 createControlField('006','                                        ');
820                 loadRecord();
821             } else if (event.keyCode == 118 && event.ctrlKey) { // ctrl + F7
822                 box = null;
823                 createControlField('007','                                        ');
824                 loadRecord();
825             } else if (event.keyCode == 119 && event.ctrlKey) { // ctrl + F8
826                 box = null;
827                 createControlField('008', get_new_008());
828                 loadRecord();
829             }
830
831             return true;
832
833         } else { // event on a control field
834             if (event.keyCode == 38) { 
835                 return setFocusToNextTag(row, 'up'); 
836             } else if (event.keyCode == 40) { 
837                 return setFocusToNextTag(row, 'down');
838             }
839         }
840     };
841
842     box.addEventListener(
843         'keypress', 
844         function () {
845             if (element.nodeKind() == 'attribute') element[0]=box.value;
846             else element.setChildren( box.value );
847             return true;
848         },
849         false
850     );
851
852     box.addEventListener(
853         'change', 
854         function () {
855             if (element.nodeKind() == 'attribute') element[0]=box.value;
856             else element.setChildren( box.value );
857             return true;
858         },
859         false
860     );
861
862     box.addEventListener(
863         'keypress', 
864         function () {
865             if (element.nodeKind() == 'attribute') element[0]=box.value;
866             else element.setChildren( box.value );
867             return true;
868         },
869         true
870     );
871
872     // 'input' event catches the box value after the keypress
873     box.addEventListener(
874         'input', 
875         function () {
876             if (element.nodeKind() == 'attribute') element[0]=box.value;
877             else element.setChildren( box.value );
878             return true;
879         },
880         true
881     );
882
883     box.addEventListener(
884         'keyup', 
885         function () {
886             if (element.localName() == 'controlfield')
887                 eval('fillFixedFields();');
888         },
889         true
890     );
891
892     return box;
893 }
894
895 function toggleFFE () {
896     var grid = document.getElementById('leaderGrid');
897     if (grid.hidden) {
898         grid.hidden = false;
899     } else {
900         grid.hidden = true;
901     }
902     return true;
903 }
904
905 function changeFFEditor (type) {
906     var grid = document.getElementById('leaderGrid');
907     grid.setAttribute('type',type);
908     document.getElementById('recordTypeLabel').setAttribute('value',type);
909
910     // Hide FFEditor rows that we don't need for our current type
911     // If all of the labels for a given row do not include our
912     // desired type in their set attribute, we can hide that row
913     dojo.query('rows', grid).query('row').forEach(function(node, index, arr) {
914         if (dojo.query('label[set~=' + type + ']', node).length == 0) {
915             node.hidden = true;
916         }
917     });
918
919     getFFValuesForType(
920         type,
921         function() {
922             updateFFEditorContexts(grid, type);
923         },
924         function() {
925             alert(  /* XXX i18n - the marc editor either isn't tied in to
926                        an overall i18n infrastructure, or it's different
927                        enough from other Evergreen parts that I don't
928                        understand the right way to do i18n here */
929                 "failed to load fixed field values for rec_type '" +
930                 type + "'\n"
931             );
932         }
933     );
934 }
935
936 function getFFValuesForType(type, callback, errback) {
937     if (_fixed_field_values[type]) {
938         callback();
939     } else {
940         dojo.require("openils.Util");
941
942         fieldmapper.standardRequest(
943             ["open-ils.cat",
944                 "open-ils.cat.biblio.fixed_field_values.by_rec_type"], {
945                 "async": true,
946                 "params": [type],
947                 "oncomplete": function(r) {
948                     if (r = openils.Util.readResponse(r)) {
949                         _fixed_field_values[type] = r;
950                         callback();
951                     } else {
952                         errback();
953                     }
954                 },
955                 "onerror": errback
956             }
957         );
958     }
959 }
960
961 function updateFFEditorContexts(grid, type) {
962
963     /* XXX Dojo to navigate a XUL DOM.  Bad form?  We do it elsewhere in this
964      * file, but I think I've heard it scoffed at. */
965
966     var rows_node = dojo.query("rows", grid)[0];
967     dojo.query("row", rows_node).forEach(
968         function(row) {
969             dojo.query("textbox", row).forEach(
970                 function(tb) {
971                     var name = tb.getAttribute("name");
972                     if (_fixed_field_values[type][name]) {
973                         tb.setAttribute(
974                             "context", getFFContextMenu(type, name)
975                         );
976                     } else {
977                         tb.setAttribute("context", "clipboard");
978                     }
979                 }
980             );
981         }
982     );
983 }
984
985 function getFFContextMenu(type, name) {
986     if (!_fixed_field_context_menus[type]) {
987         _fixed_field_context_menus[type] = {};
988     }
989
990     var context_menu_id = "_fixed_field_context_menus_" + type + "_" + name;
991
992     if (!_fixed_field_context_menus[type][name]) {
993         var p = document.getElementsByTagName("popupset")[0];
994         var m = document.createElement("menupopup");
995         m.setAttribute("id", context_menu_id);
996         _fixed_field_values[type][name].forEach(
997             function(v) {
998                 var mi = document.createElement("menuitem");
999                 var funcname = "_ff_anon_" +
1000                     String(_fixed_field_anonymous_func_counter++);
1001
1002                 /* These anon functions linger and take up memory, but due
1003                  * to caching there's a limit on how much, and that limit is
1004                  * a function of the size of the dataset defined in the
1005                  * ccvm/cmfpm tables. */
1006                 var code = v[0];
1007                 var len = v[2];
1008                 var def = v[3];
1009                 var el = document.getElementById(name + "_tb");
1010                 window[funcname] = function() {
1011                     el.value = code;
1012                     while (el.value.length < len) {
1013                         el.value += def;
1014                     }
1015                     updateFixedFields(el);
1016                     oils_lock_page();
1017                 };
1018
1019                 /* In XUL land we can't set an element's
1020                  * attribute like oncommand to a code reference. It has to be
1021                  * an actual string to be eval'd. */
1022                 mi.setAttribute("oncommand", funcname + "()");
1023                 mi.setAttribute("label", v[0] + ": " + v[1]); /* XXX i18n ? */
1024                 m.appendChild(mi);
1025             }
1026         );
1027         _fixed_field_context_menus[type][name] = m;
1028         p.appendChild(m);
1029     }
1030
1031     return context_menu_id;
1032 }
1033
1034 /* This just sets up a special context menu for a 007 data field to use, so
1035  * that users can right-click for a menu and get a choice to launch the
1036  * Physical Characteristics Wizard.
1037  */
1038 function preparePhysCharWizardContext() {
1039     var menu = document.getElementById("physCharWizardContext");
1040     menu.appendChild(document.createElement("menuseparator"));
1041
1042     var clipb_children = document.getElementById("clipboard").childNodes;
1043     for (var i = 0; i < clipb_children.length; i++) /* collection not array */ {
1044         var child = clipb_children[i];
1045         if (child.nodeName == 'menuitem')
1046             menu.appendChild(child.cloneNode(true));
1047     }
1048 }
1049
1050 function launchPhysCharWizard(popup_node) {
1051     try {
1052         new openils.widget.PhysCharWizard({
1053             "node": popup_node,
1054             "onapply": function(v) {
1055                 createControlField("007", v);
1056                 oils_lock_page();
1057                 loadRecord();
1058             }
1059         });
1060     } catch (E) {
1061         alert("Exception raised by openils.widget.PhysCharWizard:\n" + E);
1062     }
1063 }
1064
1065 function fillFixedFields () {
1066     try {
1067             var grid = document.getElementById('leaderGrid');
1068             var marc_rec = new MARC.Record ({ delimiter : '$', marcxml : xml_record.toXMLString() });
1069
1070             var list = [];
1071             var pre_list = grid.getElementsByTagName('label');
1072             for (var i in pre_list) {
1073                 if ( pre_list[i].getAttribute && pre_list[i].getAttribute('set').indexOf(grid.getAttribute('type')) > -1 ) {
1074                     list.push( pre_list[i] );
1075                 }
1076             }
1077
1078             for (var i in list) {
1079                 var name = list[i].getAttribute('name');
1080                 var value = marc_rec.extractFixedField(name, true);
1081
1082                 if (value === null) continue;
1083
1084                 list[i].nextSibling.value = value;
1085             }
1086
1087             return true;
1088     } catch(E) {
1089         alert('FIXME, MARC Editor, fillFixedFields: ' + E);
1090     }
1091 }
1092
1093 function updateFixedFields (element) {
1094     var new_value = element.value;
1095
1096     var marc_rec = new MARC.Record ({ delimiter : '$', marcxml : xml_record.toXMLString() });
1097     marc_rec.setFixedField(element.getAttribute('name'), new_value);
1098
1099     var xml_string = marc_rec.toXmlString();
1100     xml_record = new XML( xml_string );
1101     if (xml_record..record[0]) xml_record = xml_record..record[0];
1102     loadRecord();
1103     // Put the cursor back to the current fixed field
1104     element.select();
1105
1106     return true;
1107 }
1108
1109 function marcLeader (leader) {
1110     var row = createRow(
1111         { class : 'marcLeaderRow',
1112           tag : 'ldr' },
1113         createLabel(
1114             { value : 'LDR',
1115               class : 'marcTag',
1116               tooltiptext : $('catStrings').getString('staff.cat.marcedit.marcTag.LDR.label') } ),
1117         createLabel(
1118             { value : '',
1119               class : 'marcInd1' } ),
1120         createLabel(
1121             { value : '',
1122               class : 'marcInd2' } ),
1123         createLabel(
1124             { value : leader.text(),
1125               class : 'marcLeader' } )
1126     );
1127
1128     return row;
1129 }
1130
1131 function marcControlfield (field) {
1132     tagname = field.@tag.toString().substr(2);
1133     var row;
1134     if (tagname == '1' || tagname == '3' || tagname == '6' || tagname == '7' || tagname == '8') {
1135         row = createRow(
1136             { class : 'marcControlfieldRow',
1137               tag : '_' + tagname },
1138             createLabel(
1139                 { value : field.@tag,
1140                   class : 'marcTag',
1141                   context : 'tags_popup',
1142                   onmouseover : 'getTooltip(this, "tag");',
1143                   tooltipid : 'tag' + field.@tag } ),
1144             createLabel(
1145                 { value : field.@ind1,
1146                   class : 'marcInd1',
1147                   onmouseover : 'getTooltip(this, "ind1");',
1148                   tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
1149             createLabel(
1150                 { value : field.@ind2,
1151                   class : 'marcInd2',
1152                   onmouseover : 'getTooltip(this, "ind2");',
1153                   tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
1154             createMARCTextbox(
1155                 field,
1156                 { value : field.text(),
1157                   class : 'plain marcEditableControlfield',
1158                   name : 'CONTROL' + tagname,
1159                   context : tagname == 7 ? 'physCharWizardContext': 'clipboard',
1160                   size : 50,
1161                   maxlength : 50 } )
1162             );
1163     } else {
1164         row = createRow(
1165             { class : 'marcControlfieldRow',
1166               tag : '_' + tagname },
1167             createLabel(
1168                 { value : field.@tag,
1169                   class : 'marcTag',
1170                   onmouseover : 'getTooltip(this, "tag");',
1171                   tooltipid : 'tag' + field.@tag } ),
1172             createLabel(
1173                 { value : field.@ind1,
1174                   class : 'marcInd1',
1175                   onmouseover : 'getTooltip(this, "ind1");',
1176                   tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
1177             createLabel(
1178                 { value : field.@ind2,
1179                   class : 'marcInd2',
1180                   onmouseover : 'getTooltip(this, "ind2");',
1181                   tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
1182             createLabel(
1183                 { value : field.text(),
1184                   class : 'marcControlfield' } )
1185         );
1186     }
1187
1188     return row;
1189 }
1190
1191 function stackSubfields(checkbox) {
1192     var list = document.getElementsByAttribute('name','sf_box');
1193
1194     var o = 'vertical';
1195     if (!checkbox.checked) o = 'horizontal';
1196     
1197     for (var i = 0; i < list.length; i++) {
1198         if (list[i]) list[i].setAttribute('orient',o);
1199     }
1200 }
1201
1202 function fastItemAdd_toggle(checkbox) {
1203     var x = document.getElementById('fastItemAdd_textboxes');
1204     if (checkbox.checked) {
1205         x.hidden = false;
1206         document.getElementById('fastItemAdd_callnumber').focus();
1207         document.getElementById('fastItemAdd_callnumber').select();
1208     } else {
1209         x.hidden = true;
1210     }
1211 }
1212
1213 function fastItemAdd_attempt(doc_id) {
1214     try {
1215         if (typeof window.xulG.fast_add_item != 'function') { return; }
1216         if (!document.getElementById('fastItemAdd_checkbox').checked) { return; }
1217         if (!document.getElementById('fastItemAdd_callnumber').value) { return; }
1218         if (!document.getElementById('fastItemAdd_barcode').value) { return; }
1219         window.xulG.fast_add_item( doc_id, document.getElementById('fastItemAdd_callnumber').value, document.getElementById('fastItemAdd_barcode').value );
1220         document.getElementById('fastItemAdd_barcode').value = '';
1221         return true;
1222     } catch(E) {
1223         alert('fastItemAdd_attempt: ' + E);
1224     }
1225 }
1226
1227 function save_attempt(xml_string) {
1228     try {
1229         var tabs = window.parent.parent.document.getElementById('main_tabs');
1230
1231         if (tabs) {
1232             var idx = tabs.selectedIndex;
1233             var tab = tabs.childNodes[idx];
1234         }
1235         var result = window.xulG.save.func( xml_string );
1236         // I'd prefer to pass on_complete on through to fast_item_add,
1237         // but with the way these window scopes get destroyed with
1238         // tab replacement, maybe not a good idea
1239         var replace_on_complete = false;
1240         if (result) {
1241             if (tab) {
1242                 tab.marc_edit_changed = false;
1243             }
1244             oils_unlock_page();
1245             oils_unlock_page(); /* kludge for LP1491875 */
1246             if (result.id) {
1247                 replace_on_complete = fastItemAdd_attempt(result.id);
1248             }
1249         // When fastItemAdd is not used, replace_on_complete is undefined
1250         // When it is used, and successful, replace_on_complete will be set to true 
1251         // then we need it to execute on_complete()
1252             if ((replace_on_complete==undefined || replace_on_complete ) && typeof result.on_complete == 'function') {
1253                 result.on_complete();
1254             }
1255         }
1256     } catch(E) {
1257         alert('save_attempt: ' + E);
1258     }
1259 }
1260
1261 function marcDatafield (field) {
1262     var row = createRow(
1263         { class : 'marcDatafieldRow' },
1264         createMARCTextbox(
1265             field.@tag,
1266             { value : field.@tag,
1267               class : 'plain marcTag',
1268               name : 'marcTag',
1269               context : 'tags_popup',
1270               oninput : 'if (this.value.length == 3) { this.nextSibling.focus(); }',
1271               size : 3,
1272               maxlength : 3,
1273               onmouseover : 'current_focus = this; getTooltip(this, "tag");' } ),
1274         createMARCTextbox(
1275             field.@ind1,
1276             { value : field.@ind1,
1277               class : 'plain marcInd1',
1278               name : 'marcInd1',
1279               oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
1280               size : 1,
1281               maxlength : 1,
1282               onmouseover : 'current_focus = this; getContextMenu(this, "ind1"); getTooltip(this, "ind1");',
1283               oncontextmenu : 'getContextMenu(this, "ind1");' } ),
1284         createMARCTextbox(
1285             field.@ind2,
1286             { value : field.@ind2,
1287               class : 'plain marcInd2',
1288               name : 'marcInd2',
1289               oninput : 'if (this.value.length == 1) { this.nextSibling.firstChild.firstChild.focus(); }',
1290               size : 1,
1291               maxlength : 1,
1292               onmouseover : 'current_focus = this; getContextMenu(this, "ind2"); getTooltip(this, "ind2");',
1293               oncontextmenu : 'getContextMenu(this, "ind2");' } ),
1294         createHbox({ name : 'sf_box' })
1295     );
1296
1297     if (!current_focus && field.@tag == '') current_focus = row.childNodes[0];
1298     if (!current_focus && field.@ind1 == '') current_focus = row.childNodes[1];
1299     if (!current_focus && field.@ind2 == '') current_focus = row.childNodes[2];
1300
1301     var sf_box = row.lastChild;
1302     if (document.getElementById('stackSubfields').checked)
1303         sf_box.setAttribute('orient','vertical');
1304
1305     sf_box.addEventListener(
1306         'click',
1307         function (e) {
1308             if (sf_box === e.target) {
1309                 sf_box.lastChild.lastChild.focus();
1310             } else if (e.target.parentNode === sf_box) {
1311                 e.target.lastChild.focus();
1312             }
1313         },
1314         false
1315     );
1316
1317
1318     for (var i in field.subfield) {
1319         var sf = field.subfield[i];
1320         sf_box.appendChild(
1321             marcSubfield(sf)
1322         );
1323
1324         dojo.query('.marcSubfield', sf_box).forEach(wrap_long_fields);
1325
1326         if (sf.@code == '' && (!current_focus || current_focus.className.match(/Ind/)))
1327             current_focus = sf_box.lastChild.childNodes[1];
1328     }
1329
1330     return row;
1331 }
1332
1333 function marcSubfield (sf) {            
1334     return createHbox(
1335         { class : 'marcSubfieldBox' },
1336         createLabel(
1337             { value : "\u2021",
1338               class : 'plain marcSubfieldDelimiter',
1339               onmouseover : 'getTooltip(this.nextSibling, "subfield");',
1340               oncontextmenu : 'getContextMenu(this.nextSibling, "subfield");',
1341                 //onclick : 'this.nextSibling.focus();',
1342                 onfocus : 'this.nextSibling.focus();',
1343               size : 2 } ),
1344         createMARCTextbox(
1345             sf.@code,
1346             { value : sf.@code,
1347               class : 'plain marcSubfieldCode',
1348               align: 'start',
1349               name : 'marcSubfieldCode',
1350               onmouseover : 'current_focus = this; getContextMenu(this, "subfield"); getTooltip(this, "subfield");',
1351               oncontextmenu : 'getContextMenu(this, "subfield");',
1352               oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
1353               size : 2,
1354               maxlength : 1 } ),
1355         createMARCTextbox(
1356             sf,
1357             { value : sf.text(),
1358               name : sf.parent().@tag + ':' + sf.@code,
1359               class : 'plain marcSubfield', 
1360               align: 'start',
1361               onmouseover : 'getTooltip(this, "subfield");',
1362               contextmenu : function (event) { getAuthorityContextMenu(event.target, sf) },
1363               size : new String(sf.text()).length + 2,
1364               oninput : "this.setAttribute('size', this.value.length + 2);"
1365             } )
1366     );
1367 }
1368
1369 function loadRecord() {
1370     try {
1371             var tabs = window.parent.parent.document.getElementById('main_tabs');
1372
1373             if (tabs) {
1374                 var idx = tabs.selectedIndex;
1375                 var tab = tabs.childNodes[idx];
1376                 if (tab) {
1377                     tab.marc_edit_changed = false;
1378                     tab.marc_edit_allow_multiple_locks = true;
1379                 }
1380             }
1381
1382             var grid_rows = document.getElementById('recGrid').lastChild;
1383
1384             while (grid_rows.firstChild) grid_rows.removeChild(grid_rows.firstChild);
1385
1386             grid_rows.appendChild( marcLeader( xml_record.leader ) );
1387
1388             for (var i in xml_record.controlfield) {
1389                 grid_rows.appendChild( marcControlfield( xml_record.controlfield[i] ) );
1390             }
1391
1392             for (var i in xml_record.datafield) {
1393                 grid_rows.appendChild( marcDatafield( xml_record.datafield[i] ) );
1394             }
1395
1396             grid_rows.getElementsByAttribute('class','marcDatafieldRow')[0].firstChild.focus();
1397
1398             var marc_rec = new MARC.Record ({ delimiter : '$', marcxml : xml_record.toXMLString() });
1399             changeFFEditor(marc_rec.recordType());
1400             fillFixedFields();
1401     } catch(E) {
1402         alert('FIXME, MARC Editor, loadRecord: ' + E);
1403     }
1404 }
1405
1406
1407 function genToolTips () {
1408     for (var i in bib_data.field) {
1409         var f = bib_data.field[i];
1410     
1411         tag_menu.appendChild(
1412             createMenuitem(
1413                 { label : f.@tag,
1414                   oncommand : 
1415                       'current_focus.value = "' + f.@tag + '";' +
1416                     'var e = document.createEvent("MutationEvents");' +
1417                     'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1418                     'current_focus.inputField.dispatchEvent(e);',
1419                   disabled : f.@tag < '010' ? "true" : "false",
1420                   tooltiptext : f.description }
1421             )
1422         );
1423     
1424         var i1_popup = createMenuPopup({position : 'after_start', id : 't' + f.@tag + 'i1' });
1425         context_menus.appendChild( i1_popup );
1426     
1427         var i2_popup = createMenuPopup({position : 'after_start', id : 't' + f.@tag + 'i2' });
1428         context_menus.appendChild( i2_popup );
1429     
1430         var sf_popup = createMenuPopup({position : 'after_start', id : 't' + f.@tag + 'sf' });
1431         context_menus.appendChild( sf_popup );
1432     
1433         tooltip_hash['tag' + f.@tag] = f.description;
1434         for (var j in f.indicator) {
1435             var ind = f.indicator[j];
1436             tooltip_hash['tag' + f.@tag + 'ind' + ind.@position + 'val' + ind.@value] = ind.description;
1437     
1438             if (ind.@position == 1) {
1439                 i1_popup.appendChild(
1440                     createMenuitem(
1441                         { label : ind.@value,
1442                           oncommand : 
1443                               'current_focus.value = "' + ind.@value + '";' +
1444                             'var e = document.createEvent("MutationEvents");' +
1445                             'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1446                             'current_focus.inputField.dispatchEvent(e);',
1447                           tooltiptext : ind.description }
1448                     )
1449                 );
1450             }
1451     
1452             if (ind.@position == 2) {
1453                 i2_popup.appendChild(
1454                     createMenuitem(
1455                         { label : ind.@value,
1456                           oncommand : 
1457                               'current_focus.value = "' + ind.@value + '";' +
1458                             'var e = document.createEvent("MutationEvents");' +
1459                             'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1460                             'current_focus.inputField.dispatchEvent(e);',
1461                           tooltiptext : ind.description }
1462                     )
1463                 );
1464             }
1465         }
1466     
1467         for (var j in f.subfield) {
1468             var sf = f.subfield[j];
1469             tooltip_hash['tag' + f.@tag + 'sf' + sf.@code] = sf.description;
1470     
1471             sf_popup.appendChild(
1472                 createMenuitem(
1473                     { label : sf.@code,
1474                       oncommand : 
1475                           'current_focus.value = "' + sf.@code + '";' +
1476                         'var e = document.createEvent("MutationEvents");' +
1477                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1478                         'current_focus.inputField.dispatchEvent(e);',
1479                       tooltiptext : sf.description
1480                     }
1481                 )
1482             );
1483         }
1484     }
1485 }
1486
1487 function getTooltip (target, type) {
1488
1489     var tt = '';
1490     if (type == 'subfield')
1491         tt = 'tag' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf' + target.parentNode.childNodes[1].value;
1492
1493     if (type == 'ind1')
1494         tt = 'tag' + target.parentNode.firstChild.value + 'ind1val' + target.value;
1495
1496     if (type == 'ind2')
1497         tt = 'tag' + target.parentNode.firstChild.value + 'ind2val' + target.value;
1498
1499     if (type == 'tag')
1500         tt = 'tag' + target.parentNode.firstChild.value;
1501
1502     if (!document.getElementById( tt )) {
1503         p.appendChild(
1504             createTooltip(
1505                 { id : tt,
1506                   flex : "1",
1507                   orient : 'vertical',
1508                   onpopupshown : 'this.width = this.firstChild.boxObject.width + 10; this.height = this.firstChild.boxObject.height + 10;',
1509                   class : 'tooltip' },
1510                 createDescription({}, document.createTextNode( tooltip_hash[tt] ) )
1511             )
1512         );
1513     }
1514
1515     target.tooltip = tt;
1516     return true;
1517 }
1518
1519 function getContextMenu (target, type) {
1520
1521     var tt = '';
1522     if (type == 'subfield')
1523         tt = 't' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf';
1524
1525     if (type == 'ind1')
1526         tt = 't' + target.parentNode.firstChild.value + 'i1';
1527
1528     if (type == 'ind2')
1529         tt = 't' + target.parentNode.firstChild.value + 'i2';
1530
1531     target.setAttribute('context', tt);
1532     return true;
1533 }
1534
1535 var control_map = {
1536     100 : {
1537         'a' : { 100 : 'a' },
1538         'd' : { 100 : 'd' },
1539         'e' : { 100 : 'e' },
1540         'q' : { 100 : 'q' }
1541     },
1542     110 : {
1543         'a' : { 110 : 'a' },
1544         'd' : { 110 : 'd' }
1545     },
1546     111 : {
1547         'a' : { 111 : 'a' },
1548         'd' : { 111 : 'd' }
1549     },
1550     130 : {
1551         'a' : { 130 : 'a' },
1552         'd' : { 130 : 'd' }
1553     },
1554     240 : {
1555         'a' : { 130 : 'a' },
1556         'd' : { 130 : 'd' }
1557     },
1558     400 : {
1559         'a' : { 100 : 'a' },
1560         'd' : { 100 : 'd' }
1561     },
1562     410 : {
1563         'a' : { 110 : 'a' },
1564         'd' : { 110 : 'd' }
1565     },
1566     411 : {
1567         'a' : { 111 : 'a' },
1568         'd' : { 111 : 'd' }
1569     },
1570     440 : {
1571         'a' : { 130 : 'a' },
1572         'n' : { 130 : 'n' },
1573         'p' : { 130 : 'p' }
1574     },
1575     700 : {
1576         'a' : { 100 : 'a' },
1577         'd' : { 100 : 'd' },
1578         'q' : { 100 : 'q' },
1579         't' : { 100 : 't' }
1580     },
1581     710 : {
1582         'a' : { 110 : 'a' },
1583         'd' : { 110 : 'd' }
1584     },
1585     711 : {
1586         'a' : { 111 : 'a' },
1587         'c' : { 111 : 'c' },
1588         'd' : { 111 : 'd' }
1589     },
1590     730 : {
1591         'a' : { 130 : 'a' },
1592         'd' : { 130 : 'd' }
1593     },
1594     800 : {
1595         'a' : { 100 : 'a' },
1596         'd' : { 100 : 'd' }
1597     },
1598     810 : {
1599         'a' : { 110 : 'a' },
1600         'd' : { 110 : 'd' }
1601     },
1602     811 : {
1603         'a' : { 111 : 'a' },
1604         'd' : { 111 : 'd' }
1605     },
1606     830 : {
1607         'a' : { 130 : 'a' },
1608         'd' : { 130 : 'd' }
1609     },
1610     600 : {
1611         'a' : { 100 : 'a' },
1612         'd' : { 100 : 'd' },
1613         'q' : { 100 : 'q' },
1614         't' : { 100 : 't' },
1615         'v' : { 180 : 'v',
1616             100 : 'v',
1617             181 : 'v',
1618             182 : 'v',
1619             185 : 'v'
1620         },
1621         'x' : { 180 : 'x',
1622             100 : 'x',
1623             181 : 'x',
1624             182 : 'x',
1625             185 : 'x'
1626         },
1627         'y' : { 180 : 'y',
1628             100 : 'y',
1629             181 : 'y',
1630             182 : 'y',
1631             185 : 'y'
1632         },
1633         'z' : { 180 : 'z',
1634             100 : 'z',
1635             181 : 'z',
1636             182 : 'z',
1637             185 : 'z'
1638         }
1639     },
1640     610 : {
1641         'a' : { 110 : 'a' },
1642         'd' : { 110 : 'd' },
1643         't' : { 110 : 't' },
1644         'v' : { 180 : 'v',
1645             110 : 'v',
1646             181 : 'v',
1647             182 : 'v',
1648             185 : 'v'
1649         },
1650         'x' : { 180 : 'x',
1651             110 : 'x',
1652             181 : 'x',
1653             182 : 'x',
1654             185 : 'x'
1655         },
1656         'y' : { 180 : 'y',
1657             110 : 'y',
1658             181 : 'y',
1659             182 : 'y',
1660             185 : 'y'
1661         },
1662         'z' : { 180 : 'z',
1663             110 : 'z',
1664             181 : 'z',
1665             182 : 'z',
1666             185 : 'z'
1667         }
1668     },
1669     611 : {
1670         'a' : { 111 : 'a' },
1671         'd' : { 111 : 'd' },
1672         't' : { 111 : 't' },
1673         'v' : { 180 : 'v',
1674             111 : 'v',
1675             181 : 'v',
1676             182 : 'v',
1677             185 : 'v'
1678         },
1679         'x' : { 180 : 'x',
1680             111 : 'x',
1681             181 : 'x',
1682             182 : 'x',
1683             185 : 'x'
1684         },
1685         'y' : { 180 : 'y',
1686             111 : 'y',
1687             181 : 'y',
1688             182 : 'y',
1689             185 : 'y'
1690         },
1691         'z' : { 180 : 'z',
1692             111 : 'z',
1693             181 : 'z',
1694             182 : 'z',
1695             185 : 'z'
1696         }
1697     },
1698     630 : {
1699         'a' : { 130 : 'a' },
1700         'd' : { 130 : 'd' }
1701     },
1702     648 : {
1703         'a' : { 148 : 'a' },
1704         'v' : { 148 : 'v' },
1705         'x' : { 148 : 'x' },
1706         'y' : { 148 : 'y' },
1707         'z' : { 148 : 'z' }
1708     },
1709     650 : {
1710         'a' : { 150 : 'a' },
1711         'b' : { 150 : 'b' },
1712         'v' : { 180 : 'v',
1713             150 : 'v',
1714             181 : 'v',
1715             182 : 'v',
1716             185 : 'v'
1717         },
1718         'x' : { 180 : 'x',
1719             150 : 'x',
1720             181 : 'x',
1721             182 : 'x',
1722             185 : 'x'
1723         },
1724         'y' : { 180 : 'y',
1725             150 : 'y',
1726             181 : 'y',
1727             182 : 'y',
1728             185 : 'y'
1729         },
1730         'z' : { 180 : 'z',
1731             150 : 'z',
1732             181 : 'z',
1733             182 : 'z',
1734             185 : 'z'
1735         }
1736     },
1737     651 : {
1738         'a' : { 151 : 'a' },
1739         'v' : { 180 : 'v',
1740             151 : 'v',
1741             181 : 'v',
1742             182 : 'v',
1743             185 : 'v'
1744         },
1745         'x' : { 180 : 'x',
1746             151 : 'x',
1747             181 : 'x',
1748             182 : 'x',
1749             185 : 'x'
1750         },
1751         'y' : { 180 : 'y',
1752             151 : 'y',
1753             181 : 'y',
1754             182 : 'y',
1755             185 : 'y'
1756         },
1757         'z' : { 180 : 'z',
1758             151 : 'z',
1759             181 : 'z',
1760             182 : 'z',
1761             185 : 'z'
1762         }
1763     },
1764     655 : {
1765         'a' : { 155 : 'a' },
1766         'v' : { 180 : 'v',
1767             155 : 'v',
1768             181 : 'v',
1769             182 : 'v',
1770             185 : 'v'
1771         },
1772         'x' : { 180 : 'x',
1773             155 : 'x',
1774             181 : 'x',
1775             182 : 'x',
1776             185 : 'x'
1777         },
1778         'y' : { 180 : 'y',
1779             155 : 'y',
1780             181 : 'y',
1781             182 : 'y',
1782             185 : 'y'
1783         },
1784         'z' : { 180 : 'z',
1785             155 : 'z',
1786             181 : 'z',
1787             182 : 'z',
1788             185 : 'z'
1789         }
1790     }
1791 };
1792
1793 function getAuthorityContextMenu (target, sf) {
1794     var menu_id = sf.parent().@tag + ':' + sf.@code + '-authority-context-' + sf;
1795
1796     var page = 0;
1797     var old = dojo.byId( menu_id );
1798     if (old) {
1799         page = auth_pages[menu_id];
1800         old.parentNode.removeChild(old);
1801     } else {
1802         auth_pages[menu_id] = 0;
1803     }
1804
1805     var sf_popup = createMenuPopup({ id : menu_id, flex : 1 });
1806
1807     sf_popup.addEventListener("popuphiding", function(event) {
1808         if (show_auth_menu) {
1809             show_auth_menu = false;
1810             getAuthorityContextMenu(target, sf);
1811             dojo.byId(menu_id).openPopup();
1812         }  
1813     }, false);
1814
1815     context_menus.appendChild( sf_popup );
1816
1817     var found_acs = [];
1818     dojo.forEach( acs.controlSetList(), function (acs_id) {
1819         if (acs.controlSet(acs_id).control_map[sf.parent().@tag]) found_acs.push(acs_id);
1820     });
1821
1822     if (!found_acs.length) {
1823         sf_popup.appendChild(createLabel( { value : $('catStrings').getString('staff.cat.marcedit.not_authority_field.label') } ) );
1824         target.setAttribute('context', 'clipboard');
1825         return false;
1826     }
1827
1828     if (sf.toString().replace(/\s*/, '')) {
1829         return browseAuthority(sf_popup, menu_id, target, sf, 20, page);
1830     }
1831
1832     return true;
1833 }
1834
1835 /* Apply the complete 1xx */
1836 function applyFullAuthority ( target, ui_sf, e4x_sf ) {
1837     var new_vals = dojo.query('*[tag^="1"]', target);
1838     return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1839 }
1840
1841 function applySelectedAuthority ( target, ui_sf, e4x_sf ) {
1842     var new_vals = target.getElementsByAttribute('checked','true');
1843     return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1844 }
1845
1846 function applyAuthority ( target, ui_sf, e4x_sf, new_vals ) {
1847     var field = e4x_sf.parent();
1848
1849     for (var i = 0; i < new_vals.length; i++) {
1850
1851         var sf_list = field.subfield;
1852         for (var j in sf_list) {
1853
1854             if (sf_list[j].@code == new_vals[i].getAttribute('subfield')) {
1855                 sf_list[j] = new_vals[i].getAttribute('value');
1856                 new_vals[i].setAttribute('subfield','');
1857                 break;
1858             }
1859         }
1860     }
1861
1862     for (var i = 0; i < new_vals.length; i++) {
1863
1864         /* indicators for the authority datafield are carried over in the main entry linking subfield */
1865         if (new_vals[i].getAttribute('subfield') == '0') {
1866             field.@ind1 = new_vals[i].getAttribute('ind1');
1867             field.@ind2 = new_vals[i].getAttribute('ind2');
1868         }
1869
1870         if (!new_vals[i].getAttribute('subfield')) continue;
1871
1872         var val = new_vals[i].getAttribute('value');
1873
1874         var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{val}</subfield>;
1875         sf.@code = new_vals[i].getAttribute('subfield');
1876
1877         field.insertChildAfter(field.subfield[field.subfield.length() - 1], sf);
1878     }
1879
1880     var row = marcDatafield( field );
1881
1882     var node = ui_sf;
1883     while (node.nodeName != 'row') {
1884         node = node.parentNode;
1885     }
1886
1887     node.parentNode.replaceChild( row, node );
1888     return true;
1889 }
1890
1891 function validateAuthority (button) {
1892     var grid = document.getElementById('recGrid');
1893     var label = button.getAttribute('label');
1894
1895     //loop over rows
1896     var rows = grid.lastChild.childNodes;
1897     for (var i = 0; i < rows.length; i++) {
1898         var row = rows[i];
1899         var tag = row.firstChild;
1900
1901         var done = false;
1902         dojo.forEach(acs.controlSetList(), function (acs_id) {
1903             if (done) return;
1904             var control_map = acs.controlSet(acs_id).control_map;
1905     
1906             if (!control_map[tag.value]) return;
1907             button.setAttribute('label', label + ' - ' + tag.value);
1908     
1909             var ind1 = tag.nextSibling;
1910             var ind2 = ind1.nextSibling;
1911             var subfields = ind2.nextSibling.childNodes;
1912     
1913             var sf_list = [];
1914             for (var j = 0; j < subfields.length; j++) {
1915                 var sf = subfields[j];
1916                 sf_list.push( [ sf.childNodes[1].value, sf.childNodes[2].value ] );
1917             }
1918
1919             var matches = acs.findMatchingAuthorities(
1920                 new MARC.Field({
1921                     'tag'       : tag.value,
1922                     'subfields' : sf_list
1923                 })
1924             );
1925
1926             // matches = [ { "$csetId" : [ ... ] } ]
1927
1928             var found = false;
1929             if (matches[0]) { // probably set
1930                 for (var cset in matches[0]) {
1931                     var arr = matches[0][cset];
1932                     if (arr.length) {
1933                         // protect against errant empty string values
1934                         if (arr.length == 1 && arr[0] == '')
1935                             continue;
1936                         found = true;
1937                         break;
1938                     }
1939                 }
1940             }
1941
1942     
1943             // XXX If adt, etc should be validated separately from vxz, etc then move this up into the above for loop
1944             for (var j = 0; j < subfields.length; j++) {
1945                 var sf = subfields[j];
1946                 if (!found) {
1947                     dojo.removeClass(sf.childNodes[2], 'marcValidated');
1948                     dojo.addClass(sf.childNodes[2], 'marcUnvalidated');
1949                 } else {
1950                     dojo.removeClass(sf.childNodes[2], 'marcUnvalidated');
1951                     dojo.addClass(sf.childNodes[2], 'marcValidated');
1952                 }
1953             }
1954
1955             if (found) done = true;
1956         });
1957     }
1958
1959     button.setAttribute('label', label);
1960
1961     return true;
1962 }
1963
1964
1965 /*
1966 function validateBibField (tags, searches) {
1967     var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.validate.tag";
1968     url += '&param="tags"&param=' + js2JSON(tags);
1969     url += '&param="searches"&param=' + js2JSON(searches);
1970
1971
1972     var req = new XMLHttpRequest();
1973     req.open('GET',url,false);
1974     req.send(null);
1975
1976     return req;
1977
1978 }
1979 */
1980
1981 function searchAuthority (term, tag, sf, limit) {
1982     var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.fts";
1983     url += '&param="term"&param="' + term + '"';
1984     url += '&param="limit"&param=' + limit;
1985     url += '&param="tag"&param=' + tag;
1986     url += '&param="subfield"&param="' + sf + '"';
1987
1988
1989     var req = new XMLHttpRequest();
1990     req.open('GET',url,false);
1991     req.send(null);
1992
1993     return req;
1994
1995 }
1996
1997 /* TODO new authority browse support for context sets, and use that here */
1998 function browseAuthority (sf_popup, menu_id, target, sf, limit, page) {
1999     dojo.require('dojox.xml.parser');
2000
2001     var target_tag = sf.parent().@tag.toString();
2002
2003     var found_acs = [];
2004     dojo.forEach( acs.controlSetList(), function (acs_id) {
2005         if (acs.controlSet(acs_id).control_map[target_tag]) found_acs.push(acs_id);
2006     });
2007
2008     var cmap;
2009     if (!found_acs.length) {
2010         target.setAttribute('context', 'clipboard');
2011         return false;
2012     } else {
2013         cmap = acs.controlSet(found_acs[0]).control_map;
2014     }
2015
2016     // map tag + subfield to the appropriate authority browse axis:
2017     // currently authority.author, authority.subject, authority.title, authority.topic
2018     // based on mappings in OpenILS::Application::SuperCat, though Authority Control
2019     // Sets will change that
2020
2021     var axis_list = acs.bibFieldBrowseAxes( target_tag );
2022
2023     // No matching tag means no authorities to search - shortcut
2024     if (axis_list.length == 0) {
2025         target.setAttribute('context', 'clipboard');
2026         return false;
2027     }
2028
2029     var type = 'authority.' + axis_list[0]; // Just take the first for now
2030                                             // TODO support multiple axes ... loop?
2031     if (!limit) {
2032         limit = 10;
2033     }
2034
2035     if (!page) {
2036         page = 0;
2037     }
2038
2039     var sf_string = '';
2040     var sf_list = sf.parent().subfield;
2041     for ( var i in sf_list) {
2042         if (!cmap[target_tag][sf_list[i].@code.toString()]) {
2043             continue; // This is not a controlled subfield, such as $0
2044         }
2045         sf_string += sf_list[i].toString() + ' ';
2046         if (sf_list[i] === sf) break;
2047     }
2048
2049     var url = '/opac/extras/browse/marcxml/'
2050         + type + '.refs'
2051         + '/1' // OU - currently unscoped
2052         + '/' + sf_string
2053         + '/' + page
2054         + '/' + limit
2055     ;
2056
2057     // would be good to carve this out into a separate function
2058     dojo.xhrGet({"url":url, "sync": true, "preventCache": true, "handleAs":"xml", "load": function(records) {
2059         var create_menu = createMenu({ label: $('catStrings').getString('staff.cat.marcedit.create_authority.label')});
2060
2061         var cm_popup = create_menu.appendChild(
2062             createMenuPopup()
2063         );
2064
2065         cm_popup.appendChild(
2066             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_now.label'),
2067                 command : function() { 
2068                     // Call middle-layer function to create and save the new authority
2069                     var source_f = summarizeField(sf);
2070                     var new_auth = fieldmapper.standardRequest(
2071                         ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib"],
2072                         [source_f, xulG.marc_control_number_identifier, ses()]
2073                     );
2074                     if (new_auth && new_auth.id()) {
2075                         addNewAuthorityID(new_auth, sf, target);
2076                     }
2077                 }
2078             })
2079         );
2080
2081         cm_popup.appendChild(
2082             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_edit.label'),
2083                 command : function() { 
2084                     // Generate the new authority by calling the new middle-layer
2085                     // function (a non-saving variant), then display in another
2086                     // MARC editor
2087                     var source_f = summarizeField(sf);
2088                     var authtoken = ses();
2089                     dojo.require('openils.PermaCrud');
2090                     var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2091                     var rec = fieldmapper.standardRequest(
2092                         ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib.readonly"],
2093                         { "params": [source_f, xulG.marc_control_number_identifier] }
2094                     );
2095                     loadMarcEditor(pcrud, rec, target, sf);
2096                 }
2097             })
2098         );
2099
2100         sf_popup.appendChild(create_menu);
2101         sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2102
2103         // append "Previous page" results browser
2104         sf_popup.appendChild(
2105             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.previous_page.label'),
2106                 command : function(event) { 
2107                     auth_pages[menu_id] -= 1;
2108                     show_auth_menu = true;
2109                 }
2110             })
2111         );
2112         sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2113
2114         dojo.query('record', records).forEach(function(record) {
2115             var main_text = '';
2116             var see_from = [];
2117             var see_also = [];
2118             var auth_id = dojox.xml.parser.textContent(dojo.query('datafield[tag="901"]', record).query('subfield[code="c"]')[0]);
2119             var auth_org = '';
2120             if (dojo.query('controlfield[tag="003"]', record).length > 0) {
2121                 auth_org = dojox.xml.parser.textContent(dojo.query('controlfield[tag="003"]', record)[0]);
2122             }
2123
2124             // Grab the fields with tags beginning with 1 (main entries) and iterate through the subfields
2125             dojo.query('datafield[tag^="1"]', record).forEach(function(field) {
2126                 dojo.query('subfield', field).forEach(function(subfield) {
2127                     if (main_text) {
2128                         main_text += ' / ';
2129                     }
2130                     main_text += dojox.xml.parser.textContent(subfield);
2131                 });
2132             });
2133
2134             // Grab the fields with tags beginning with 4 (see from entries) and iterate through the subfields
2135             dojo.query('datafield[tag^="4"]', record).forEach(function(field) {
2136                 var see_text = '';
2137                 dojo.query('subfield', field).forEach(function(subfield) {
2138                     if (see_text) {
2139                         see_text += ' / ';
2140                     }
2141                     see_text += dojox.xml.parser.textContent(subfield);
2142                 });
2143                 see_from.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_from', [see_text]));
2144             });
2145
2146             // Grab the fields with tags beginning with 5 (see also entries) and iterate through the subfields
2147             dojo.query('datafield[tag^="5"]', record).forEach(function(field) {
2148                 var see_text = '';
2149                 dojo.query('subfield', field).forEach(function(subfield) {
2150                     if (see_text) {
2151                         see_text += ' / ';
2152                     }
2153                     see_text += dojox.xml.parser.textContent(subfield);
2154                 });
2155                 see_also.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_also', [see_text]));
2156             });
2157
2158             buildAuthorityPopup(main_text, record, auth_org, auth_id, sf_popup, target, sf);
2159
2160             dojo.forEach(see_from, function(entry_text) {
2161                 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2162             });
2163
2164             // To-do: instead of launching the standard selector menu, invoke
2165             // a new authority search using the 5XX entry text
2166             dojo.forEach(see_also, function(entry_text) {
2167                 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2168             });
2169
2170         });
2171
2172         if (sf_popup.childNodes.length == 0) {
2173             sf_popup.appendChild(createLabel( { value : $('catStrings').getString('staff.cat.marcedit.no_authority_match.label') } ) );
2174         } else {
2175             // append "Next page" results browser
2176             sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2177             sf_popup.appendChild(
2178                 createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.next_page.label'),
2179                     command : function(event) { 
2180                         auth_pages[menu_id] += 1;
2181                         show_auth_menu = true;
2182                     }
2183                 })
2184             );
2185         }
2186
2187         target.setAttribute('context', menu_id);
2188         return true;
2189     }});
2190
2191 }
2192
2193 function buildAuthorityPopup (entry_text, record, auth_org, auth_id, sf_popup, target, sf, style) {
2194     var grid = dojo.query('[name="authority-marc-template"]')[0].cloneNode(true);
2195     grid.setAttribute('name','-none-');
2196     grid.setAttribute('style','overflow:scroll');
2197
2198     var submenu = createMenu( { "label": entry_text } );
2199
2200     var popup = createMenuPopup({ "flex": "1" });
2201     if (style) {
2202         submenu.setAttribute('style', style);
2203         popup.setAttribute('style', 'font-style: normal; margin-left: 0em;');
2204     }
2205     submenu.appendChild(popup);
2206
2207     dojo.query('datafield[tag^="1"]', record).forEach(function(field) {
2208         buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2209     });
2210     dojo.query('datafield[tag^="4"]', record).forEach(function(field) {
2211         buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2212     });
2213     dojo.query('datafield[tag^="5"]', record).forEach(function(field) {
2214         buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2215     });
2216
2217     grid.hidden = false;
2218     popup.appendChild( grid );
2219
2220     popup.appendChild(
2221         createMenuitem(
2222             { label : $('catStrings').getString('staff.cat.marcedit.apply_selected.label'),
2223               command : function (event) {
2224                     applySelectedAuthority(event.target.previousSibling, target, sf);
2225                     return true;
2226               }
2227             }
2228         )
2229     );
2230
2231     popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2232
2233     popup.appendChild(
2234         createMenuitem(
2235             { label : $('catStrings').getString('staff.cat.marcedit.apply_full.label'),
2236               command : function (event) {
2237                     applyFullAuthority(event.target.previousSibling.previousSibling.previousSibling, target, sf);
2238                     return true;
2239               }
2240             }
2241         )
2242     );
2243
2244     sf_popup.appendChild( submenu );
2245 }
2246
2247 function buildAuthorityPopupSelector (field, grid, auth_org, auth_id) {
2248     var row = createRow(
2249         { },
2250         createLabel( { "value" : dojo.attr(field, 'tag') } ),
2251         createLabel( { "value" : dojo.attr(field, 'ind1') } ),
2252         createLabel( { "value" : dojo.attr(field, 'ind2') } )
2253     );
2254
2255     var sf_box = createHbox();
2256     dojo.query('subfield', field).forEach(function(subfield) {
2257         sf_box.appendChild(
2258             createCheckbox(
2259                 { "label"    : '\u2021' + dojo.attr(subfield, 'code') + ' ' + dojox.xml.parser.textContent(subfield),
2260                   "subfield" : dojo.attr(subfield, 'code'),
2261                   "tag"      : dojo.attr(field, 'tag'),
2262                   "value"    : dojox.xml.parser.textContent(subfield)
2263                 }
2264             )
2265         );
2266         row.appendChild(sf_box);
2267     });
2268
2269     // Append the authority linking subfield only for main entries
2270     if (dojo.attr(field, 'tag').charAt(0) == '1') {
2271         sf_box.appendChild(
2272             createCheckbox(
2273                 { "label"    : '\u2021' + '0' + ' (' + auth_org + ')' + auth_id,
2274                   "subfield" : '0',
2275                   "tag"      : dojo.attr(field, 'tag'),
2276                   "ind1"     : dojo.attr(field, 'ind1'),
2277                   "ind2"     : dojo.attr(field, 'ind2'),
2278                   "value"    : '(' + auth_org + ')' + auth_id
2279                 }
2280             )
2281         );
2282     }
2283     row.appendChild(sf_box);
2284
2285     grid.lastChild.appendChild(row);
2286 }
2287
2288 function summarizeField(sf) {
2289     var source_f= {
2290         "tag": '',
2291         "ind1": '',
2292         "ind2": '',
2293         "subfields": []
2294     };
2295
2296     source_f.tag = sf.parent().@tag.toString();
2297     source_f.ind1 = sf.parent().@ind1.toString();
2298     source_f.ind2 = sf.parent().@ind2.toString();
2299
2300     var found_acs = [];
2301     dojo.forEach( acs.controlSetList(), function (acs_id) {
2302         if (acs.controlSet(acs_id).control_map[sf.parent().@tag]) found_acs.push(acs_id);
2303     });
2304
2305     var cmap;
2306     if (!found_acs.length) {
2307         return false;
2308     } else {
2309         cmap = acs.controlSet(found_acs[0]).control_map;
2310     }
2311
2312     for (var i = 0; i < sf.parent().subfield.length(); i++) {
2313         var sf_iter = sf.parent().subfield[i];
2314
2315         /* Filter out subfields that are not controlled for this tag */
2316         if (!cmap[source_f.tag][sf_iter.@code.toString()]) {
2317             continue;
2318         }
2319
2320         source_f.subfields.push([sf_iter.@code.toString(), sf_iter.toString()]);
2321     }
2322
2323     return source_f;
2324 }
2325
2326 function buildBibSourceList (authtoken, recId) {
2327     /* TODO: Work out how to set the bib source of the bre that does not yet
2328      * exist - this is specifically in the case of Z39.50 imports. Right now
2329      * we just avoid populating and showing the config.bib_source list
2330      */
2331     if (!recId) {
2332         return false;
2333     }
2334
2335     var bib = xulG.record.bre;
2336
2337     dojo.require('openils.PermaCrud');
2338
2339     // cbsList = the XUL menulist that contains the available bib sources 
2340     var cbsList = dojo.byId('bib-source-list');
2341
2342     // bibSources = an array containing all of the bib source objects
2343     var bibSources = new openils.PermaCrud({"authtoken": authtoken}).retrieveAll('cbs');
2344
2345     // A tad ugly, but gives us the index of the bib source ID in cbsList
2346     var x = 0;
2347     var cbsListArr = [];
2348     dojo.forEach(bibSources, function (item) {
2349         cbsList.appendItem(item.source(), item.id());
2350         cbsListArr[item.id()] = x;
2351         x++;
2352     });
2353
2354     // Show the current value of the bib source for this record
2355     cbsList.selectedIndex = cbsListArr[bib.source()];
2356
2357     // Display the bib source selection widget
2358     dojo.byId('bib-source-list-caption').hidden = false;
2359     dojo.byId('bib-source-list').hidden = false;
2360     dojo.byId('bib-source-list-button').disabled = true;
2361     dojo.byId('bib-source-list-button').hidden = false;
2362 }
2363
2364 // Fired when the "Update Source" button is clicked
2365 // Updates the value of the bib source for the current record
2366 function updateBibSource() {
2367     var authtoken = ses();
2368     var cbs = dojo.byId('bib-source-list').selectedItem.value;
2369     var recId = xulG.record.id;
2370     var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2371     var bib = pcrud.retrieve('bre', recId);
2372     if (bib.source() != cbs) {
2373         bib.source(cbs);
2374         bib.ischanged = true;
2375         pcrud.update(bib);
2376     }
2377 }
2378
2379 function onBibSourceSelect() {
2380     var cbs = dojo.byId('bib-source-list').selectedItem.value;
2381     var bib = xulG.record.bre;
2382     if (bib.source() != cbs) {
2383         dojo.byId('bib-source-list-button').disabled = false;   
2384     } else {
2385         dojo.byId('bib-source-list-button').disabled = true;   
2386     }
2387 }
2388
2389 function addNewAuthorityID(authority, sf, target) {
2390     var id_sf = <subfield code="0" xmlns="http://www.loc.gov/MARC21/slim">({xulG.marc_control_number_identifier}){authority.id()}</subfield>;
2391     sf.parent().appendChild(id_sf);
2392     var new_sf = marcSubfield(id_sf);
2393
2394     var node = target;
2395     while (dojo.attr(node, 'name') != 'sf_box') {
2396         node = node.parentNode;
2397     }
2398     node.appendChild( new_sf );
2399
2400     alert($('catStrings').getString('staff.cat.marcedit.create_authority_success.label'));
2401 }
2402
2403 function loadMarcEditor(pcrud, marcxml, target, sf) {
2404     /*
2405        To run in Firefox directly, must set signed.applets.codebase_principal_support
2406        to true in about:config
2407      */
2408     win = window.open('/xul/server/cat/marcedit.xul', '_blank', 'chrome'); // XXX version?
2409
2410     // Match marc2are.pl last_xact_id format, roughly
2411     var now = new Date;
2412     var xact_id = 'IMPORT-' + Date.parse(now);
2413     
2414     win.xulG = {
2415         "record": {"marc": marcxml, "rtype": "are"},
2416         "save": {
2417             "label": $('catStrings').getString('staff.cat.marcedit.save.label'),
2418             "func": function(xmlString) {
2419                 var rec = new are();
2420                 rec.marc(xmlString);
2421                 rec.last_xact_id(xact_id);
2422                 rec.isnew(true);
2423                 pcrud.create(rec, {
2424                     "oncomplete": function (r, objs) {
2425                         var new_rec = objs[0];
2426                         if (!new_rec) {
2427                             return '';
2428                         }
2429
2430                         addNewAuthorityID(new_rec, sf, target);
2431
2432                         win.close();
2433                     }
2434                 });
2435             }
2436         }
2437     };
2438 }
2439
2440