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