]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/marcedit.js
added "insert before" functionality for datafield rows
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / marcedit.js
1 var xmlDeclaration = /^<\?xml version[^>]+?>/;
2
3 var serializer = new XMLSerializer();
4 var marcns = new Namespace("http://www.loc.gov/MARC21/slim");
5 var gw = new Namespace("http://opensrf.org/-/namespaces/gateway/v1");
6 var xulns = new Namespace("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
7 default xml namespace = marcns;
8
9 var tooltip_hash = {};
10 var current_focus;
11 var _record_type;
12 var bib_data;
13
14 var xml_record;
15
16 function mangle_005() {
17         var now = new Date();
18         var y = now.getUTCFullYear();
19
20         var m = now.getUTCMonth() + 1;
21         if (m < 10) m = '0' + m;
22         
23         var d = now.getUTCDate();
24         if (d < 10) d = '0' + d;
25         
26         var H = now.getUTCHours();
27         if (H < 10) H = '0' + H;
28         
29         var M = now.getUTCMinutes();
30         if (M < 10) M = '0' + M;
31         
32         var S = now.getUTCSeconds();
33         if (S < 10) S = '0' + S;
34         
35
36         var stamp = '' + y + m + d + H + M + S + '.0';
37         createControlField('005',stamp);
38
39 }
40
41 function createControlField (tag,data) {
42         // first, remove the old field, if any;
43         for (var i in xml_record.controlfield.(@tag == tag)) delete xml_record.controlfield.(@tag == tag)[i];
44
45         var cf = <controlfield tag="" xmlns="http://www.loc.gov/MARC21/slim">{ data }</controlfield>;
46         cf.@tag = tag;
47
48         // then, find the right position and insert it
49         var done = 0;
50         var cfields = xml_record.controlfield;
51         var base = Number(tag.substring(2));
52         for (var i in cfields) {
53                 var t = Number(cfields[i].@tag.toString().substring(2));
54                 if (t > base) {
55                         xml_record.insertChildBefore( cfields[i], cf );
56                         done = 1
57                         break;
58                 }
59         }
60
61         if (!done) xml_record.insertChildBefore( xml_record.datafield[0], cf );
62
63         return cf;
64 }
65
66 function xml_escape_unicode ( str ) {
67         return str.replace(
68                 /([\u0080-\ufffe])/g,
69                 function (r,s) { return "&#x" + s.charCodeAt(0).toString(16) + ";"; }
70         );
71 }
72
73 function my_init() {
74         try {
75                 // Fake xulG for standalone...
76                 try {
77                         window.xulG.record;
78                 } catch (e) {
79                         window.xulG = {};
80                         window.xulG.record = {};
81                         window.xulG.save = {};
82
83                         window.xulG.save.label = 'Save Record';
84                         window.xulG.save.func = function (r) { alert(r); }
85
86                         var cgi = new CGI();
87                         var _rid = cgi.param('record');
88                         if (_rid) {
89                                 window.xulG.record.url = '/opac/extras/supercat/retrieve/marcxml/record/' + _rid;
90                         }
91                 }
92                 // End faking part...
93
94                 document.getElementById('save-button').setAttribute('label', window.xulG.save.label);
95                 document.getElementById('save-button').setAttribute('oncommand',
96                         'mangle_005(); ' + 
97                         'var xml_string = xml_escape_unicode( xml_record.toXMLString() ); ' + 
98                         'window.xulG.save.func( xml_string ); ' +
99                         'loadRecord(xml_record);'
100                 );
101
102                 if (window.xulG.record.url) {
103                         var req =  new XMLHttpRequest();
104                         req.open('GET',window.xulG.record.url,false);
105                         req.send(null);
106                         window.xulG.record.marc = req.responseText.replace(xmlDeclaration, '');
107                 }
108
109                 xml_record = new XML( window.xulG.record.marc );
110                 if (xml_record..record[0]) xml_record = xml_record..record[0];
111
112                 // Get the tooltip xml all async like
113                 req =  new XMLHttpRequest();
114                 req.open('GET','marcedit-tooltips.xml',true);
115                 req.onreadystatechange = function () {
116                         if (req.readyState == 4) {
117                                 bib_data = new XML( req.responseText.replace(xmlDeclaration, '') );
118                                 genToolTips();
119                         }
120                 }
121                 req.send(null);
122
123                 loadRecord(xml_record);
124         } catch(E) {
125                 alert('FIXME, MARC Editor, my_init: ' + E);
126         }
127 }
128
129
130 function createComplexHTMLElement (e, attrs, objects, text) {
131         var l = document.createElementNS('http://www.w3.org/1999/xhtml',e);
132
133         if (attrs) {
134                 for (var i in attrs) l.setAttribute(i,attrs[i]);
135         }
136
137         if (objects) {
138                 for ( var i in objects ) l.appendChild( objects[i] );
139         }
140
141         if (text) {
142                 l.appendChild( document.createTextNode(text) )
143         }
144
145         return l;
146 }
147
148 function createComplexXULElement (e, attrs, objects) {
149         var l = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',e);
150
151         if (attrs) {
152                 for (var i in attrs) {
153                         if (typeof attrs[i] == 'function') {
154                                 l.addEventListener( i, attrs[i], true );
155                         } else {
156                                 l.setAttribute(i,attrs[i]);
157                         }
158                 }
159         } 
160
161         if (objects) {
162                 for ( var i in objects ) l.appendChild( objects[i] );
163         }
164
165         return l;
166 }
167
168 function createDescription (attrs) {
169         return createComplexXULElement('description', attrs, Array.prototype.slice.apply(arguments, [1]) );
170 }
171
172 function createTooltip (attrs) {
173         return createComplexXULElement('tooltip', attrs, Array.prototype.slice.apply(arguments, [1]) );
174 }
175
176 function createLabel (attrs) {
177         return createComplexXULElement('label', attrs, Array.prototype.slice.apply(arguments, [1]) );
178 }
179
180 function createVbox (attrs) {
181         return createComplexXULElement('vbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
182 }
183
184 function createHbox (attrs) {
185         return createComplexXULElement('hbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
186 }
187
188 function createRow (attrs) {
189         return createComplexXULElement('row', attrs, Array.prototype.slice.apply(arguments, [1]) );
190 }
191
192 function createTextbox (attrs) {
193         return createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
194 }
195
196 function createMenu (attrs) {
197         return createComplexXULElement('menu', attrs, Array.prototype.slice.apply(arguments, [1]) );
198 }
199
200 function createMenuPopup (attrs) {
201         return createComplexXULElement('menupopup', attrs, Array.prototype.slice.apply(arguments, [1]) );
202 }
203
204 function createPopup (attrs) {
205         return createComplexXULElement('popup', attrs, Array.prototype.slice.apply(arguments, [1]) );
206 }
207
208 function createMenuitem (attrs) {
209         return createComplexXULElement('menuitem', attrs, Array.prototype.slice.apply(arguments, [1]) );
210 }
211
212 function createCheckbox (attrs) {
213         return createComplexXULElement('checkbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
214 }
215
216 function createMARCTextbox (element,attrs) {
217
218         var box = createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [2]) );
219         box.onkeypress = function (event) {
220                 var root_node;
221                 var node = element;
222                 while(node = node.parent()) {
223                         root_node = node;
224                 }
225
226                 var row = event.target;
227                 while (row.tagName != 'row') row = row.parentNode;
228
229                 if (element.nodeKind() == 'attribute') element[0]=box.value;
230                 else element.setChildren( box.value );
231
232                 if (element.localName() != 'controlfield') {
233                         if (event.charCode == 100 && event.ctrlKey) { // ctrl+d
234
235                                 var index_sf, target, move_data;
236                                 if (element.localName() == 'subfield') {
237                                         index_sf = element;
238                                         target = event.target.parentNode;
239
240                                         var start = event.target.selectionStart;
241                                         var end = event.target.selectionEnd - event.target.selectionStart ?
242                                                         event.target.selectionEnd :
243                                                         event.target.value.length;
244
245                                         move_data = event.target.value.substring(start,end);
246                                         event.target.value = event.target.value.substring(0,start) + event.target.value.substring(end);
247                                         event.target.setAttribute('size', event.target.value.length + 2);
248         
249                                         element.setChildren( event.target.value );
250
251                                 } else if (element.localName() == 'code') {
252                                         index_sf = element.parent();
253                                         target = event.target.parentNode;
254                                 } else if (element.localName() == 'tag' || element.localName() == 'ind1' || element.localName() == 'ind2') {
255                                         index_sf = element.parent().children()[element.parent().children().length() - 1];
256                                         target = event.target.parentNode.lastChild.lastChild;
257                                 }
258
259                                 var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{ move_data }</subfield>;
260
261                                 index_sf.parent().insertChildAfter( index_sf, sf );
262
263                                 var new_sf = marcSubfield(sf);
264
265                                 if (target === target.parentNode.lastChild) {
266                                         target.parentNode.appendChild( new_sf );
267                                 } else {
268                                         target.parentNode.insertBefore( new_sf, target.nextSibling );
269                                 }
270
271                                 new_sf.firstChild.nextSibling.focus();
272
273                                 event.preventDefault();
274                                 return false;
275
276                         } else if (event.keyCode == 13 || event.keyCode == 77) {
277                                 if (event.ctrlKey) { // ctrl+enter
278
279                                         var index;
280                                         if (element.localName() == 'subfield') index = element.parent();
281                                         if (element.localName() == 'code') index = element.parent().parent();
282                                         if (element.localName() == 'tag') index = element.parent();
283                                         if (element.localName() == 'ind1') index = element.parent();
284                                         if (element.localName() == 'ind2') index = element.parent();
285
286                                         var df = <datafield tag="" ind1="" ind2="" xmlns="http://www.loc.gov/MARC21/slim"><subfield code="" /></datafield>;
287
288                                         if (event.shiftKey) { // ctrl+shift+enter
289                                                 index.parent().insertChildBefore( index, df );
290                                         } else {
291                                                 index.parent().insertChildAfter( index, df );
292                                         }
293
294                                         var new_df = marcDatafield(df);
295
296                                         if (row.parentNode.lastChild === row) {
297                                                 row.parentNode.appendChild( new_df );
298                                         } else {
299                                                 if (event.shiftKey) { // ctrl+shift+enter
300                                                         row.parentNode.insertBefore( new_df, row );
301                                                 } else {
302                                                         row.parentNode.insertBefore( new_df, row.nextSibling );
303                                                 }
304                                         }
305
306                                         new_df.firstChild.focus();
307
308                                         event.preventDefault();
309                                         return false;
310
311                                 } else if (event.shiftKey) {
312                                         if (row.previousSibling.className.match('marcDatafieldRow'))
313                                                 row.previousSibling.firstChild.focus();
314                                 } else {
315                                         row.nextSibling.firstChild.focus();
316                                 }
317
318                         } else if (event.keyCode == 46 && event.ctrlKey) { // ctrl+del
319
320                                 var index;
321                                 if (element.localName() == 'subfield') index = element.parent();
322                                 if (element.localName() == 'code') index = element.parent().parent();
323                                 if (element.localName() == 'tag') index = element.parent();
324                                 if (element.localName() == 'ind1') index = element.parent();
325                                 if (element.localName() == 'ind2') index = element.parent();
326
327                                 for (var i in index.parent().children()) {
328                                         if (index === index.parent().children()[i]) {
329                                                 delete index.parent().children()[i];
330                                                 break;
331                                         }
332                                 }
333
334                                 row.previousSibling.firstChild.focus();
335                                 row.parentNode.removeChild(row);
336
337                                 event.preventDefault();
338                                 return false;
339
340                         } else if (event.keyCode == 46 && event.shiftKey) { // shift+del
341
342                                 var index;
343                                 if (element.localName() == 'subfield') index = element;
344                                 if (element.localName() == 'code') index = element.parent();
345
346                                 if (index) {
347                                         for (var i in index.parent().children()) {
348                                                 if (index === index.parent().children()[i]) {
349                                                         delete index.parent().children()[i];
350                                                         break;
351                                                 }
352                                         }
353
354                                         if (event.target.parentNode === event.target.parentNode.parentNode.lastChild) {
355                                                 event.target.parentNode.previousSibling.lastChild.focus();
356                                         } else {
357                                                 event.target.parentNode.nextSibling.firstChild.nextSibling.focus();
358                                         }
359
360                                         event.target.parentNode.parentNode.removeChild(event.target.parentNode);
361
362                                         event.preventDefault();
363                                         return false;
364                                 }
365                         } else if (event.keyCode == 64 && event.ctrlKey) { // ctrl + F6
366                                 createControlField('006','');
367                                 loadRecord(xml_record);
368                         } else if (event.keyCode == 65 && event.ctrlKey) { // ctrl + F7
369                                 createControlField('007','');
370                                 loadRecord(xml_record);
371                         } else if (event.keyCode == 66 && event.ctrlKey) { // ctrl + F8
372                                 createControlField('008','');
373                                 loadRecord(xml_record);
374                         }
375                         return true;
376                 }
377         };
378
379         box.addEventListener(
380                 'keypress', 
381                 function () {
382                         if (element.nodeKind() == 'attribute') element[0]=box.value;
383                         else element.setChildren( box.value );
384                         return true;
385                 },
386                 false
387         );
388
389         box.addEventListener(
390                 'change', 
391                 function () {
392                         if (element.nodeKind() == 'attribute') element[0]=box.value;
393                         else element.setChildren( box.value );
394                         return true;
395                 },
396                 false
397         );
398
399         box.addEventListener(
400                 'keypress', 
401                 function () {
402                         if (element.nodeKind() == 'attribute') element[0]=box.value;
403                         else element.setChildren( box.value );
404                         return true;
405                 },
406                 true
407         );
408
409         box.addEventListener(
410                 'keyup', 
411                 function () {
412                         if (element.localName() == 'controlfield')
413                                 eval('fillFixedFields(xml_record);');
414                 },
415                 true
416         );
417
418         return box;
419 }
420
421 var rec_type = {
422         BKS : { Type : /[at]{1}/,       BLvl : /[acdm]{1}/ },
423         SER : { Type : /[a]{1}/,        BLvl : /[bs]{1}/ },
424         VIS : { Type : /[gkro]{1}/,     BLvl : /[abcdms]{1}/ },
425         MIX : { Type : /[p]{1}/,        BLvl : /[cd]{1}/ },
426         MAP : { Type : /[ef]{1}/,       BLvl : /[abcdms]{1}/ },
427         SCO : { Type : /[cd]{1}/,       BLvl : /[abcdms]{1}/ },
428         REC : { Type : /[ij]{1}/,       BLvl : /[abcdms]{1}/ },
429         COM : { Type : /[m]{1}/,        BLvl : /[abcdms]{1}/ }
430 };
431
432 var ff_pos = {
433         Ctry : {
434                 _8 : {
435                         BKS : {start : 15, len : 3, def : ' ' },
436                         SER : {start : 15, len : 3, def : ' ' },
437                         VIS : {start : 15, len : 3, def : ' ' },
438                         MIX : {start : 15, len : 3, def : ' ' },
439                         MAP : {start : 15, len : 3, def : ' ' },
440                         SCO : {start : 15, len : 3, def : ' ' },
441                         REC : {start : 15, len : 3, def : ' ' },
442                         COM : {start : 15, len : 3, def : ' ' },
443                 }
444         },
445         Lang : {
446                 _8 : {
447                         BKS : {start : 35, len : 3, def : ' ' },
448                         SER : {start : 35, len : 3, def : ' ' },
449                         VIS : {start : 35, len : 3, def : ' ' },
450                         MIX : {start : 35, len : 3, def : ' ' },
451                         MAP : {start : 35, len : 3, def : ' ' },
452                         SCO : {start : 35, len : 3, def : ' ' },
453                         REC : {start : 35, len : 3, def : ' ' },
454                         COM : {start : 35, len : 3, def : ' ' },
455                 }
456         },
457         MRec : {
458                 _8 : {
459                         BKS : {start : 38, len : 1, def : ' ' },
460                         SER : {start : 38, len : 1, def : ' ' },
461                         VIS : {start : 38, len : 1, def : ' ' },
462                         MIX : {start : 38, len : 1, def : ' ' },
463                         MAP : {start : 38, len : 1, def : ' ' },
464                         SCO : {start : 38, len : 1, def : ' ' },
465                         REC : {start : 38, len : 1, def : ' ' },
466                         COM : {start : 38, len : 1, def : ' ' },
467                 }
468         },
469         DtSt : {
470                 _8 : {
471                         BKS : {start : 6, len : 1, def : ' ' },
472                         SER : {start : 6, len : 1, def : 'c' },
473                         VIS : {start : 6, len : 1, def : ' ' },
474                         MIX : {start : 6, len : 1, def : ' ' },
475                         MAP : {start : 6, len : 1, def : ' ' },
476                         SCO : {start : 6, len : 1, def : ' ' },
477                         REC : {start : 6, len : 1, def : ' ' },
478                         COM : {start : 6, len : 1, def : ' ' },
479                 }
480         },
481         Type : {
482                 ldr : {
483                         BKS : {start : 6, len : 1, def : 'a' },
484                         SER : {start : 6, len : 1, def : 'a' },
485                         VIS : {start : 6, len : 1, def : 'g' },
486                         MIX : {start : 6, len : 1, def : 'p' },
487                         MAP : {start : 6, len : 1, def : 'e' },
488                         SCO : {start : 6, len : 1, def : 'c' },
489                         REC : {start : 6, len : 1, def : 'i' },
490                         COM : {start : 6, len : 1, def : 'm' },
491                 }
492         },
493         Ctrl : {
494                 ldr : {
495                         BKS : {start : 8, len : 1, def : ' ' },
496                         SER : {start : 8, len : 1, def : ' ' },
497                         VIS : {start : 8, len : 1, def : ' ' },
498                         MIX : {start : 8, len : 1, def : ' ' },
499                         MAP : {start : 8, len : 1, def : ' ' },
500                         SCO : {start : 8, len : 1, def : ' ' },
501                         REC : {start : 8, len : 1, def : ' ' },
502                         COM : {start : 8, len : 1, def : ' ' },
503                 }
504         },
505         BLvl : {
506                 ldr : {
507                         BKS : {start : 7, len : 1, def : 'm' },
508                         SER : {start : 7, len : 1, def : 's' },
509                         VIS : {start : 7, len : 1, def : 'm' },
510                         MIX : {start : 7, len : 1, def : 'c' },
511                         MAP : {start : 7, len : 1, def : 'm' },
512                         SCO : {start : 7, len : 1, def : 'm' },
513                         REC : {start : 7, len : 1, def : 'm' },
514                         COM : {start : 7, len : 1, def : 'm' },
515                 }
516         },
517         Desc : {
518                 ldr : {
519                         BKS : {start : 18, len : 1, def : ' ' },
520                         SER : {start : 18, len : 1, def : ' ' },
521                         VIS : {start : 18, len : 1, def : ' ' },
522                         MIX : {start : 18, len : 1, def : ' ' },
523                         MAP : {start : 18, len : 1, def : ' ' },
524                         SCO : {start : 18, len : 1, def : ' ' },
525                         REC : {start : 18, len : 1, def : ' ' },
526                         COM : {start : 18, len : 1, def : ' ' },
527                 }
528         },
529         ELvl : {
530                 ldr : {
531                         BKS : {start : 17, len : 1, def : ' ' },
532                         SER : {start : 17, len : 1, def : ' ' },
533                         VIS : {start : 17, len : 1, def : ' ' },
534                         MIX : {start : 17, len : 1, def : ' ' },
535                         MAP : {start : 17, len : 1, def : ' ' },
536                         SCO : {start : 17, len : 1, def : ' ' },
537                         REC : {start : 17, len : 1, def : ' ' },
538                         COM : {start : 17, len : 1, def : ' ' },
539                 }
540         },
541         Indx : {
542                 _8 : {
543                         BKS : {start : 31, len : 1, def : '0' },
544                         MAP : {start : 31, len : 1, def : '0' },
545                 },
546                 _6 : {
547                         BKS : {start : 14, len : 1, def : '0' },
548                         MAP : {start : 14, len : 1, def : '0' },
549                 }
550         },
551         Date1 : {
552                 _8 : {
553                         BKS : {start : 7, len : 4, def : ' ' },
554                         SER : {start : 7, len : 4, def : ' ' },
555                         VIS : {start : 7, len : 4, def : ' ' },
556                         MIX : {start : 7, len : 4, def : ' ' },
557                         MAP : {start : 7, len : 4, def : ' ' },
558                         SCO : {start : 7, len : 4, def : ' ' },
559                         REC : {start : 7, len : 4, def : ' ' },
560                         COM : {start : 7, len : 4, def : ' ' },
561                 },
562         },
563         Date2 : {
564                 _8 : {
565                         BKS : {start : 11, len : 4, def : ' ' },
566                         SER : {start : 11, len : 4, def : '9' },
567                         VIS : {start : 11, len : 4, def : ' ' },
568                         MIX : {start : 11, len : 4, def : ' ' },
569                         MAP : {start : 11, len : 4, def : ' ' },
570                         SCO : {start : 11, len : 4, def : ' ' },
571                         REC : {start : 11, len : 4, def : ' ' },
572                         COM : {start : 11, len : 4, def : ' ' },
573                 },
574         },
575         LitF : {
576                 _8 : {
577                         BKS : {start : 33, len : 1, def : '0' },
578                 },
579                 _6 : {
580                         BKS : {start : 16, len : 1, def : '0' },
581                 }
582         },
583         Biog : {
584                 _8 : {
585                         BKS : {start : 34, len : 1, def : ' ' },
586                 },
587                 _6 : {
588                         BKS : {start : 17, len : 1, def : ' ' },
589                 }
590         },
591         Ills : {
592                 _8 : {
593                         BKS : {start : 18, len : 4, def : ' ' },
594                 },
595                 _6 : {
596                         BKS : {start : 1, len : 4, def : ' ' },
597                 }
598         },
599         Fest : {
600                 _8 : {
601                         BKS : {start : 30, len : 1, def : '0' },
602                 },
603                 _6 : {
604                         BKS : {start : 13, len : 1, def : '0' },
605                 }
606         },
607         Conf : {
608                 _8 : {
609                         BKS : {start : 29, len : 1, def : '0' },
610                         SER : {start : 29, len : 1, def : '0' },
611                 },
612                 _6 : {
613                         BKS : {start : 12, len : 1, def : '0' },
614                         SER : {start : 12, len : 1, def : '0' },
615                 }
616         },
617         Cont : {
618                 _8 : {
619                         BKS : {start : 24, len : 4, def : ' ' },
620                         SER : {start : 25, len : 3, def : ' ' },
621                 },
622                 _6 : {
623                         BKS : {start : 7, len : 4, def : ' ' },
624                         SER : {start : 8, len : 3, def : ' ' },
625                 }
626         },
627         GPub : {
628                 _8 : {
629                         BKS : {start : 28, len : 1, def : ' ' },
630                         SER : {start : 28, len : 1, def : ' ' },
631                         VIS : {start : 28, len : 1, def : ' ' },
632                         MAP : {start : 28, len : 1, def : ' ' },
633                         COM : {start : 28, len : 1, def : ' ' },
634                 },
635                 _6 : {
636                         BKS : {start : 11, len : 1, def : ' ' },
637                         SER : {start : 11, len : 1, def : ' ' },
638                         VIS : {start : 11, len : 1, def : ' ' },
639                         MAP : {start : 11, len : 1, def : ' ' },
640                         COM : {start : 11, len : 1, def : ' ' },
641                 }
642         },
643         Audn : {
644                 _8 : {
645                         BKS : {start : 22, len : 1, def : ' ' },
646                         SER : {start : 22, len : 1, def : ' ' },
647                         VIS : {start : 22, len : 1, def : ' ' },
648                         SCO : {start : 22, len : 1, def : ' ' },
649                         REC : {start : 22, len : 1, def : ' ' },
650                         COM : {start : 22, len : 1, def : ' ' },
651                 },
652                 _6 : {
653                         BKS : {start : 5, len : 1, def : ' ' },
654                         SER : {start : 5, len : 1, def : ' ' },
655                         VIS : {start : 5, len : 1, def : ' ' },
656                         SCO : {start : 5, len : 1, def : ' ' },
657                         REC : {start : 5, len : 1, def : ' ' },
658                         COM : {start : 5, len : 1, def : ' ' },
659                 }
660         },
661         Form : {
662                 _8 : {
663                         BKS : {start : 23, len : 1, def : ' ' },
664                         SER : {start : 23, len : 1, def : ' ' },
665                         VIS : {start : 29, len : 1, def : ' ' },
666                         MIX : {start : 23, len : 1, def : ' ' },
667                         MAP : {start : 29, len : 1, def : ' ' },
668                         SCO : {start : 23, len : 1, def : ' ' },
669                         REC : {start : 23, len : 1, def : ' ' },
670                 },
671                 _6 : {
672                         BKS : {start : 6, len : 1, def : ' ' },
673                         SER : {start : 6, len : 1, def : ' ' },
674                         VIS : {start : 12, len : 1, def : ' ' },
675                         MIX : {start : 6, len : 1, def : ' ' },
676                         MAP : {start : 12, len : 1, def : ' ' },
677                         SCO : {start : 6, len : 1, def : ' ' },
678                         REC : {start : 6, len : 1, def : ' ' },
679                 }
680         },
681         'S/L' : {
682                 _8 : {
683                         SER : {start : 34, len : 1, def : '0' },
684                 },
685                 _6 : {
686                         SER : {start : 17, len : 1, def : '0' },
687                 }
688         },
689         'Alph' : {
690                 _8 : {
691                         SER : {start : 33, len : 1, def : ' ' },
692                 },
693                 _6 : {
694                         SER : {start : 16, len : 1, def : ' ' },
695                 }
696         },
697 };
698
699 function recordType (rec) {
700         try {
701                 var _l = rec.leader.toString();
702
703                 var _t = _l.substr(ff_pos.Type.ldr.BKS.start, ff_pos.Type.ldr.BKS.len);
704                 var _b = _l.substr(ff_pos.BLvl.ldr.BKS.start, ff_pos.BLvl.ldr.BKS.len);
705
706                 for (var t in rec_type) {
707                         if (_t.match(rec_type[t].Type) && _b.match(rec_type[t].BLvl)) {
708                                 document.getElementById('recordTypeLabel').value = t;
709                         _record_type = t;
710                                 return t;
711                         }
712                 }
713         } catch(E) {
714                 alert('FIXME, MARC Editor, recordType: ' + E);
715         }
716 }
717
718 function toggleFFE () {
719         var grid = document.getElementById('leaderGrid');
720         if (grid.hidden) {
721                 grid.hidden = false;
722         } else {
723                 grid.hidden = true;
724         }
725         return true;
726 }
727
728 function changeFFEditor (type) {
729         var grid = document.getElementById('leaderGrid');
730         grid.setAttribute('type',type);
731 }
732
733 function fillFixedFields (rec) {
734         try {
735                         var grid = document.getElementById('leaderGrid');
736
737                         var rtype = _record_type;
738
739                         var _l = rec.leader.toString();
740                         var _6 = rec.controlfield.(@tag=='006').toString();
741                         var _7 = rec.controlfield.(@tag=='007').toString();
742                         var _8 = rec.controlfield.(@tag=='008').toString();
743
744                         var list = [];
745                         var pre_list = grid.getElementsByTagName('label');
746                         for (var i in pre_list) {
747                                 if ( pre_list[i].getAttribute && pre_list[i].getAttribute('set').indexOf(grid.getAttribute('type')) > -1 ) {
748                                         list.push( pre_list[i] );
749                                 }
750                         }
751
752                         for (var i in list) {
753                                 var name = list[i].getAttribute('name');
754
755                                 if (!ff_pos[name])
756                                         continue;
757
758                                 var value = '';
759                                 if ( ff_pos[name].ldr && ff_pos[name].ldr[rtype] )
760                                         value = _l.substr(ff_pos[name].ldr[rtype].start, ff_pos[name].ldr[rtype].len);
761
762                                 if ( ff_pos[name]._8 && ff_pos[name]._8[rtype] )
763                                         value = _8.substr(ff_pos[name]._8[rtype].start, ff_pos[name]._8[rtype].len);
764
765                                 if ( !value && ff_pos[name]._6 && ff_pos[name]._6[rtype] )
766                                         value = _6.substr(ff_pos[name]._6[rtype].start, ff_pos[name]._6[rtype].len);
767
768                                 if ( ff_pos[name]._7 && ff_pos[name]._7[rtype] )
769                                         value = _7.substr(ff_pos[name]._7[rtype].start, ff_pos[name]._7[rtype].len);
770                                 
771                                 if (!value) {
772                                         var d;
773                                         var p;
774                                         if (ff_pos[name].ldr && ff_pos[name].ldr[rtype]) {
775                                                 d = ff_pos[name].ldr[rtype].def;
776                                                 p = 'ldr';
777                                         }
778
779                                         if (ff_pos[name]._8 && ff_pos[name]._8[rtype]) {
780                                                 d = ff_pos[name]._8[rtype].def;
781                                                 p = '_8';
782                                         }
783
784                                         if (!value && ff_pos[name]._6 && ff_pos[name]._6[rtype]) {
785                                                 d = ff_pos[name]._6[rtype].def;
786                                                 p = '_6';
787                                         }
788
789                                         if (ff_pos[name]._7 && ff_pos[name]._7[rtype]) {
790                                                 d = ff_pos[name]._7[rtype].def;
791                                                 p = '_7';
792                                         }
793
794                                         if (!value) {
795                                                 for (var j = 0; j < ff_pos[name][p][rtype].len; j++) {
796                                                         value += d;
797                                                 }
798                                         }
799                                 }
800
801                                 list[i].nextSibling.value = value;
802                         }
803
804                         return true;
805         } catch(E) {
806                 alert('FIXME, MARC Editor, fillFixedFields: ' + E);
807         }
808 }
809
810 function updateFixedFields (element) {
811         var grid = document.getElementById('leaderGrid');
812         var recGrid = document.getElementById('recGrid');
813
814         var rtype = _record_type;
815         var new_value = element.value;
816
817         var parts = {
818                 ldr : _record.leader,
819                 _6 : _record.controlfield.(@tag=='006'),
820                 _7 : _record.controlfield.(@tag=='007'),
821                 _8 : _record.controlfield.(@tag=='008'),
822         };
823
824         var name = element.getAttribute('name');
825         for (var i in ff_pos[name]) {
826
827                 if (!ff_pos[name][i][rtype]) continue;
828                 if (!parts[i]) continue;
829
830                 var before = parts[i].substr(0, ff_pos[name][i][rtype].start);
831                 var after = parts[i].substr(ff_pos[name][i][rtype].start + ff_pos[name][i][rtype].len);
832
833                 for (var j = 0; new_value.length < ff_pos[name][i][rtype].len; j++) {
834                         new_value += ff_pos[name][i][rtype].def;
835                 }
836
837                 parts[i].setChildren( before + new_value + after );
838                 recGrid.getElementsByAttribute('tag',i)[0].lastChild.value = parts[i].toString();
839         }
840
841         return true;
842 }
843
844 function marcLeader (leader) {
845         var row = createRow(
846                 { class : 'marcLeaderRow',
847                   tag : 'ldr' },
848                 createLabel(
849                         { value : 'LDR',
850                           class : 'marcTag',
851                           tooltiptext : "MARC Leader" } ),
852                 createLabel(
853                         { value : '',
854                           class : 'marcInd1' } ),
855                 createLabel(
856                         { value : '',
857                           class : 'marcInd2' } ),
858                 createLabel(
859                         { value : leader.text(),
860                           class : 'marcLeader' } )
861         );
862
863         return row;
864 }
865
866 function marcControlfield (field) {
867         tagname = field.@tag.toString().substr(2);
868         var row;
869         if (tagname == '6' || tagname == '7' || tagname == '8') {
870                 row = createRow(
871                         { class : 'marcControlfieldRow',
872                           tag : '_' + tagname },
873                         createLabel(
874                                 { value : field.@tag,
875                                   class : 'marcTag',
876                                   context : 'tags_popup',
877                                   onmouseover : 'getTooltip(this, "tag");',
878                                   tooltipid : 'tag' + field.@tag } ),
879                         createLabel(
880                                 { value : field.@ind1,
881                                   class : 'marcInd1',
882                                   onmouseover : 'getTooltip(this, "ind1");',
883                                   tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
884                         createLabel(
885                                 { value : field.@ind2,
886                                   class : 'marcInd2',
887                                   onmouseover : 'getTooltip(this, "ind2");',
888                                   tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
889                         createMARCTextbox(
890                                 field,
891                                 { value : field.text(),
892                                   class : 'plain marcEditableControlfield',
893                                   name : 'CONTROL' + tagname,
894                                   oncontext : 'return false();',
895                                   size : 50,
896                                   maxlength : 50 } )
897                         );
898         } else {
899                 row = createRow(
900                         { class : 'marcControlfieldRow',
901                           tag : '_' + tagname },
902                         createLabel(
903                                 { value : field.@tag,
904                                   class : 'marcTag',
905                                   onmouseover : 'getTooltip(this, "tag");',
906                                   tooltipid : 'tag' + field.@tag } ),
907                         createLabel(
908                                 { value : field.@ind1,
909                                   class : 'marcInd1',
910                                   onmouseover : 'getTooltip(this, "ind1");',
911                                   tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
912                         createLabel(
913                                 { value : field.@ind2,
914                                   class : 'marcInd2',
915                                   onmouseover : 'getTooltip(this, "ind2");',
916                                   tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
917                         createLabel(
918                                 { value : field.text(),
919                                   class : 'marcControlfield' } )
920                 );
921         }
922
923         return row;
924 }
925
926 function stackSubfields(checkbox) {
927         var list = document.getElementsByAttribute('name','sf_box');
928
929         var o = 'vertical';
930         if (checkbox.checked) o = 'horizontal';
931         
932         for (var i = 0; i < list.length; i++) {
933                 if (list[i]) list[i].setAttribute('orient',o);
934         }
935 }
936
937 function marcDatafield (field) {
938         var row = createRow(
939                 { class : 'marcDatafieldRow' },
940                 createMARCTextbox(
941                         field.@tag,
942                         { value : field.@tag,
943                           class : 'plain marcTag',
944                           name : 'marcTag',
945                           context : 'tags_popup',
946                           oninput : 'if (this.value.length == 3) { this.nextSibling.focus(); }',
947                           size : 3,
948                           maxlength : 3,
949                           onmouseover : 'current_focus = this; getTooltip(this, "tag");' } ),
950                 createMARCTextbox(
951                         field.@ind1,
952                         { value : field.@ind1,
953                           class : 'plain marcInd1',
954                           name : 'marcInd1',
955                           oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
956                           size : 1,
957                           maxlength : 1,
958                           onmouseover : 'current_focus = this; getContextMenu(this, "ind1"); getTooltip(this, "ind1");',
959                           oncontextmenu : 'getContextMenu(this, "ind1");' } ),
960                 createMARCTextbox(
961                         field.@ind2,
962                         { value : field.@ind2,
963                           class : 'plain marcInd2',
964                           name : 'marcInd2',
965                           oninput : 'if (this.value.length == 1) { this.nextSibling.firstChild.firstChild.focus(); }',
966                           size : 1,
967                           maxlength : 1,
968                           onmouseover : 'current_focus = this; getContextMenu(this, "ind2"); getTooltip(this, "ind2");',
969                           oncontextmenu : 'getContextMenu(this, "ind2");' } ),
970                 createHbox({ name : 'sf_box' })
971         );
972
973         if (!current_focus && field.@tag == '') current_focus = row.childNodes[0];
974         if (!current_focus && field.@ind1 == '') current_focus = row.childNodes[1];
975         if (!current_focus && field.@ind2 == '') current_focus = row.childNodes[2];
976
977         var sf_box = row.lastChild;
978         if (document.getElementById('stackSubfields').checked)
979                 sf_box.setAttribute('orient','vertical');
980
981         sf_box.addEventListener(
982                 'click',
983                 function (e) {
984                         if (sf_box === e.target) {
985                                 sf_box.lastChild.lastChild.focus();
986                         } else if (e.target.parentNode === sf_box) {
987                                 e.target.lastChild.focus();
988                         }
989                 },
990                 false
991         );
992
993
994         for (var i in field.subfield) {
995                 var sf = field.subfield[i];
996                 sf_box.appendChild(
997                         marcSubfield(sf)
998                 );
999
1000                 if (sf.@code == '' && (!current_focus || current_focus.className.match(/Ind/)))
1001                         current_focus = sf_box.lastChild.childNodes[1];
1002         }
1003
1004         return row;
1005 }
1006
1007 function marcSubfield (sf) {                    
1008         return createHbox(
1009                 { class : 'marcSubfieldBox' },
1010                 createLabel(
1011                         { value : "\u2021",
1012                           class : 'plain marcSubfieldDelimiter',
1013                           onmouseover : 'getTooltip(this.nextSibling, "subfield");',
1014                           oncontextmenu : 'getContextMenu(this.nextSibling, "subfield");',
1015                           //onclick : 'this.nextSibling.focus();',
1016                           onfocus : 'this.nextSibling.focus();',
1017                           size : 2 } ),
1018                 createMARCTextbox(
1019                         sf.@code,
1020                         { value : sf.@code,
1021                           class : 'plain marcSubfieldCode',
1022                           name : 'marcSubfieldCode',
1023                           onmouseover : 'current_focus = this; getContextMenu(this, "subfield"); getTooltip(this, "subfield");',
1024                           oncontextmenu : 'getContextMenu(this, "subfield");',
1025                           oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
1026                           size : 2,
1027                           maxlength : 1 } ),
1028                 createMARCTextbox(
1029                         sf,
1030                         { value : sf.text(),
1031                           name : sf.parent().@tag + ':' + sf.@code,
1032                           class : 'plain marcSubfield', 
1033                           onmouseover : 'getTooltip(this, "subfield");',
1034                           contextmenu : function (event) { getAuthorityContextMenu(event.target, sf) },
1035                           size : new String(sf.text()).length + 2,
1036                           oninput : "this.setAttribute('size', this.value.length + 2);",
1037                         } )
1038         );
1039 }
1040
1041 function loadRecord(rec) {
1042         try {
1043                         _record = rec;
1044                         var grid_rows = document.getElementById('recGrid').lastChild;
1045
1046                         while (grid_rows.firstChild) grid_rows.removeChild(grid_rows.firstChild);
1047
1048                         grid_rows.appendChild( marcLeader( rec.leader ) );
1049
1050                         for (var i in rec.controlfield) {
1051                                 grid_rows.appendChild( marcControlfield( rec.controlfield[i] ) );
1052                         }
1053
1054                         for (var i in rec.datafield) {
1055                                 grid_rows.appendChild( marcDatafield( rec.datafield[i] ) );
1056                         }
1057
1058                         grid_rows.getElementsByAttribute('class','marcDatafieldRow')[0].firstChild.focus();
1059                         changeFFEditor(recordType(rec));
1060                         fillFixedFields(rec);
1061         } catch(E) {
1062                 alert('FIXME, MARC Editor, loadRecord: ' + E);
1063         }
1064 }
1065
1066 var context_menus = createComplexXULElement('popupset');
1067 document.documentElement.appendChild( context_menus );
1068
1069 var tag_menu = createPopup({position : 'after_start', id : 'tags_popup'});
1070 context_menus.appendChild( tag_menu );
1071
1072 tag_menu.appendChild(
1073         createMenuitem(
1074                 { label : 'Add Row',
1075                   oncommand : 
1076                         'var e = document.createEvent("KeyEvents");' +
1077                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,13,0);' +
1078                         'current_focus.inputField.dispatchEvent(e);',
1079                  }
1080         )
1081 );
1082
1083 tag_menu.appendChild(
1084         createMenuitem(
1085                 { label : 'Remove Row',
1086                   oncommand : 
1087                         'var e = document.createEvent("KeyEvents");' +
1088                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,46,0);' +
1089                         'current_focus.inputField.dispatchEvent(e);',
1090                 }
1091         )
1092 );
1093
1094 tag_menu.appendChild( createComplexXULElement( 'separator' ) );
1095
1096 tag_menu.appendChild(
1097         createMenuitem(
1098                 { label : 'Add/Replace 006',
1099                   oncommand : 
1100                         'var e = document.createEvent("KeyEvents");' +
1101                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,64,0);' +
1102                         'current_focus.inputField.dispatchEvent(e);',
1103                  }
1104         )
1105 );
1106
1107 tag_menu.appendChild(
1108         createMenuitem(
1109                 { label : 'Add/Replace 007',
1110                   oncommand : 
1111                         'var e = document.createEvent("KeyEvents");' +
1112                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,65,0);' +
1113                         'current_focus.inputField.dispatchEvent(e);',
1114                 }
1115         )
1116 );
1117
1118 tag_menu.appendChild(
1119         createMenuitem(
1120                 { label : 'Add/Replace 008',
1121                   oncommand : 
1122                         'var e = document.createEvent("KeyEvents");' +
1123                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,66,0);' +
1124                         'current_focus.inputField.dispatchEvent(e);',
1125                 }
1126         )
1127 );
1128
1129 tag_menu.appendChild( createComplexXULElement( 'separator' ) );
1130
1131
1132
1133 function genToolTips () {
1134         for (var i in bib_data.field) {
1135                 var f = bib_data.field[i];
1136         
1137                 tag_menu.appendChild(
1138                         createMenuitem(
1139                                 { label : f.@tag,
1140                                   oncommand : 
1141                                         'current_focus.value = "' + f.@tag + '";' +
1142                                         'var e = document.createEvent("MutationEvents");' +
1143                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1144                                         'current_focus.inputField.dispatchEvent(e);',
1145                                   disabled : f.@tag < '010' ? "true" : "false",
1146                                   tooltiptext : f.description }
1147                         )
1148                 );
1149         
1150                 var i1_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i1' });
1151                 context_menus.appendChild( i1_popup );
1152         
1153                 var i2_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i2' });
1154                 context_menus.appendChild( i2_popup );
1155         
1156                 var sf_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'sf' });
1157                 context_menus.appendChild( sf_popup );
1158         
1159                 tooltip_hash['tag' + f.@tag] = f.description;
1160                 for (var j in f.indicator) {
1161                         var ind = f.indicator[j];
1162                         tooltip_hash['tag' + f.@tag + 'ind' + ind.@position + 'val' + ind.@value] = ind.description;
1163         
1164                         if (ind.@position == 1) {
1165                                 i1_popup.appendChild(
1166                                         createMenuitem(
1167                                                 { label : ind.@value,
1168                                                   oncommand : 
1169                                                         'current_focus.value = "' + ind.@value + '";' +
1170                                                         'var e = document.createEvent("MutationEvents");' +
1171                                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1172                                                         'current_focus.inputField.dispatchEvent(e);',
1173                                                   tooltiptext : ind.description }
1174                                         )
1175                                 );
1176                         }
1177         
1178                         if (ind.@position == 2) {
1179                                 i2_popup.appendChild(
1180                                         createMenuitem(
1181                                                 { label : ind.@value,
1182                                                   oncommand : 
1183                                                         'current_focus.value = "' + ind.@value + '";' +
1184                                                         'var e = document.createEvent("MutationEvents");' +
1185                                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1186                                                         'current_focus.inputField.dispatchEvent(e);',
1187                                                   tooltiptext : ind.description }
1188                                         )
1189                                 );
1190                         }
1191                 }
1192         
1193                 for (var j in f.subfield) {
1194                         var sf = f.subfield[j];
1195                         tooltip_hash['tag' + f.@tag + 'sf' + sf.@code] = sf.description;
1196         
1197                         sf_popup.appendChild(
1198                                 createMenuitem(
1199                                         { label : sf.@code,
1200                                           oncommand : 
1201                                                 'current_focus.value = "' + sf.@code + '";' +
1202                                                 'var e = document.createEvent("MutationEvents");' +
1203                                                 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1204                                                 'current_focus.inputField.dispatchEvent(e);',
1205                                           tooltiptext : sf.description,
1206                                         }
1207                                 )
1208                         );
1209                 }
1210         }
1211 }
1212
1213 var p = createComplexXULElement('popupset');
1214 document.documentElement.appendChild( p );
1215
1216 function getTooltip (target, type) {
1217
1218         var tt = '';
1219         if (type == 'subfield')
1220                 tt = 'tag' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf' + target.parentNode.childNodes[1].value;
1221
1222         if (type == 'ind1')
1223                 tt = 'tag' + target.parentNode.firstChild.value + 'ind1val' + target.value;
1224
1225         if (type == 'ind2')
1226                 tt = 'tag' + target.parentNode.firstChild.value + 'ind2val' + target.value;
1227
1228         if (type == 'tag')
1229                 tt = 'tag' + target.parentNode.firstChild.value;
1230
1231         if (!document.getElementById( tt )) {
1232                 p.appendChild(
1233                         createTooltip(
1234                                 { id : tt,
1235                                   flex : "1",
1236                                   orient : 'vertical',
1237                                   onpopupshown : 'this.width = this.firstChild.boxObject.width + 10; this.height = this.firstChild.boxObject.height + 10;',
1238                                   class : 'tooltip' },
1239                                 createDescription({}, document.createTextNode( tooltip_hash[tt] ) )
1240                         )
1241                 );
1242         }
1243
1244         target.tooltip = tt;
1245         return true;
1246 }
1247
1248 function getContextMenu (target, type) {
1249
1250         var tt = '';
1251         if (type == 'subfield')
1252                 tt = 't' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf';
1253
1254         if (type == 'ind1')
1255                 tt = 't' + target.parentNode.firstChild.value + 'i1';
1256
1257         if (type == 'ind2')
1258                 tt = 't' + target.parentNode.firstChild.value + 'i2';
1259
1260         target.setAttribute('context', tt);
1261         return true;
1262 }
1263
1264 var authority_tag_map = {
1265         100 : ['[100,400,500,700]',100],
1266         400 : ['[100,400,500,700]',100],
1267         700 : ['[100,400,500,700]',100],
1268         800 : ['[100,400,500,700]',100],
1269         110 : ['[110,410,510,710]',110],
1270         410 : ['[110,410,510,710]',110],
1271         710 : ['[110,410,510,710]',110],
1272         810 : ['[110,410,510,710]',110],
1273         111 : ['[111,411,511,711]',111],
1274         411 : ['[111,411,511,711]',111],
1275         711 : ['[111,411,511,711]',111],
1276         811 : ['[111,411,511,711]',111],
1277         240 : ['[130,430,530,730]',130],
1278         440 : ['[130,430,530,730]',130],
1279         130 : ['[130,430,530,730]',130],
1280         730 : ['[130,430,530,730]',130],
1281         830 : ['[130,430,530,730]',130],
1282         600 : ['[100,400,480,481,482,485,500,580,581,582,585,700,780,781,782,785]',100],
1283         650 : ['[150,450,480,481,482,485,550,580,581,582,585,750,780,781,782,785]',150],
1284         651 : ['[151,451,480,481,482,485,551,580,581,582,585,751,780,781,782,785]',151],
1285         655 : ['[155,455,480,481,482,485,555,580,581,582,585,755,780,781,782,785]',155],
1286 };
1287
1288 function getAuthorityContextMenu (target, sf) {
1289         var menu_id = sf.parent().@tag + ':' + sf.@code + '-authority-context-' + sf;
1290
1291         var old = document.getElementById( menu_id );
1292         if (old) old.parentNode.removeChild(old);
1293
1294         var sf_popup = createPopup({ id : menu_id, flex : 1 });
1295         context_menus.appendChild( sf_popup );
1296
1297         if (!authority_tag_map[sf.parent().@tag]) {
1298                 sf_popup.appendChild(createLabel( { value : "Not a controlled subfield" } ) );
1299                 target.setAttribute('context', menu_id);
1300                 return false;
1301         }
1302
1303         var auth_data = searchAuthority( sf, authority_tag_map[sf.parent().@tag][0], sf.@code, 50);
1304
1305         var res = new XML( auth_data.responseText );
1306
1307         var rec_list = [];
1308
1309         var recs = res.gw::payload.gw::array.gw::string;
1310         for (var i in recs) {
1311                 var x = recs[i];
1312                 var xml = new XML(x.toString());
1313                 var main = xml.datafield.(@tag.toString().match(/^1/)).subfield;
1314
1315                 if (! (main[0].parent().@tag == authority_tag_map[sf.parent().@tag][1]) ) continue;
1316
1317                 var main_text = '';
1318                 for (var i in main) {
1319                         if (main_text) main_text += ' / ';
1320                         main_text += main[i];
1321                 }
1322
1323                 rec_list.push( [ main_text, xml ] );
1324         }
1325         
1326         for (var i in rec_list.sort( function (a, b) { if(a[0] > b[0]) return 1; return -1; } )) {
1327
1328                 var main_text = rec_list[i][0];
1329                 var xml = rec_list[i][1];
1330                 var main = xml.datafield.(@tag.toString().match(/^1/)).subfield;
1331
1332                 if (! (main[0].parent().@tag == authority_tag_map[sf.parent().@tag][1]) ) continue;
1333
1334                 var grid = document.getElementsByAttribute('name','authority-marc-template')[0].cloneNode(true);
1335                 grid.setAttribute('name','-none-');
1336                 grid.setAttribute('style','overflow:scroll');
1337
1338
1339                 var submenu = createMenu( { label : main_text } );
1340
1341                 var popup = createMenuPopup({ flex : "1" });
1342                 submenu.appendChild(popup);
1343
1344                 var fields = xml.datafield;
1345                 for (var j in fields) {
1346
1347                         var row = createRow(
1348                                 {},
1349                                 createLabel( { value : fields[j].@tag } ),
1350                                 createLabel( { value : fields[j].@ind1 } ),
1351                                 createLabel( { value : fields[j].@ind2 } )
1352                         );
1353
1354                         var sf_box = createHbox();
1355
1356                         var subfields = fields[j].subfield;
1357                         for (var k in subfields) {
1358                                 sf_box.appendChild(
1359                                         createCheckbox(
1360                                                 { label    : '\u2021' + subfields[k].@code + ' ' + subfields[k],
1361                                                   subfield : subfields[k].@code,
1362                                                   tag      : subfields[k].parent().@tag,
1363                                                   value    : subfields[k]
1364                                                 }
1365                                         )
1366                                 );
1367                                 row.appendChild(sf_box);
1368                         }
1369
1370                         grid.lastChild.appendChild(row);
1371                 }
1372
1373                 grid.hidden = false;
1374                 popup.appendChild( grid );
1375
1376                 popup.appendChild(
1377                         createMenuitem(
1378                                 { label : 'Apply Selected',
1379                                   command : function (event) {
1380                                                 applyAuthority(event.target.previousSibling, target, sf);
1381                                                 return true;
1382                                   },
1383                                 }
1384                         )
1385                 );
1386
1387                 sf_popup.appendChild( submenu );
1388         }
1389
1390         if (sf_popup.childNodes.length == 0)
1391                 sf_popup.appendChild(createLabel( { value : "No matching authority records found" } ) );
1392
1393         target.setAttribute('context', menu_id);
1394         return true;
1395 }
1396
1397 function applyAuthority ( target, ui_sf, e4x_sf ) {
1398
1399         var new_vals = target.getElementsByAttribute('checked','true');
1400         var field = e4x_sf.parent();
1401
1402         for (var i = 0; i < new_vals.length; i++) {
1403
1404                 var sf_list = field.subfield;
1405                 for (var j in sf_list) {
1406
1407                         if (sf_list[j].@code == new_vals[i].getAttribute('subfield')) {
1408                                 sf_list[j] = new_vals[i].getAttribute('value');
1409                                 new_vals[i].setAttribute('subfield','');
1410                                 break;
1411                         }
1412                 }
1413         }
1414
1415         for (var i = 0; i < new_vals.length; i++) {
1416                 if (!new_vals[i].getAttribute('subfield')) continue;
1417
1418                 var val = new_vals[i].getAttribute('value');
1419
1420                 var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{val}</subfield>;
1421                 sf.@code = new_vals[i].getAttribute('subfield');
1422
1423                 field.insertChildAfter(field.subfield[field.subfield.length() - 1], sf);
1424         }
1425
1426         var row = marcDatafield( field );
1427
1428         var node = ui_sf;
1429         while (node.nodeName != 'row') {
1430                 node = node.parentNode;
1431         }
1432
1433         node.parentNode.replaceChild( row, node );
1434         return true;
1435 }
1436
1437 var control_map = {
1438         100 : {
1439                 'a' : { 100 : 'a' },
1440                 'd' : { 100 : 'd' },
1441         },
1442         110 : {
1443                 'a' : { 110 : 'a' },
1444                 'd' : { 110 : 'd' },
1445         },
1446         111 : {
1447                 'a' : { 111 : 'a' },
1448                 'd' : { 111 : 'd' },
1449         },
1450         130 : {
1451                 'a' : { 130 : 'a' },
1452                 'd' : { 130 : 'd' },
1453         },
1454         240 : {
1455                 'a' : { 130 : 'a' },
1456                 'd' : { 130 : 'd' },
1457         },
1458         400 : {
1459                 'a' : { 100 : 'a' },
1460                 'd' : { 100 : 'd' },
1461         },
1462         410 : {
1463                 'a' : { 110 : 'a' },
1464                 'd' : { 110 : 'd' },
1465         },
1466         411 : {
1467                 'a' : { 111 : 'a' },
1468                 'd' : { 111 : 'd' },
1469         },
1470         440 : {
1471                 'a' : { 130 : 'a' },
1472                 'n' : { 130 : 'n' },
1473                 'p' : { 130 : 'p' },
1474         },
1475         700 : {
1476                 'a' : { 100 : 'a' },
1477                 'd' : { 100 : 'd' },
1478         },
1479         710 : {
1480                 'a' : { 110 : 'a' },
1481                 'd' : { 110 : 'd' },
1482         },
1483         711 : {
1484                 'a' : { 111 : 'a' },
1485                 'd' : { 111 : 'd' },
1486         },
1487         730 : {
1488                 'a' : { 130 : 'a' },
1489                 'd' : { 130 : 'd' },
1490         },
1491         800 : {
1492                 'a' : { 100 : 'a' },
1493                 'd' : { 100 : 'd' },
1494         },
1495         810 : {
1496                 'a' : { 110 : 'a' },
1497                 'd' : { 110 : 'd' },
1498         },
1499         811 : {
1500                 'a' : { 111 : 'a' },
1501                 'd' : { 111 : 'd' },
1502         },
1503         830 : {
1504                 'a' : { 130 : 'a' },
1505                 'd' : { 130 : 'd' },
1506         },
1507         600 : {
1508                 'a' : { 100 : 'a' },
1509                 'd' : { 100 : 'd' },
1510                 't' : { 100 : 't' },
1511                 'v' : { 180 : 'v',
1512                         100 : 'v',
1513                         181 : 'v',
1514                         182 : 'v',
1515                         185 : 'v',
1516                 },
1517                 'x' : { 180 : 'x',
1518                         100 : 'x',
1519                         181 : 'x',
1520                         182 : 'x',
1521                         185 : 'x',
1522                 },
1523                 'y' : { 180 : 'y',
1524                         100 : 'y',
1525                         181 : 'y',
1526                         182 : 'y',
1527                         185 : 'y',
1528                 },
1529                 'z' : { 180 : 'z',
1530                         100 : 'z',
1531                         181 : 'z',
1532                         182 : 'z',
1533                         185 : 'z',
1534                 },
1535         },
1536         650 : {
1537                 'a' : { 150 : 'a' },
1538                 'b' : { 150 : 'b' },
1539                 'v' : { 180 : 'v',
1540                         150 : 'v',
1541                         181 : 'v',
1542                         182 : 'v',
1543                         185 : 'v',
1544                 },
1545                 'x' : { 180 : 'x',
1546                         150 : 'x',
1547                         181 : 'x',
1548                         182 : 'x',
1549                         185 : 'x',
1550                 },
1551                 'y' : { 180 : 'y',
1552                         150 : 'y',
1553                         181 : 'y',
1554                         182 : 'y',
1555                         185 : 'y',
1556                 },
1557                 'z' : { 180 : 'z',
1558                         150 : 'z',
1559                         181 : 'z',
1560                         182 : 'z',
1561                         185 : 'z',
1562                 },
1563         },
1564         651 : {
1565                 'a' : { 151 : 'a' },
1566                 'v' : { 180 : 'v',
1567                         151 : 'v',
1568                         181 : 'v',
1569                         182 : 'v',
1570                         185 : 'v',
1571                 },
1572                 'x' : { 180 : 'x',
1573                         151 : 'x',
1574                         181 : 'x',
1575                         182 : 'x',
1576                         185 : 'x',
1577                 },
1578                 'y' : { 180 : 'y',
1579                         151 : 'y',
1580                         181 : 'y',
1581                         182 : 'y',
1582                         185 : 'y',
1583                 },
1584                 'z' : { 180 : 'z',
1585                         151 : 'z',
1586                         181 : 'z',
1587                         182 : 'z',
1588                         185 : 'z',
1589                 },
1590         },
1591         655 : {
1592                 'a' : { 155 : 'a' },
1593                 'v' : { 180 : 'v',
1594                         155 : 'v',
1595                         181 : 'v',
1596                         182 : 'v',
1597                         185 : 'v',
1598                 },
1599                 'x' : { 180 : 'x',
1600                         155 : 'x',
1601                         181 : 'x',
1602                         182 : 'x',
1603                         185 : 'x',
1604                 },
1605                 'y' : { 180 : 'y',
1606                         155 : 'y',
1607                         181 : 'y',
1608                         182 : 'y',
1609                         185 : 'y',
1610                 },
1611                 'z' : { 180 : 'z',
1612                         155 : 'z',
1613                         181 : 'z',
1614                         182 : 'z',
1615                         185 : 'z',
1616                 },
1617         },
1618 };
1619
1620 function validateAuthority (button) {
1621         var grid = document.getElementById('recGrid');
1622         var label = button.getAttribute('label');
1623
1624         //loop over rows
1625         var rows = grid.lastChild.childNodes;
1626         for (var i = 0; i < rows.length; i++) {
1627                 var row = rows[i];
1628                 var tag = row.firstChild;
1629
1630                 if (!control_map[tag.value]) continue
1631
1632                 var ind1 = tag.nextSibling;
1633                 var ind2 = ind1.nextSibling;
1634                 var subfields = ind2.nextSibling.childNodes;
1635
1636                 for (var j = 0; j < subfields.length; j++) {
1637                         var sf = subfields[j];
1638
1639                         if (!control_map[tag.value][sf.childNodes[1].value]) continue;
1640
1641                         button.setAttribute('label', label + ' - ' + tag.value + sf.childNodes[1].value);
1642
1643                         var found = 0;
1644                         for (var k in control_map[tag.value][sf.childNodes[1].value]) {
1645                                 var x = searchAuthority(sf.childNodes[2].value, k, control_map[tag.value][sf.childNodes[1].value][k], 1);
1646                                 var res = new XML( x.responseText );
1647
1648                                 if (res.gw::payload.gw::array.gw::string.length()) {
1649                                         found = 1;
1650                                         break;
1651                                 }
1652                         }
1653
1654                         if (!found) {
1655                                 sf.childNodes[2].inputField.style.color = 'red';
1656                         } else {
1657                                 sf.childNodes[2].inputField.style.color = 'black';
1658                         }
1659                 }
1660         }
1661
1662         button.setAttribute('label', label);
1663
1664         return true;
1665 }
1666
1667
1668 function searchAuthority (term, tag, sf, limit) {
1669         var url = "/gateway?format=xml&service=open-ils.search&method=open-ils.search.authority.fts";
1670         url += '&param="term"&param="' + term + '"';
1671         url += '&param="limit"&param=' + limit;
1672         url += '&param="tag"&param=' + tag;
1673         url += '&param="subfield"&param="' + sf + '"';
1674
1675
1676         var req = new XMLHttpRequest();
1677         req.open('GET',url,false);
1678         req.send(null);
1679
1680         return req;
1681
1682 }
1683