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