]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/marcedit.js
Prevent authority context menu on whitespace content
[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     if (sf.toString().replace(/\s*/, '')) {
1932         browseAuthority(sf_popup, menu_id, target, sf, 20, page);
1933     }
1934
1935     return true;
1936 }
1937
1938 /* Apply the complete 1xx */
1939 function applyFullAuthority ( target, ui_sf, e4x_sf ) {
1940     var new_vals = dojo.query('*[tag^="1"]', target);
1941     return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1942 }
1943
1944 function applySelectedAuthority ( target, ui_sf, e4x_sf ) {
1945     var new_vals = target.getElementsByAttribute('checked','true');
1946     return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1947 }
1948
1949 function applyAuthority ( target, ui_sf, e4x_sf, new_vals ) {
1950     var field = e4x_sf.parent();
1951
1952     for (var i = 0; i < new_vals.length; i++) {
1953
1954         var sf_list = field.subfield;
1955         for (var j in sf_list) {
1956
1957             if (sf_list[j].@code == new_vals[i].getAttribute('subfield')) {
1958                 sf_list[j] = new_vals[i].getAttribute('value');
1959                 new_vals[i].setAttribute('subfield','');
1960                 break;
1961             }
1962         }
1963     }
1964
1965     for (var i = 0; i < new_vals.length; i++) {
1966         if (!new_vals[i].getAttribute('subfield')) continue;
1967
1968         var val = new_vals[i].getAttribute('value');
1969
1970         var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{val}</subfield>;
1971         sf.@code = new_vals[i].getAttribute('subfield');
1972
1973         field.insertChildAfter(field.subfield[field.subfield.length() - 1], sf);
1974     }
1975
1976     var row = marcDatafield( field );
1977
1978     var node = ui_sf;
1979     while (node.nodeName != 'row') {
1980         node = node.parentNode;
1981     }
1982
1983     node.parentNode.replaceChild( row, node );
1984     return true;
1985 }
1986
1987 var control_map = {
1988     100 : {
1989         'a' : { 100 : 'a' },
1990         'd' : { 100 : 'd' },
1991         'e' : { 100 : 'e' },
1992         'q' : { 100 : 'q' }
1993     },
1994     110 : {
1995         'a' : { 110 : 'a' },
1996         'd' : { 110 : 'd' }
1997     },
1998     111 : {
1999         'a' : { 111 : 'a' },
2000         'd' : { 111 : 'd' }
2001     },
2002     130 : {
2003         'a' : { 130 : 'a' },
2004         'd' : { 130 : 'd' }
2005     },
2006     240 : {
2007         'a' : { 130 : 'a' },
2008         'd' : { 130 : 'd' }
2009     },
2010     400 : {
2011         'a' : { 100 : 'a' },
2012         'd' : { 100 : 'd' }
2013     },
2014     410 : {
2015         'a' : { 110 : 'a' },
2016         'd' : { 110 : 'd' }
2017     },
2018     411 : {
2019         'a' : { 111 : 'a' },
2020         'd' : { 111 : 'd' }
2021     },
2022     440 : {
2023         'a' : { 130 : 'a' },
2024         'n' : { 130 : 'n' },
2025         'p' : { 130 : 'p' }
2026     },
2027     700 : {
2028         'a' : { 100 : 'a' },
2029         'd' : { 100 : 'd' },
2030         'q' : { 100 : 'q' },
2031         't' : { 100 : 't' }
2032     },
2033     710 : {
2034         'a' : { 110 : 'a' },
2035         'd' : { 110 : 'd' }
2036     },
2037     711 : {
2038         'a' : { 111 : 'a' },
2039         'c' : { 111 : 'c' },
2040         'd' : { 111 : 'd' }
2041     },
2042     730 : {
2043         'a' : { 130 : 'a' },
2044         'd' : { 130 : 'd' }
2045     },
2046     800 : {
2047         'a' : { 100 : 'a' },
2048         'd' : { 100 : 'd' }
2049     },
2050     810 : {
2051         'a' : { 110 : 'a' },
2052         'd' : { 110 : 'd' }
2053     },
2054     811 : {
2055         'a' : { 111 : 'a' },
2056         'd' : { 111 : 'd' }
2057     },
2058     830 : {
2059         'a' : { 130 : 'a' },
2060         'd' : { 130 : 'd' }
2061     },
2062     600 : {
2063         'a' : { 100 : 'a' },
2064         'd' : { 100 : 'd' },
2065         'q' : { 100 : 'q' },
2066         't' : { 100 : 't' },
2067         'v' : { 180 : 'v',
2068             100 : 'v',
2069             181 : 'v',
2070             182 : 'v',
2071             185 : 'v'
2072         },
2073         'x' : { 180 : 'x',
2074             100 : 'x',
2075             181 : 'x',
2076             182 : 'x',
2077             185 : 'x'
2078         },
2079         'y' : { 180 : 'y',
2080             100 : 'y',
2081             181 : 'y',
2082             182 : 'y',
2083             185 : 'y'
2084         },
2085         'z' : { 180 : 'z',
2086             100 : 'z',
2087             181 : 'z',
2088             182 : 'z',
2089             185 : 'z'
2090         }
2091     },
2092     610 : {
2093         'a' : { 110 : 'a' },
2094         'd' : { 110 : 'd' },
2095         't' : { 110 : 't' },
2096         'v' : { 180 : 'v',
2097             110 : 'v',
2098             181 : 'v',
2099             182 : 'v',
2100             185 : 'v'
2101         },
2102         'x' : { 180 : 'x',
2103             110 : 'x',
2104             181 : 'x',
2105             182 : 'x',
2106             185 : 'x'
2107         },
2108         'y' : { 180 : 'y',
2109             110 : 'y',
2110             181 : 'y',
2111             182 : 'y',
2112             185 : 'y'
2113         },
2114         'z' : { 180 : 'z',
2115             110 : 'z',
2116             181 : 'z',
2117             182 : 'z',
2118             185 : 'z'
2119         }
2120     },
2121     611 : {
2122         'a' : { 111 : 'a' },
2123         'd' : { 111 : 'd' },
2124         't' : { 111 : 't' },
2125         'v' : { 180 : 'v',
2126             111 : 'v',
2127             181 : 'v',
2128             182 : 'v',
2129             185 : 'v'
2130         },
2131         'x' : { 180 : 'x',
2132             111 : 'x',
2133             181 : 'x',
2134             182 : 'x',
2135             185 : 'x'
2136         },
2137         'y' : { 180 : 'y',
2138             111 : 'y',
2139             181 : 'y',
2140             182 : 'y',
2141             185 : 'y'
2142         },
2143         'z' : { 180 : 'z',
2144             111 : 'z',
2145             181 : 'z',
2146             182 : 'z',
2147             185 : 'z'
2148         }
2149     },
2150     630 : {
2151         'a' : { 130 : 'a' },
2152         'd' : { 130 : 'd' }
2153     },
2154     648 : {
2155         'a' : { 148 : 'a' },
2156         'v' : { 148 : 'v' },
2157         'x' : { 148 : 'x' },
2158         'y' : { 148 : 'y' },
2159         'z' : { 148 : 'z' }
2160     },
2161     650 : {
2162         'a' : { 150 : 'a' },
2163         'b' : { 150 : 'b' },
2164         'v' : { 180 : 'v',
2165             150 : 'v',
2166             181 : 'v',
2167             182 : 'v',
2168             185 : 'v'
2169         },
2170         'x' : { 180 : 'x',
2171             150 : 'x',
2172             181 : 'x',
2173             182 : 'x',
2174             185 : 'x'
2175         },
2176         'y' : { 180 : 'y',
2177             150 : 'y',
2178             181 : 'y',
2179             182 : 'y',
2180             185 : 'y'
2181         },
2182         'z' : { 180 : 'z',
2183             150 : 'z',
2184             181 : 'z',
2185             182 : 'z',
2186             185 : 'z'
2187         }
2188     },
2189     651 : {
2190         'a' : { 151 : 'a' },
2191         'v' : { 180 : 'v',
2192             151 : 'v',
2193             181 : 'v',
2194             182 : 'v',
2195             185 : 'v'
2196         },
2197         'x' : { 180 : 'x',
2198             151 : 'x',
2199             181 : 'x',
2200             182 : 'x',
2201             185 : 'x'
2202         },
2203         'y' : { 180 : 'y',
2204             151 : 'y',
2205             181 : 'y',
2206             182 : 'y',
2207             185 : 'y'
2208         },
2209         'z' : { 180 : 'z',
2210             151 : 'z',
2211             181 : 'z',
2212             182 : 'z',
2213             185 : 'z'
2214         }
2215     },
2216     655 : {
2217         'a' : { 155 : 'a' },
2218         'v' : { 180 : 'v',
2219             155 : 'v',
2220             181 : 'v',
2221             182 : 'v',
2222             185 : 'v'
2223         },
2224         'x' : { 180 : 'x',
2225             155 : 'x',
2226             181 : 'x',
2227             182 : 'x',
2228             185 : 'x'
2229         },
2230         'y' : { 180 : 'y',
2231             155 : 'y',
2232             181 : 'y',
2233             182 : 'y',
2234             185 : 'y'
2235         },
2236         'z' : { 180 : 'z',
2237             155 : 'z',
2238             181 : 'z',
2239             182 : 'z',
2240             185 : 'z'
2241         }
2242     }
2243 };
2244
2245 function validateAuthority (button) {
2246     var grid = document.getElementById('recGrid');
2247     var label = button.getAttribute('label');
2248
2249     //loop over rows
2250     var rows = grid.lastChild.childNodes;
2251     for (var i = 0; i < rows.length; i++) {
2252         var row = rows[i];
2253         var tag = row.firstChild;
2254
2255         if (!control_map[tag.value]) continue
2256         button.setAttribute('label', label + ' - ' + tag.value);
2257
2258         var ind1 = tag.nextSibling;
2259         var ind2 = ind1.nextSibling;
2260         var subfields = ind2.nextSibling.childNodes;
2261
2262         var tags = {};
2263
2264         for (var j = 0; j < subfields.length; j++) {
2265             var sf = subfields[j];
2266             var sf_code = sf.childNodes[1].value;
2267             var sf_value = sf.childNodes[2].value;
2268
2269             if (!control_map[tag.value][sf_code]) continue;
2270
2271             var found = 0;
2272             for (var a_tag in control_map[tag.value][sf_code]) {
2273                 if (!tags[a_tag]) tags[a_tag] = [];
2274                 tags[a_tag].push({ term : sf_value, subfield : sf_code });
2275             }
2276
2277         }
2278
2279         for (var val_tag in tags) {
2280             var auth_data = validateBibField( [val_tag], tags[val_tag]);
2281             var res = new XML( auth_data.responseText );
2282             found = parseInt(res.gw::payload.gw::string.toString());
2283             if (found) break;
2284         }
2285
2286         // XXX If adt, etc should be validated separately from vxz, etc then move this up into the above for loop
2287         for (var j = 0; j < subfields.length; j++) {
2288             var sf = subfields[j];
2289             if (!found) {
2290                 dojo.removeClass(sf.childNodes[2], 'marcValidated');
2291                 dojo.addClass(sf.childNodes[2], 'marcUnvalidated');
2292             } else {
2293                 dojo.removeClass(sf.childNodes[2], 'marcUnvalidated');
2294                 dojo.addClass(sf.childNodes[2], 'marcValidated');
2295             }
2296         }
2297     }
2298
2299     button.setAttribute('label', label);
2300
2301     return true;
2302 }
2303
2304
2305 function validateBibField (tags, searches) {
2306     var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.validate.tag";
2307     url += '&param="tags"&param=' + js2JSON(tags);
2308     url += '&param="searches"&param=' + js2JSON(searches);
2309
2310
2311     var req = new XMLHttpRequest();
2312     req.open('GET',url,false);
2313     req.send(null);
2314
2315     return req;
2316
2317 }
2318 function searchAuthority (term, tag, sf, limit) {
2319     var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.fts";
2320     url += '&param="term"&param="' + term + '"';
2321     url += '&param="limit"&param=' + limit;
2322     url += '&param="tag"&param=' + tag;
2323     url += '&param="subfield"&param="' + sf + '"';
2324
2325
2326     var req = new XMLHttpRequest();
2327     req.open('GET',url,false);
2328     req.send(null);
2329
2330     return req;
2331
2332 }
2333
2334 function browseAuthority (sf_popup, menu_id, target, sf, limit, page) {
2335     dojo.require('dojox.xml.parser');
2336
2337     // map tag + subfield to the appropriate authority browse axis:
2338     // currently authority.author, authority.subject, authority.title, authority.topic
2339     // based on mappings in OpenILS::Application::SuperCat
2340
2341     var type;
2342
2343     // Map based on replacing the first char of the selected tag with '1'
2344     switch ('1' + (sf.parent().@tag.toString()).substring(1)) {
2345         case "130":
2346             type = 'authority.title';
2347             break;
2348
2349         case "100":
2350         case "110":
2351         case "111":
2352             type = 'authority.author';
2353             break;
2354
2355         case "150":
2356             type = 'authority.topic';
2357             break;
2358
2359         case "148":
2360         case "151":
2361         case "155":
2362             type = 'authority.subject';
2363             break;
2364
2365         // No matching tag means no authorities to search - shortcut
2366         default:
2367             return;
2368     }
2369
2370     if (!limit) {
2371         limit = 10;
2372     }
2373
2374     if (!page) {
2375         page = 0;
2376     }
2377
2378     var url = '/opac/extras/browse/marcxml/'
2379         + type + '.refs'
2380         + '/1' // OU - currently unscoped
2381         + '/' + sf.toString()
2382         + '/' + page
2383         + '/' + limit
2384     ;
2385
2386     // would be good to carve this out into a separate function
2387     dojo.xhrGet({"url":url, "sync": true, "preventCache": true, "handleAs":"xml", "load": function(records) {
2388         var create_menu = createMenu({ label: $('catStrings').getString('staff.cat.marcedit.create_authority.label')});
2389
2390         var cm_popup = create_menu.appendChild(
2391             createMenuPopup()
2392         );
2393
2394         cm_popup.appendChild(
2395             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_now.label'),
2396                 command : function() { 
2397                     // Call middle-layer function to create and save the new authority
2398                     var source_f = summarizeField(sf);
2399                     var new_auth = fieldmapper.standardRequest(
2400                         ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib"],
2401                         [source_f, xulG.marc_control_number_identifier, ses()]
2402                     );
2403                     if (new_auth && new_auth.id()) {
2404                         var id_sf = <subfield code="0" xmlns="http://www.loc.gov/MARC21/slim">({xulG.marc_control_number_identifier}){new_auth.id()}</subfield>;
2405                         sf.parent().appendChild(id_sf);
2406                         var new_sf = marcSubfield(id_sf);
2407                         target.parentNode.appendChild(new_sf);
2408                         alert($('catStrings').getString('staff.cat.marcedit.create_authority_success.label'));
2409                     }
2410                 }
2411             })
2412         );
2413
2414         cm_popup.appendChild(
2415             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_edit.label'),
2416                 command : function() { 
2417                     // Generate the new authority by calling the new middle-layer
2418                     // function (a non-saving variant), then display in another
2419                     // MARC editor
2420                     var source_f = summarizeField(sf);
2421                     var authtoken = ses();
2422                     dojo.require('openils.PermaCrud');
2423                     var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2424                     var rec = fieldmapper.standardRequest(
2425                         ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib.readonly"],
2426                         { "params": [source_f, xulG.marc_control_number_identifier] }
2427                     );
2428                     loadMarcEditor(pcrud, rec, target, sf);
2429                 }
2430             })
2431         );
2432
2433         sf_popup.appendChild(create_menu);
2434         sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2435
2436         // append "Previous page" results browser
2437         sf_popup.appendChild(
2438             createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.previous_page.label'),
2439                 command : function(event) { 
2440                     auth_pages[menu_id] -= 1;
2441                     show_auth_menu = true;
2442                 }
2443             })
2444         );
2445         sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2446
2447         dojo.query('record', records).forEach(function(record) {
2448             var main_text = '';
2449             var see_from = [];
2450             var see_also = [];
2451             var auth_id = dojox.xml.parser.textContent(dojo.query('datafield[tag="901"] subfield[code="c"]', record)[0]);
2452             var auth_org = dojox.xml.parser.textContent(dojo.query('controlfield[tag="003"]', record)[0]);
2453
2454             // Grab the fields with tags beginning with 1 (main entries) and iterate through the subfields
2455             dojo.query('datafield[tag^="1"]', record).forEach(function(field) {
2456                 dojo.query('subfield', field).forEach(function(subfield) {
2457                     if (main_text) {
2458                         main_text += ' / ';
2459                     }
2460                     main_text += dojox.xml.parser.textContent(subfield);
2461                 });
2462             });
2463
2464             // Grab the fields with tags beginning with 4 (see from entries) and iterate through the subfields
2465             dojo.query('datafield[tag^="4"]', record).forEach(function(field) {
2466                 var see_text = '';
2467                 dojo.query('subfield', field).forEach(function(subfield) {
2468                     if (see_text) {
2469                         see_text += ' / ';
2470                     }
2471                     see_text += dojox.xml.parser.textContent(subfield);
2472                 });
2473                 see_from.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_from', [see_text]));
2474             });
2475
2476             // Grab the fields with tags beginning with 5 (see also entries) and iterate through the subfields
2477             dojo.query('datafield[tag^="5"]', record).forEach(function(field) {
2478                 var see_text = '';
2479                 dojo.query('subfield', field).forEach(function(subfield) {
2480                     if (see_text) {
2481                         see_text += ' / ';
2482                     }
2483                     see_text += dojox.xml.parser.textContent(subfield);
2484                 });
2485                 see_also.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_also', [see_text]));
2486             });
2487
2488             buildAuthorityPopup(main_text, record, auth_org, auth_id, sf_popup, target, sf);
2489
2490             dojo.forEach(see_from, function(entry_text) {
2491                 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2492             });
2493
2494             // To-do: instead of launching the standard selector menu, invoke
2495             // a new authority search using the 5XX entry text
2496             dojo.forEach(see_also, function(entry_text) {
2497                 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2498             });
2499
2500         });
2501
2502         if (sf_popup.childNodes.length == 0) {
2503             sf_popup.appendChild(createLabel( { value : $('catStrings').getString('staff.cat.marcedit.no_authority_match.label') } ) );
2504         } else {
2505             // append "Next page" results browser
2506             sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2507             sf_popup.appendChild(
2508                 createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.next_page.label'),
2509                     command : function(event) { 
2510                         auth_pages[menu_id] += 1;
2511                         show_auth_menu = true;
2512                     }
2513                 })
2514             );
2515         }
2516
2517         target.setAttribute('context', menu_id);
2518         return true;
2519     }});
2520
2521 }
2522
2523 function buildAuthorityPopup (entry_text, record, auth_org, auth_id, sf_popup, target, sf, style) {
2524     var grid = dojo.query('[name="authority-marc-template"]')[0].cloneNode(true);
2525     grid.setAttribute('name','-none-');
2526     grid.setAttribute('style','overflow:scroll');
2527
2528     var submenu = createMenu( { "label": entry_text } );
2529
2530     var popup = createMenuPopup({ "flex": "1" });
2531     if (style) {
2532         submenu.setAttribute('style', style);
2533         popup.setAttribute('style', 'font-style: normal; margin-left: 0em;');
2534     }
2535     submenu.appendChild(popup);
2536
2537     dojo.query('datafield[tag^="1"], datafield[tag^="4"], datafield[tag^="5"]', record).forEach(function(field) {
2538         buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2539     });
2540
2541     grid.hidden = false;
2542     popup.appendChild( grid );
2543
2544     popup.appendChild(
2545         createMenuitem(
2546             { label : $('catStrings').getString('staff.cat.marcedit.apply_selected.label'),
2547               command : function (event) {
2548                     applySelectedAuthority(event.target.previousSibling, target, sf);
2549                     return true;
2550               }
2551             }
2552         )
2553     );
2554
2555     popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2556
2557     popup.appendChild(
2558         createMenuitem(
2559             { label : $('catStrings').getString('staff.cat.marcedit.apply_full.label'),
2560               command : function (event) {
2561                     applyFullAuthority(event.target.previousSibling.previousSibling.previousSibling, target, sf);
2562                     return true;
2563               }
2564             }
2565         )
2566     );
2567
2568     sf_popup.appendChild( submenu );
2569 }
2570
2571 function buildAuthorityPopupSelector (field, grid, auth_org, auth_id) {
2572     var row = createRow(
2573         { },
2574         createLabel( { "value" : dojo.attr(field, 'tag') } ),
2575         createLabel( { "value" : dojo.attr(field, 'ind1') } ),
2576         createLabel( { "value" : dojo.attr(field, 'ind2') } )
2577     );
2578
2579     var sf_box = createHbox();
2580     dojo.query('subfield', field).forEach(function(subfield) {
2581         sf_box.appendChild(
2582             createCheckbox(
2583                 { "label"    : '\u2021' + dojo.attr(subfield, 'code') + ' ' + dojox.xml.parser.textContent(subfield),
2584                   "subfield" : dojo.attr(subfield, 'code'),
2585                   "tag"      : dojo.attr(field, 'tag'),
2586                   "value"    : dojox.xml.parser.textContent(subfield)
2587                 }
2588             )
2589         );
2590         row.appendChild(sf_box);
2591     });
2592
2593     // Append the authority linking subfield only for main entries
2594     if (dojo.attr(field, 'tag').charAt(0) == '1') {
2595         sf_box.appendChild(
2596             createCheckbox(
2597                 { "label"    : '\u2021' + '0' + ' (' + auth_org + ')' + auth_id,
2598                   "subfield" : '0',
2599                   "tag"      : dojo.attr(field, 'tag'),
2600                   "value"    : '(' + auth_org + ')' + auth_id
2601                 }
2602             )
2603         );
2604     }
2605     row.appendChild(sf_box);
2606
2607     grid.lastChild.appendChild(row);
2608 }
2609
2610 function summarizeField(sf) {
2611     var source_f= {
2612         "tag": '',
2613         "ind1": '',
2614         "ind2": '',
2615         "subfields": []
2616     };
2617     for (var i = 0; i < sf.parent().subfield.length(); i++) {
2618         source_f.subfields.push([sf.parent().subfield[i].@code.toString(), sf.parent().subfield[i].toString()]);
2619     }
2620     source_f.tag = sf.parent().@tag.toString();
2621     source_f.ind1 = sf.parent().@ind1.toString();
2622     source_f.ind1 = sf.parent().@ind2.toString();
2623     return source_f;
2624 }
2625
2626
2627 function buildBibSourceList (authtoken, recId) {
2628     /* TODO: Work out how to set the bib source of the bre that does not yet
2629      * exist - this is specifically in the case of Z39.50 imports. Right now
2630      * we just avoid populating and showing the config.bib_source list
2631      */
2632     if (!recId) {
2633         return false;
2634     }
2635
2636     var bib = xulG.record.bre;
2637
2638     dojo.require('openils.PermaCrud');
2639
2640     // cbsList = the XUL menulist that contains the available bib sources 
2641     var cbsList = dojo.byId('bib-source-list');
2642
2643     // bibSources = an array containing all of the bib source objects
2644     var bibSources = new openils.PermaCrud({"authtoken": authtoken}).retrieveAll('cbs');
2645
2646     // A tad ugly, but gives us the index of the bib source ID in cbsList
2647     var x = 0;
2648     var cbsListArr = [];
2649     dojo.forEach(bibSources, function (item) {
2650         cbsList.appendItem(item.source(), item.id());
2651         cbsListArr[item.id()] = x;
2652         x++;
2653     });
2654
2655     // Show the current value of the bib source for this record
2656     cbsList.selectedIndex = cbsListArr[bib.source()];
2657
2658     // Display the bib source selection widget
2659     dojo.byId('bib-source-list-caption').hidden = false;
2660     dojo.byId('bib-source-list').hidden = false;
2661     dojo.byId('bib-source-list-button').disabled = true;
2662     dojo.byId('bib-source-list-button').hidden = false;
2663 }
2664
2665 // Fired when the "Update Source" button is clicked
2666 // Updates the value of the bib source for the current record
2667 function updateBibSource() {
2668     var authtoken = ses();
2669     var cbs = dojo.byId('bib-source-list').selectedItem.value;
2670     var recId = xulG.record.id;
2671     var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2672     var bib = pcrud.retrieve('bre', recId);
2673     if (bib.source() != cbs) {
2674         bib.source(cbs);
2675         bib.ischanged = true;
2676         pcrud.update(bib);
2677     }
2678 }
2679
2680 function onBibSourceSelect() {
2681     var cbs = dojo.byId('bib-source-list').selectedItem.value;
2682     var bib = xulG.record.bre;
2683     if (bib.source() != cbs) {
2684         dojo.byId('bib-source-list-button').disabled = false;   
2685     } else {
2686         dojo.byId('bib-source-list-button').disabled = true;   
2687     }
2688 }
2689
2690 function loadMarcEditor(pcrud, marcxml, target, sf) {
2691     /*
2692        To run in Firefox directly, must set signed.applets.codebase_principal_support
2693        to true in about:config
2694      */
2695     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
2696     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
2697
2698     // Match marc2are.pl last_xact_id format, roughly
2699     var now = new Date;
2700     var xact_id = 'IMPORT-' + Date.parse(now);
2701     
2702     win.xulG = {
2703         "record": {"marc": marcxml, "rtype": "are"},
2704         "save": {
2705             "label": $('catStrings').getString('staff.cat.marcedit.save.label'),
2706             "func": function(xmlString) {
2707                 var rec = new are();
2708                 rec.marc(xmlString);
2709                 rec.last_xact_id(xact_id);
2710                 rec.isnew(true);
2711                 pcrud.create(rec, {
2712                     "oncomplete": function (r, objs) {
2713                         var new_rec = objs[0];
2714                         if (!new_rec) {
2715                             return '';
2716                         }
2717                         var id_sf = <subfield code="0" xmlns="http://www.loc.gov/MARC21/slim">({xulG.marc_control_number_identifier}){new_rec.id()}</subfield>;
2718                         sf.parent().appendChild(id_sf);
2719                         var new_sf = marcSubfield(id_sf);
2720                         target.parentNode.appendChild(new_sf);
2721                         alert($('catStrings').getString('staff.cat.marcedit.create_authority_success.label'));
2722                         win.close();
2723                     }
2724                 });
2725             }
2726         }
2727     };
2728 }
2729
2730