]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/marcedit.js
Merge branch 'master' of ssh://senator@yeti.esilibrary.com/home/evergreen/evergreen...
[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 function mangle_005() {
46     var now = new Date();
47     var y = now.getUTCFullYear();
48
49     var m = now.getUTCMonth() + 1;
50     if (m < 10) m = '0' + m;
51     
52     var d = now.getUTCDate();
53     if (d < 10) d = '0' + d;
54     
55     var H = now.getUTCHours();
56     if (H < 10) H = '0' + H;
57     
58     var M = now.getUTCMinutes();
59     if (M < 10) M = '0' + M;
60     
61     var S = now.getUTCSeconds();
62     if (S < 10) S = '0' + S;
63     
64
65     var stamp = '' + y + m + d + H + M + S + '.0';
66     createControlField('005',stamp);
67
68 }
69
70 function createControlField (tag,data) {
71     // first, remove the old field, if any;
72     for (var i in xml_record.controlfield.(@tag == tag)) delete xml_record.controlfield.(@tag == tag)[i];
73
74     var cf = <controlfield tag="" xmlns="http://www.loc.gov/MARC21/slim">{ data }</controlfield>;
75     cf.@tag = tag;
76
77     // then, find the right position and insert it
78     var done = 0;
79     var cfields = xml_record.controlfield;
80     var base = Number(tag.substring(2));
81     for (var i in cfields) {
82         var t = Number(cfields[i].@tag.toString().substring(2));
83         if (t > base) {
84             xml_record.insertChildBefore( cfields[i], cf );
85             done = 1
86             break;
87         }
88     }
89
90     if (!done) xml_record.insertChildBefore( xml_record.datafield[0], cf );
91
92     return cf;
93 }
94
95 function xml_escape_unicode ( str ) {
96     return str.replace(
97         /([\u0080-\ufffe])/g,
98         function (r,s) { return "&#x" + s.charCodeAt(0).toString(16) + ";"; }
99     );
100 }
101
102 function wrap_long_fields (node) {
103     var text_size = dojo.attr(node, 'size');
104     var hard_width = 100; 
105     if (text_size > hard_width) {
106         dojo.attr(node, 'multiline', 'true');
107         dojo.attr(node, 'cols', hard_width);
108         var text_rows = (text_size / hard_width) + 1;
109         dojo.attr(node, 'rows', text_rows);
110     }
111 }
112
113 function set_flat_editor (useFlatText) {
114
115     dojo.require('MARC.Record');
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         loadRecord(xml_record);
140     } else {
141         var xml_string = xml_record.toXMLString();
142
143         // push the xml record into the textbox
144         var rec = new MARC.Record ({ delimiter : '$', marcxml : xml_string });
145         $('text-editor-box').value = rec.toBreaker();
146     }
147 }
148
149 function my_init() {
150     try {
151
152         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
153         if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
154         JSAN.errorLevel = "die"; // none, warn, or die
155         JSAN.addRepository('/xul/server/');
156
157         // Fake xulG for standalone...
158         try {
159             window.xulG.record;
160         } catch (e) {
161             window.xulG = {};
162             window.xulG.record = {};
163             window.xulG.save = {};
164             window.xulG.marc_control_number_identifier = 'CONS';
165
166             window.xulG.save.label = $('catStrings').getString('staff.cat.marcedit.save.label');
167             window.xulG.save.func = function (r) { alert(r); }
168
169             var cgi = new CGI();
170             var _rid = cgi.param('record');
171             if (_rid) {
172                 window.xulG.record.id = _rid;
173                 window.xulG.record.url = '/opac/extras/supercat/retrieve/marcxml/record/' + _rid;
174             }
175         }
176
177         // End faking part...
178
179         /* Check for an explicitly passed record type
180          * This is not the same as the fixed-field record type; we can't trust
181          * the fixed fields when making modifications to the attributes for a
182          * given record (in particular, config.bib_source only applies for bib
183          * records, but an auth or MFHD record with the same ID and bad fixed
184          * fields could trample the config.bib_source value for the
185          * corresponding bib record if we're not careful.
186          *
187          * are = authority record
188          * sre = serial record (MFHD)
189          * bre = bibliographic record
190          */
191         if (!window.xulG.record.rtype) {
192             var cgi = new CGI();
193             window.xulG.record.rtype = cgi.param('rtype') || false;
194         }
195
196         document.getElementById('save-button').setAttribute('label', window.xulG.save.label);
197         document.getElementById('save-button').setAttribute('oncommand',
198             'if ($("xul-editor").hidden) set_flat_editor(false); ' +
199             'mangle_005(); ' + 
200             'var xml_string = xml_escape_unicode( xml_record.toXMLString() ); ' + 
201             'save_attempt( xml_string ); ' +
202             'loadRecord(xml_record);'
203         );
204
205         if (window.xulG.record.url) {
206             var req =  new XMLHttpRequest();
207             req.open('POST',window.xulG.record.url,false);
208             req.send(null);
209             window.xulG.record.marc = req.responseText.replace(xmlDeclaration, '');
210         }
211
212         xml_record = new XML( window.xulG.record.marc );
213         if (xml_record..record[0]) xml_record = xml_record..record[0];
214
215         // Get the tooltip xml all async like
216         req =  new XMLHttpRequest();
217
218         // Set a default locale in case preferences fail us
219         var locale = "en-US";
220
221         // Try to get the locale from our preferences
222         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
223         try {
224             const Cc = Components.classes;
225             const Ci = Components.interfaces;
226             locale = Cc["@mozilla.org/preferences-service;1"].
227                 getService(Ci.nsIPrefBranch).
228                 getCharPref("general.useragent.locale");
229         }
230         catch (e) { }
231
232         // TODO: We should send a HEAD request to check for the existence of the desired file
233         // then fall back to the default locale if preferred locale is not necessary;
234         // however, for now we have a simplistic check:
235         //
236         // we currently have translations for only two locales; in the absence of a
237         // valid locale, default to the almighty en-US
238         if (locale != 'en-US' && locale != 'fr-CA') {
239             locale = 'en-US';
240         }
241
242         // grab the right tooltip based on MARC type
243         var tooltip_doc = 'marcedit-tooltips.xml';
244         switch (window.xulG.record.rtype) {
245             case 'bre':
246                 tooltip_doc = 'marcedit-tooltips.xml';
247                 break; 
248             case 'are':
249                 tooltip_doc = 'marcedit-tooltips-authority.xml';
250                 locale = 'en-US'; // FIXME - note TODO above; at moment only en-US has this
251                 break; 
252             case 'sre':
253                 tooltip_doc = 'marcedit-tooltips-mfhd.xml';
254                 locale = 'en-US'; // FIXME - note TODO above; at moment only en-US has this
255                 break; 
256             default: 
257                 tooltip_doc = 'marcedit-tooltips.xml';
258         }
259
260         // Get the locale-specific tooltips
261         req.open('GET','/xul/server/locale/' + locale + '/' + tooltip_doc,true);
262
263         context_menus = createComplexXULElement('popupset');
264         document.documentElement.appendChild( context_menus );
265
266         tag_menu = createPopup({position : 'after_start', id : 'tags_popup'});
267         context_menus.appendChild( tag_menu );
268
269         tag_menu.appendChild(
270             createMenuitem(
271                 { label : $('catStrings').getString('staff.cat.marcedit.add_row.label'),
272                   oncommand : 
273                     'var e = document.createEvent("KeyEvents");' +
274                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,13,0);' +
275                     'current_focus.inputField.dispatchEvent(e);'
276                  }
277             )
278         );
279
280         tag_menu.appendChild(
281             createMenuitem(
282                 { label : $('catStrings').getString('staff.cat.marcedit.insert_row.label'),
283                   oncommand : 
284                     'var e = document.createEvent("KeyEvents");' +
285                     'e.initKeyEvent("keypress",1,1,null,1,0,1,0,13,0);' +
286                     'current_focus.inputField.dispatchEvent(e);'
287                  }
288             )
289         );
290
291         tag_menu.appendChild(
292             createMenuitem(
293                 { label : $('catStrings').getString('staff.cat.marcedit.remove_row.label'),
294                   oncommand : 
295                     'var e = document.createEvent("KeyEvents");' +
296                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,46,0);' +
297                     'current_focus.inputField.dispatchEvent(e);'
298                 }
299             )
300         );
301
302         tag_menu.appendChild( createComplexXULElement( 'separator' ) );
303
304         tag_menu.appendChild(
305             createMenuitem(
306                 { label : $('catStrings').getString('staff.cat.marcedit.replace_006.label'),
307                   oncommand : 
308                     'var e = document.createEvent("KeyEvents");' +
309                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,64,0);' +
310                     'current_focus.inputField.dispatchEvent(e);'
311                  }
312             )
313         );
314
315         tag_menu.appendChild(
316             createMenuitem(
317                 { label : $('catStrings').getString('staff.cat.marcedit.replace_007.label'),
318                   oncommand : 
319                     'var e = document.createEvent("KeyEvents");' +
320                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,65,0);' +
321                     'current_focus.inputField.dispatchEvent(e);'
322                 }
323             )
324         );
325
326         tag_menu.appendChild(
327             createMenuitem(
328                 { label : $('catStrings').getString('staff.cat.marcedit.replace_008.label'),
329                   oncommand : 
330                     'var e = document.createEvent("KeyEvents");' +
331                     'e.initKeyEvent("keypress",1,1,null,1,0,0,0,66,0);' +
332                     'current_focus.inputField.dispatchEvent(e);'
333                 }
334             )
335         );
336
337         tag_menu.appendChild( createComplexXULElement( 'separator' ) );
338
339         p = createComplexXULElement('popupset');
340         document.documentElement.appendChild( p );
341
342         req.onreadystatechange = function () {
343             if (req.readyState == 4) {
344                 bib_data = new XML( req.responseText.replace(xmlDeclaration, '') );
345                 genToolTips();
346             }
347         }
348         req.send(null);
349
350         loadRecord(xml_record);
351
352         if (! xulG.fast_add_item) {
353             document.getElementById('fastItemAdd_checkbox').hidden = true;
354         }
355         document.getElementById('fastItemAdd_textboxes').hidden = document.getElementById('fastItemAdd_checkbox').hidden || !document.getElementById('fastItemAdd_checkbox').checked;
356
357         // Only show bib sources for bib records that already exist in the database
358         if (xulG.record.rtype == 'bre' && xulG.record.id) {
359             dojo.require('openils.PermaCrud');
360             var authtoken = ses();
361             // Retrieve the current record attributes
362             var bib = new openils.PermaCrud({"authtoken": authtoken}).retrieve('bre', xulG.record.id);
363
364             // Remember the current bib source of the record
365             xulG.record.bre = bib;
366
367             buildBibSourceList(authtoken, xulG.record.id);
368         }
369
370     } catch(E) {
371         alert('FIXME, MARC Editor, my_init: ' + E);
372     }
373 }
374
375
376 function createComplexHTMLElement (e, attrs, objects, text) {
377     var l = document.createElementNS('http://www.w3.org/1999/xhtml',e);
378
379     if (attrs) {
380         for (var i in attrs) l.setAttribute(i,attrs[i]);
381     }
382
383     if (objects) {
384         for ( var i in objects ) l.appendChild( objects[i] );
385     }
386
387     if (text) {
388         l.appendChild( document.createTextNode(text) )
389     }
390
391     return l;
392 }
393
394 function createComplexXULElement (e, attrs, objects) {
395     var l = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',e);
396
397     if (attrs) {
398         for (var i in attrs) {
399             if (typeof attrs[i] == 'function') {
400                 l.addEventListener( i, attrs[i], true );
401             } else {
402                 l.setAttribute(i,attrs[i]);
403             }
404         }
405     } 
406
407     if (objects) {
408         for ( var i in objects ) l.appendChild( objects[i] );
409     }
410
411     return l;
412 }
413
414 function createDescription (attrs) {
415     return createComplexXULElement('description', attrs, Array.prototype.slice.apply(arguments, [1]) );
416 }
417
418 function createTooltip (attrs) {
419     return createComplexXULElement('tooltip', attrs, Array.prototype.slice.apply(arguments, [1]) );
420 }
421
422 function createLabel (attrs) {
423     return createComplexXULElement('label', attrs, Array.prototype.slice.apply(arguments, [1]) );
424 }
425
426 function createVbox (attrs) {
427     return createComplexXULElement('vbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
428 }
429
430 function createHbox (attrs) {
431     return createComplexXULElement('hbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
432 }
433
434 function createRow (attrs) {
435     return createComplexXULElement('row', attrs, Array.prototype.slice.apply(arguments, [1]) );
436 }
437
438 function createTextbox (attrs) {
439     return createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
440 }
441
442 function createMenu (attrs) {
443     return createComplexXULElement('menu', attrs, Array.prototype.slice.apply(arguments, [1]) );
444 }
445
446 function createMenuPopup (attrs) {
447     return createComplexXULElement('menupopup', attrs, Array.prototype.slice.apply(arguments, [1]) );
448 }
449
450 function createPopup (attrs) {
451     return createComplexXULElement('popup', attrs, Array.prototype.slice.apply(arguments, [1]) );
452 }
453
454 function createMenuitem (attrs) {
455     return createComplexXULElement('menuitem', attrs, Array.prototype.slice.apply(arguments, [1]) );
456 }
457
458 function createCheckbox (attrs) {
459     return createComplexXULElement('checkbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
460 }
461
462 // Find the next textbox that we can use for a focus point
463 // For control fields, use the first editable text box
464 // For data fields, focus on the first subfield text box
465 function setFocusToNextTag (row, direction) {
466     var keep_looking = true;
467     while (keep_looking && (direction == 'up' ? row = row.previousSibling : row = row.nextSibling)) {
468         // Is it a datafield?
469         dojo.query('hbox hbox textbox', row).forEach(function(node, index, arr) {
470             node.focus();
471             keep_looking = false;
472         });
473
474         // No, it's a control field; use the first textbox
475         if (keep_looking) {
476             dojo.query('textbox', row).forEach(function(node, index, arr) {
477                 node.focus();
478                 keep_looking = false;
479             });
480         }
481     }
482
483     return true;
484 }
485
486
487 function createMARCTextbox (element,attrs) {
488
489     var box = createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [2]) );
490     box.addEventListener('keypress',function(ev) { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); if (!(ev.altKey || ev.ctrlKey || ev.metaKey)) { oils_lock_page(); } },false);
491     box.onkeypress = function (event) {
492         var root_node;
493         var node = element;
494         while(node = node.parent()) {
495             root_node = node;
496         }
497
498         var row = event.target;
499         while (row.tagName != 'row') row = row.parentNode;
500
501         if (element.nodeKind() == 'attribute') element[0]=box.value;
502         else element.setChildren( box.value );
503
504         if (element.localName() != 'controlfield') {
505             if ((event.charCode == 100 || event.charCode == 105) && event.ctrlKey) { // ctrl+d or ctrl+i
506
507                 var index_sf, target, move_data;
508                 if (element.localName() == 'subfield') {
509                     index_sf = element;
510                     target = event.target.parentNode;
511
512                     var start = event.target.selectionStart;
513                     var end = event.target.selectionEnd - event.target.selectionStart ?
514                             event.target.selectionEnd :
515                             event.target.value.length;
516
517                     move_data = event.target.value.substring(start,end);
518                     event.target.value = event.target.value.substring(0,start) + event.target.value.substring(end);
519                     event.target.setAttribute('size', event.target.value.length + 2);
520     
521                     element.setChildren( event.target.value );
522
523                 } else if (element.localName() == 'code') {
524                     index_sf = element.parent();
525                     target = event.target.parentNode;
526                 } else if (element.localName() == 'tag' || element.localName() == 'ind1' || element.localName() == 'ind2') {
527                     index_sf = element.parent().children()[element.parent().children().length() - 1];
528                     target = event.target.parentNode.lastChild.lastChild;
529                 }
530
531                 var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{ move_data }</subfield>;
532
533                 index_sf.parent().insertChildAfter( index_sf, sf );
534
535                 var new_sf = marcSubfield(sf);
536
537                 if (target === target.parentNode.lastChild) {
538                     target.parentNode.appendChild( new_sf );
539                 } else {
540                     target.parentNode.insertBefore( new_sf, target.nextSibling );
541                 }
542
543                 new_sf.firstChild.nextSibling.focus();
544
545                 event.preventDefault();
546                 return false;
547
548             } else if (event.keyCode == 13 || event.keyCode == 77) {
549                 if (event.ctrlKey) { // ctrl+enter
550
551                     var index;
552                     if (element.localName() == 'subfield') index = element.parent();
553                     if (element.localName() == 'code') index = element.parent().parent();
554                     if (element.localName() == 'tag') index = element.parent();
555                     if (element.localName() == 'ind1') index = element.parent();
556                     if (element.localName() == 'ind2') index = element.parent();
557
558                     var df = <datafield tag="" ind1="" ind2="" xmlns="http://www.loc.gov/MARC21/slim"><subfield code="" /></datafield>;
559
560                     if (event.shiftKey) { // ctrl+shift+enter
561                         index.parent().insertChildBefore( index, df );
562                     } else {
563                         index.parent().insertChildAfter( index, df );
564                     }
565
566                     var new_df = marcDatafield(df);
567
568                     if (row.parentNode.lastChild === row) {
569                         row.parentNode.appendChild( new_df );
570                     } else {
571                         if (event.shiftKey) { // ctrl+shift+enter
572                             row.parentNode.insertBefore( new_df, row );
573                         } else {
574                             row.parentNode.insertBefore( new_df, row.nextSibling );
575                         }
576                     }
577
578                     new_df.firstChild.focus();
579
580                     event.preventDefault();
581                     return false;
582
583                 } else if (event.shiftKey) {
584                     if (row.previousSibling.className.match('marcDatafieldRow'))
585                         row.previousSibling.firstChild.focus();
586                 } else {
587                     row.nextSibling.firstChild.focus();
588                 }
589
590             } else if (event.keyCode == 38 || event.keyCode == 40) { // up-arrow or down-arrow
591                 if (event.ctrlKey) { // CTRL key: copy the field
592                     var index;
593                     if (element.localName() == 'subfield') index = element.parent();
594                     if (element.localName() == 'code') index = element.parent().parent();
595                     if (element.localName() == 'tag') index = element.parent();
596                     if (element.localName() == 'ind1') index = element.parent();
597                     if (element.localName() == 'ind2') index = element.parent();
598
599                     var copyField = index.copy();
600
601                     if (event.keyCode == 38) { // ctrl+up-arrow
602                         index.parent().insertChildBefore( index, copyField );
603                     } else {
604                         index.parent().insertChildAfter( index, copyField );
605                     }
606
607                     var new_df = marcDatafield(copyField);
608
609                     if (row.parentNode.lastChild === row) {
610                         row.parentNode.appendChild( new_df );
611                     } else {
612                         if (event.keyCode == 38) { // ctrl+up-arrow
613                             row.parentNode.insertBefore( new_df, row );
614                         } else { // ctrl+down-arrow
615                             row.parentNode.insertBefore( new_df, row.nextSibling );
616                         }
617                     }
618
619                     new_df.firstChild.focus();
620
621                     event.preventDefault();
622
623                     return false;
624                 } else {
625                     if (event.keyCode == 38) {
626                         return setFocusToNextTag(row, 'up');
627                     }
628                     if (event.keyCode == 40) {
629                         return setFocusToNextTag(row, 'down');
630                     }
631                     return false;
632                 }
633
634             } else if (event.keyCode == 46 && event.ctrlKey) { // ctrl+del
635
636                 var index;
637                 if (element.localName() == 'subfield') index = element.parent();
638                 if (element.localName() == 'code') index = element.parent().parent();
639                 if (element.localName() == 'tag') index = element.parent();
640                 if (element.localName() == 'ind1') index = element.parent();
641                 if (element.localName() == 'ind2') index = element.parent();
642
643                 for (var i in index.parent().children()) {
644                     if (index === index.parent().children()[i]) {
645                         delete index.parent().children()[i];
646                         break;
647                     }
648                 }
649
650                 row.previousSibling.firstChild.focus();
651                 row.parentNode.removeChild(row);
652
653                 event.preventDefault();
654                 return false;
655
656             } else if (event.keyCode == 46 && event.shiftKey) { // shift+del
657
658                 var index;
659                 if (element.localName() == 'subfield') index = element;
660                 if (element.localName() == 'code') index = element.parent();
661
662                 if (index) {
663                     for (var i in index.parent().children()) {
664                         if (index === index.parent().children()[i]) {
665                             delete index.parent().children()[i];
666                             break;
667                         }
668                     }
669
670                     if (event.target.parentNode === event.target.parentNode.parentNode.lastChild) {
671                         event.target.parentNode.previousSibling.lastChild.focus();
672                     } else {
673                         event.target.parentNode.nextSibling.firstChild.nextSibling.focus();
674                     }
675
676                     event.target.parentNode.parentNode.removeChild(event.target.parentNode);
677
678                     event.preventDefault();
679                     return false;
680                 }
681             } else if (event.keyCode == 64 && event.ctrlKey) { // ctrl + F6
682                 createControlField('006','                                        ');
683                 loadRecord(xml_record);
684             } else if (event.keyCode == 65 && event.ctrlKey) { // ctrl + F7
685                 createControlField('007','                                        ');
686                 loadRecord(xml_record);
687             } else if (event.keyCode == 66 && event.ctrlKey) { // ctrl + F8
688                 createControlField('008','                                        ');
689                 loadRecord(xml_record);
690             }
691
692             return true;
693
694         } else { // event on a control field
695             if (event.keyCode == 38) { 
696                 return setFocusToNextTag(row, 'up'); 
697             } else if (event.keyCode == 40) { 
698                 return setFocusToNextTag(row, 'down');
699             }
700         }
701     };
702
703     box.addEventListener(
704         'keypress', 
705         function () {
706             if (element.nodeKind() == 'attribute') element[0]=box.value;
707             else element.setChildren( box.value );
708             return true;
709         },
710         false
711     );
712
713     box.addEventListener(
714         'change', 
715         function () {
716             if (element.nodeKind() == 'attribute') element[0]=box.value;
717             else element.setChildren( box.value );
718             return true;
719         },
720         false
721     );
722
723     box.addEventListener(
724         'keypress', 
725         function () {
726             if (element.nodeKind() == 'attribute') element[0]=box.value;
727             else element.setChildren( box.value );
728             return true;
729         },
730         true
731     );
732
733     // 'input' event catches the box value after the keypress
734     box.addEventListener(
735         'input', 
736         function () {
737             if (element.nodeKind() == 'attribute') element[0]=box.value;
738             else element.setChildren( box.value );
739             return true;
740         },
741         true
742     );
743
744     box.addEventListener(
745         'keyup', 
746         function () {
747             if (element.localName() == 'controlfield')
748                 eval('fillFixedFields(xml_record);');
749         },
750         true
751     );
752
753     return box;
754 }
755
756 var rec_type = {
757     BKS : { Type : /[at]{1}/,    BLvl : /[acdm]{1}/ },
758     SER : { Type : /[a]{1}/,    BLvl : /[bs]{1}/ },
759     VIS : { Type : /[gkro]{1}/,    BLvl : /[abcdms]{1}/ },
760     MIX : { Type : /[p]{1}/,    BLvl : /[cd]{1}/ },
761     MAP : { Type : /[ef]{1}/,    BLvl : /[abcdms]{1}/ },
762     SCO : { Type : /[cd]{1}/,    BLvl : /[abcdms]{1}/ },
763     REC : { Type : /[ij]{1}/,    BLvl : /[abcdms]{1}/ },
764     COM : { Type : /[m]{1}/,    BLvl : /[abcdms]{1}/ },
765     AUT : { Type : /[z]{1}/,    BLvl : /.{1}/ },
766     MFHD : { Type : /[uvxy]{1}/,  BLvl : /.{1}/ }
767 };
768
769 var ff_pos = {
770     TrAr : {
771         _8 : {
772             SCO : {start : 33, len : 1, def : ' ' },
773             REC : {start : 33, len : 1, def : 'n' }
774         },
775         _6 : {
776             SCO : {start : 16, len : 1, def : ' ' },
777             REC : {start : 16, len : 1, def : 'n' }
778         }
779     },
780     TMat : {
781         _8 : {
782             VIS : {start : 33, len : 1, def : ' ' }
783         },
784         _6 : {
785             VIS : {start : 16, len : 1, def : ' ' }
786         }
787     },
788     Time : {
789         _8 : {
790             VIS : {start : 18, len : 3, def : ' ' }
791         },
792         _6 : {
793             VIS : {start : 1, len : 3, def : ' ' }
794         }
795     },
796     Tech : {
797         _8 : {
798             VIS : {start : 34, len : 1, def : 'n' }
799         },
800         _6 : {
801             VIS : {start : 17, len : 1, def : 'n' }
802         }
803     },
804     SrTp : {
805         _8 : {
806             SER : {start : 21, len : 1, def : ' ' }
807         },
808         _6 : {
809             SER : {start : 4, len : 1, def : ' ' }
810         }
811     },
812     Srce : {
813         _8 : {
814             BKS : {start : 39, len : 1, def : 'd' },
815             SER : {start : 39, len : 1, def : 'd' },
816             VIS : {start : 39, len : 1, def : 'd' },
817             MIX : {start : 39, len : 1, def : 'd' },
818             MAP : {start : 39, len : 1, def : 'd' },
819             SCO : {start : 39, len : 1, def : 'd' },
820             REC : {start : 39, len : 1, def : 'd' },
821             COM : {start : 39, len : 1, def : 'd' }
822         }
823     },
824     SpFm : {
825         _8 : {
826             MAP : {start : 33, len : 2, def : ' ' }
827         },
828         _6 : {
829             MAP : {start : 16, len : 2, def : ' ' }
830         }
831     },
832     Relf : {
833         _8 : {
834             MAP : {start : 18, len : 4, def : ' ' }
835         },
836         _6 : {
837             MAP : {start : 1, len : 4, def : ' ' }
838         }
839     },
840     Regl : {
841         _8 : {
842             SER : {start : 19, len : 1, def : ' ' }
843         },
844         _6 : {
845             SER : {start : 2, len : 1, def : ' ' }
846         }
847     },
848     Proj : {
849         _8 : {
850             MAP : {start : 22, len : 2, def : ' ' }
851         },
852         _6 : {
853             MAP : {start : 5, len : 2, def : ' ' }
854         }
855     },
856     Part : {
857         _8 : {
858             SCO : {start : 21, len : 1, def : ' ' },
859             REC : {start : 21, len : 1, def : 'n' }
860         },
861         _6 : {
862             SCO : {start : 4, len : 1, def : ' ' },
863             REC : {start : 4, len : 1, def : 'n' }
864         }
865     },
866     Orig : {
867         _8 : {
868             SER : {start : 22, len : 1, def : ' ' }
869         },
870         _6 : {
871             SER : {start : 5, len : 1, def : ' ' }
872         }
873     },
874     LTxt : {
875         _8 : {
876             SCO : {start : 30, len : 2, def : ' ' },
877             REC : {start : 30, len : 2, def : ' ' }
878         },
879         _6 : {
880             SCO : {start : 13, len : 2, def : ' ' },
881             REC : {start : 13, len : 2, def : ' ' }
882         }
883     },
884     Freq : {
885         _8 : {
886             SER : {start : 18, len : 1, def : ' ' }
887         },
888         _6 : {
889             SER : {start : 1, len : 1, def : ' ' }
890         }
891     },
892     FMus : {
893         _8 : {
894             SCO : {start : 20, len : 1, def : ' ' },
895             REC : {start : 20, len : 1, def : 'n' }
896         },
897         _6 : {
898             SCO : {start : 3, len : 1, def : ' ' },
899             REC : {start : 3, len : 1, def : 'n' }
900         }
901     },
902     File : {
903         _8 : {
904             COM : {start : 26, len : 1, def : 'u' }
905         },
906         _6 : {
907             COM : {start : 9, len : 1, def : 'u' }
908         }
909     },
910     EntW : {
911         _8 : {
912             SER : {start : 24, len : 1, def : ' ' }
913         },
914         _6 : {
915             SER : {start : 7, len : 1, def : ' ' }
916         }
917     },
918     AccM : {
919         _8 : {
920             SCO : {start : 24, len : 6, def : ' ' },
921             REC : {start : 24, len : 6, def : ' ' }
922         },
923         _6 : {
924             SCO : {start : 7, len : 6, def : ' ' },
925             REC : {start : 7, len : 6, def : ' ' }
926         }
927     },
928     Comp : {
929         _8 : {
930             SCO : {start : 18, len : 2, def : ' ' },
931             REC : {start : 18, len : 2, def : ' ' }
932         },
933         _6 : {
934             SCO : {start : 1, len : 2, def : ' ' },
935             REC : {start : 1, len : 2, def : ' ' }
936         }
937     },
938     CrTp : {
939         _8 : {
940             MAP : {start : 25, len : 1, def : ' ' }
941         },
942         _6 : {
943             MAP : {start : 8, len : 1, def : ' ' }
944         }
945     },
946     Ctry : {
947         _8 : {
948             BKS : {start : 15, len : 3, def : ' ' },
949             SER : {start : 15, len : 3, def : ' ' },
950             VIS : {start : 15, len : 3, def : ' ' },
951             MIX : {start : 15, len : 3, def : ' ' },
952             MAP : {start : 15, len : 3, def : ' ' },
953             SCO : {start : 15, len : 3, def : ' ' },
954             REC : {start : 15, len : 3, def : ' ' },
955             COM : {start : 15, len : 3, def : ' ' }
956         }
957     },
958     Lang : {
959         _8 : {
960             BKS : {start : 35, len : 3, def : ' ' },
961             SER : {start : 35, len : 3, def : ' ' },
962             VIS : {start : 35, len : 3, def : ' ' },
963             MIX : {start : 35, len : 3, def : ' ' },
964             MAP : {start : 35, len : 3, def : ' ' },
965             SCO : {start : 35, len : 3, def : ' ' },
966             REC : {start : 35, len : 3, def : ' ' },
967             COM : {start : 35, len : 3, def : ' ' }
968         }
969     },
970     MRec : {
971         _8 : {
972             BKS : {start : 38, len : 1, def : ' ' },
973             SER : {start : 38, len : 1, def : ' ' },
974             VIS : {start : 38, len : 1, def : ' ' },
975             MIX : {start : 38, len : 1, def : ' ' },
976             MAP : {start : 38, len : 1, def : ' ' },
977             SCO : {start : 38, len : 1, def : ' ' },
978             REC : {start : 38, len : 1, def : ' ' },
979             COM : {start : 38, len : 1, def : ' ' }
980         }
981     },
982     DtSt : {
983         _8 : {
984             BKS : {start : 6, len : 1, def : ' ' },
985             SER : {start : 6, len : 1, def : 'c' },
986             VIS : {start : 6, len : 1, def : ' ' },
987             MIX : {start : 6, len : 1, def : ' ' },
988             MAP : {start : 6, len : 1, def : ' ' },
989             SCO : {start : 6, len : 1, def : ' ' },
990             REC : {start : 6, len : 1, def : ' ' },
991             COM : {start : 6, len : 1, def : ' ' }
992         }
993     },
994     Type : {
995         ldr : {
996             BKS : {start : 6, len : 1, def : 'a' },
997             SER : {start : 6, len : 1, def : 'a' },
998             VIS : {start : 6, len : 1, def : 'g' },
999             MIX : {start : 6, len : 1, def : 'p' },
1000             MAP : {start : 6, len : 1, def : 'e' },
1001             SCO : {start : 6, len : 1, def : 'c' },
1002             REC : {start : 6, len : 1, def : 'i' },
1003             COM : {start : 6, len : 1, def : 'm' },
1004             AUT : {start : 6, len : 1, def : 'z' },
1005             MFHD : {start : 6, len : 1, def : 'y' }
1006         }
1007     },
1008     Ctrl : {
1009         ldr : {
1010             BKS : {start : 8, len : 1, def : ' ' },
1011             SER : {start : 8, len : 1, def : ' ' },
1012             VIS : {start : 8, len : 1, def : ' ' },
1013             MIX : {start : 8, len : 1, def : ' ' },
1014             MAP : {start : 8, len : 1, def : ' ' },
1015             SCO : {start : 8, len : 1, def : ' ' },
1016             REC : {start : 8, len : 1, def : ' ' },
1017             COM : {start : 8, len : 1, def : ' ' }
1018         }
1019     },
1020     BLvl : {
1021         ldr : {
1022             BKS : {start : 7, len : 1, def : 'm' },
1023             SER : {start : 7, len : 1, def : 's' },
1024             VIS : {start : 7, len : 1, def : 'm' },
1025             MIX : {start : 7, len : 1, def : 'c' },
1026             MAP : {start : 7, len : 1, def : 'm' },
1027             SCO : {start : 7, len : 1, def : 'm' },
1028             REC : {start : 7, len : 1, def : 'm' },
1029             COM : {start : 7, len : 1, def : 'm' }
1030         }
1031     },
1032     Desc : {
1033         ldr : {
1034             BKS : {start : 18, len : 1, def : ' ' },
1035             SER : {start : 18, len : 1, def : ' ' },
1036             VIS : {start : 18, len : 1, def : ' ' },
1037             MIX : {start : 18, len : 1, def : ' ' },
1038             MAP : {start : 18, len : 1, def : ' ' },
1039             SCO : {start : 18, len : 1, def : ' ' },
1040             REC : {start : 18, len : 1, def : ' ' },
1041             COM : {start : 18, len : 1, def : 'i' }
1042         }
1043     },
1044     Item : {
1045         ldr : {
1046             MFHD : {start : 18, len : 1, def : 'i' }
1047         }
1048     },
1049     ELvl : {
1050         ldr : {
1051             BKS : {start : 17, len : 1, def : ' ' },
1052             SER : {start : 17, len : 1, def : ' ' },
1053             VIS : {start : 17, len : 1, def : ' ' },
1054             MIX : {start : 17, len : 1, def : ' ' },
1055             MAP : {start : 17, len : 1, def : ' ' },
1056             SCO : {start : 17, len : 1, def : ' ' },
1057             REC : {start : 17, len : 1, def : ' ' },
1058             COM : {start : 17, len : 1, def : ' ' },
1059             AUT : {start : 17, len : 1, def : 'n' },
1060             MFHD : {start : 17, len : 1, def : 'u' }
1061         }
1062     },
1063     Indx : {
1064         _8 : {
1065             BKS : {start : 31, len : 1, def : '0' },
1066             MAP : {start : 31, len : 1, def : '0' }
1067         },
1068         _6 : {
1069             BKS : {start : 14, len : 1, def : '0' },
1070             MAP : {start : 14, len : 1, def : '0' }
1071         }
1072     },
1073     Date1 : {
1074         _8 : {
1075             BKS : {start : 7, len : 4, def : ' ' },
1076             SER : {start : 7, len : 4, def : ' ' },
1077             VIS : {start : 7, len : 4, def : ' ' },
1078             MIX : {start : 7, len : 4, def : ' ' },
1079             MAP : {start : 7, len : 4, def : ' ' },
1080             SCO : {start : 7, len : 4, def : ' ' },
1081             REC : {start : 7, len : 4, def : ' ' },
1082             COM : {start : 7, len : 4, def : ' ' }
1083         }
1084     },
1085     Date2 : {
1086         _8 : {
1087             BKS : {start : 11, len : 4, def : ' ' },
1088             SER : {start : 11, len : 4, def : '9' },
1089             VIS : {start : 11, len : 4, def : ' ' },
1090             MIX : {start : 11, len : 4, def : ' ' },
1091             MAP : {start : 11, len : 4, def : ' ' },
1092             SCO : {start : 11, len : 4, def : ' ' },
1093             REC : {start : 11, len : 4, def : ' ' },
1094             COM : {start : 11, len : 4, def : ' ' }
1095         }
1096     },
1097     LitF : {
1098         _8 : {
1099             BKS : {start : 33, len : 1, def : '0' }
1100         },
1101         _6 : {
1102             BKS : {start : 16, len : 1, def : '0' }
1103         }
1104     },
1105     Biog : {
1106         _8 : {
1107             BKS : {start : 34, len : 1, def : ' ' }
1108         },
1109         _6 : {
1110             BKS : {start : 17, len : 1, def : ' ' }
1111         }
1112     },
1113     Ills : {
1114         _8 : {
1115             BKS : {start : 18, len : 4, def : ' ' }
1116         },
1117         _6 : {
1118             BKS : {start : 1, len : 4, def : ' ' }
1119         }
1120     },
1121     Fest : {
1122         _8 : {
1123             BKS : {start : 30, len : 1, def : '0' }
1124         },
1125         _6 : {
1126             BKS : {start : 13, len : 1, def : '0' }
1127         }
1128     },
1129     Conf : {
1130         _8 : {
1131             BKS : {start : 29, len : 1, def : '0' },
1132             SER : {start : 29, len : 1, def : '0' }
1133         },
1134         _6 : {
1135             BKS : {start : 12, len : 1, def : '0' },
1136             SER : {start : 12, len : 1, def : '0' }
1137         }
1138     },
1139     Cont : {
1140         _8 : {
1141             BKS : {start : 24, len : 4, def : ' ' },
1142             SER : {start : 25, len : 3, def : ' ' }
1143         },
1144         _6 : {
1145             BKS : {start : 7, len : 4, def : ' ' },
1146             SER : {start : 8, len : 3, def : ' ' }
1147         }
1148     },
1149     GPub : {
1150         _8 : {
1151             BKS : {start : 28, len : 1, def : ' ' },
1152             SER : {start : 28, len : 1, def : ' ' },
1153             VIS : {start : 28, len : 1, def : ' ' },
1154             MAP : {start : 28, len : 1, def : ' ' },
1155             COM : {start : 28, len : 1, def : ' ' }
1156         },
1157         _6 : {
1158             BKS : {start : 11, len : 1, def : ' ' },
1159             SER : {start : 11, len : 1, def : ' ' },
1160             VIS : {start : 11, len : 1, def : ' ' },
1161             MAP : {start : 11, len : 1, def : ' ' },
1162             COM : {start : 11, len : 1, def : ' ' }
1163         }
1164     },
1165     Audn : {
1166         _8 : {
1167             BKS : {start : 22, len : 1, def : ' ' },
1168             SER : {start : 22, len : 1, def : ' ' },
1169             VIS : {start : 22, len : 1, def : ' ' },
1170             SCO : {start : 22, len : 1, def : ' ' },
1171             REC : {start : 22, len : 1, def : ' ' },
1172             COM : {start : 22, len : 1, def : ' ' }
1173         },
1174         _6 : {
1175             BKS : {start : 5, len : 1, def : ' ' },
1176             SER : {start : 5, len : 1, def : ' ' },
1177             VIS : {start : 5, len : 1, def : ' ' },
1178             SCO : {start : 5, len : 1, def : ' ' },
1179             REC : {start : 5, len : 1, def : ' ' },
1180             COM : {start : 5, len : 1, def : ' ' }
1181         }
1182     },
1183     Form : {
1184         _8 : {
1185             BKS : {start : 23, len : 1, def : ' ' },
1186             SER : {start : 23, len : 1, def : ' ' },
1187             VIS : {start : 29, len : 1, def : ' ' },
1188             MIX : {start : 23, len : 1, def : ' ' },
1189             MAP : {start : 29, len : 1, def : ' ' },
1190             SCO : {start : 23, len : 1, def : ' ' },
1191             REC : {start : 23, len : 1, def : ' ' }
1192         },
1193         _6 : {
1194             BKS : {start : 6, len : 1, def : ' ' },
1195             SER : {start : 6, len : 1, def : ' ' },
1196             VIS : {start : 12, len : 1, def : ' ' },
1197             MIX : {start : 6, len : 1, def : ' ' },
1198             MAP : {start : 12, len : 1, def : ' ' },
1199             SCO : {start : 6, len : 1, def : ' ' },
1200             REC : {start : 6, len : 1, def : ' ' }
1201         }
1202     },
1203     'S/L' : {
1204         _8 : {
1205             SER : {start : 34, len : 1, def : '0' }
1206         },
1207         _6 : {
1208             SER : {start : 17, len : 1, def : '0' }
1209         }
1210     },
1211     'Alph' : {
1212         _8 : {
1213             SER : {start : 33, len : 1, def : ' ' }
1214         },
1215         _6 : {
1216             SER : {start : 16, len : 1, def : ' ' }
1217         }
1218     },
1219     "GeoDiv" : {
1220         "_8" : {
1221             "AUT" : {"start" : 6, "len" : 1, "def" : ' ' }
1222         }
1223     },
1224     "Roman" : {
1225         "_8" : {
1226             "AUT" : {"start" : 7, "len" : 1, "def" : ' ' }
1227         }
1228     },
1229     "CatLang" : {
1230         "_8" : {
1231             "AUT" : {"start" : 8, "len" : 1, "def" : ' ' }
1232         }
1233     },
1234     "Kind" : {
1235         "_8" : {
1236             "AUT" : {"start" : 9, "len" : 1, "def" : ' ' }
1237         }
1238     },
1239     "Rules" : {
1240         "_8" : {
1241             "AUT" : {"start" : 10, "len" : 1, "def" : ' ' }
1242         }
1243     },
1244     "SHSys" : {
1245         "_8" : {
1246             "AUT" : {"start" : 11, "len" : 1, "def" : ' ' }
1247         }
1248     },
1249     "SerType" : {
1250         "_8" : {
1251             "AUT" : {"start" : 12, "len" : 1, "def" : ' ' }
1252         }
1253     },
1254     "SerNum" : {
1255         "_8" : {
1256             "AUT" : {"start" : 13, "len" : 1, "def" : ' ' }
1257         }
1258     },
1259     "HeadMain" : {
1260         "_8" : {
1261             "AUT" : {"start" : 14, "len" : 1, "def" : ' ' }
1262         }
1263     },
1264     "HeadSubj" : {
1265         "_8" : {
1266             "AUT" : {"start" : 15, "len" : 1, "def" : ' ' }
1267         }
1268     },
1269     "HeadSer" : {
1270         "_8" : {
1271             "AUT" : {"start" : 16, "len" : 1, "def" : ' ' }
1272         }
1273     },
1274     "TypeSubd" : {
1275         "_8" : {
1276             "AUT" : {"start" : 17, "len" : 1, "def" : ' ' }
1277         }
1278     },
1279     "TypeGov" : {
1280         "_8" : {
1281             "AUT" : {"start" : 28, "len" : 1, "def" : ' ' }
1282         }
1283     },
1284     "RefEval" : {
1285         "_8" : {
1286             "AUT" : {"start" : 29, "len" : 1, "def" : ' ' }
1287         }
1288     },
1289     "RecUpd" : {
1290         "_8" : {
1291             "AUT" : {"start" : 31, "len" : 1, "def" : ' ' }
1292         }
1293     },
1294     "NameDiff" : {
1295         "_8" : {
1296             "AUT" : {"start" : 32, "len" : 1, "def" : ' ' }
1297         }
1298     },
1299     "Level" : {
1300         "_8" : {
1301             "AUT" : {"start" : 33, "len" : 1, "def" : ' ' }
1302         }
1303     },
1304     "ModRec" : {
1305         "_8" : {
1306             "AUT" : {"start" : 38, "len" : 1, "def" : ' ' }
1307         }
1308     },
1309     "CatSrc" : {
1310         "_8" : {
1311             "AUT" : {"start" : 39, "len" : 1, "def" : ' ' }
1312         }
1313     }
1314 };
1315
1316 function recordType (rec) {
1317     try {
1318         var _l = rec.leader.toString();
1319
1320         var _t = _l.substr(ff_pos.Type.ldr.BKS.start, ff_pos.Type.ldr.BKS.len);
1321         var _b = _l.substr(ff_pos.BLvl.ldr.BKS.start, ff_pos.BLvl.ldr.BKS.len);
1322
1323         for (var t in rec_type) {
1324             if (_t.match(rec_type[t].Type) && _b.match(rec_type[t].BLvl)) {
1325                 document.getElementById('recordTypeLabel').value = t;
1326                 _record_type = t;
1327                 return t;
1328             }
1329         }
1330
1331         // in case we don't have a valid record type ...
1332         _record_type = 'BKS';
1333         return _record_type;
1334
1335     } catch(E) {
1336         alert('FIXME, MARC Editor, recordType: ' + E);
1337     }
1338 }
1339
1340 function toggleFFE () {
1341     var grid = document.getElementById('leaderGrid');
1342     if (grid.hidden) {
1343         grid.hidden = false;
1344     } else {
1345         grid.hidden = true;
1346     }
1347     return true;
1348 }
1349
1350 function changeFFEditor (type) {
1351     var grid = document.getElementById('leaderGrid');
1352     grid.setAttribute('type',type);
1353
1354     // Hide FFEditor rows that we don't need for our current type
1355     // If all of the labels for a given row do not include our
1356     // desired type in their set attribute, we can hide that row
1357     dojo.query('rows row', grid).forEach(function(node, index, arr) {
1358         if (dojo.query('label[set~=' + type + ']', node).length == 0) {
1359             node.hidden = true;
1360         }
1361     });
1362
1363 }
1364
1365 function fillFixedFields (rec) {
1366     try {
1367             var grid = document.getElementById('leaderGrid');
1368
1369             var rtype = _record_type;
1370
1371             var _l = rec.leader.toString();
1372             var _6 = rec.controlfield.(@tag=='006').toString();
1373             var _7 = rec.controlfield.(@tag=='007').toString();
1374             var _8 = rec.controlfield.(@tag=='008').toString();
1375
1376             var list = [];
1377             var pre_list = grid.getElementsByTagName('label');
1378             for (var i in pre_list) {
1379                 if ( pre_list[i].getAttribute && pre_list[i].getAttribute('set').indexOf(grid.getAttribute('type')) > -1 ) {
1380                     list.push( pre_list[i] );
1381                 }
1382             }
1383
1384             for (var i in list) {
1385                 var name = list[i].getAttribute('name');
1386
1387                 if (!ff_pos[name])
1388                     continue;
1389
1390                 var value = '';
1391                 if ( ff_pos[name].ldr && ff_pos[name].ldr[rtype] )
1392                     value = _l.substr(ff_pos[name].ldr[rtype].start, ff_pos[name].ldr[rtype].len);
1393
1394                 if ( ff_pos[name]._8 && ff_pos[name]._8[rtype] )
1395                     value = _8.substr(ff_pos[name]._8[rtype].start, ff_pos[name]._8[rtype].len);
1396
1397                 if ( !value && ff_pos[name]._6 && ff_pos[name]._6[rtype] )
1398                     value = _6.substr(ff_pos[name]._6[rtype].start, ff_pos[name]._6[rtype].len);
1399
1400                 if ( ff_pos[name]._7 && ff_pos[name]._7[rtype] )
1401                     value = _7.substr(ff_pos[name]._7[rtype].start, ff_pos[name]._7[rtype].len);
1402                 
1403                 if (!value) {
1404                     var d;
1405                     var p;
1406                     if (ff_pos[name].ldr && ff_pos[name].ldr[rtype]) {
1407                         d = ff_pos[name].ldr[rtype].def;
1408                         p = 'ldr';
1409                     }
1410
1411                     if (ff_pos[name]._8 && ff_pos[name]._8[rtype]) {
1412                         d = ff_pos[name]._8[rtype].def;
1413                         p = '_8';
1414                     }
1415
1416                     if (!value && ff_pos[name]._6 && ff_pos[name]._6[rtype]) {
1417                         d = ff_pos[name]._6[rtype].def;
1418                         p = '_6';
1419                     }
1420
1421                     if (ff_pos[name]._7 && ff_pos[name]._7[rtype]) {
1422                         d = ff_pos[name]._7[rtype].def;
1423                         p = '_7';
1424                     }
1425
1426                     if (p && !value) {
1427                         for (var j = 0; j < ff_pos[name][p][rtype].len; j++) {
1428                             value += d;
1429                         }
1430                     }
1431                 }
1432
1433                 list[i].nextSibling.value = value;
1434             }
1435
1436             return true;
1437     } catch(E) {
1438         alert('FIXME, MARC Editor, fillFixedFields: ' + E);
1439     }
1440 }
1441
1442 function updateFixedFields (element) {
1443     var grid = document.getElementById('leaderGrid');
1444     var recGrid = document.getElementById('recGrid');
1445
1446     var rtype = _record_type;
1447     var new_value = element.value;
1448
1449     var parts = {
1450         ldr : _record.leader,
1451         _6 : _record.controlfield.(@tag=='006'),
1452         _7 : _record.controlfield.(@tag=='007'),
1453         _8 : _record.controlfield.(@tag=='008')
1454     };
1455
1456     var name = element.getAttribute('name');
1457     for (var i in ff_pos[name]) {
1458
1459         if (!ff_pos[name][i][rtype]) continue;
1460         if (!parts[i]) {
1461             // we're missing the required field.  Add it now.
1462
1463             var newfield;
1464             if (i == '_6') newfield = '006';
1465             else if (i == '_7') newfield = '007';
1466             else if (i == '_8') newfield = '008';
1467             else continue;
1468
1469             createControlField(newfield,'                                        ');
1470             parts[i] = _record.controlfield.(@tag==newfield);
1471         }
1472
1473         var before = parts[i].substr(0, ff_pos[name][i][rtype].start);
1474         var after = parts[i].substr(ff_pos[name][i][rtype].start + ff_pos[name][i][rtype].len);
1475
1476         for (var j = 0; new_value.length < ff_pos[name][i][rtype].len; j++) {
1477             new_value += ff_pos[name][i][rtype].def;
1478         }
1479
1480         parts[i].setChildren( before + new_value + after );
1481         recGrid.getElementsByAttribute('tag',i)[0].lastChild.value = parts[i].toString();
1482     }
1483
1484     return true;
1485 }
1486
1487 function marcLeader (leader) {
1488     var row = createRow(
1489         { class : 'marcLeaderRow',
1490           tag : 'ldr' },
1491         createLabel(
1492             { value : 'LDR',
1493               class : 'marcTag',
1494               tooltiptext : $('catStrings').getString('staff.cat.marcedit.marcTag.LDR.label') } ),
1495         createLabel(
1496             { value : '',
1497               class : 'marcInd1' } ),
1498         createLabel(
1499             { value : '',
1500               class : 'marcInd2' } ),
1501         createLabel(
1502             { value : leader.text(),
1503               class : 'marcLeader' } )
1504     );
1505
1506     return row;
1507 }
1508
1509 function marcControlfield (field) {
1510     tagname = field.@tag.toString().substr(2);
1511     var row;
1512     if (tagname == '1' || tagname == '3' || tagname == '6' || tagname == '7' || tagname == '8') {
1513         row = createRow(
1514             { class : 'marcControlfieldRow',
1515               tag : '_' + tagname },
1516             createLabel(
1517                 { value : field.@tag,
1518                   class : 'marcTag',
1519                   context : 'tags_popup',
1520                   onmouseover : 'getTooltip(this, "tag");',
1521                   tooltipid : 'tag' + field.@tag } ),
1522             createLabel(
1523                 { value : field.@ind1,
1524                   class : 'marcInd1',
1525                   onmouseover : 'getTooltip(this, "ind1");',
1526                   tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
1527             createLabel(
1528                 { value : field.@ind2,
1529                   class : 'marcInd2',
1530                   onmouseover : 'getTooltip(this, "ind2");',
1531                   tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
1532             createMARCTextbox(
1533                 field,
1534                 { value : field.text(),
1535                   class : 'plain marcEditableControlfield',
1536                   name : 'CONTROL' + tagname,
1537                   context : 'clipboard',
1538                   size : 50,
1539                   maxlength : 50 } )
1540             );
1541     } else {
1542         row = createRow(
1543             { class : 'marcControlfieldRow',
1544               tag : '_' + tagname },
1545             createLabel(
1546                 { value : field.@tag,
1547                   class : 'marcTag',
1548                   onmouseover : 'getTooltip(this, "tag");',
1549                   tooltipid : 'tag' + field.@tag } ),
1550             createLabel(
1551                 { value : field.@ind1,
1552                   class : 'marcInd1',
1553                   onmouseover : 'getTooltip(this, "ind1");',
1554                   tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
1555             createLabel(
1556                 { value : field.@ind2,
1557                   class : 'marcInd2',
1558                   onmouseover : 'getTooltip(this, "ind2");',
1559                   tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
1560             createLabel(
1561                 { value : field.text(),
1562                   class : 'marcControlfield' } )
1563         );
1564     }
1565
1566     return row;
1567 }
1568
1569 function stackSubfields(checkbox) {
1570     var list = document.getElementsByAttribute('name','sf_box');
1571
1572     var o = 'vertical';
1573     if (!checkbox.checked) o = 'horizontal';
1574     
1575     for (var i = 0; i < list.length; i++) {
1576         if (list[i]) list[i].setAttribute('orient',o);
1577     }
1578 }
1579
1580 function fastItemAdd_toggle(checkbox) {
1581     var x = document.getElementById('fastItemAdd_textboxes');
1582     if (checkbox.checked) {
1583         x.hidden = false;
1584         document.getElementById('fastItemAdd_callnumber').focus();
1585         document.getElementById('fastItemAdd_callnumber').select();
1586     } else {
1587         x.hidden = true;
1588     }
1589 }
1590
1591 function fastItemAdd_attempt(doc_id) {
1592     try {
1593         if (typeof window.xulG.fast_add_item != 'function') { return; }
1594         if (!document.getElementById('fastItemAdd_checkbox').checked) { return; }
1595         if (!document.getElementById('fastItemAdd_callnumber').value) { return; }
1596         if (!document.getElementById('fastItemAdd_barcode').value) { return; }
1597         window.xulG.fast_add_item( doc_id, document.getElementById('fastItemAdd_callnumber').value, document.getElementById('fastItemAdd_barcode').value );
1598         document.getElementById('fastItemAdd_barcode').value = '';
1599     } catch(E) {
1600         alert('fastItemAdd_attempt: ' + E);
1601     }
1602 }
1603
1604 function save_attempt(xml_string) {
1605     try {
1606         var result = window.xulG.save.func( xml_string );   
1607         if (result) {
1608             oils_unlock_page();
1609             if (result.id) fastItemAdd_attempt(result.id);
1610             if (typeof result.on_complete == 'function') result.on_complete();
1611         }
1612     } catch(E) {
1613         alert('save_attempt: ' + E);
1614     }
1615 }
1616
1617 function marcDatafield (field) {
1618     var row = createRow(
1619         { class : 'marcDatafieldRow' },
1620         createMARCTextbox(
1621             field.@tag,
1622             { value : field.@tag,
1623               class : 'plain marcTag',
1624               name : 'marcTag',
1625               context : 'tags_popup',
1626               oninput : 'if (this.value.length == 3) { this.nextSibling.focus(); }',
1627               size : 3,
1628               maxlength : 3,
1629               onmouseover : 'current_focus = this; getTooltip(this, "tag");' } ),
1630         createMARCTextbox(
1631             field.@ind1,
1632             { value : field.@ind1,
1633               class : 'plain marcInd1',
1634               name : 'marcInd1',
1635               oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
1636               size : 1,
1637               maxlength : 1,
1638               onmouseover : 'current_focus = this; getContextMenu(this, "ind1"); getTooltip(this, "ind1");',
1639               oncontextmenu : 'getContextMenu(this, "ind1");' } ),
1640         createMARCTextbox(
1641             field.@ind2,
1642             { value : field.@ind2,
1643               class : 'plain marcInd2',
1644               name : 'marcInd2',
1645               oninput : 'if (this.value.length == 1) { this.nextSibling.firstChild.firstChild.focus(); }',
1646               size : 1,
1647               maxlength : 1,
1648               onmouseover : 'current_focus = this; getContextMenu(this, "ind2"); getTooltip(this, "ind2");',
1649               oncontextmenu : 'getContextMenu(this, "ind2");' } ),
1650         createHbox({ name : 'sf_box' })
1651     );
1652
1653     if (!current_focus && field.@tag == '') current_focus = row.childNodes[0];
1654     if (!current_focus && field.@ind1 == '') current_focus = row.childNodes[1];
1655     if (!current_focus && field.@ind2 == '') current_focus = row.childNodes[2];
1656
1657     var sf_box = row.lastChild;
1658     if (document.getElementById('stackSubfields').checked)
1659         sf_box.setAttribute('orient','vertical');
1660
1661     sf_box.addEventListener(
1662         'click',
1663         function (e) {
1664             if (sf_box === e.target) {
1665                 sf_box.lastChild.lastChild.focus();
1666             } else if (e.target.parentNode === sf_box) {
1667                 e.target.lastChild.focus();
1668             }
1669         },
1670         false
1671     );
1672
1673
1674     for (var i in field.subfield) {
1675         var sf = field.subfield[i];
1676         sf_box.appendChild(
1677             marcSubfield(sf)
1678         );
1679
1680         dojo.query('.marcSubfield', sf_box).forEach(wrap_long_fields);
1681
1682         if (sf.@code == '' && (!current_focus || current_focus.className.match(/Ind/)))
1683             current_focus = sf_box.lastChild.childNodes[1];
1684     }
1685
1686     return row;
1687 }
1688
1689 function marcSubfield (sf) {            
1690     return createHbox(
1691         { class : 'marcSubfieldBox' },
1692         createLabel(
1693             { value : "\u2021",
1694               class : 'plain marcSubfieldDelimiter',
1695               onmouseover : 'getTooltip(this.nextSibling, "subfield");',
1696               oncontextmenu : 'getContextMenu(this.nextSibling, "subfield");',
1697                 //onclick : 'this.nextSibling.focus();',
1698                 onfocus : 'this.nextSibling.focus();',
1699               size : 2 } ),
1700         createMARCTextbox(
1701             sf.@code,
1702             { value : sf.@code,
1703               class : 'plain marcSubfieldCode',
1704               name : 'marcSubfieldCode',
1705               onmouseover : 'current_focus = this; getContextMenu(this, "subfield"); getTooltip(this, "subfield");',
1706               oncontextmenu : 'getContextMenu(this, "subfield");',
1707               oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
1708               size : 2,
1709               maxlength : 1 } ),
1710         createMARCTextbox(
1711             sf,
1712             { value : sf.text(),
1713               name : sf.parent().@tag + ':' + sf.@code,
1714               class : 'plain marcSubfield', 
1715               onmouseover : 'getTooltip(this, "subfield");',
1716               contextmenu : function (event) { getAuthorityContextMenu(event.target, sf) },
1717               size : new String(sf.text()).length + 2,
1718               oninput : "this.setAttribute('size', this.value.length + 2);"
1719             } )
1720     );
1721 }
1722
1723 function loadRecord(rec) {
1724     try {
1725             _record = rec;
1726             var grid_rows = document.getElementById('recGrid').lastChild;
1727
1728             while (grid_rows.firstChild) grid_rows.removeChild(grid_rows.firstChild);
1729
1730             grid_rows.appendChild( marcLeader( rec.leader ) );
1731
1732             for (var i in rec.controlfield) {
1733                 grid_rows.appendChild( marcControlfield( rec.controlfield[i] ) );
1734             }
1735
1736             for (var i in rec.datafield) {
1737                 grid_rows.appendChild( marcDatafield( rec.datafield[i] ) );
1738             }
1739
1740             grid_rows.getElementsByAttribute('class','marcDatafieldRow')[0].firstChild.focus();
1741             changeFFEditor(recordType(rec));
1742             fillFixedFields(rec);
1743     } catch(E) {
1744         alert('FIXME, MARC Editor, loadRecord: ' + E);
1745     }
1746 }
1747
1748
1749 function genToolTips () {
1750     for (var i in bib_data.field) {
1751         var f = bib_data.field[i];
1752     
1753         tag_menu.appendChild(
1754             createMenuitem(
1755                 { label : f.@tag,
1756                   oncommand : 
1757                       'current_focus.value = "' + f.@tag + '";' +
1758                     'var e = document.createEvent("MutationEvents");' +
1759                     'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1760                     'current_focus.inputField.dispatchEvent(e);',
1761                   disabled : f.@tag < '010' ? "true" : "false",
1762                   tooltiptext : f.description }
1763             )
1764         );
1765     
1766         var i1_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i1' });
1767         context_menus.appendChild( i1_popup );
1768     
1769         var i2_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i2' });
1770         context_menus.appendChild( i2_popup );
1771     
1772         var sf_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'sf' });
1773         context_menus.appendChild( sf_popup );
1774     
1775         tooltip_hash['tag' + f.@tag] = f.description;
1776         for (var j in f.indicator) {
1777             var ind = f.indicator[j];
1778             tooltip_hash['tag' + f.@tag + 'ind' + ind.@position + 'val' + ind.@value] = ind.description;
1779     
1780             if (ind.@position == 1) {
1781                 i1_popup.appendChild(
1782                     createMenuitem(
1783                         { label : ind.@value,
1784                           oncommand : 
1785                               'current_focus.value = "' + ind.@value + '";' +
1786                             'var e = document.createEvent("MutationEvents");' +
1787                             'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1788                             'current_focus.inputField.dispatchEvent(e);',
1789                           tooltiptext : ind.description }
1790                     )
1791                 );
1792             }
1793     
1794             if (ind.@position == 2) {
1795                 i2_popup.appendChild(
1796                     createMenuitem(
1797                         { label : ind.@value,
1798                           oncommand : 
1799                               'current_focus.value = "' + ind.@value + '";' +
1800                             'var e = document.createEvent("MutationEvents");' +
1801                             'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1802                             'current_focus.inputField.dispatchEvent(e);',
1803                           tooltiptext : ind.description }
1804                     )
1805                 );
1806             }
1807         }
1808     
1809         for (var j in f.subfield) {
1810             var sf = f.subfield[j];
1811             tooltip_hash['tag' + f.@tag + 'sf' + sf.@code] = sf.description;
1812     
1813             sf_popup.appendChild(
1814                 createMenuitem(
1815                     { label : sf.@code,
1816                       oncommand : 
1817                           'current_focus.value = "' + sf.@code + '";' +
1818                         'var e = document.createEvent("MutationEvents");' +
1819                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1820                         'current_focus.inputField.dispatchEvent(e);',
1821                       tooltiptext : sf.description
1822                     }
1823                 )
1824             );
1825         }
1826     }
1827 }
1828
1829 function getTooltip (target, type) {
1830
1831     var tt = '';
1832     if (type == 'subfield')
1833         tt = 'tag' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf' + target.parentNode.childNodes[1].value;
1834
1835     if (type == 'ind1')
1836         tt = 'tag' + target.parentNode.firstChild.value + 'ind1val' + target.value;
1837
1838     if (type == 'ind2')
1839         tt = 'tag' + target.parentNode.firstChild.value + 'ind2val' + target.value;
1840
1841     if (type == 'tag')
1842         tt = 'tag' + target.parentNode.firstChild.value;
1843
1844     if (!document.getElementById( tt )) {
1845         p.appendChild(
1846             createTooltip(
1847                 { id : tt,
1848                   flex : "1",
1849                   orient : 'vertical',
1850                   onpopupshown : 'this.width = this.firstChild.boxObject.width + 10; this.height = this.firstChild.boxObject.height + 10;',
1851                   class : 'tooltip' },
1852                 createDescription({}, document.createTextNode( tooltip_hash[tt] ) )
1853             )
1854         );
1855     }
1856
1857     target.tooltip = tt;
1858     return true;
1859 }
1860
1861 function getContextMenu (target, type) {
1862
1863     var tt = '';
1864     if (type == 'subfield')
1865         tt = 't' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf';
1866
1867     if (type == 'ind1')
1868         tt = 't' + target.parentNode.firstChild.value + 'i1';
1869
1870     if (type == 'ind2')
1871         tt = 't' + target.parentNode.firstChild.value + 'i2';
1872
1873     target.setAttribute('context', tt);
1874     return true;
1875 }
1876
1877 var authority_tag_map = {
1878     100 : ['[100,500,700]',100],
1879     700 : ['[100,500,700]',100],
1880     800 : ['[100,500,700]',100],
1881     110 : ['[110,510,710]',110],
1882     610 : ['[110,510,710]',110],
1883     710 : ['[110,510,710]',110],
1884     810 : ['[110,510,710]',110],
1885     111 : ['[111,511,711]',111],
1886     611 : ['[111,511,711]',111],
1887     711 : ['[111,511,711]',111],
1888     811 : ['[111,511,711]',111],
1889     240 : ['[130,530,730]',130],
1890     130 : ['[130,530,730]',130],
1891     730 : ['[130,530,730]',130],
1892     830 : ['[130,530,730]',130],
1893     600 : ['[100,500,580,581,582,585,700,780,781,782,785]',100],
1894     630 : ['[130,530,730]',130],
1895     648 : ['[148,548]',148],
1896     650 : ['[150,550,580,581,582,585,750,780,781,782,785]',150],
1897     651 : ['[151,551,580,581,582,585,751,780,781,782,785]',151],
1898     655 : ['[155,555,580,581,582,585,755,780,781,782,785]',155]
1899 };
1900
1901 function getAuthorityContextMenu (target, sf) {
1902     var menu_id = sf.parent().@tag + ':' + sf.@code + '-authority-context-' + sf;
1903
1904     var page = 0;
1905     var old = dojo.byId( menu_id );
1906     if (old) {
1907         page = auth_pages[menu_id];
1908         old.parentNode.removeChild(old);
1909     } else {
1910         auth_pages[menu_id] = 0;
1911     }
1912
1913     var sf_popup = createPopup({ id : menu_id, flex : 1 });
1914
1915     sf_popup.addEventListener("popuphiding", function(event) {
1916         if (show_auth_menu) {
1917             show_auth_menu = false;
1918             getAuthorityContextMenu(target, sf);
1919             dojo.byId(menu_id).openPopup();
1920         }  
1921     }, false);
1922
1923     context_menus.appendChild( sf_popup );
1924
1925     if (!authority_tag_map[sf.parent().@tag]) {
1926         sf_popup.appendChild(createLabel( { value : $('catStrings').getString('staff.cat.marcedit.not_authority_field.label') } ) );
1927         target.setAttribute('context', 'clipboard');
1928         return false;
1929     }
1930
1931     browseAuthority( sf_popup, menu_id, target, sf, 20, page);
1932
1933     return true;
1934 }
1935
1936 /* Apply the complete 1xx */
1937 function applyFullAuthority ( target, ui_sf, e4x_sf ) {
1938     var new_vals = dojo.query('*[tag^="1"]', target);
1939     return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1940 }
1941
1942 function applySelectedAuthority ( target, ui_sf, e4x_sf ) {
1943     var new_vals = target.getElementsByAttribute('checked','true');
1944     return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1945 }
1946
1947 function applyAuthority ( target, ui_sf, e4x_sf, new_vals ) {
1948     var field = e4x_sf.parent();
1949
1950     for (var i = 0; i < new_vals.length; i++) {
1951
1952         var sf_list = field.subfield;
1953         for (var j in sf_list) {
1954
1955             if (sf_list[j].@code == new_vals[i].getAttribute('subfield')) {
1956                 sf_list[j] = new_vals[i].getAttribute('value');
1957                 new_vals[i].setAttribute('subfield','');
1958                 break;
1959             }
1960         }
1961     }
1962
1963     for (var i = 0; i < new_vals.length; i++) {
1964         if (!new_vals[i].getAttribute('subfield')) continue;
1965
1966         var val = new_vals[i].getAttribute('value');
1967
1968         var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{val}</subfield>;
1969         sf.@code = new_vals[i].getAttribute('subfield');
1970
1971         field.insertChildAfter(field.subfield[field.subfield.length() - 1], sf);
1972     }
1973
1974     var row = marcDatafield( field );
1975
1976     var node = ui_sf;
1977     while (node.nodeName != 'row') {
1978         node = node.parentNode;
1979     }
1980
1981     node.parentNode.replaceChild( row, node );
1982     return true;
1983 }
1984
1985 var control_map = {
1986     100 : {
1987         'a' : { 100 : 'a' },
1988         'd' : { 100 : 'd' },
1989         'e' : { 100 : 'e' },
1990         'q' : { 100 : 'q' }
1991     },
1992     110 : {
1993         'a' : { 110 : 'a' },
1994         'd' : { 110 : 'd' }
1995     },
1996     111 : {
1997         'a' : { 111 : 'a' },
1998         'd' : { 111 : 'd' }
1999     },
2000     130 : {
2001         'a' : { 130 : 'a' },
2002         'd' : { 130 : 'd' }
2003     },
2004     240 : {
2005         'a' : { 130 : 'a' },
2006         'd' : { 130 : 'd' }
2007     },
2008     400 : {
2009         'a' : { 100 : 'a' },
2010         'd' : { 100 : 'd' }
2011     },
2012     410 : {
2013         'a' : { 110 : 'a' },
2014         'd' : { 110 : 'd' }
2015     },
2016     411 : {
2017         'a' : { 111 : 'a' },
2018         'd' : { 111 : 'd' }
2019     },
2020     440 : {
2021         'a' : { 130 : 'a' },
2022         'n' : { 130 : 'n' },
2023         'p' : { 130 : 'p' }
2024     },
2025     700 : {
2026         'a' : { 100 : 'a' },
2027         'd' : { 100 : 'd' },
2028         'q' : { 100 : 'q' },
2029         't' : { 100 : 't' }
2030     },
2031     710 : {
2032         'a' : { 110 : 'a' },
2033         'd' : { 110 : 'd' }
2034     },
2035     711 : {
2036         'a' : { 111 : 'a' },
2037         'c' : { 111 : 'c' },
2038         'd' : { 111 : 'd' }
2039     },
2040     730 : {
2041         'a' : { 130 : 'a' },
2042         'd' : { 130 : 'd' }
2043     },
2044     800 : {
2045         'a' : { 100 : 'a' },
2046         'd' : { 100 : 'd' }
2047     },
2048     810 : {
2049         'a' : { 110 : 'a' },
2050         'd' : { 110 : 'd' }
2051     },
2052     811 : {
2053         'a' : { 111 : 'a' },
2054         'd' : { 111 : 'd' }
2055     },
2056     830 : {
2057         'a' : { 130 : 'a' },
2058         'd' : { 130 : 'd' }
2059     },
2060     600 : {
2061         'a' : { 100 : 'a' },
2062         'd' : { 100 : 'd' },
2063         'q' : { 100 : 'q' },
2064         't' : { 100 : 't' },
2065         'v' : { 180 : 'v',
2066             100 : 'v',
2067             181 : 'v',
2068             182 : 'v',
2069             185 : 'v'
2070         },
2071         'x' : { 180 : 'x',
2072             100 : 'x',
2073             181 : 'x',
2074             182 : 'x',
2075             185 : 'x'
2076         },
2077         'y' : { 180 : 'y',
2078             100 : 'y',
2079             181 : 'y',
2080             182 : 'y',
2081             185 : 'y'
2082         },
2083         'z' : { 180 : 'z',
2084             100 : 'z',
2085             181 : 'z',
2086             182 : 'z',
2087             185 : 'z'
2088         }
2089     },
2090     610 : {
2091         'a' : { 110 : 'a' },
2092         'd' : { 110 : 'd' },
2093         't' : { 110 : 't' },
2094         'v' : { 180 : 'v',
2095             110 : 'v',
2096             181 : 'v',
2097             182 : 'v',
2098             185 : 'v'
2099         },
2100         'x' : { 180 : 'x',
2101             110 : 'x',
2102             181 : 'x',
2103             182 : 'x',
2104             185 : 'x'
2105         },
2106         'y' : { 180 : 'y',
2107             110 : 'y',
2108             181 : 'y',
2109             182 : 'y',
2110             185 : 'y'
2111         },
2112         'z' : { 180 : 'z',
2113             110 : 'z',
2114             181 : 'z',
2115             182 : 'z',
2116             185 : 'z'
2117         }
2118     },
2119     611 : {
2120         'a' : { 111 : 'a' },
2121         'd' : { 111 : 'd' },
2122         't' : { 111 : 't' },
2123         'v' : { 180 : 'v',
2124             111 : 'v',
2125             181 : 'v',
2126             182 : 'v',
2127             185 : 'v'
2128         },
2129         'x' : { 180 : 'x',
2130             111 : 'x',
2131             181 : 'x',
2132             182 : 'x',
2133             185 : 'x'
2134         },
2135         'y' : { 180 : 'y',
2136             111 : 'y',
2137             181 : 'y',
2138             182 : 'y',
2139             185 : 'y'
2140         },
2141         'z' : { 180 : 'z',
2142             111 : 'z',
2143             181 : 'z',
2144             182 : 'z',
2145             185 : 'z'
2146         }
2147     },
2148     630 : {
2149         'a' : { 130 : 'a' },
2150         'd' : { 130 : 'd' }
2151     },
2152     648 : {
2153         'a' : { 148 : 'a' },
2154         'v' : { 148 : 'v' },
2155         'x' : { 148 : 'x' },
2156         'y' : { 148 : 'y' },
2157         'z' : { 148 : 'z' }
2158     },
2159     650 : {
2160         'a' : { 150 : 'a' },
2161         'b' : { 150 : 'b' },
2162         'v' : { 180 : 'v',
2163             150 : 'v',
2164             181 : 'v',
2165             182 : 'v',
2166             185 : 'v'
2167         },
2168         'x' : { 180 : 'x',
2169             150 : 'x',
2170             181 : 'x',
2171             182 : 'x',
2172             185 : 'x'
2173         },
2174         'y' : { 180 : 'y',
2175             150 : 'y',
2176             181 : 'y',
2177             182 : 'y',
2178             185 : 'y'
2179         },
2180         'z' : { 180 : 'z',
2181             150 : 'z',
2182             181 : 'z',
2183             182 : 'z',
2184             185 : 'z'
2185         }
2186     },
2187     651 : {
2188         'a' : { 151 : 'a' },
2189         'v' : { 180 : 'v',
2190             151 : 'v',
2191             181 : 'v',
2192             182 : 'v',
2193             185 : 'v'
2194         },
2195         'x' : { 180 : 'x',
2196             151 : 'x',
2197             181 : 'x',
2198             182 : 'x',
2199             185 : 'x'
2200         },
2201         'y' : { 180 : 'y',
2202             151 : 'y',
2203             181 : 'y',
2204             182 : 'y',
2205             185 : 'y'
2206         },
2207         'z' : { 180 : 'z',
2208             151 : 'z',
2209             181 : 'z',
2210             182 : 'z',
2211             185 : 'z'
2212         }
2213     },
2214     655 : {
2215         'a' : { 155 : 'a' },
2216         'v' : { 180 : 'v',
2217             155 : 'v',
2218             181 : 'v',
2219             182 : 'v',
2220             185 : 'v'
2221         },
2222         'x' : { 180 : 'x',
2223             155 : 'x',
2224             181 : 'x',
2225             182 : 'x',
2226             185 : 'x'
2227         },
2228         'y' : { 180 : 'y',
2229             155 : 'y',
2230             181 : 'y',
2231             182 : 'y',
2232             185 : 'y'
2233         },
2234         'z' : { 180 : 'z',
2235             155 : 'z',
2236             181 : 'z',
2237             182 : 'z',
2238             185 : 'z'
2239         }
2240     }
2241 };
2242
2243 function validateAuthority (button) {
2244     var grid = document.getElementById('recGrid');
2245     var label = button.getAttribute('label');
2246
2247     //loop over rows
2248     var rows = grid.lastChild.childNodes;
2249     for (var i = 0; i < rows.length; i++) {
2250         var row = rows[i];
2251         var tag = row.firstChild;
2252
2253         if (!control_map[tag.value]) continue
2254         button.setAttribute('label', label + ' - ' + tag.value);
2255
2256         var ind1 = tag.nextSibling;
2257         var ind2 = ind1.nextSibling;
2258         var subfields = ind2.nextSibling.childNodes;
2259
2260         var tags = {};
2261
2262         for (var j = 0; j < subfields.length; j++) {
2263             var sf = subfields[j];
2264             var sf_code = sf.childNodes[1].value;
2265             var sf_value = sf.childNodes[2].value;
2266
2267             if (!control_map[tag.value][sf_code]) continue;
2268
2269             var found = 0;
2270             for (var a_tag in control_map[tag.value][sf_code]) {
2271                 if (!tags[a_tag]) tags[a_tag] = [];
2272                 tags[a_tag].push({ term : sf_value, subfield : sf_code });
2273             }
2274
2275         }
2276
2277         for (var val_tag in tags) {
2278             var auth_data = validateBibField( [val_tag], tags[val_tag]);
2279             var res = new XML( auth_data.responseText );
2280             found = parseInt(res.gw::payload.gw::string.toString());
2281             if (found) break;
2282         }
2283
2284         // XXX If adt, etc should be validated separately from vxz, etc then move this up into the above for loop
2285         for (var j = 0; j < subfields.length; j++) {
2286             var sf = subfields[j];
2287             if (!found) {
2288                 dojo.removeClass(sf.childNodes[2], 'marcValidated');
2289                 dojo.addClass(sf.childNodes[2], 'marcUnvalidated');
2290             } else {
2291                 dojo.removeClass(sf.childNodes[2], 'marcUnvalidated');
2292                 dojo.addClass(sf.childNodes[2], 'marcValidated');
2293             }
2294         }
2295     }
2296
2297     button.setAttribute('label', label);
2298
2299     return true;
2300 }
2301
2302
2303 function validateBibField (tags, searches) {
2304     var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.validate.tag";
2305     url += '&param="tags"&param=' + js2JSON(tags);
2306     url += '&param="searches"&param=' + js2JSON(searches);
2307
2308
2309     var req = new XMLHttpRequest();
2310     req.open('GET',url,false);
2311     req.send(null);
2312
2313     return req;
2314
2315 }
2316 function searchAuthority (term, tag, sf, limit) {
2317     var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.fts";
2318     url += '&param="term"&param="' + term + '"';
2319     url += '&param="limit"&param=' + limit;
2320     url += '&param="tag"&param=' + tag;
2321     url += '&param="subfield"&param="' + sf + '"';
2322
2323
2324     var req = new XMLHttpRequest();
2325     req.open('GET',url,false);
2326     req.send(null);
2327
2328     return req;
2329
2330 }
2331
2332 function browseAuthority (sf_popup, menu_id, target, sf, limit, page) {
2333     dojo.require('dojox.xml.parser');
2334
2335     // map tag + subfield to the appropriate authority browse axis:
2336     // currently authority.author, authority.subject, authority.title, authority.topic
2337     // based on mappings in OpenILS::Application::SuperCat
2338
2339     var type;
2340
2341     // Map based on replacing the first char of the selected tag with '1'
2342     switch ('1' + (sf.parent().@tag.toString()).substring(1)) {
2343         case "130":
2344             type = 'authority.title';
2345             break;
2346
2347         case "100":
2348         case "110":
2349         case "111":
2350             type = 'authority.author';
2351             break;
2352
2353         case "150":
2354             type = 'authority.topic';
2355             break;
2356
2357         case "148":
2358         case "151":
2359         case "155":
2360             type = 'authority.subject';
2361             break;
2362
2363         // No matching tag means no authorities to search - shortcut
2364         default:
2365             return;
2366     }
2367
2368     if (!limit) {
2369         limit = 10;
2370     }
2371
2372     if (!page) {
2373         page = 0;
2374     }
2375
2376     var url = '/opac/extras/browse/marcxml/'
2377         + type + '.refs'
2378         + '/1' // OU - currently unscoped
2379         + '/' + sf.toString()
2380         + '/' + page
2381         + '/' + limit
2382     ;
2383
2384     // would be good to carve this out into a separate function
2385     dojo.xhrGet({"url":url, "sync": true, "preventCache": true, "handleAs":"xml", "load": function(records) {
2386         var create_menu = createMenu({ label: $('catStrings').getString('staff.cat.marcedit.create_authority.label')});
2387
2388         var cm_popup = create_menu.appendChild(
2389             createMenuPopup()
2390         );
2391
2392         cm_popup.appendChild(
2393             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_now.label'),
2394                 command : function() { 
2395                     // Call middle-layer function to create and save the new authority
2396                     var source_f = summarizeField(sf);
2397                     var new_auth = fieldmapper.standardRequest(
2398                         ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib"],
2399                         [source_f, xulG.marc_control_number_identifier, ses()]
2400                     );
2401                     if (new_auth && new_auth.id()) {
2402                         var id_sf = <subfield code="0" xmlns="http://www.loc.gov/MARC21/slim">({xulG.marc_control_number_identifier}){new_auth.id()}</subfield>;
2403                         sf.parent().appendChild(id_sf);
2404                         var new_sf = marcSubfield(id_sf);
2405                         target.parentNode.appendChild(new_sf);
2406                         alert($('catStrings').getString('staff.cat.marcedit.create_authority_success.label'));
2407                     }
2408                 }
2409             })
2410         );
2411
2412         cm_popup.appendChild(
2413             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_edit.label'),
2414                 command : function() { 
2415                     // Generate the new authority by calling the new middle-layer
2416                     // function (a non-saving variant), then display in another
2417                     // MARC editor
2418                     var source_f = summarizeField(sf);
2419                     var authtoken = ses();
2420                     dojo.require('openils.PermaCrud');
2421                     var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2422                     var rec = fieldmapper.standardRequest(
2423                         ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib.readonly"],
2424                         { "params": [source_f, xulG.marc_control_number_identifier] }
2425                     );
2426                     loadMarcEditor(pcrud, rec, target, sf);
2427                 }
2428             })
2429         );
2430
2431         sf_popup.appendChild(create_menu);
2432         sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2433
2434         // append "Previous page" results browser
2435         sf_popup.appendChild(
2436             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.previous_page.label'),
2437                 command : function(event) { 
2438                     auth_pages[menu_id] -= 1;
2439                     show_auth_menu = true;
2440                 }
2441             })
2442         );
2443         sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2444
2445         dojo.query('record', records).forEach(function(record) {
2446             var main_text = '';
2447             var see_from = [];
2448             var see_also = [];
2449             var auth_id = dojox.xml.parser.textContent(dojo.query('datafield[tag="901"] subfield[code="c"]', record)[0]);
2450             var auth_org = dojox.xml.parser.textContent(dojo.query('controlfield[tag="003"]', record)[0]);
2451
2452             // Grab the fields with tags beginning with 1 (main entries) and iterate through the subfields
2453             dojo.query('datafield[tag^="1"]', record).forEach(function(field) {
2454                 dojo.query('subfield', field).forEach(function(subfield) {
2455                     if (main_text) {
2456                         main_text += ' / ';
2457                     }
2458                     main_text += dojox.xml.parser.textContent(subfield);
2459                 });
2460             });
2461
2462             // Grab the fields with tags beginning with 4 (see from entries) and iterate through the subfields
2463             dojo.query('datafield[tag^="4"]', record).forEach(function(field) {
2464                 var see_text = '';
2465                 dojo.query('subfield', field).forEach(function(subfield) {
2466                     if (see_text) {
2467                         see_text += ' / ';
2468                     }
2469                     see_text += dojox.xml.parser.textContent(subfield);
2470                 });
2471                 see_from.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_from', [see_text]));
2472             });
2473
2474             // Grab the fields with tags beginning with 5 (see also entries) and iterate through the subfields
2475             dojo.query('datafield[tag^="5"]', record).forEach(function(field) {
2476                 var see_text = '';
2477                 dojo.query('subfield', field).forEach(function(subfield) {
2478                     if (see_text) {
2479                         see_text += ' / ';
2480                     }
2481                     see_text += dojox.xml.parser.textContent(subfield);
2482                 });
2483                 see_also.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_also', [see_text]));
2484             });
2485
2486             buildAuthorityPopup(main_text, record, auth_org, auth_id, sf_popup, target, sf);
2487
2488             dojo.forEach(see_from, function(entry_text) {
2489                 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2490             });
2491
2492             // To-do: instead of launching the standard selector menu, invoke
2493             // a new authority search using the 5XX entry text
2494             dojo.forEach(see_also, function(entry_text) {
2495                 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2496             });
2497
2498         });
2499
2500         if (sf_popup.childNodes.length == 0) {
2501             sf_popup.appendChild(createLabel( { value : $('catStrings').getString('staff.cat.marcedit.no_authority_match.label') } ) );
2502         } else {
2503             // append "Next page" results browser
2504             sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2505             sf_popup.appendChild(
2506                 createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.next_page.label'),
2507                     command : function(event) { 
2508                         auth_pages[menu_id] += 1;
2509                         show_auth_menu = true;
2510                     }
2511                 })
2512             );
2513         }
2514
2515         target.setAttribute('context', menu_id);
2516         return true;
2517     }});
2518
2519 }
2520
2521 function buildAuthorityPopup (entry_text, record, auth_org, auth_id, sf_popup, target, sf, style) {
2522     var grid = dojo.query('[name="authority-marc-template"]')[0].cloneNode(true);
2523     grid.setAttribute('name','-none-');
2524     grid.setAttribute('style','overflow:scroll');
2525
2526     var submenu = createMenu( { "label": entry_text } );
2527
2528     var popup = createMenuPopup({ "flex": "1" });
2529     if (style) {
2530         submenu.setAttribute('style', style);
2531         popup.setAttribute('style', 'font-style: normal; margin-left: 0em;');
2532     }
2533     submenu.appendChild(popup);
2534
2535     dojo.query('datafield[tag^="1"], datafield[tag^="4"], datafield[tag^="5"]', record).forEach(function(field) {
2536         buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2537     });
2538
2539     grid.hidden = false;
2540     popup.appendChild( grid );
2541
2542     popup.appendChild(
2543         createMenuitem(
2544             { label : $('catStrings').getString('staff.cat.marcedit.apply_selected.label'),
2545               command : function (event) {
2546                     applySelectedAuthority(event.target.previousSibling, target, sf);
2547                     return true;
2548               }
2549             }
2550         )
2551     );
2552
2553     popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2554
2555     popup.appendChild(
2556         createMenuitem(
2557             { label : $('catStrings').getString('staff.cat.marcedit.apply_full.label'),
2558               command : function (event) {
2559                     applyFullAuthority(event.target.previousSibling.previousSibling.previousSibling, target, sf);
2560                     return true;
2561               }
2562             }
2563         )
2564     );
2565
2566     sf_popup.appendChild( submenu );
2567 }
2568
2569 function buildAuthorityPopupSelector (field, grid, auth_org, auth_id) {
2570     var row = createRow(
2571         { },
2572         createLabel( { "value" : dojo.attr(field, 'tag') } ),
2573         createLabel( { "value" : dojo.attr(field, 'ind1') } ),
2574         createLabel( { "value" : dojo.attr(field, 'ind2') } )
2575     );
2576
2577     var sf_box = createHbox();
2578     dojo.query('subfield', field).forEach(function(subfield) {
2579         sf_box.appendChild(
2580             createCheckbox(
2581                 { "label"    : '\u2021' + dojo.attr(subfield, 'code') + ' ' + dojox.xml.parser.textContent(subfield),
2582                   "subfield" : dojo.attr(subfield, 'code'),
2583                   "tag"      : dojo.attr(field, 'tag'),
2584                   "value"    : dojox.xml.parser.textContent(subfield)
2585                 }
2586             )
2587         );
2588         row.appendChild(sf_box);
2589     });
2590
2591     // Append the authority linking subfield only for main entries
2592     if (dojo.attr(field, 'tag').charAt(0) == '1') {
2593         sf_box.appendChild(
2594             createCheckbox(
2595                 { "label"    : '\u2021' + '0' + ' (' + auth_org + ')' + auth_id,
2596                   "subfield" : '0',
2597                   "tag"      : dojo.attr(field, 'tag'),
2598                   "value"    : '(' + auth_org + ')' + auth_id
2599                 }
2600             )
2601         );
2602     }
2603     row.appendChild(sf_box);
2604
2605     grid.lastChild.appendChild(row);
2606 }
2607
2608 function summarizeField(sf) {
2609     var source_f= {
2610         "tag": '',
2611         "ind1": '',
2612         "ind2": '',
2613         "subfields": []
2614     };
2615     for (var i = 0; i < sf.parent().subfield.length(); i++) {
2616         source_f.subfields.push([sf.parent().subfield[i].@code.toString(), sf.parent().subfield[i].toString()]);
2617     }
2618     source_f.tag = sf.parent().@tag.toString();
2619     source_f.ind1 = sf.parent().@ind1.toString();
2620     source_f.ind1 = sf.parent().@ind2.toString();
2621     return source_f;
2622 }
2623
2624
2625 function buildBibSourceList (authtoken, recId) {
2626     /* TODO: Work out how to set the bib source of the bre that does not yet
2627      * exist - this is specifically in the case of Z39.50 imports. Right now
2628      * we just avoid populating and showing the config.bib_source list
2629      */
2630     if (!recId) {
2631         return false;
2632     }
2633
2634     var bib = xulG.record.bre;
2635
2636     dojo.require('openils.PermaCrud');
2637
2638     // cbsList = the XUL menulist that contains the available bib sources 
2639     var cbsList = dojo.byId('bib-source-list');
2640
2641     // bibSources = an array containing all of the bib source objects
2642     var bibSources = new openils.PermaCrud({"authtoken": authtoken}).retrieveAll('cbs');
2643
2644     // A tad ugly, but gives us the index of the bib source ID in cbsList
2645     var x = 0;
2646     var cbsListArr = [];
2647     dojo.forEach(bibSources, function (item) {
2648         cbsList.appendItem(item.source(), item.id());
2649         cbsListArr[item.id()] = x;
2650         x++;
2651     });
2652
2653     // Show the current value of the bib source for this record
2654     cbsList.selectedIndex = cbsListArr[bib.source()];
2655
2656     // Display the bib source selection widget
2657     dojo.byId('bib-source-list-caption').hidden = false;
2658     dojo.byId('bib-source-list').hidden = false;
2659     dojo.byId('bib-source-list-button').disabled = true;
2660     dojo.byId('bib-source-list-button').hidden = false;
2661 }
2662
2663 // Fired when the "Update Source" button is clicked
2664 // Updates the value of the bib source for the current record
2665 function updateBibSource() {
2666     var authtoken = ses();
2667     var cbs = dojo.byId('bib-source-list').selectedItem.value;
2668     var recId = xulG.record.id;
2669     var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2670     var bib = pcrud.retrieve('bre', recId);
2671     if (bib.source() != cbs) {
2672         bib.source(cbs);
2673         bib.ischanged = true;
2674         pcrud.update(bib);
2675     }
2676 }
2677
2678 function onBibSourceSelect() {
2679     var cbs = dojo.byId('bib-source-list').selectedItem.value;
2680     var bib = xulG.record.bre;
2681     if (bib.source() != cbs) {
2682         dojo.byId('bib-source-list-button').disabled = false;   
2683     } else {
2684         dojo.byId('bib-source-list-button').disabled = true;   
2685     }
2686 }
2687
2688 function loadMarcEditor(pcrud, marcxml, target, sf) {
2689     /*
2690        To run in Firefox directly, must set signed.applets.codebase_principal_support
2691        to true in about:config
2692      */
2693     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
2694     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
2695
2696     // Match marc2are.pl last_xact_id format, roughly
2697     var now = new Date;
2698     var xact_id = 'IMPORT-' + Date.parse(now);
2699     
2700     win.xulG = {
2701         "record": {"marc": marcxml, "rtype": "are"},
2702         "save": {
2703             "label": $('catStrings').getString('staff.cat.marcedit.save.label'),
2704             "func": function(xmlString) {
2705                 var rec = new are();
2706                 rec.marc(xmlString);
2707                 rec.last_xact_id(xact_id);
2708                 rec.isnew(true);
2709                 pcrud.create(rec, {
2710                     "oncomplete": function (r, objs) {
2711                         var new_rec = objs[0];
2712                         if (!new_rec) {
2713                             return '';
2714                         }
2715                         var id_sf = <subfield code="0" xmlns="http://www.loc.gov/MARC21/slim">({xulG.marc_control_number_identifier}){new_rec.id()}</subfield>;
2716                         sf.parent().appendChild(id_sf);
2717                         var new_sf = marcSubfield(id_sf);
2718                         target.parentNode.appendChild(new_sf);
2719                         alert($('catStrings').getString('staff.cat.marcedit.create_authority_success.label'));
2720                         win.close();
2721                     }
2722                 });
2723             }
2724         }
2725     };
2726 }
2727
2728