function sortTables(col) { sortTable("subfolders", col); sortTable("files", col); fixRows("details-table"); } function sortTable(id, col) { var table = document.getElementById(id); if (table.sortAsc == null || table.lastSorted != col) { table.sortAsc = true; table.lastSorted = col; } else { table.sortAsc = !table.sortAsc; } var tmp, i, j, sval, sx, test, cmp; for (i = 0; i < table.rows.length - 1; i++) { sx = i; sval = getInnerText(table.rows[i].cells[col]); for (j = i + 1; j < table.rows.length; j++) { test = getInnerText(table.rows[j].cells[col]); cmp = compareValues(sval, test, col); if (table.sortAsc) cmp = -cmp; if (cmp > 0) { sx = j; sval = test; } } if (sx > i) { tmp = table.removeChild(table.rows[sx]); table.insertBefore(tmp, table.rows[i]); } } showArrows(col, table.sortAsc); } function fixRows(table) { table = document.getElementById(table); var i, row; for (i = 1; i < table.rows.length; i++) { row = table.rows[i]; if (i % 2 != 0) row.className = "tc1"; else row.className = "tc2"; } } function showArrows(col, down) { var i, img, cells; cells = document.getElementById("details-table").rows[0].cells; for (i = 0; i < cells.length; i++) { if (cells[i].lastChild.tagName == "IMG") { cells[i].removeChild(cells[i].lastChild); } } img = document.createElement("IMG"); img.setAttribute("src", (down) ? "http://www.leavenworthcounty.org/wumpus/images/sort-down.gif" : "http://www.leavenworthcounty.org/wumpus/images/sort-up.gif"); cells[col].appendChild(img); } function getInnerText(el) { var i, s; s = ""; for (i = 0; i < el.childNodes.length; i++) { if (el.childNodes[i].nodeType == 3) s += el.childNodes[i].nodeValue; else if (el.childNodes[i].nodeType == 1 && el.childNodes[i].tagName == "BR") s += " "; else s += getInnerText(el.childNodes[i]); } return s; } function compareValues(v1, v2, col) { switch(col) { case 1: case 3: v1 = v1.toLowerCase(); v2 = v2.toLowerCase(); break; case 2: v1 = toSize(v1); v2 = toSize(v2); break; case 4: v1 = toDate(v1); v2 = toDate(v2); break; default: break; } if (v1 == v2) return 0; if (v1 > v2) return 1; return -1; } function toSize(str) { var lr = str.split(" "); if (lr[1] == "bytes") { return lr[0] * 1; } else if (lr[1] == "KB") { return lr[0] * 1024; } else if (lr[1] == "MB") { return lr[0] * 1048576; } else if (lr[1] == "GB") { return lr[0] * 1073741824; } return lr[0] * 1; } function toDate(str) { var parts = str.split(" "); var date = parts[0].split("/"); var time = parts[1].split(":"); if (time[3] == "PM") time[0] += 12; return Date.UTC(date[2],parseInt(date[1])-1,date[0],time[0],time[1],time[2]); }