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