]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
Fix MFHD edit invocation by using pcrud.retrieve to get the right MFHD record
[working/Evergreen.git] / Open-ILS / web / opac / skin / default / js / rdetail.js
1 /* */
2
3
4 detachAllEvt('common', 'run');
5 attachEvt("common", "run", rdetailDraw);
6 attachEvt("rdetail", "recordDrawn", rdetailBuildStatusColumns);
7 attachEvt("rdetail", "recordDrawn", rdetailBuildInfoRows);
8 attachEvt("rdetail", "recordDrawn", rdetailGetPageIds);
9
10 var record = null;
11 var cp_statuses = null;
12 var recordsCache = [];
13
14 var copyRowParent = null;
15 var copyRow = null;
16 var statusRow = null;
17 var numStatuses = null;
18 var defaultCN;
19 var callnumberCache = {};
20 var rdetailLocalOnly = true;
21 var globalCNCache = {};
22 var localTOC;
23 var cachedRecords;
24 var _statusPositions = {};
25
26 var rdetailShowLocal = true;
27 var rdetailShowCopyLocation = true;
28 var googleBookPreview = true;
29 var displaySerialHoldings = true;
30
31
32 var nextContainerIndex;
33
34 function rdetailReload() {
35         var args = {};
36         args[PARAM_LOCATION] = getNewSearchLocation();
37         args[PARAM_DEPTH] = depthSelGetDepth();
38         goTo(buildOPACLink(args));
39 }
40
41 var nextRecord;
42 var prevRecord;
43
44 var rdetailPrev = null;
45 var rdetailNext = null;
46 var rdetailStart = null;
47 var rdetailEnd = null;
48
49
50
51 /* looks to see if we have a next and/or previous record in the
52    record cache, if so, set up the nav links */
53 function rdetailSetPaging(ids) {
54
55         cachedRecords = {};
56         cachedRecords.ids = ids;
57
58         for( var i = 0; i < cachedRecords.ids.length; i++ ) {
59                 var rec = cachedRecords.ids[i];
60                 if( rec == getRid() ) {
61                         if( i > 0 ) prevRecord = cachedRecords.ids[i-1];
62                         if( i < cachedRecords.ids.length - 1 )
63                                 nextRecord = cachedRecords.ids[i+1];
64                         break;
65                 }
66         }
67
68         $('np_offset').appendChild(text(i + 1));
69         $('np_count').appendChild(text(getHitCount()));
70
71         if(prevRecord) {
72                 unHideMe($('np_table'));
73                 unHideMe($('np_prev'));
74                 unHideMe($('np_start'));
75                 rdetailPrev = function() { _rdetailNav(prevRecord); };
76                 rdetailStart = function() { _rdetailNav(cachedRecords.ids[0]); };
77         }
78
79         if(nextRecord) {
80                 unHideMe($('np_table'));
81                 unHideMe($('np_next'));
82                 unHideMe($('np_end'));
83                 rdetailNext = function() { _rdetailNav(nextRecord); };
84                 rdetailEnd = function() { _rdetailNav(cachedRecords.ids[cachedRecords.ids.length-1]); };
85         }
86
87         runEvt('rdetail', 'nextPrevDrawn', i, cachedRecords.ids.length);
88 }
89
90
91 function _rdetailNav(id, offset) {
92         var args = {};
93         args[PARAM_RID] = id;
94         goTo(buildOPACLink(args));
95 }
96
97 function rdetailDraw() {
98
99         detachAllEvt('common','depthChanged');
100         detachAllEvt('common','locationUpdated');
101         attachEvt('common','depthChanged', rdetailReload);
102         attachEvt('common','locationUpdated', rdetailReload);
103         attachEvt('common','holdUpdated', rdetailReload);
104         attachEvt('common','holdUpdateCanceled', rdetailReload);
105
106         copyRowParent = G.ui.rdetail.cp_info_row.parentNode;
107         copyRow = copyRowParent.removeChild(G.ui.rdetail.cp_info_row);
108         statusRow = G.ui.rdetail.cp_status.parentNode;
109         statusRow.id = '__rdsrow';
110
111         G.ui.rdetail.cp_info_local.onclick = rdetailShowLocalCopies;
112         G.ui.rdetail.cp_info_all.onclick = rdetailShowAllCopies;
113
114         if(getLocation() == globalOrgTree.id())
115                 hideMe(G.ui.rdetail.cp_info_all);
116
117         var req = new Request(FETCH_RMODS, getRid());
118         req.callback(_rdetailDraw);
119         req.send();
120
121         if (displaySerialHoldings) {
122                 var req = new Request(FETCH_MFHD_SUMMARY, getRid());
123                 req.callback(_holdingsDraw);
124                 req.send();
125         }
126
127         detachAllEvt("result", "idsReceived");
128         G.evt.result.hitCountReceived = [];
129         G.evt.result.recordReceived = [];
130         G.evt.result.copyCountsReceived = [];
131         G.evt.result.allRecordsReceived = [];
132 }
133
134 function rdetailGetPageIds() {
135         attachEvt("result", "idsReceived", rdetailSetPaging );
136         resultFetchAllRecords = true;
137         rresultCollectIds(true);
138 }
139
140
141 function buildunAPISpan (span, type, id) {
142         var cgi = new CGI();
143         var d = new Date();
144
145         addCSSClass(span,'unapi-id');
146
147         span.setAttribute(
148                         'title', 'tag:' + cgi.server_name + ',' +
149                         d.getFullYear() + ':' + type + '/' + id
150                         );
151 }
152
153 function rdetailViewMarc(r,id) {
154         hideMe($('rdetail_extras_loading'));
155         $('rdetail_view_marc_box').innerHTML = r.getResultObject();
156
157         var div = elem('div', { "class" : 'hide_me' });
158         var span = div.appendChild( elem('abbr') );
159
160         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
161
162         $('rdetail_view_marc_box').insertBefore(span, $('rdetail_view_marc_box').firstChild);
163 }
164
165
166 function rdetailShowLocalCopies() {
167         rdetailShowLocal = true;
168         rdetailBuildInfoRows();
169         hideMe(G.ui.rdetail.cp_info_local);
170         unHideMe(G.ui.rdetail.cp_info_all);
171         hideMe(G.ui.rdetail.cp_info_none); 
172 }
173
174 function rdetailShowAllCopies() {
175
176         rdetailShowLocal = false;
177         rdetailBuildInfoRows();
178         hideMe(G.ui.rdetail.cp_info_all);
179         unHideMe(G.ui.rdetail.cp_info_local);
180         hideMe(G.ui.rdetail.cp_info_none); 
181 }
182
183 function OpenMarcEditWindow(pcrud, rec) {
184         /*
185            To run in Firefox directly, must set signed.applets.codebase_principal_support
186            to true in about:config
187          */
188         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
189         win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
190         dojo.require('openils.PermaCrud');
191
192         win.xulG = {
193                 record : {marc : rec.marc()},
194                 save : {
195                         label: 'Save MFHD',
196                         func: function(xmlString) {
197                                 rec.marc(xmlString);
198                                 rec.ischanged(true);
199                                 pcrud.update(rec);
200                         },
201                 }
202         };
203         return null;
204 }
205
206 function loadMarcEditor(recId) {
207         var pcrud = new openils.PermaCrud({"authtoken": G.user.session});
208         var rec = pcrud.retrieve("sre", recId);
209         if (rec) {
210                 OpenMarcEditWindow(pcrud, rec);
211         }
212         return null;
213 }
214
215 /*
216  * This function could be written much more intelligently
217  * Limited brain power means that I'm brute-forcing it for now
218  */
219 function _holdingsDraw(h) {
220         holdings = h.getResultObject();
221         if (!holdings) { return null; }
222
223         dojo.forEach(holdings, _holdingsDrawMFHD);
224         return null;
225 }
226
227 function _holdingsDrawMFHD(holdings, entryNum) {
228         if (!orgIsMine(findOrgUnit(holdings.owning_lib()), findOrgUnit(getLocation()))) {
229                 return null;
230         }
231         var hh = holdings.holdings();
232         var hch = holdings.current_holdings();
233         var hs = holdings.supplements();
234         var hcs = holdings.current_supplements();
235         var hi = holdings.indexes();
236         var hci = holdings.current_indexes();
237         var ho = holdings.online();
238         var hm = holdings.missing();
239         var hinc = holdings.incomplete();
240
241         if (    hh.length == 0 && hch.length == 0 && hs.length == 0 &&
242                 hcs.length == 0 && hi.length == 0 && hci.length == 0 &&
243                 ho.length == 0 && hm.length == 0 && hinc.length == 0
244         ) {
245                 return null;
246         }
247
248         dojo.place("<table><caption id='mfhdHoldingsCaption' class='rdetail_header color_1'>Holdings summary</caption><tbody id='rdetail_holdings_tbody_" + entryNum + "'></tbody></table>", "rdetail_details_table", "after");
249         if (hh.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Holdings', hh); }
250         if (hch.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Current holdings', hch); }
251         if (hs.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Supplements', hs); }
252         if (hcs.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Current supplements', hcs); }
253         if (hi.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Indexes', hi); }
254         if (hci.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Current indexes', hci); }
255         if (ho.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Online', ho); }
256         if (hm.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Missing volumes', hm); }
257         if (hinc.length > 0) { _holdingsDrawMFHDEntry(entryNum, 'Incomplete volumes', hm); }
258
259         if (isXUL()) {
260                 dojo.require('openils.Event');
261                 dojo.require('openils.PermaCrud');
262                 dojo.place("<span> - </span><a class='classic_link' href='javascript:loadMarcEditor(" + holdings.id() + ")'> Edit</a>", "mfhdHoldingsCaption", "last");
263         }
264         return null;
265 }
266
267 function _holdingsDrawMFHDEntry(entryNum, entryName, entry) {
268         var flatEntry = entry.toString().replace(/,/g, ', ');
269         dojo.place("<tr><td> </td><td nowrap='nowrap' class='rdetail_desc'>" + entryName + "</td><td class='rdetail_item'>" + flatEntry + "</td></tr>", "rdetail_holdings_tbody_" + entryNum, "last");
270         return null;
271 }
272
273 function _rdetailDraw(r) {
274         record = r.getResultObject();
275
276         runEvt('rdetail', 'recordRetrieved', record.doc_id());
277
278         G.ui.rdetail.title.appendChild(text(record.title()));
279         buildSearchLink(STYPE_AUTHOR, record.author(), G.ui.rdetail.author);
280         G.ui.rdetail.isbn.appendChild(text(cleanISBN(record.isbn())));
281         G.ui.rdetail.edition.appendChild(text(record.edition()));
282         G.ui.rdetail.pubdate.appendChild(text(record.pubdate()));
283         G.ui.rdetail.publisher.appendChild(text(record.publisher()));
284         $('rdetail_physical_desc').appendChild(text(record.physical_description()));
285         r = record.types_of_resource();
286         if(r) {
287                 G.ui.rdetail.tor.appendChild(text(r[0]));
288                 setResourcePic( G.ui.rdetail.tor_pic, r[0]);
289         }
290         G.ui.rdetail.abstr.appendChild(text(record.synopsis()));
291
292         try{
293                 if(record.isbn()) {
294                         if(ENABLE_ADDED_CONTENT_ATTRIB_LINKS) {
295                                 unHideMe($('rdetail.jacket_attrib_div'));
296                                 var href = $('rdetail.jacket_attrib_link').getAttribute('href') +cleanISBN(record.isbn());
297                                 $('rdetail.jacket_attrib_link').setAttribute('href', href);
298                         }
299                         rdetailCheckForGBPreview();
300
301                 } else {
302                         hideMe($("rdetail.jacket_attrib_div"));
303                         hideMe($("rdetail_img_link"));
304                 }
305         } catch(E) {}
306
307
308         // see if the record has any external links 
309         var links = record.online_loc();
310         for( var i = 0; links && links.length > 0 && i < links.length; i = i + 3 ) {
311                 var href = links[i];
312                 // avoid matching "HTTP: The Complete Reference"
313                 if( href.match(/https?:\/|ftps?:\/|mailto:/i) ) {
314                         unHideMe($('rdetail_online_row'));
315                         // MODS can contain a display label (used for the text of the link)
316                         // as well as a note about the URL; many legacy systems conflate the
317                         // two and generate MARC records that expect the note to be used as
318                         // the text of the link, with no display label; here's the canonical
319                         // format:
320                         //
321                         // 856 40 $uhttp://localhost$yDisplay label$zPublic note
322                         //
323                         // Note that the MARC21slim2MODS XSL concatenates $3 and $y together
324                         // (as $y was defined later in MARC21's life as the display label)
325                         var displayLabel = '' + links[i+1];
326                         var note = '' + links[i+2];
327                         if(!displayLabel || displayLabel.match(/https?:\/|ftps?:\/|mailto:/i)) {
328                                 if(!note || note.match(/https?:\/|ftps?:\/|mailto:/i)) {
329                                         displayLabel = href;
330                                 } else {
331                                         displayLabel = note;
332                                 }
333                         }
334                         $('rdetail_online').appendChild(elem('a', {href:href,'class':'classic_link'}, displayLabel));
335                         if (!note && note != displayLabel) {
336                                 $('rdetail_online').appendChild(elem('span', {'class':'url_note'}, ' - ' + note));
337                         }
338                         $('rdetail_online').appendChild(elem('br'));
339                 }
340         }
341
342         // Fill in our unAPI ID, if anyone cares
343         var abbrs = document.getElementsByTagName('abbr');
344         var span;
345         for (var i = 0; i < abbrs.length; i = i + 1) {
346                 if (abbrs[i].getAttribute('name') == 'unapi') {
347                         span = abbrs[i];
348                         break;
349                 }
350         }
351         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
352
353         $('rdetail_place_hold').setAttribute(
354                         'href','javascript:holdsDrawEditor({record:"'+record.doc_id()+'",type:"T"});');
355
356         $('rdetail_img_link').setAttribute('href', buildISBNSrc(cleanISBN(record.isbn()), 'large'));
357         G.ui.rdetail.image.setAttribute("src", buildISBNSrc(cleanISBN(record.isbn())));
358         runEvt("rdetail", "recordDrawn");
359         recordsCache.push(record);
360
361         rdetailSetExtrasSelector();
362
363         var breq = new Request(FETCH_BRE, [getRid()]);
364         breq.callback( rdetailCheckDeleted );
365         breq.send();
366
367         resultBuildCaches( [ record ] );
368         resultDrawSubjects();
369         resultDrawSeries();
370
371         // grab added content 
372         acCollectData(cleanISBN(record.isbn()), rdetailhandleAC);
373 }
374
375
376
377 function rdetailCheckDeleted(r) {
378         var br = r.getResultObject()[0];
379         if( isTrue(br.deleted()) ) {
380                 hideMe($('rdetail_place_hold'));
381                 $('rdetail_more_actions_selector').disabled = true;
382                 unHideMe($('rdetail_deleted_exp'));
383         }
384 }
385
386 function rdetailSetExtrasSelector() {
387         if(!grabUser()) return;
388         unHideMe($('rdetail_more_actions'));
389
390         var req = new Request( 
391                         FETCH_CONTAINERS, G.user.session, G.user.id(), 'biblio', 'bookbag' );
392         req.callback(rdetailAddBookbags);
393         req.send();
394 }
395
396 function rdetailAddBookbags(r) {
397
398         var containers = r.getResultObject();
399         var selector = $('rdetail_more_actions_selector');
400         var found = false;
401         var index = 3;
402         doSelectorActions(selector);
403
404         for( var i = 0; i != containers.length; i++ ) {
405                 found = true;
406                 var container = containers[i];
407                 insertSelectorVal( selector, index++, container.name(), 
408                                 "container_" + container.id(), rdetailAddToBookbag,  1 );
409         }
410
411         nextContainerIndex = index;
412 }
413
414 var _actions = {};
415 function rdetailNewBookbag() {
416         var name = prompt($('rdetail_bb_new').innerHTML,"");
417         if(!name) return;
418
419         var id;
420         if( id = containerCreate( name ) ) {
421                 alert($('rdetail_bb_success').innerHTML);
422                 var selector = $('rdetail_more_actions_selector');
423                 insertSelectorVal( selector, nextContainerIndex++, name, 
424                                 "container_" + id, rdetailAddToBookbag, 1 );
425                 setSelector( selector, 'start' );
426         }
427 }
428
429
430 function rdetailAddToBookbag() {
431         var selector = $('rdetail_more_actions_selector');
432         var id = selector.options[selector.selectedIndex].value;
433         setSelector( selector, 'start' );
434
435         if( containerCreateItem( id.substring(10), record.doc_id() )) {
436                 alert($('rdetail_bb_item_success').innerHTML);
437         }
438 }
439
440
441 var rdetailMarcFetched = false;
442 function rdetailShowExtra(type, args) {
443
444         hideMe($('rdetail_copy_info_div'));
445         hideMe($('rdetail_reviews_div'));
446         hideMe($('rdetail_toc_div'));
447         hideMe($('rdetail_anotes_div'));
448         hideMe($('rdetail_excerpt_div'));
449         hideMe($('rdetail_preview_div'));
450         hideMe($('rdetail_marc_div'));
451         hideMe($('cn_browse'));
452         hideMe($('rdetail_cn_browse_div'));
453         hideMe($('rdetail_notes_div'));
454
455         removeCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
456         removeCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
457         removeCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
458         removeCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
459         removeCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
460         removeCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
461         removeCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
462         removeCSSClass($('rdetail_annotation_link'), 'rdetail_extras_selected');
463         removeCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
464
465         switch(type) {
466
467                 case "copyinfo": 
468                         unHideMe($('rdetail_copy_info_div')); 
469                         addCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
470                         break;
471
472                 case "reviews": 
473                         addCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
474                         unHideMe($('rdetail_reviews_div')); 
475                         break;
476
477                 case "excerpt": 
478                         addCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
479                         unHideMe($('rdetail_excerpt_div'));
480                         break;
481
482                 case "preview": 
483                         addCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
484                         unHideMe($('rdetail_preview_div'));
485                         rdetailDisplayGBPreview();
486                         break;
487
488                 case "anotes": 
489                         addCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
490                         unHideMe($('rdetail_anotes_div'));
491                         break;
492
493                 case "toc": 
494                         addCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
495                         unHideMe($('rdetail_toc_div'));
496                         break;
497
498                 case "marc": 
499                         addCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
500                         unHideMe($('rdetail_marc_div')); 
501                         if(rdetailMarcFetched) return;
502                         unHideMe($('rdetail_extras_loading'));
503                         rdetailMarcFetched = true;
504                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
505                         req.callback(rdetailViewMarc); 
506                         req.send();
507                         break;
508
509                 case 'cn':
510                         addCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
511                         unHideMe($('rdetail_cn_browse_div'));
512                         rdetailShowCNBrowse(defaultCN, getLocation(), null, true);
513                         break;
514
515         }
516 }
517
518 function rdetailVolumeDetails(args) {
519         var row = $(args.rowid);
520         var tbody = row.parentNode;
521         cpdBuild( tbody, row, record, args.cn, args.org, args.depth, args.copy_location );
522         return;
523 }
524
525 function rdetailBuildCNList() {
526
527         var select = $('cn_browse_selector');
528         var index = 0;
529         var arr = [];
530         for( var cn in callnumberCache ) arr.push( cn );
531         arr.sort();
532
533         if( arr.length == 0 ) {
534                 hideMe($('rdetail_cn_browse_select_div'));
535                 return;
536         }
537
538         for( var i in arr ) {
539                 var cn = arr[i];
540                 var opt = new Option(cn);
541                 select.options[index++] = opt;
542         }
543         select.onchange = rdetailGatherCN;
544 }
545
546 function rdetailGatherCN() {
547         var cn = getSelectorVal($('cn_browse_selector'));
548         rdetailShowCNBrowse( cn, getLocation(), getDepth(), true );
549         setSelector( $('cn_browse_selector'), cn );
550 }
551
552
553 function rdetailShowCNBrowse( cn, loc, depth, fromOnclick ) {
554
555         if(!cn) {
556                 unHideMe($('cn_browse_none'));
557                 hideMe($('rdetail_cn_browse_select_div'));
558                 return;
559         }
560
561         unHideMe($('rdetail_cn_browse_select_div'));
562         rdetailBuildCNList();
563         setSelector( $('cn_browse_selector'), cn );
564         hideMe($('rdetail_copy_info_div'));
565         hideMe($('rdetail_reviews_div'));
566         hideMe($('rdetail_toc_div'));
567         hideMe($('rdetail_marc_div'));
568         unHideMe($('rdetail_cn_browse_div'));
569         unHideMe($('cn_browse'));
570         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
571         cnBrowseGo(cn, loc, depth);
572 }
573
574 function rdetailhandleAC(data) {
575
576         if( data.reviews.html ) {
577                 $('rdetail_review_container').innerHTML = data.reviews.html;
578                 unHideMe($('rdetail_reviews_link'));
579         }
580
581         if( data.toc.html ) {
582                 $('rdetail_toc_div').innerHTML = data.toc.html;
583                 unHideMe($('rdetail_toc_link'));
584         }
585
586         if( data.excerpt.html ) {
587                 $('rdetail_excerpt_div').innerHTML = data.excerpt.html;
588                 unHideMe($('rdetail_excerpt_link'));
589         }
590
591         if( data.anotes.html ) {
592                 $('rdetail_anotes_div').innerHTML = data.anotes.html;
593                 unHideMe($('rdetail_anotes_link'));
594         }
595 }
596
597 function rdetailShowReviews(r) {
598         hideMe($('rdetail_extras_loading'));
599         var res = r.getResultObject();
600         var par = $('rdetail_reviews_div');
601         var template = par.removeChild($('rdetail_review_template'));
602         if( res && res.length > 0 ) {
603                 unHideMe($('rdetail_reviews_link'));
604                 for( var i = 0; i != res.length; i++ ) {
605                         var rev = res[i];       
606                         if( rev.text && rev.info ) {
607                                 var node = template.cloneNode(true);
608                                 $n(node, 'review_header').appendChild(text(rev.info));
609                                 $n(node, 'review_text').appendChild(text(rev.text));
610                                 par.appendChild(node);
611                         }
612                 }
613         }
614 }
615
616
617 function rdetailShowTOC(r) {
618         hideMe($('rdetail_extras_loading'));
619         var resp = r.getResultObject();
620         if(resp) {
621                 unHideMe($('rdetail_toc_link'));
622                 $('rdetail_toc_div').innerHTML = resp;
623         }
624 }
625
626 function rdetailBuildInfoRows() {
627         var req;
628         var method = FETCH_COPY_COUNTS_SUMMARY;
629         if (rdetailShowCopyLocation)
630                 method = FETCH_COPY_LOCATION_COUNTS_SUMMARY;
631
632         if( rdetailShowLocal ) 
633                 req = new Request(method, record.doc_id(), getLocation(), getDepth())
634         else
635                 req = new Request(method, record.doc_id());
636         req.callback(_rdetailBuildInfoRows);
637         req.send();
638 }
639
640 function _rdetailRows(node) {
641
642         if( rdetailShowLocal && getLocation() != globalOrgTree.id() ) {
643                 var loc = findOrgUnit(getLocation());
644                 if( node ) {
645                         if( !orgIsMine(node, loc) && !orgIsMine(loc,node) ) return;
646                 } else {
647                         for( var i = 0; i < globalOrgTree.children().length; i++ ) {
648                                 var org = findOrgUnit(globalOrgTree.children()[i]);
649                                 if( orgIsMine(org, loc) ) {
650                                         node = org;
651                                         break;
652                                 }
653                         }
654                 } 
655         }
656
657         if(!node && findOrgType(globalOrgTree.ou_type()).can_have_vols())
658                 node = globalOrgTree;
659
660
661         /* don't show hidden orgs */
662
663         if(node) {
664
665                 if(!isXUL() && !isTrue(node.opac_visible())) return;
666
667                 var row = copyRow.cloneNode(true);
668                 row.id = "cp_info_" + node.id();
669
670                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
671                 var cntd  = findNodeByName( row, config.names.rdetail.cn_cell );
672                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
673                 var actions = $n(row, 'rdetail_actions_cell');
674
675                 var p = libtd.getElementsByTagName('a')[0];
676                 libtd.insertBefore(text(node.name()), p);
677                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
678
679                 if(!findOrgType(node.ou_type()).can_have_vols()) {
680
681                         row.removeChild(cntd);
682                         row.removeChild(cpctd);
683                         row.removeChild(actions);
684                         row.setAttribute('novols', '1');
685
686                         libtd.setAttribute("colspan", numStatuses + 3 );
687                         libtd.colSpan = numStatuses + 3;
688                         addCSSClass(row, 'copy_info_region_row');
689                 } 
690
691                 copyRowParent.appendChild(row);
692
693         } else { node = globalOrgTree; }
694
695         for( var c in node.children() ) 
696                 _rdetailRows(node.children()[c]);
697 }
698
699 function rdetailCNPrint(orgid, cn) {
700         var div = cpdBuildPrintWindow( record, orgid);
701         var template = div.removeChild($n(div, 'cnrow'));
702         var rowNode = $("cp_info_" + orgid);
703         cpdStylePopupWindow(div);
704         openWindow(div.innerHTML);
705 }
706
707 var localCNFound = false;
708 var ctr = 0;
709 function _rdetailBuildInfoRows(r) {
710
711         if (rdetailShowCopyLocation)
712                 unHideMe( $n( $('rdetail_copy_info_table'), 'rdetail_copylocation_header' ) );
713
714         removeChildren(copyRowParent);
715
716         _rdetailRows();
717
718         var summary = r.getResultObject();
719         if(!summary) return;
720
721         var found = false;
722         for( var i = 0; i < summary.length; i++ ) {
723
724                 var arr = summary[i];
725                 globalCNCache[arr[1]] = 1;
726                 var thisOrg = findOrgUnit(arr[0]);
727                 var rowNode = $("cp_info_" + thisOrg.id());
728                 if(!rowNode) continue;
729
730                 if(rowNode.getAttribute("used")) {
731
732                         if( rowNode.nextSibling ) {
733                                 sib = rowNode.nextSibling;
734                                 o ='cp_info_'+thisOrg.id()+'_';
735                                 /* push the new row on as the last row for this org unit */
736                                 while( sib && sib.id.match(o) ) {
737                                         sib = sib.nextSibling;
738                                 }
739                                 if(sib)
740                                         rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), sib);
741                                 else
742                                         rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
743                         } else {
744                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
745                         }
746
747                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
748                         n.appendChild(text(thisOrg.name()));
749                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
750                         rowNode.id = "cp_info_" + thisOrg.id() + '_' + (++ctr); 
751
752                 } else {
753                         rowNode.setAttribute("used", "1");
754                 }
755
756                 var cpc_temp = rowNode.removeChild(
757                                 findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
758
759                 var statuses = arr[2];
760                 var cl = '';
761                 if (rdetailShowCopyLocation) {
762                         cl = arr[2];
763                         statuses = arr[3];
764                 }
765
766
767                 rdetailApplyStatuses(rowNode, cpc_temp, statuses);
768
769                 var isLocal = false;
770                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
771                         found = true; 
772                         isLocal = true; 
773                         if(!localCNFound) {
774                                 localCNFound = true;
775                                 defaultCN = arr[1];
776                         }
777                 }
778
779                 //if(isLocal) unHideMe(rowNode);
780                 unHideMe(rowNode);
781
782                 rdetailSetPath( thisOrg, isLocal );
783                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal, thisOrg, cl );
784
785                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
786         }
787
788         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
789 }
790
791 function rdetailBuildBrowseInfo(row, cn, local, orgNode, cl) {
792
793         if(local) {
794                 var cache = callnumberCache[cn];
795                 if( cache ) cache.count++;
796                 else callnumberCache[cn] = { count : 1 };
797         }
798
799         var depth = getDepth();
800         if( !local ) depth = findOrgDepth(globalOrgTree);
801
802         $n(row, 'rdetail_callnumber_cell').appendChild(text(cn));
803
804         if (rdetailShowCopyLocation) {
805                 var cl_cell = $n(row, 'rdetail_copylocation_cell');
806                 cl_cell.appendChild(text(cl));
807                 unHideMe(cl_cell);
808         }
809
810         _debug('setting action clicks for cn ' + cn);
811
812         var dHref = 'javascript:rdetailVolumeDetails('+
813                         '{copy_location : "'+cl+'", rowid : "'+row.id+'", cn :"'+cn+'", depth:"'+depth+'", org:"'+orgNode.id()+'", local: '+local+'});';
814
815         var bHref = 'javascript:rdetailShowCNBrowse("' + cn + '", '+orgNode.id()+', "'+depth+'");'; 
816
817         unHideMe( $n(row, 'details') )
818                 $n(row, 'details').setAttribute('href', dHref);
819         unHideMe( $n(row, 'browse') )
820                 $n(row, 'browse').setAttribute('href', bHref);
821
822         if(isXUL()) {
823                 unHideMe($n(row, 'hold_div'));
824                 $n(row, 'hold').onclick = function() {
825                         var req = new Request(FETCH_VOLUME_BY_INFO, cn, record.doc_id(), orgNode.id());
826                         req.callback(
827                                         function(r) {
828                                         var vol = r.getResultObject();
829                                         holdsDrawEditor({type: 'V', volumeObject : vol});
830                                         }
831                                     );
832                         req.send();
833                 };
834         }
835 }
836
837 // sets the path to org as 'active' and displays the path if it's local 
838 function rdetailSetPath(org, local) {
839         if( findOrgDepth(org) == 0 ) return;
840         var row = $("cp_info_" + org.id());
841         row.setAttribute("hasinfo", "1");
842         unHideMe(row);
843         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
844 }
845
846 //Append all the statuses for a given summary to the 
847 //copy summary table 
848 function rdetailApplyStatuses( row, template, statuses ) {
849         for( var j in _statusPositions ) {
850                 var stat = _statusPositions[j];
851                 var val = statuses[stat.id()];
852                 var nn = template.cloneNode(true);
853                 if(val) nn.appendChild(text(val));
854                 else nn.appendChild(text(0));
855                 row.appendChild(nn);
856         }
857 }
858
859 //Add one td (creating a new column) to the copy summary
860 //table for each opac_visible copy status
861 function rdetailBuildStatusColumns() {
862
863         rdetailGrabCopyStatuses();
864         var parent = statusRow;
865         var template = parent.removeChild(G.ui.rdetail.cp_status);
866
867         var i = 0;
868         for( i = 0; i < cp_statuses.length; i++ ) {
869
870                 var c = cp_statuses[i];
871                 if( c && isTrue(c.opac_visible()) ) {
872                         var name = c.name();
873                         _statusPositions[i] = c;
874                         var node = template.cloneNode(true);
875                         var data = findNodeByName( node, config.names.rdetail.cp_status);
876
877                         data.appendChild(text(name));
878                         parent.appendChild(node);
879                 }
880         }       
881
882         numStatuses = 0;
883         for(x in _statusPositions) numStatuses++; 
884 }
885
886 function rdetailGrabCopyStatuses() {
887         if(cp_statuses) return cp_statuses;
888         var req = new Request(FETCH_COPY_STATUSES);
889         req.send(true);
890         cp_statuses = req.result();
891         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
892 }
893
894 function _rdetailSortStatuses(a, b) {
895         return parseInt(a.id()) - parseInt(b.id());
896 }
897
898 /**
899  * Check for a Google Book preview 
900  */
901 function rdetailCheckForGBPreview() {
902         if (!googleBookPreview) return;
903         searchForGBPreview( cleanISBN(record.isbn()) );
904 }
905
906 /**
907  *
908  * @param {DOM object} query The form element containing the
909  *                     input parameters "isbns"
910  */
911 function searchForGBPreview( isbn ) {
912
913         // Delete any previous Google Booksearch JSON queries.
914         var GBPJsonScript = document.getElementById("GBPJsonScript");
915         if (GBPJsonScript) {
916                 GBPJsonScript.parentNode.removeChild(GBPJsonScript);
917         }
918
919         // Add a script element with the src as the user's Google Booksearch query. 
920         // JSON output is specified by including the alt=json-in-script argument
921         // and the callback function is also specified as a URI argument.
922         var GBPScriptElement = document.createElement("script");
923
924         GBPScriptElement.setAttribute("id", "GBPJsonScript");
925         GBPScriptElement.setAttribute("src",
926                         "http://books.google.com/books?bibkeys=" + 
927                         isbn + "&jscmd=viewapi&callback=GBPreviewCallback");
928         GBPScriptElement.setAttribute("type", "text/javascript");
929
930         // make the request to Google booksearch
931         document.documentElement.firstChild.appendChild(GBPScriptElement);
932 }
933
934 /**
935  * This function is the call-back function for the JSON scripts which 
936  * executes a Google book search response.
937  *
938  * XXX I18N of text needed
939  *
940  * @param {JSON} booksInfo is the JSON object pulled from the Google books service.
941  */
942 function GBPreviewCallback(GBPBookInfo) {
943         var GBPreviewDiv = document.getElementById("rdetail_preview_div");
944         var GBPBook;
945
946         for ( i in GBPBookInfo ) {
947                 GBPBook = GBPBookInfo[i];
948         }
949
950         if ( !GBPBook ) {
951                 return;
952         }
953
954         if ( GBPBook.preview != "noview" ) {
955                 if ( GBPBook.preview == 'full' ) {
956                         setText( $('rdetail_preview_link'), $('rdetail_preview_full_text').innerHTML );
957                         $('rdetail_preview_link_a').title = $('rdetail_preview_title').innerHTML;      
958                 }
959
960                 // Add a button below the book cover image to load the preview.
961                 GBPBadge = document.createElement( 'img' );
962                 GBPBadge.src = 'http://books.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif';
963                 GBPBadge.title = $('rdetail_preview_badge').innerHTML;
964                 GBPBadge.style.border = 0;
965                 GBPBadgelink = document.createElement( 'a' );
966                 GBPBadgelink.href = 'javascript:rdetailShowExtra("preview");';
967                 GBPBadgelink.appendChild( GBPBadge );
968                 $('rdetail_image_cell').appendChild( GBPBadgelink );
969                 $('rdetail_preview_div').style.height = 600;
970
971                 /* Display the "Preview" tab in the Extras section */
972                 unHideMe( $('rdetail_preview_link' ) );
973         }
974 }
975
976 /**
977  *  This is called when the user clicks on the 'Preview' link.  We assume
978  *  a preview is available from Google if this link was made visible.
979  *
980  * XXX I18N of Google Book Preview language attribute needed
981  */
982 function rdetailDisplayGBPreview() {
983         unHideMe($('rdetail_extras_loading'));
984         GBPreviewPane = $('rdetail_preview_div');
985         if ( GBPreviewPane.getAttribute('loaded') == null ||
986                 GBPreviewPane.getAttribute('loaded') == "false" ) {
987                 google.load("books", "0", {"callback" : rdetailGBPViewerLoadCallback, "language": "en"} );
988                 GBPreviewPane.setAttribute('loaded', 'true');
989         }
990 }
991
992 function rdetailGBPViewerLoadCallback() {
993         hideMe($('rdetail_extras_loading'));
994         var GBPViewer = new google.books.DefaultViewer(document.getElementById('rdetail_preview_div'));
995         GBPViewer.load('ISBN:' + cleanISBN(record.isbn()) );
996
997 }