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