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