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