// An inefficient, but effective bubble sort var xmlpath = 'http://elwood.mcom.com/bobclary/arun/'; var enabled = true; var sortableProps = new Array('Author', 'Title', 'ISBN'); var i; var xmlhttpBook; // We uppercase the tagName as a workaround for a bug // that loses the original case of the tag. //Create an XMLHttpRequest Object on both browsers if(document.all) xmlhttpBook = new ActiveXObject('MSXML2.XMLHTTP'); else xmlhttpBook = new XMLHttpRequest(); searchBooks(); function sort(key) { var bookNodeList = document.getElementsByTagName('Book'); // We need to create a 'non-live' array to operate on. Since // we'll be moving things around in this array, we can't use // the read-only, live one returned by getElementsByTagName. var bookArray = new Array(); for (i = 0; i < bookNodeList.length; i++) bookArray[i] = bookNodeList[i]; collectInfo(bookArray, sortableProps); var i, j; var count = bookArray.length; var parent, child; for (i = count-1; i>= 0; i--) { for (j = 1; j <= i; j++) { if (bookArray[j-1][key] > bookArray[j][key]) { // Move the item both in the local array and // in the tree child = bookArray[j]; parent = child.parentNode; bookArray[j] = bookArray[j-1]; bookArray[j-1] = child; parent.removeChild(child); parent.insertBefore(child, bookArray[j]); } } } } // Set user properties on the nodes in the collection // based on information found in its children. For example, // make a property 'Author' based on the content of the // 'Author' element found in the childNode list of the node. // This makes later sorting more efficient function collectInfo(nodes, propNames) { var i, j, k; var ncount = nodes.length; var pcount = propNames.length; for (i = 0; i < ncount; i++) { var node = nodes[i]; var childNodes = node.childNodes; var ccount = childNodes.length; for (j = 0; j < ccount; j++) { var child = childNodes[j]; if (document.all) { var tagName = child.tagName; for (k = 0; k < pcount; k++) { var prop = propNames[k]; if (prop == tagName) { node[prop] = child.firstChild.data; } } } else if (child.nodeType == Node.ELEMENT_NODE) { var tagName = child.tagName; for (k = 0; k < pcount; k++) { var prop = propNames[k]; if (prop == tagName) { node[prop] = child.firstChild.data; } } } } } } function toggleStyleSheet() { document.styleSheets[2].disabled = enabled; enabled = !enabled; } // XXX This is a workaround for a bug where // changing the disabled state of a stylesheet can't // be done in an event handler. For now, we do it // in a zero-delay timeout. function initiateToggle() { setTimeout(toggleStyleSheet, 0); } function searchBooks() { var selection = document.getElementById('toggleBooks').value; var path = xmlpath + selection; var searchBookSet = document.getElementsByTagName('BookSet')[0]; // Direct string inequality -- optimise later! if (document.all) { xmlhttpBook.Open('GET', path, false); xmlhttpBook.Send(null); } else { xmlhttpBook.open('GET', path, false); xmlhttpBook.send(null); } var xmlResponse = xmlhttpBook.responseXML; if (!xmlResponse.documentElement) alert('Sorry, the request could not get through to the server'); else { var resultBookSet = xmlResponse.getElementsByTagName('BookSet')[0]; var importedBookSet = document.importNode(resultBookSet, true); searchBookSet.parentNode.replaceChild(importedBookSet, searchBookSet); } }