e713b06ca820494f584203fb143c82eeb60386fa
[pazpar2-moved-to-github.git] / www / iphone / example_client.js
1 /* A very simple client that shows a basic usage of the pz2.js
2 */
3
4 // create a parameters array and pass it to the pz2's constructor
5 // then register the form submit event with the pz2.search function
6 // autoInit is set to true on default
7 var usesessions = false;
8 var pazpar2path = '/service-proxy/';
9 var showResponseType = '';
10 // Facet configuration
11 var querys = {'su': '', 'au': '', 'xt': ''};
12 var query_client_server = {'su': 'subject', 'au': 'author', 'xt': 'xtargets'};
13 var querys_server = {};
14 var useLimit = 1;
15 // Fail to get JSON working stabil.
16 var showResponseType = 'xml';
17
18 var imageHelper = new ImageHelper();
19
20 if (document.location.hash == '#pazpar2' || document.location.search.match("useproxy=false")) {
21     usesessions = true;
22     pazpar2path = '/pazpar2/search.pz2';
23     showResponseType = 'xml';
24 }
25
26 my_paz = new pz2( { "onshow": my_onshow,
27 //                    "showtime": 2000,            //each timer (show, stat, term, bytarget) can be specified this way
28                     "pazpar2path": pazpar2path,
29                     "oninit": my_oninit,
30                     "onstat": null,
31                     "onterm": my_onterm_iphone,
32                     "termlist": "xtargets,subject,author",
33                     "onbytarget": null,
34                     "usesessions" : usesessions,
35                     "showResponseType": showResponseType,
36                     "onrecord": my_onrecord,
37                     "errorhandler" : my_onerror} 
38 );
39 // some state vars
40 var curPage = 1;
41 var recPerPage = 10;
42 var recToShowPageSize = 20;
43 var recToShow = recToShowPageSize;
44 var recIDs = {};
45 var totalRec = 0;
46 var curDetRecId = '';
47 var curDetRecData = null;
48 var curSort = 'relevance';
49 var curFilter = 'ALL';
50 var submitted = false;
51 var SourceMax = 16;
52 var SubjectMax = 10;
53 var AuthorMax = 10;
54 var tab = "recordview"; 
55
56 var triedPass = "";
57 var triedUser = "";
58
59 //
60 // pz2.js event handlers:
61 //
62 function my_onerror(error) {
63     switch(error.code) {
64         // No targets!
65     case "8": alert("No resources were selected for the search"); break;
66         // target not configured, this is a pazpar2 bug
67         // but for now simply research
68     case "9": 
69         triggerSearch(); 
70         break;
71         // authentication
72     case "100" : 
73         auth.check(loggedIn, login);
74         //window.location = "login.html"; break;
75     default: 
76         alert("Unhandled error: " + error.code);
77         throw error; // display error in JavaScript console
78     }
79 }
80
81 function loginFormSubmit() {
82     triedUser = document.loginForm.username.value;
83     triedPass = document.loginForm.password.value;
84     auth.login( {"username": triedUser,
85                 "password": triedPass},
86         authCb, authCb);
87 }
88
89 function handleKeyPress(e)  
90 {  
91   var key;  
92   if(window.event)  
93     key = window.event.keyCode;  
94   else  
95     key = e.which;  
96
97   if(key == 13 || key == 10)  
98   {  
99       button = document.getElementById('button');
100       button.focus();
101       button.click();
102
103       return false;  
104   }  
105   else  
106     return true;  
107 }  
108
109 function authCb(authData) {
110     if (!authData.loginFailed) {
111         triedUser = "";
112         triedPass = "";
113     }
114
115     if (authData.loggedIn == true) {        
116         showhide("recordview");
117     }
118 }
119
120 function logOutClick() {
121     auth.logOut(authCb, authCb);
122 }
123
124 function loggedOut() {
125     var login = document.getElementById("login");
126     login.innerHTML = 'Login';
127 }
128
129 function loggingOutFailed() {
130     alert("Logging out failed");
131 }
132
133 function login() {
134     showhide("login");
135 }
136
137 function logout() {
138     auth.logOut(loggedOut, loggingOutFailed, true);
139 }
140
141 function logInOrOut() {
142     var loginElement = document.getElementById("login");
143     if (loginElement.innerHTML == 'Login')
144         login();
145     else
146         logout();
147 }
148 function loggedIn() {
149     var login = document.getElementById("login");
150     login.innerHTML = 'Logout';
151     document.getElementById("log").innerHTML = login.innerHTML;
152 }
153
154 function auth_check() {
155     auth.check(loggedIn, login);
156     domReady();
157 }
158
159 //
160 // Pz2.js event handlers:
161 //
162 function my_oninit() {
163     my_paz.stat();
164     my_paz.bytarget();
165 }
166
167 function showMoreRecords() {
168     var i = recToShow;
169     recToShow += recToShowPageSize;
170     for ( ; i < recToShow && i < recPerPage; i++) {
171         var element = document.getElementById(recIDs[i]);
172         if (element)
173             element.style.display = '';
174     }
175     if (i == recPerPage) {
176         var element = document.getElementById('recdiv_END');
177         if (element)
178             element.style.display = 'none';
179     }
180 }
181
182 function hideRecords() {
183     for ( var i = 0; i < recToShow; i++) {
184         var element = document.getElementById(recIDs[i]);
185         if (element && recIDs != curDetRecId)
186             element.style.display = 'none';
187     }
188     var element = document.getElementById('recdiv_END');
189     if (element)
190         element.style.display = 'none';
191 }
192
193 function showRecords() {
194     for (var i = 0 ; i < recToShow && i < recPerPage; i++) {
195         var element = document.getElementById(recIDs[i]);
196         if (element)
197             element.style.display = '';
198     }
199     var element = document.getElementById('recdiv_END');
200     if (element) {
201         if (i == recPerPage)
202             element.style.display = 'none';
203         else
204             element.style.display = '';
205     }
206 }
207
208
209
210
211 function my_onshow(data) {
212     totalRec = data.merged;
213     // move it out
214     var pager = document.getElementById("pager");
215     pager.innerHTML = "";
216     drawPager(pager);
217     pager.innerHTML +='<div class="status">Displaying: ' 
218                     + (data.start + 1) + ' to ' + (data.start + data.num) +
219                      ' of ' + data.merged + ' (found: ' 
220                      + data.total + ')</div>';
221
222     var results = document.getElementById("results");
223     
224     var html = [];
225     if (data.hits == undefined) 
226         return ;
227     var style = '';
228     for (var i = 0; i < data.hits.length; i++) {
229         var hit = data.hits[i];
230         var recDivID = "recdiv_" + hit.recid; 
231         recIDs[i] = recDivID;
232         var lines = 0;
233         if (i == recToShow)
234             style = ' style="display:none" ';
235         html.push('<li class="img arrow" id="' + recDivID + '" ' + style +  '>' );
236         html.push('<a class="img" href="#' + i + '" id="rec_'+hit.recid + '" onclick="showDetails(this.id);return false;" >');
237         if (1) {
238             var useThumbnails = hit["md-use_thumbnails"];
239             var thumburls = hit["md-thumburl"];
240             if (thumburls && (useThumbnails == undefined || useThumbnails == "1")) {
241                 var thumbnailtag = imageHelper.getImageTagByRecId(hit.recid,"md-thumburl", 60, "S"); 
242                 html.push(thumbnailtag);
243             } else { 
244                 if (hit["md-isbn"] != undefined) { 
245                     var coverimagetag = imageHelper.getImageTagByRecId(hit.recid, "md-isbn", 60, "S"); 
246                     if (coverimagetag.length>0) { 
247                         html.push(coverimagetag);
248                     } else { 
249                         html.push("&nbsp;");
250                     }
251                 }
252             }
253         } 
254         html.push("</a>");
255         html.push('<a href="#" id="rec_'+hit.recid + '" onclick="showDetails(this.id);return false;">');
256         html.push(hit["md-title"]); 
257         html.push("</a>");
258
259         if (hit["md-title-remainder"] != undefined) {
260             html.push('<a href="#" id="rec_'+hit.recid + '" onclick="showDetails(this.id);return false;">');
261             html.push(hit["md-title-remainder"]);
262             html.push("</a>");
263             lines++;
264         }
265         if (hit["md-author"] != undefined) {
266             html.push('<a href="#" id="rec_'+hit.recid + '" onclick="showDetails(this.id);return false;">');
267             html.push(hit["md-author"]);
268             html.push("</a>");
269             lines++;
270         }
271         else if (hit["md-title-responsibility"] != undefined) {
272             html.push('<a href="#" id="rec_'+hit.recid + '" onclick="showDetails(this.id);return false;">');
273             html.push(hit["md-title-responsibility"]);
274             html.push("</a>");
275             lines++;
276         }
277         for (var idx = lines ; idx < 2 ; idx++) {
278             html.push('<a href="#" id="rec_'+hit.recid + '" onclick="showDetails(this.id);return false;">');
279             html.push("&nbsp;");            
280             html.push("</a>");
281         }
282 /*
283         if (hit.recid == curDetRecId) {
284             html.push(renderDetails_iphone(curDetRecData));
285         }
286 */
287         html.push('</li>');
288     }
289     if (data.activeclients == 0)
290         document.getElementById("loading").style.display = 'none';
291 /*
292     // set up "More..." if needed. 
293     style = 'display:none';
294     if (recToShow < recPerPage) {
295         style = 'display:block';
296     }
297     html.push('<li class="img" id="recdiv_END" style="' + style + '"><a onclick="showMoreRecords()">More...</a></li>');     
298 */
299     replaceHtml(results, html.join(''));
300 }
301
302 function my_onstat(data) {
303     var stat = document.getElementById("stat");
304     if (stat == null)
305         return;
306     
307     stat.innerHTML = '<b> .:STATUS INFO</b> -- Active clients: '
308                         + data.activeclients
309                         + '/' + data.clients + ' -- </span>'
310                         + '<span>Retrieved records: ' + data.records
311                         + '/' + data.hits + ' :.</span>';
312 }
313
314 function showhide(newtab, hash) {
315     var showtermlist = false;
316     if (newtab != null)
317         tab = newtab;
318     
319     if (tab == "recordview") {
320         document.getElementById("recordview").style.display = '';
321         if (hash != undefined)
322             document.location.hash = hash;
323     }
324     else 
325         document.getElementById("recordview").style.display = 'none';
326
327     if (tab == "xtargets") {
328         document.getElementById("term_xtargets").style.display = '';
329         showtermlist = true;
330     }
331     else
332         document.getElementById("term_xtargets").style.display = 'none';
333
334     if (tab == "subjects") {
335         document.getElementById("term_subjects").style.display = '';
336         showtermlist = true;
337     }
338     else
339         document.getElementById("term_subjects").style.display = 'none';
340
341     if (tab == "authors") {
342         document.getElementById("term_authors").style.display = '';
343         showtermlist = true;
344     }
345     else
346         document.getElementById("term_authors").style.display = 'none';
347
348     if (tab == "detailview") {
349         document.getElementById("detailview").style.display = '';
350     }
351     else {
352         document.getElementById("detailview").style.display = 'none';
353         var element = document.getElementById("rec_" + curDetRecId);
354         if (element != undefined)
355             element.scrollIntoView();
356
357     }
358     if (showtermlist == false) 
359         document.getElementById("termlist").style.display = 'none';
360     else 
361         document.getElementById("termlist").style.display = '';
362
363     var tabDiv = document.getElementById("loginDiv");
364     if (tab == "login") {
365         tabDiv.style.display = '';
366     }
367     else {
368         tabDiv.style.display = 'none';
369     }
370 }
371
372 function my_onterm(data) {
373     var termlists = [];
374     
375     termlists.push('<div id="term_xtargets" >');
376     termlists.push('<h4 class="termtitle">Sources</h4>');
377     termlists.push('<ul class="termlist">');
378     termlists.push('<li> <a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
379     for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
380         termlists.push('<li class="termlist"><a href="#" target_id='+data.xtargets[i].id
381             + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">' 
382             + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
383     }
384     termlists.push('</ul>');
385     termlists.push('</div>');
386      
387     termlists.push('<div id="term_subjects" >');
388     termlists.push('<h4>Subjects</h4>');
389     termlists.push('<ul class="termlist">');
390     termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
391     for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
392         termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">' 
393                        + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
394     }
395     termlists.push('</ul>');
396     termlists.push('</div>');
397             
398     termlists.push('<div id="term_authors" >');
399     termlists.push('<h4 class="termtitle">Authors</h4>');
400     termlists.push('<ul class="termlist">');
401     termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All</a></li>');
402     for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
403         termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'au\', \'' + data.author[i].name +'\');return false;">' 
404                             + data.author[i].name 
405                             + '  (' 
406                             + data.author[i].freq 
407                             + ')</a></li>');
408     }
409     termlists.push('</ul>');
410     termlists.push('</div>');
411     var termlist = document.getElementById("termlist");
412     replaceHtml(termlist, termlists.join(''));
413     showhide();
414 }
415
416 var termlist = {};
417 function my_onterm_iphone(data) {
418     my_onterm(data);
419     var targets = "reset_xt|All\n";
420     
421     for (var i = 0; i < data.xtargets.length; i++ ) {
422         
423         targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
424     }
425     termlist["xtargets"] = targets;
426     var subjects = "reset_su|All\n";
427     for (var i = 0; i < data.subject.length; i++ ) {
428         subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
429     }
430     termlist["subjects"] = subjects;
431     var authors = "reset_au|All\n";
432     for (var i = 0; i < data.author.length; i++ ) {
433         authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
434     }
435     termlist["authors"] = authors;
436     callback.send("termlist", "refresh");
437 }
438
439 function getTargets() {
440         return termlist['xtargets'];
441 }
442
443 function getSubjects() {
444         return termlist['subjects'];
445 }
446
447 function getAuthors() {
448         return termlist['authors'];
449 }
450
451 function my_onrecord(data) {
452
453     // FIXME: record is async!!
454     clearTimeout(my_paz.recordTimer);
455     // in case on_show was faster to redraw element
456     var detailRecordDiv = document.getElementById('detailrecord');
457     if (!detailRecordDiv) 
458         return;
459     curDetRecData = data;
460     var html = renderDetails_iphone(curDetRecData);
461     detailRecordDiv.innerHTML = html;
462     showhide('detailview');
463     document.getElementById("loading").style.display = 'none';
464 }
465
466 function my_onrecord_iphone(data) {
467     my_onrecord(data);
468     callback.send("record", data.recid, data, data.xtargets[i].freq);
469 }
470
471
472 function my_onbytarget(data) {
473     var targetDiv = document.getElementById("bytarget");
474     var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
475         +'<td>Records</td><td>State</td></tr></thead><tbody>';
476     
477     for (var i = 0; i < data.length; i++ ) {
478         table += "<tr><td>" + data[i].id +
479             "</td><td>" + data[i].hits +
480             "</td><td>" + data[i].diagnostic +
481             "</td><td>" + data[i].records +
482             "</td><td>" + data[i].state + "</td></tr>";
483     }
484
485     table += '</tbody></table>';
486     targetDiv.innerHTML = table;
487 }
488
489 ////////////////////////////////////////////////////////////////////////////////
490 ////////////////////////////////////////////////////////////////////////////////
491
492 // wait until the DOM is ready
493 function domReady () 
494
495     document.search.onsubmit = onFormSubmitEventHandler;
496     document.search.query.value = '';
497     document.select.sort.onchange = onSelectDdChange;
498     document.select.perpage.onchange = onSelectDdChange;
499     if (document.location.search.match("inApp=true")) 
500         applicationMode(true);
501     else
502         applicationMode(false);
503
504     var params = parseQueryString(window.location.search);
505     if (params.query) {
506         document.search.query.value = params.query;
507         onFormSubmitEventHandler();
508     }
509 }
510  
511 function applicationMode(newmode) 
512 {
513     var searchdiv = document.getElementById("searchForm");
514     if (newmode)
515         inApp = newmode;
516     if (inApp) {
517         document.getElementById("heading").style.display="none";
518         searchdiv.style.display = 'none';
519     }
520     else { 
521         
522         document.getElementById("nav").style.display="";
523         document.getElementById("normal").style.display="inline";
524         document.getElementById("normal").style.visibility="";
525         searchdiv.style.display = '';
526         document.search.onsubmit = onFormSubmit;
527     }
528     callback.init();
529 }
530 // when search button pressed
531 function onFormSubmitEventHandler() 
532 {
533     resetPage();
534     document.getElementById("logo").style.display = 'none';
535     loadSelect();
536     triggerSearch();
537     submitted = true;
538     return true;
539 }
540
541 function onSelectDdChange()
542 {
543     if (!submitted) return false;
544     resetPage();
545     loadSelect();
546     my_paz.show(0, recPerPage, curSort);
547     return false;
548 }
549
550 function resetPage()
551 {
552     curPage = 1;
553     totalRec = 0;
554 }
555
556 function getFacets() {
557     var result = "";
558     for (var key in querys_server) {
559         if (result.length > 0)
560             result += ","
561         result += querys_server[key];
562     }
563     return result;
564 }
565
566 function triggerSearch ()
567 {
568     // Restore to initial page size
569     recToShow = recToShowPageSize;
570     document.getElementById("loading").style.display = 'inline';
571     my_paz.search(document.search.query.value, recPerPage, curSort, curFilter, undefined,
572         {
573            "limit" : getFacets() 
574         }
575         );
576     showhide("recordview");
577 }
578
579 function loadSelect ()
580 {
581     curSort = document.select.sort.value;
582     recPerPage = document.select.perpage.value;
583 }
584
585 // limit the query after clicking the facet
586 function limitQuery(field, value)
587 {
588   var newQuery = ' and ' + field + '="' + value + '"';
589   querys[field] += newQuery;
590   document.search.query.value += newQuery;
591   onFormSubmitEventHandler();
592   showhide("recordview");
593 }
594
595 // limit the query after clicking the facet
596 function limitQueryServer(field, value)
597 {
598     // Check for client field usage
599     var fieldname = query_client_server[field];
600     if (!fieldname) 
601         fieldname = field;      
602     
603     var newQuery = fieldname + '=' + value.replace(",", "\\,").replace("|", "\\,");
604     // Does it already exists?
605     if (querys_server[fieldname]) 
606         querys_server[fieldname] += "," + newQuery;
607     else
608         querys_server[fieldname] = newQuery;
609 //  document.search.query.value += newQuery;
610   onFormSubmitEventHandler();
611   showhide("recordview");
612 }
613
614
615
616 // limit the query after clicking the facet
617 function removeQuery (field, value) {
618         document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
619     onFormSubmitEventHandler();
620     showhide("recordview");
621 }
622
623 // limit the query after clicking the facet
624 function limitOrResetQuery (field, value, selected) {
625     if (useLimit) {
626         limitOrResetQueryServer(field,value, selected);
627         return ;
628     }
629     if (field == 'reset_su' || field == 'reset_au') {
630         var reset_field = field.substring(6);
631         document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
632         querys[reset_field] = '';
633         onFormSubmitEventHandler();
634         showhide("recordview");
635     }
636     else 
637         limitQuery(field, value);
638         //alert("limitOrResetQuerry: query after: " + document.search.query.value);
639 }
640
641 // limit the query after clicking the facet
642 function limitOrResetQueryServer (field, value, selected) {
643     if (field.substring(0,6) == 'reset_') {
644         var clientname = field.substring(6);
645         var fieldname = query_client_server[clientname];
646         if (!fieldname) 
647             fieldname = clientname;     
648         delete querys_server[fieldname];
649         onFormSubmitEventHandler();
650         showhide("recordview");
651     }
652     else 
653         limitQueryServer(field, value);
654         //alert("limitOrResetQuerry: query after: " + document.search.query.value);
655 }
656
657
658
659
660 // limit by target functions
661 function limitTarget (id, name)
662 {
663     curFilter = 'pz:id=' + id;
664     resetPage();
665     loadSelect();
666     triggerSearch();
667     return false;
668 }
669
670 function delimitTarget ()
671 {
672     curFilter = 'ALL'; 
673     resetPage();
674     loadSelect();
675     triggerSearch();
676     return false;
677 }
678
679 function limitOrResetTarget(id, name) {
680         if (id == 'reset_xt') {
681                 delimitTarget();
682         }
683         else {
684                 limitTarget(id,name);
685         }
686 }
687
688 function drawPager (pagerDiv)
689 {
690     //client indexes pages from 1 but pz2 from 0
691     var onsides = 2;
692     var pages = Math.ceil(totalRec / recPerPage);
693     
694     var firstClkbl = ( curPage - onsides > 0 ) 
695         ? curPage - onsides
696         : 1;
697
698     var lastClkbl = firstClkbl + 2*onsides < pages
699         ? firstClkbl + 2*onsides
700         : pages;
701
702     var prev = '<span id="prev">Prev</span><b> | </b>';
703     if (curPage > 1)
704         var prev = '<a href="#" id="prev" onclick="pagerPrev();">'
705         +'Prev</a><b> | </b>';
706
707     var middle = '';
708     for(var i = firstClkbl; i <= lastClkbl; i++) {
709         var numLabel = i;
710         if(i == curPage)
711             numLabel = '<b>' + i + '</b>';
712
713         middle += '<a href="#" onclick="showPage(' + i + ')"> '
714             + numLabel + ' </a>';
715     }
716     
717     var next = '<b> | </b><span id="next">Next</span>';
718     if (pages - curPage > 0)
719     var next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
720         +'Next</a>';
721
722     predots = '';
723     if (firstClkbl > 1)
724         predots = '...';
725
726     postdots = '';
727     if (lastClkbl < pages)
728         postdots = '...';
729
730     pagerDiv.innerHTML += '<div class="pager">' 
731         + prev + predots + middle + postdots + next + '</div>';
732 }
733
734 function showPage (pageNum)
735 {
736     curPage = pageNum;
737     my_paz.showPage( curPage - 1 );
738 }
739
740 // simple paging functions
741
742 function pagerNext() {
743     if ( totalRec - recPerPage*curPage > 0) {
744         my_paz.showNext();
745         curPage++;
746     }
747 }
748
749 function pagerPrev() {
750     if ( my_paz.showPrev() != false )
751         curPage--;
752 }
753
754 // swithing view between targets and records
755
756 function switchView(view) {
757     
758     var targets = document.getElementById('targetview');
759     var records = document.getElementById('recordview');
760     
761     switch(view) {
762         case 'targetview':
763             targets.style.display = "block";            
764             records.style.display = "none";
765             break;
766         case 'recordview':
767             targets.style.display = "none";            
768             records.style.display = "block";
769             break;
770         default:
771             alert('Unknown view.');
772     }
773 }
774
775 // detailed record drawing
776 function showDetails (prefixRecId) {
777     var recId = prefixRecId.replace('rec_', '');
778     var oldRecId = curDetRecId;
779     curDetRecId = recId;
780     
781     // if the same clicked, just show it again
782     if (recId == oldRecId) {
783         showhide('detailview');
784         return;
785     }
786     // request the record
787     document.getElementById("loading").style.display = 'inline';
788     my_paz.record(recId);
789 }
790
791 function replaceHtml(el, html) {
792   var oldEl = typeof el === "string" ? document.getElementById(el) : el;
793   /*@cc_on // Pure innerHTML is slightly faster in IE
794     oldEl.innerHTML = html;
795     return oldEl;
796     @*/
797   var newEl = oldEl.cloneNode(false);
798   newEl.innerHTML = html;
799   oldEl.parentNode.replaceChild(newEl, oldEl);
800   /* Since we just removed the old element from the DOM, return a reference
801      to the new element, which can be used to restore variable references. */
802   return newEl;
803 };
804
805 function renderDetails(data, marker)
806 {
807     var details = '<div class="details" id="det_'+data.recid+'"><table>';
808     if (marker) details += '<tr><td>'+ marker + '</td></tr>';
809     if (data["md-title"] != undefined) {
810         details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
811         if (data["md-title-remainder"] != undefined) {
812               details += ' : <span>' + data["md-title-remainder"] + ' </span>';
813         }
814         if (data["md-author"] != undefined) {
815               details += ' <span><i>'+ data["md-auhtor"] +'</i></span>';
816         }
817         details += '</td></tr>';
818     }
819     if (data["md-date"] != undefined)
820         details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
821     if (data["md-author"] != undefined)
822         details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
823     if (data["md-electronic-url"] != undefined)
824         details += '<tr><td><b>URL</b></td><td><b>:</b> <a href="' + data["md-electronic-url"] + '" target="_blank">' + data["md-electronic-url"] + '</a>' + '</td></tr>';
825     if (data["location"][0]["md-subject"] != undefined)
826         details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
827     if (data["location"][0]["@name"] != undefined)
828         details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
829     details += '</table></div>';
830     return details;
831 }
832
833 function renderLine(title, value, tag) {
834     if (tag == undefined)
835         tag = 'big';
836     if (value != undefined)
837         return '<li><h3>' + title + '</h3><' + tag + '>' + value + '</' + tag + '></li>';
838     return '';
839 }
840
841 function renderLines(title, values, name, tag) {
842     if (tag == undefined)
843         tag = 'big';
844     var result = "";
845     result = '<li><h3>' + title + '</h3><' + tag + '>';
846     if (values != undefined && values.length)
847         for (var idx = 0 ; idx < values.length ; idx++)
848             result += values[idx][name];
849     result += '</' + tag + '></li>';
850     return result;
851 }
852
853 function renderLinesURL(title, values, name, url) {
854     var result = "";
855     result = '<li><h3>' + title + '</h3><span style="display: inline-block;">';
856     if (values != undefined && values.length) {
857         for (var idx = 0 ; idx < values.length ; idx++) {
858             if (values[idx][url] != undefined)
859                 result += '<a href="' + values[idx][url] + '">' + values[idx][name] + '</a><br>';
860             else
861                 result += values[idx][name] + '<br>';
862         }
863     }
864     result += '</span></li>';
865     return result;
866 }
867
868 function renderLineURL(title, URL, display) {
869     if (URL != undefined)
870         return '<li><h3>' + title + '</h3><a href="' + URL + '" target="_blank">' + display + '</a></li>';
871     return '';
872 }
873
874 function renderLineEmail(dtitle, email, display) {
875     if (email != undefined)
876         return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
877     return '';
878 }
879
880 function renderDetails_iphone(data, marker)
881 {
882         //return renderDetails(data,marker);
883
884     if (!data) 
885         return ""; 
886     var details = '<div class="details" id="det_'+data.recid+'" >'
887 /*
888     details = '<div id="header" id="det_'+data.recid+'">' 
889         + '<h1>Detailed Info</h1>' 
890         + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>' 
891         + '</div>';
892 */
893     if (marker) 
894         details += '<h4>'+ marker + '</h4>'; 
895     details += '<ul class="field" >';
896     if (data["md-title"] != undefined) {
897         details += '<li><h3>Title</h3>' + data["md-title"];
898         if (data["md-title-remainder"] != undefined) {
899               details += ' - ' + data["md-title-remainder"] + ' ';
900         }
901 /*
902         if (data["md-author"] != undefined) {
903               details += '<i>'+ data["md-author"] +'</i>';
904         } else if (data["md-title-responsibility"] != undefined) {
905               details += '<i>'+ data["md-title-responsibility"] +'</i>';
906         }
907 */
908         details += '</li>'
909     }
910     var coverimagetag = imageHelper.getImageTagByRecId(data.recid, "md-isbn", undefined, "M");
911     details 
912         +=renderLine('Date',    data["md-date"])
913         + renderLine('Author',  data["md-author"])
914 //      + renderLineURL('URL',  data["md-electronic-url"], data["md-electronic-url"])
915         + renderLine('Description',     data["md-description"]);
916         + renderLines('Subjects', data["location"], "md-subject");
917
918     if (coverimagetag.length>0) {
919         details += renderLine('Cover', coverimagetag);
920     }
921
922     details += renderLinesURL('Location', data["location"], "@name", "md-url_recipe");
923     details += '<li><a href="#" onclick="showhide(\'recordview\')">Back</a></li>';
924     details += '</ul></div>';
925     return details;
926 }
927
928 //EOF