]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/marcedit.js
disable volume controls for volume_copy_creator when doing Add Items
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / marcedit.js
1 var marcns = new Namespace("http://www.loc.gov/MARC21/slim");
2 var xulns = new Namespace("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
3 default xml namespace = marcns;
4
5 var tooltip_hash = {};
6 var current_focus;
7 var _record_type;
8 var bib_data;
9
10 function createComplexHTMLElement (e, attrs, objects, text) {
11         var l = document.createElementNS('http://www.w3.org/1999/xhtml',e);
12
13         if (attrs) {
14                 for (var i in attrs) l.setAttribute(i,attrs[i]);
15         }
16
17         if (objects) {
18                 for ( var i in objects ) l.appendChild( objects[i] );
19         }
20
21         if (text) {
22                 l.appendChild( document.createTextNode(text) )
23         }
24
25         return l;
26 }
27
28 function createComplexXULElement (e, attrs, objects) {
29         var l = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',e);
30
31         if (attrs) {
32                 for (var i in attrs) {
33                         if (typeof attrs[i] == 'function') {
34                                 l.addEventListener( i, attrs[i], true );
35                         } else {
36                                 l.setAttribute(i,attrs[i]);
37                         }
38                 }
39         } 
40
41         if (objects) {
42                 for ( var i in objects ) l.appendChild( objects[i] );
43         }
44
45         return l;
46 }
47
48 function createDescription (attrs) {
49         return createComplexXULElement('description', attrs, Array.prototype.slice.apply(arguments, [1]) );
50 }
51
52 function createTooltip (attrs) {
53         return createComplexXULElement('tooltip', attrs, Array.prototype.slice.apply(arguments, [1]) );
54 }
55
56 function createLabel (attrs) {
57         return createComplexXULElement('label', attrs, Array.prototype.slice.apply(arguments, [1]) );
58 }
59
60 function createVbox (attrs) {
61         return createComplexXULElement('vbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
62 }
63
64 function createHbox (attrs) {
65         return createComplexXULElement('hbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
66 }
67
68 function createRow (attrs) {
69         return createComplexXULElement('row', attrs, Array.prototype.slice.apply(arguments, [1]) );
70 }
71
72 function createTextbox (attrs) {
73         return createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
74 }
75
76 function createMenu (attrs) {
77         return createComplexXULElement('menu', attrs, Array.prototype.slice.apply(arguments, [1]) );
78 }
79
80 function createMenuPopup (attrs) {
81         return createComplexXULElement('menupopup', attrs, Array.prototype.slice.apply(arguments, [1]) );
82 }
83
84 function createPopup (attrs) {
85         return createComplexXULElement('popup', attrs, Array.prototype.slice.apply(arguments, [1]) );
86 }
87
88 function createMenuitem (attrs) {
89         return createComplexXULElement('menuitem', attrs, Array.prototype.slice.apply(arguments, [1]) );
90 }
91
92 function createMARCTextbox (element,attrs) {
93
94         var box = createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [2]) );
95         box.onkeypress = function (event) {
96                 var root_node;
97                 var node = element;
98                 while(node = node.parent()) {
99                         root_node = node;
100                 }
101
102                 var row = event.target;
103                 while (row.tagName != 'row') row = row.parentNode;
104
105                 if (element.nodeKind() == 'attribute') element[0]=box.value;
106                 else element.setChildren( box.value );
107
108                 if (event.charCode == 100 && event.ctrlKey) { // ctrl+d
109
110                         var index_sf, target, move_data;
111                         if (element.localName() == 'subfield') {
112                                 index_sf = element;
113                                 target = event.target.parentNode;
114
115                                 var start = event.target.selectionStart;
116                                 var end = event.target.selectionEnd - event.target.selectionStart ?
117                                                 event.target.selectionEnd :
118                                                 event.target.value.length;
119
120                                 move_data = event.target.value.substring(start,end);
121                                 event.target.value = event.target.value.substring(0,start) + event.target.value.substring(end);
122                                 event.target.setAttribute('size', event.target.value.length + 2);
123
124                                 element.setChildren( event.target.value );
125
126                         } else if (element.localName() == 'code') {
127                                 index_sf = element.parent();
128                                 target = event.target.parentNode;
129                         } else if (element.localName() == 'tag' || element.localName() == 'ind1' || element.localName() == 'ind2') {
130                                 index_sf = element.parent().children()[element.parent().children().length() - 1];
131                                 target = event.target.parentNode.lastChild.lastChild;
132                         }
133
134                         var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{ move_data }</subfield>;
135
136                         index_sf.parent().insertChildAfter( index_sf, sf );
137
138                         var new_sf = marcSubfield(sf);
139
140                         if (target === target.parentNode.lastChild) {
141                                 target.parentNode.appendChild( new_sf );
142                         } else {
143                                 target.parentNode.insertBefore( new_sf, target.nextSibling );
144                         }
145
146                         new_sf.firstChild.nextSibling.focus();
147
148                         event.preventDefault();
149                         return false;
150
151                 } else if (event.keyCode == 13 || event.keyCode == 77) {
152                         if (event.ctrlKey) { // ctrl+enter
153
154                                 var index;
155                                 if (element.localName() == 'subfield') index = element.parent();
156                                 if (element.localName() == 'code') index = element.parent().parent();
157                                 if (element.localName() == 'tag') index = element.parent();
158                                 if (element.localName() == 'ind1') index = element.parent();
159                                 if (element.localName() == 'ind2') index = element.parent();
160
161                                 var df = <datafield tag="" ind1="" ind2="" xmlns="http://www.loc.gov/MARC21/slim"><subfield code="" /></datafield>;
162
163                                 index.parent().insertChildAfter( index, df );
164
165                                 var new_df = marcDatafield(df);
166
167                                 if (row.parentNode.lastChild === row) {
168                                         row.parentNode.appendChild( new_df );
169                                 } else {
170                                         row.parentNode.insertBefore( new_df, row.nextSibling );
171                                 }
172
173                                 new_df.firstChild.focus();
174
175                                 event.preventDefault();
176                                 return false;
177
178                         } else if (event.shiftKey) {
179                                 if (row.previousSibling.className.match('marcDatafieldRow'))
180                                         row.previousSibling.firstChild.focus();
181                         } else {
182                                 row.nextSibling.firstChild.focus();
183                         }
184
185                 } else if (event.keyCode == 46 && event.ctrlKey) { // ctrl+del
186
187                         var index;
188                         if (element.localName() == 'subfield') index = element.parent();
189                         if (element.localName() == 'code') index = element.parent().parent();
190                         if (element.localName() == 'tag') index = element.parent();
191                         if (element.localName() == 'ind1') index = element.parent();
192                         if (element.localName() == 'ind2') index = element.parent();
193
194                         for (var i in index.parent().children()) {
195                                 if (index === index.parent().children()[i]) {
196                                         delete index.parent().children()[i];
197                                         break;
198                                 }
199                         }
200
201                         row.previousSibling.firstChild.focus();
202                         row.parentNode.removeChild(row);
203
204                         event.preventDefault();
205                         return false;
206
207                 } else if (event.keyCode == 46 && event.shiftKey) { // shift+del
208
209                         var index;
210                         if (element.localName() == 'subfield') index = element;
211                         if (element.localName() == 'code') index = element.parent();
212
213                         if (index) {
214                                 for (var i in index.parent().children()) {
215                                         if (index === index.parent().children()[i]) {
216                                                 delete index.parent().children()[i];
217                                                 break;
218                                         }
219                                 }
220
221                                 if (event.target.parentNode === event.target.parentNode.parentNode.lastChild) {
222                                         event.target.parentNode.previousSibling.lastChild.focus();
223                                 } else {
224                                         event.target.parentNode.nextSibling.firstChild.nextSibling.focus();
225                                 }
226
227                                 event.target.parentNode.parentNode.removeChild(event.target.parentNode);
228
229                                 event.preventDefault();
230                                 return false;
231                         }
232                 }
233                 return true;
234         };
235
236         box.addEventListener(
237                 'keypress', 
238                 function () {
239                         if (element.nodeKind() == 'attribute') element[0]=box.value;
240                         else element.setChildren( box.value );
241                         return true;
242                 },
243                 false
244         );
245
246         box.addEventListener(
247                 'change', 
248                 function () {
249                         if (element.nodeKind() == 'attribute') element[0]=box.value;
250                         else element.setChildren( box.value );
251                         return true;
252                 },
253                 false
254         );
255
256         box.addEventListener(
257                 'keypress', 
258                 function () {
259                         if (element.nodeKind() == 'attribute') element[0]=box.value;
260                         else element.setChildren( box.value );
261                         return true;
262                 },
263                 true
264         );
265
266         return box;
267 }
268
269 var rec_type = {
270         BKS : { Type : /[at]{1}/,       BLvl : /[acdm]{1}/ },
271         SER : { Type : /[a]{1}/,        BLvl : /[bs]{1}/ },
272         VIS : { Type : /[gkro]{1}/,     BLvl : /[abcdms]{1}/ },
273         MIX : { Type : /[p]{1}/,        BLvl : /[cd]{1}/ },
274         MAP : { Type : /[ef]{1}/,       BLvl : /[abcdms]{1}/ },
275         SCO : { Type : /[cd]{1}/,       BLvl : /[abcdms]{1}/ },
276         REC : { Type : /[ij]{1}/,       BLvl : /[abcdms]{1}/ },
277         COM : { Type : /[m]{1}/,        BLvl : /[abcdms]{1}/ }
278 };
279
280 var ff_pos = {
281         Ctry : {
282                 _8 : {
283                         BKS : {start : 15, len : 3, def : ' ' },
284                         SER : {start : 15, len : 3, def : ' ' },
285                         VIS : {start : 15, len : 3, def : ' ' },
286                         MIX : {start : 15, len : 3, def : ' ' },
287                         MAP : {start : 15, len : 3, def : ' ' },
288                         SCO : {start : 15, len : 3, def : ' ' },
289                         REC : {start : 15, len : 3, def : ' ' },
290                         COM : {start : 15, len : 3, def : ' ' },
291                 }
292         },
293         Lang : {
294                 _8 : {
295                         BKS : {start : 35, len : 3, def : ' ' },
296                         SER : {start : 35, len : 3, def : ' ' },
297                         VIS : {start : 35, len : 3, def : ' ' },
298                         MIX : {start : 35, len : 3, def : ' ' },
299                         MAP : {start : 35, len : 3, def : ' ' },
300                         SCO : {start : 35, len : 3, def : ' ' },
301                         REC : {start : 35, len : 3, def : ' ' },
302                         COM : {start : 35, len : 3, def : ' ' },
303                 }
304         },
305         MRec : {
306                 _8 : {
307                         BKS : {start : 38, len : 1, def : ' ' },
308                         SER : {start : 38, len : 1, def : ' ' },
309                         VIS : {start : 38, len : 1, def : ' ' },
310                         MIX : {start : 38, len : 1, def : ' ' },
311                         MAP : {start : 38, len : 1, def : ' ' },
312                         SCO : {start : 38, len : 1, def : ' ' },
313                         REC : {start : 38, len : 1, def : ' ' },
314                         COM : {start : 38, len : 1, def : ' ' },
315                 }
316         },
317         DtSt : {
318                 _8 : {
319                         BKS : {start : 6, len : 1, def : ' ' },
320                         SER : {start : 6, len : 1, def : 'c' },
321                         VIS : {start : 6, len : 1, def : ' ' },
322                         MIX : {start : 6, len : 1, def : ' ' },
323                         MAP : {start : 6, len : 1, def : ' ' },
324                         SCO : {start : 6, len : 1, def : ' ' },
325                         REC : {start : 6, len : 1, def : ' ' },
326                         COM : {start : 6, len : 1, def : ' ' },
327                 }
328         },
329         Type : {
330                 ldr : {
331                         BKS : {start : 6, len : 1, def : 'a' },
332                         SER : {start : 6, len : 1, def : 'a' },
333                         VIS : {start : 6, len : 1, def : 'g' },
334                         MIX : {start : 6, len : 1, def : 'p' },
335                         MAP : {start : 6, len : 1, def : 'e' },
336                         SCO : {start : 6, len : 1, def : 'c' },
337                         REC : {start : 6, len : 1, def : 'i' },
338                         COM : {start : 6, len : 1, def : 'm' },
339                 }
340         },
341         Ctrl : {
342                 ldr : {
343                         BKS : {start : 8, len : 1, def : ' ' },
344                         SER : {start : 8, len : 1, def : ' ' },
345                         VIS : {start : 8, len : 1, def : ' ' },
346                         MIX : {start : 8, len : 1, def : ' ' },
347                         MAP : {start : 8, len : 1, def : ' ' },
348                         SCO : {start : 8, len : 1, def : ' ' },
349                         REC : {start : 8, len : 1, def : ' ' },
350                         COM : {start : 8, len : 1, def : ' ' },
351                 }
352         },
353         BLvl : {
354                 ldr : {
355                         BKS : {start : 7, len : 1, def : 'm' },
356                         SER : {start : 7, len : 1, def : 's' },
357                         VIS : {start : 7, len : 1, def : 'm' },
358                         MIX : {start : 7, len : 1, def : 'c' },
359                         MAP : {start : 7, len : 1, def : 'm' },
360                         SCO : {start : 7, len : 1, def : 'm' },
361                         REC : {start : 7, len : 1, def : 'm' },
362                         COM : {start : 7, len : 1, def : 'm' },
363                 }
364         },
365         Desc : {
366                 ldr : {
367                         BKS : {start : 18, len : 1, def : ' ' },
368                         SER : {start : 18, len : 1, def : ' ' },
369                         VIS : {start : 18, len : 1, def : ' ' },
370                         MIX : {start : 18, len : 1, def : ' ' },
371                         MAP : {start : 18, len : 1, def : ' ' },
372                         SCO : {start : 18, len : 1, def : ' ' },
373                         REC : {start : 18, len : 1, def : ' ' },
374                         COM : {start : 18, len : 1, def : ' ' },
375                 }
376         },
377         ELvl : {
378                 ldr : {
379                         BKS : {start : 17, len : 1, def : ' ' },
380                         SER : {start : 17, len : 1, def : ' ' },
381                         VIS : {start : 17, len : 1, def : ' ' },
382                         MIX : {start : 17, len : 1, def : ' ' },
383                         MAP : {start : 17, len : 1, def : ' ' },
384                         SCO : {start : 17, len : 1, def : ' ' },
385                         REC : {start : 17, len : 1, def : ' ' },
386                         COM : {start : 17, len : 1, def : ' ' },
387                 }
388         },
389         Indx : {
390                 _8 : {
391                         BKS : {start : 31, len : 1, def : '0' },
392                         MAP : {start : 31, len : 1, def : '0' },
393                 },
394                 _6 : {
395                         BKS : {start : 14, len : 1, def : '0' },
396                         MAP : {start : 14, len : 1, def : '0' },
397                 }
398         },
399         Date1 : {
400                 _8 : {
401                         BKS : {start : 7, len : 4, def : ' ' },
402                         SER : {start : 7, len : 4, def : ' ' },
403                         VIS : {start : 7, len : 4, def : ' ' },
404                         MIX : {start : 7, len : 4, def : ' ' },
405                         MAP : {start : 7, len : 4, def : ' ' },
406                         SCO : {start : 7, len : 4, def : ' ' },
407                         REC : {start : 7, len : 4, def : ' ' },
408                         COM : {start : 7, len : 4, def : ' ' },
409                 },
410         },
411         Date2 : {
412                 _8 : {
413                         BKS : {start : 11, len : 4, def : ' ' },
414                         SER : {start : 11, len : 4, def : '9' },
415                         VIS : {start : 11, len : 4, def : ' ' },
416                         MIX : {start : 11, len : 4, def : ' ' },
417                         MAP : {start : 11, len : 4, def : ' ' },
418                         SCO : {start : 11, len : 4, def : ' ' },
419                         REC : {start : 11, len : 4, def : ' ' },
420                         COM : {start : 11, len : 4, def : ' ' },
421                 },
422         },
423         LitF : {
424                 _8 : {
425                         BKS : {start : 33, len : 1, def : '0' },
426                 },
427                 _6 : {
428                         BKS : {start : 16, len : 1, def : '0' },
429                 }
430         },
431         Biog : {
432                 _8 : {
433                         BKS : {start : 34, len : 1, def : ' ' },
434                 },
435                 _6 : {
436                         BKS : {start : 17, len : 1, def : ' ' },
437                 }
438         },
439         Ills : {
440                 _8 : {
441                         BKS : {start : 18, len : 4, def : ' ' },
442                 },
443                 _6 : {
444                         BKS : {start : 1, len : 4, def : ' ' },
445                 }
446         },
447         Fest : {
448                 _8 : {
449                         BKS : {start : 30, len : 1, def : '0' },
450                 },
451                 _6 : {
452                         BKS : {start : 13, len : 1, def : '0' },
453                 }
454         },
455         Conf : {
456                 _8 : {
457                         BKS : {start : 24, len : 4, def : ' ' },
458                         SER : {start : 25, len : 3, def : ' ' },
459                 },
460                 _6 : {
461                         BKS : {start : 7, len : 4, def : ' ' },
462                         SER : {start : 8, len : 3, def : ' ' },
463                 }
464         },
465         GPub : {
466                 _8 : {
467                         BKS : {start : 28, len : 1, def : ' ' },
468                         SER : {start : 28, len : 1, def : ' ' },
469                         VIS : {start : 28, len : 1, def : ' ' },
470                         MAP : {start : 28, len : 1, def : ' ' },
471                         COM : {start : 28, len : 1, def : ' ' },
472                 },
473                 _6 : {
474                         BKS : {start : 11, len : 1, def : ' ' },
475                         SER : {start : 11, len : 1, def : ' ' },
476                         VIS : {start : 11, len : 1, def : ' ' },
477                         MAP : {start : 11, len : 1, def : ' ' },
478                         COM : {start : 11, len : 1, def : ' ' },
479                 }
480         },
481         Audn : {
482                 _8 : {
483                         BKS : {start : 22, len : 1, def : ' ' },
484                         SER : {start : 22, len : 1, def : ' ' },
485                         VIS : {start : 22, len : 1, def : ' ' },
486                         SCO : {start : 22, len : 1, def : ' ' },
487                         REC : {start : 22, len : 1, def : ' ' },
488                         COM : {start : 22, len : 1, def : ' ' },
489                 },
490                 _6 : {
491                         BKS : {start : 5, len : 1, def : ' ' },
492                         SER : {start : 5, len : 1, def : ' ' },
493                         VIS : {start : 5, len : 1, def : ' ' },
494                         SCO : {start : 5, len : 1, def : ' ' },
495                         REC : {start : 5, len : 1, def : ' ' },
496                         COM : {start : 5, len : 1, def : ' ' },
497                 }
498         },
499         Form : {
500                 _8 : {
501                         BKS : {start : 23, len : 1, def : ' ' },
502                         SER : {start : 23, len : 1, def : ' ' },
503                         VIS : {start : 29, len : 1, def : ' ' },
504                         MIX : {start : 23, len : 1, def : ' ' },
505                         MAP : {start : 29, len : 1, def : ' ' },
506                         SCO : {start : 23, len : 1, def : ' ' },
507                         REC : {start : 23, len : 1, def : ' ' },
508                 },
509                 _6 : {
510                         BKS : {start : 6, len : 1, def : ' ' },
511                         SER : {start : 6, len : 1, def : ' ' },
512                         VIS : {start : 12, len : 1, def : ' ' },
513                         MIX : {start : 6, len : 1, def : ' ' },
514                         MAP : {start : 12, len : 1, def : ' ' },
515                         SCO : {start : 6, len : 1, def : ' ' },
516                         REC : {start : 6, len : 1, def : ' ' },
517                 }
518         },
519         'S/L' : {
520                 _8 : {
521                         SER : {start : 34, len : 1, def : '0' },
522                 },
523                 _6 : {
524                         SER : {start : 17, len : 1, def : '0' },
525                 }
526         },
527         'Alph' : {
528                 _8 : {
529                         SER : {start : 33, len : 1, def : ' ' },
530                 },
531                 _6 : {
532                         SER : {start : 16, len : 1, def : ' ' },
533                 }
534         },
535 };
536
537 function recordType (rec) {
538         var _l = rec.leader.toString();
539
540         var _t = _l.substr(ff_pos.Type.ldr.BKS.start, ff_pos.Type.ldr.BKS.len);
541         var _b = _l.substr(ff_pos.BLvl.ldr.BKS.start, ff_pos.BLvl.ldr.BKS.len);
542
543         for (var t in rec_type) {
544                 if (_t.match(rec_type[t].Type) && _b.match(rec_type[t].BLvl)) {
545                         document.getElementById('recordTypeLabel').value = t;
546                         _record_type = t;
547                         return t;
548                 }
549         }
550 }
551
552 function toggleFFE () {
553         var grid = document.getElementById('leaderGrid');
554         if (grid.hidden) {
555                 grid.hidden = false;
556         } else {
557                 grid.hidden = true;
558         }
559         return true;
560 }
561
562 function changeFFEditor (type) {
563         var grid = document.getElementById('leaderGrid');
564         grid.setAttribute('type',type);
565 }
566
567 function fillFixedFields (rec) {
568         var grid = document.getElementById('leaderGrid');
569
570         var rtype = _record_type;
571
572         var _l = rec.leader.toString();
573         var _6 = rec.controlfield.(@tag=='006').toString();
574         var _7 = rec.controlfield.(@tag=='007').toString();
575         var _8 = rec.controlfield.(@tag=='008').toString();
576
577         var list = [];
578         var pre_list = grid.getElementsByTagName('label');
579         for (var i in pre_list) {
580                 if ( pre_list[i].getAttribute && pre_list[i].getAttribute('set').indexOf(grid.getAttribute('type')) > -1 ) {
581                         list.push( pre_list[i] );
582                 }
583         }
584
585         for (var i in list) {
586                 var name = list[i].getAttribute('name');
587
588                 if (!ff_pos[name])
589                         continue;
590
591                 var value = '';
592                 if ( ff_pos[name].ldr && ff_pos[name].ldr[rtype] )
593                         value = _l.substr(ff_pos[name].ldr[rtype].start, ff_pos[name].ldr[rtype].len);
594
595                 if ( ff_pos[name]._8 && ff_pos[name]._8[rtype] )
596                         value = _8.substr(ff_pos[name]._8[rtype].start, ff_pos[name]._8[rtype].len);
597
598                 if ( !value && ff_pos[name]._6 && ff_pos[name]._6[rtype] )
599                         value = _6.substr(ff_pos[name]._6[rtype].start, ff_pos[name]._6[rtype].len);
600
601                 if ( ff_pos[name]._7 && ff_pos[name]._7[rtype] )
602                         value = _7.substr(ff_pos[name]._7[rtype].start, ff_pos[name]._7[rtype].len);
603                 
604                 if (!value) {
605                         var d;
606                         var p;
607                         if (ff_pos[name].ldr && ff_pos[name].ldr[rtype]) {
608                                 d = ff_pos[name].ldr[rtype].def;
609                                 p = 'ldr';
610                         }
611
612                         if (ff_pos[name]._8 && ff_pos[name]._8[rtype]) {
613                                 d = ff_pos[name]._8[rtype].def;
614                                 p = '_8';
615                         }
616
617                         if (!value && ff_pos[name]._6 && ff_pos[name]._6[rtype]) {
618                                 d = ff_pos[name]._6[rtype].def;
619                                 p = '_6';
620                         }
621
622                         if (ff_pos[name]._7 && ff_pos[name]._7[rtype]) {
623                                 d = ff_pos[name]._7[rtype].def;
624                                 p = '_7';
625                         }
626
627                         if (!value) {
628                                 for (var j = 0; j < ff_pos[name][p][rtype].len; j++) {
629                                         value += d;
630                                 }
631                         }
632                 }
633
634                 list[i].nextSibling.value = value;
635         }
636
637         return true;
638 }
639
640 function updateFixedFields (element) {
641         var grid = document.getElementById('leaderGrid');
642         var recGrid = document.getElementById('recGrid');
643
644         var rtype = _record_type;
645         var new_value = element.value;
646
647         var parts = {
648                 ldr : _record.leader,
649                 _6 : _record.controlfield.(@tag=='006'),
650                 _7 : _record.controlfield.(@tag=='007'),
651                 _8 : _record.controlfield.(@tag=='008'),
652         };
653
654         var name = element.getAttribute('name');
655         for (var i in ff_pos[name]) {
656
657                 if (!ff_pos[name][i][rtype]) continue;
658                 if (!parts[i]) continue;
659
660                 var before = parts[i].substr(0, ff_pos[name][i][rtype].start);
661                 var after = parts[i].substr(ff_pos[name][i][rtype].start + ff_pos[name][i][rtype].len);
662
663                 for (var j = 0; new_value.length < ff_pos[name][i][rtype].len; j++) {
664                         new_value += ff_pos[name][i][rtype].def;
665                 }
666
667                 parts[i].setChildren( before + new_value + after );
668                 recGrid.getElementsByAttribute('tag',i)[0].lastChild.value = parts[i].toString();
669         }
670
671         return true;
672 }
673
674 function marcLeader (leader) {
675         var row = createRow(
676                 { class : 'marcLeaderRow',
677                   tag : 'ldr' },
678                 createLabel(
679                         { value : 'LDR',
680                           class : 'marcTag',
681                           tooltiptext : "MARC Leader" } ),
682                 createLabel(
683                         { value : '',
684                           class : 'marcInd1' } ),
685                 createLabel(
686                         { value : '',
687                           class : 'marcInd2' } ),
688                 createLabel(
689                         { value : leader.text(),
690                           class : 'marcLeader' } )
691         );
692
693         return row;
694 }
695
696 function marcControlfield (field) {
697         tagname = field.@tag.toString().substr(2);
698         var row = createRow(
699                 { class : 'marcControlfieldRow',
700                   tag : '_' + tagname },
701                 createLabel(
702                         { value : field.@tag,
703                           class : 'marcTag',
704                           onmouseover : 'getTooltip(this, "tag");',
705                           tooltipid : 'tag' + field.@tag } ),
706                 createLabel(
707                         { value : field.@ind1,
708                           class : 'marcInd1',
709                           onmouseover : 'getTooltip(this, "ind1");',
710                           tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
711                 createLabel(
712                         { value : field.@ind2,
713                           class : 'marcInd2',
714                           onmouseover : 'getTooltip(this, "ind2");',
715                           tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
716                 createLabel(
717                         { value : field.text(),
718                           class : 'marcControlfield' } )
719         );
720
721         return row;
722 }
723
724 function stackSubfields(checkbox) {
725         var list = document.getElementsByAttribute('name','sf_box');
726
727         var o = 'vertical';
728         if (checkbox.checked) o = 'horizontal';
729         
730         for (var i in list) {
731                 if (list[i]) list[i].setAttribute('orient',o);
732         }
733 }
734
735 function marcDatafield (field) {
736         var row = createRow(
737                 { class : 'marcDatafieldRow' },
738                 createMARCTextbox(
739                         field.@tag,
740                         { value : field.@tag,
741                           class : 'plain marcTag',
742                           name : 'marcTag',
743                           context : 'tags_popup',
744                           oninput : 'if (this.value.length == 3) { this.nextSibling.focus(); }',
745                           size : 3,
746                           maxlength : 3,
747                           onmouseover : 'current_focus = this; getTooltip(this, "tag");' } ),
748                 createMARCTextbox(
749                         field.@ind1,
750                         { value : field.@ind1,
751                           class : 'plain marcInd1',
752                           name : 'marcInd1',
753                           oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
754                           size : 1,
755                           maxlength : 1,
756                           onmouseover : 'current_focus = this; getContextMenu(this, "ind1"); getTooltip(this, "ind1");',
757                           oncontextmenu : 'getContextMenu(this, "ind1");' } ),
758                 createMARCTextbox(
759                         field.@ind2,
760                         { value : field.@ind2,
761                           class : 'plain marcInd2',
762                           name : 'marcInd2',
763                           oninput : 'if (this.value.length == 1) { this.nextSibling.firstChild.firstChild.focus(); }',
764                           size : 1,
765                           maxlength : 1,
766                           onmouseover : 'current_focus = this; getContextMenu(this, "ind2"); getTooltip(this, "ind2");',
767                           oncontextmenu : 'getContextMenu(this, "ind2");' } ),
768                 createHbox({ name : 'sf_box' })
769         );
770
771         if (!current_focus && field.@tag == '') current_focus = row.childNodes[0];
772         if (!current_focus && field.@ind1 == '') current_focus = row.childNodes[1];
773         if (!current_focus && field.@ind2 == '') current_focus = row.childNodes[2];
774
775         var sf_box = row.lastChild;
776         if (document.getElementById('stackSubfields').checked)
777                 sf_box.setAttribute('orient','vertical');
778
779         sf_box.addEventListener(
780                 'click',
781                 function (e) {
782                         if (sf_box === e.target) {
783                                 sf_box.lastChild.lastChild.focus();
784                         } else if (e.target.parentNode === sf_box) {
785                                 e.target.lastChild.focus();
786                         }
787                 },
788                 false
789         );
790
791
792         for (var i in field.subfield) {
793                 var sf = field.subfield[i];
794                 sf_box.appendChild(
795                         marcSubfield(sf)
796                 );
797
798                 if (sf.@code == '' && (!current_focus || current_focus.className.match(/Ind/)))
799                         current_focus = sf_box.lastChild.childNodes[1];
800         }
801
802         return row;
803 }
804
805 function marcSubfield (sf) {                    
806         return createHbox(
807                 { class : 'marcSubfieldBox' },
808                 createLabel(
809                         { value : "\u2021",
810                           class : 'plain marcSubfieldDelimiter',
811                           onmouseover : 'getTooltip(this.nextSibling, "subfield");',
812                           oncontextmenu : 'getContextMenu(this.nextSibling, "subfield");',
813                           //onclick : 'this.nextSibling.focus();',
814                           onfocus : 'this.nextSibling.focus();',
815                           size : 2 } ),
816                 createMARCTextbox(
817                         sf.@code,
818                         { value : sf.@code,
819                           class : 'plain marcSubfieldCode',
820                           name : 'marcSubfieldCode',
821                           onmouseover : 'current_focus = this; getContextMenu(this, "subfield"); getTooltip(this, "subfield");',
822                           oncontextmenu : 'getContextMenu(this, "subfield");',
823                           oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
824                           size : 2,
825                           maxlength : 1 } ),
826                 createMARCTextbox(
827                         sf,
828                         { value : sf.text(),
829                           name : sf.parent().@tag + ':' + sf.@code,
830                           class : 'plain marcSubfield', 
831                           onmouseover : 'getTooltip(this, "subfield");',
832                           contextmenu : function (event) { getAuthorityContextMenu(event.target, sf) },
833                           size : new String(sf.text()).length + 2,
834                           oninput : "this.setAttribute('size', this.value.length + 2);",
835                         } )
836         );
837 }
838
839 function loadRecord(rec) {
840         _record = rec;
841         var grid_rows = document.getElementById('recGrid').lastChild;
842
843         grid_rows.appendChild( marcLeader( rec.leader ) );
844
845         for (var i in rec.controlfield) {
846                 grid_rows.appendChild( marcControlfield( rec.controlfield[i] ) );
847         }
848
849         for (var i in rec.datafield) {
850                 grid_rows.appendChild( marcDatafield( rec.datafield[i] ) );
851         }
852
853         grid_rows.getElementsByAttribute('class','marcDatafieldRow')[0].firstChild.focus();
854         changeFFEditor(recordType(rec));
855         fillFixedFields(rec);
856 }
857
858 var context_menus = createComplexXULElement('popupset');
859 document.documentElement.appendChild( context_menus );
860
861 var tag_menu = createPopup({position : 'after_start', id : 'tags_popup'});
862 context_menus.appendChild( tag_menu );
863
864 tag_menu.appendChild(
865         createMenuitem(
866                 { label : 'Add Row',
867                   oncommand : 
868                         'var e = document.createEvent("KeyEvents");' +
869                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,13,0);' +
870                         'current_focus.inputField.dispatchEvent(e);',
871                  }
872         )
873 );
874
875 tag_menu.appendChild(
876         createMenuitem(
877                 { label : 'Remove Row',
878                   oncommand : 
879                         'var e = document.createEvent("KeyEvents");' +
880                         'e.initKeyEvent("keypress",1,1,null,1,0,0,0,46,0);' +
881                         'current_focus.inputField.dispatchEvent(e);',
882                 }
883         )
884 );
885
886 tag_menu.appendChild( createComplexXULElement( 'separator' ) );
887
888
889
890 function genToolTips () {
891         for (var i in bib_data.field) {
892                 var f = bib_data.field[i];
893         
894                 tag_menu.appendChild(
895                         createMenuitem(
896                                 { label : f.@tag,
897                                   oncommand : 
898                                         'current_focus.value = "' + f.@tag + '";' +
899                                         'var e = document.createEvent("MutationEvents");' +
900                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
901                                         'current_focus.inputField.dispatchEvent(e);',
902                                   disabled : f.@tag < '010' ? "true" : "false",
903                                   tooltiptext : f.description }
904                         )
905                 );
906         
907                 var i1_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i1' });
908                 context_menus.appendChild( i1_popup );
909         
910                 var i2_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'i2' });
911                 context_menus.appendChild( i2_popup );
912         
913                 var sf_popup = createPopup({position : 'after_start', id : 't' + f.@tag + 'sf' });
914                 context_menus.appendChild( sf_popup );
915         
916                 tooltip_hash['tag' + f.@tag] = f.description;
917                 for (var j in f.indicator) {
918                         var ind = f.indicator[j];
919                         tooltip_hash['tag' + f.@tag + 'ind' + ind.@position + 'val' + ind.@value] = ind.description;
920         
921                         if (ind.@position == 1) {
922                                 i1_popup.appendChild(
923                                         createMenuitem(
924                                                 { label : ind.@value,
925                                                   oncommand : 
926                                                         'current_focus.value = "' + ind.@value + '";' +
927                                                         'var e = document.createEvent("MutationEvents");' +
928                                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
929                                                         'current_focus.inputField.dispatchEvent(e);',
930                                                   tooltiptext : ind.description }
931                                         )
932                                 );
933                         }
934         
935                         if (ind.@position == 2) {
936                                 i2_popup.appendChild(
937                                         createMenuitem(
938                                                 { label : ind.@value,
939                                                   oncommand : 
940                                                         'current_focus.value = "' + ind.@value + '";' +
941                                                         'var e = document.createEvent("MutationEvents");' +
942                                                         'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
943                                                         'current_focus.inputField.dispatchEvent(e);',
944                                                   tooltiptext : ind.description }
945                                         )
946                                 );
947                         }
948                 }
949         
950                 for (var j in f.subfield) {
951                         var sf = f.subfield[j];
952                         tooltip_hash['tag' + f.@tag + 'sf' + sf.@code] = sf.description;
953         
954                         sf_popup.appendChild(
955                                 createMenuitem(
956                                         { label : sf.@code,
957                                           oncommand : 
958                                                 'current_focus.value = "' + sf.@code + '";' +
959                                                 'var e = document.createEvent("MutationEvents");' +
960                                                 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
961                                                 'current_focus.inputField.dispatchEvent(e);',
962                                           tooltiptext : sf.description,
963                                         }
964                                 )
965                         );
966                 }
967         }
968 }
969
970 var p = createComplexXULElement('popupset');
971 document.documentElement.appendChild( p );
972
973 function getTooltip (target, type) {
974
975         var tt = '';
976         if (type == 'subfield')
977                 tt = 'tag' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf' + target.parentNode.childNodes[1].value;
978
979         if (type == 'ind1')
980                 tt = 'tag' + target.parentNode.firstChild.value + 'ind1val' + target.value;
981
982         if (type == 'ind2')
983                 tt = 'tag' + target.parentNode.firstChild.value + 'ind2val' + target.value;
984
985         if (type == 'tag')
986                 tt = 'tag' + target.parentNode.firstChild.value;
987
988         if (!document.getElementById( tt )) {
989                 p.appendChild(
990                         createTooltip(
991                                 { id : tt,
992                                   flex : "1",
993                                   orient : 'vertical',
994                                   onpopupshown : 'this.width = this.firstChild.boxObject.width + 10; this.height = this.firstChild.boxObject.height + 10;',
995                                   class : 'tooltip' },
996                                 createDescription({}, document.createTextNode( tooltip_hash[tt] ) )
997                         )
998                 );
999         }
1000
1001         target.tooltip = tt;
1002         return true;
1003 }
1004
1005 function getContextMenu (target, type) {
1006
1007         var tt = '';
1008         if (type == 'subfield')
1009                 tt = 't' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf';
1010
1011         if (type == 'ind1')
1012                 tt = 't' + target.parentNode.firstChild.value + 'i1';
1013
1014         if (type == 'ind2')
1015                 tt = 't' + target.parentNode.firstChild.value + 'i2';
1016
1017         target.setAttribute('context', tt);
1018         return true;
1019 }
1020
1021 function getAuthorityContextMenu (target, sf) {
1022         var menu_id = sf.parent().@tag + ':' + sf.@code + '-authority-context';
1023
1024         if (target.getAttribute('context')) return true;
1025
1026         var sf_popup = createPopup({position : 'after_start', id : menu_id });
1027         context_menus.appendChild( sf_popup );
1028
1029         var val = 'foo';
1030         sf_popup.appendChild(
1031                 createMenu(
1032                         { label : 'bar' },
1033                         createMenuPopup( {},
1034                                 createMenuitem(
1035                                         { label : val,
1036                                           oncommand : 
1037                                                 'current_focus.value = "' + val + '";' +
1038                                                 'var e = document.createEvent("MutationEvents");' +
1039                                                 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1040                                                 'current_focus.inputField.dispatchEvent(e);',
1041                                           tooltiptext : val + ' description'
1042                                         }
1043                                 )
1044                         )
1045                 )
1046         );
1047
1048         target.setAttribute('context', menu_id);
1049         return true;
1050 }
1051
1052