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