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