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