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