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