+function append_text(cell, text) {
+ text_node = document.createTextNode(text);
+ cell.appendChild(text_node);
+}
+
+
+function assign_text (cell, text) {
+ clear_cell(cell);
+ append_text(cell, text);
+}
+
+function set_sort_opt(n, opt, str)
+{
+ var txt = document.createTextNode(str);
+ if (opt == cur_sort)
+ n.appendChild(txt);
+ else
+ {
+ var a = document.createElement('a');
+ a.appendChild(txt);
+ a.setAttribute('href', "");
+ a.setAttribute('onclick', "set_sort('" + opt + "'); return false");
+ n.appendChild(a);
+ }
+}
+
+function set_sort(sort)
+{
+ if (sort && sort != cur_sort)
+ {
+ cur_sort = sort;
+ if (searched)
+ check_search();
+ }
+
+ var t = document.getElementById("sortselect");
+ clear_cell(t);
+ t.appendChild(document.createTextNode("Sort results by: "));
+ set_sort_opt(t, 'relevance', 'Relevance');
+ t.appendChild(document.createTextNode(" or "));
+ set_sort_opt(t, 'title:1', 'Title');
+}
+
+function displayname(name)
+{
+ if (name == 'md-author')
+ return 'Author';
+ else if (name == 'md-subject')
+ return 'Subject';
+ else if (name == 'md-date')
+ return 'Date';
+ else if (name == 'md-isbn')
+ return 'ISBN';
+ else if (name == 'md-publisher')
+ return 'Publisher';
+ else if (name == 'md-url')
+ return 'URL';
+ else if (name == 'md-title')
+ return '@';
+ else if (name == 'md-id')
+ return 'Local ID';
+ else if (name == 'recid')
+ return '@';
+ else if (name == 'location')
+ return '@';
+ else
+ return name;
+}
+
+function hyperlink_field(name)
+{
+ if (name == 'md-author')
+ return 'au';
+ else if (name == 'md-subject')
+ return 'su';
+ else if (name == 'md-url')
+ return 'URL';
+ else
+ return 0;
+}
+
+function paint_details_tr(name, dn)
+{
+ //emit a table row
+ var dname = displayname(name);
+ var ln = create_element('b', dname);
+ var tln = document.createElement('td');
+ tln.setAttribute('width', 90);
+ tln.setAttribute('valign', 'top');
+ tln.appendChild(ln);
+ var tr = document.createElement('tr');
+ tr.appendChild(tln);
+ tr.appendChild(dn);
+ return tr;
+}
+
+function paint_data_elements(target, node)
+{
+ var nodes = node.childNodes;
+ var dn = 0;
+ var lastname = '';
+ var i;
+ for (i = 0; i < nodes.length; i++)
+ {
+ if (nodes[i].nodeType != 1)
+ continue;
+ var name = nodes[i].nodeName;
+ if (name == 'recid' || name == 'md-title')
+ continue;
+ if (name != lastname && lastname != 'location')
+ {
+ if (dn)
+ {
+ var tr = paint_details_tr(lastname, dn);
+ target.appendChild(tr);
+ }
+ dn = document.createElement('td');
+ lastname = name;
+ }
+ if (name == 'location')
+ {
+ target.appendChild(paint_details_tr('Location', paint_subrecord(nodes[i])));
+ continue;
+ }
+ if (!nodes[i].childNodes[0])
+ continue;
+ var value = nodes[i].childNodes[0].nodeValue;
+ if (dn.childNodes[0])
+ dn.appendChild(document.createTextNode('; '));
+ var hyl = hyperlink_field(name);
+ var nv;
+ if (hyl)
+ {
+ nv = create_element('a', value);
+ if (hyl == 'URL')
+ {
+ nv.setAttribute('href', value);
+ nv.setAttribute('target', '_blank');
+ }
+ else
+ {
+ nv.setAttribute('href', '#');
+ nv.setAttribute('term', value);
+ nv.setAttribute('searchfield', hyl);
+ nv.onclick = function() { hyperlink_search(this); return false; };
+ }
+ }
+ else
+ nv = document.createTextNode(value);
+ dn.appendChild(nv);
+ }
+ if (dn && lastname != 'location')
+ {
+ var tr = paint_details_tr(lastname, dn);
+ target.appendChild(tr);
+ }
+}
+
+function paint_subrecord(node)
+{
+ var table = document.createElement('table');
+ var zurl = node.getAttribute('id');
+ var tr = paint_details_tr('Source', document.createTextNode(zurl));
+ table.appendChild(tr);
+ paint_data_elements(table, node);
+ return table;
+}
+
+function paint_details(body, xml)
+{
+ clear_cell(body);
+ var table = document.createElement('table');
+ table.setAttribute('cellpadding', 2);
+ paint_data_elements(table, xml.childNodes[0]);
+ body.appendChild(table);
+ body.style.display = 'inline';
+}
+
+function show_details()
+{
+ if (xfetchDetails.readyState != 4)
+ return;
+ var xml = xfetchDetails.responseXML;
+ var error = xml.getElementsByTagName("error");
+ if (error[0])
+ {
+ var msg = error[0].childNodes[0].nodeValue;
+ alert(msg);
+ location = "?";
+ return;
+ }
+
+ var idn = xml.getElementsByTagName('recid');
+ if (!idn[0])
+ return;
+ var id = idn[0].childNodes[0].nodeValue;
+ cur_id = id;
+ cur_rec = xml;
+
+ var nodes = document.getElementsByName('listrecord');
+ var i;
+ for (i = 0; i < nodes.length; i++)
+ {
+ var dets = nodes[i].getElementsByTagName('div');
+ if (dets[0])
+ dets[0].style.display = 'none';
+ }
+
+ var body = document.getElementById('rec_' + id);
+ if (!body)
+ return;
+ paint_details(body, xml);
+}
+
+function hyperlink_search(obj)
+{
+ var field = obj.getAttribute('searchfield');
+ var term = obj.getAttribute('term');
+ var queryfield = document.getElementById('query');
+ queryfield.value = field + '=' + term;
+ start_search();
+}
+
+function fetch_details(id)
+{
+ cur_id = -1;
+ if (id == cur_id)
+ {
+ cur_id = -1;
+ return;
+ }
+ if (!session)
+ return;
+ var url = "search.pz2?session=" + session +
+ "&command=record" +
+ "&id=" + id;
+ SendXmlHttpObject(xfetchDetails = GetXmlHttpObject(), url, show_details);
+}
+