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