This JavaScript Code will cycle through all elements on a page and find the next one that should recieve the focus. This worked well in IE6 but the results in FireFox where mixed. I don't have time right now to clean it up but I wanted to post it as this is one of those gems that you know you will want in the future.
Original Source: http://www.codecomments.com/message290822.html
Original Author: Daniel Kirsch
// returns the next focusable object for a given onefunction getNextFocusElement(elm,restart,dir,_looped) {var allElm = document.getElementsByTagName('*');var found = false; //(start == true);var allowedElements = 'input,textarea,a,button';if (!dir) dir = 1;var start = dir > 0 ? 0 : allElm.length-1;var end = dir > 0 ? allElm.length : -1;jslog.info('start: ' + start + ' end: ' + end);for (var i=start; i!=end; i+=dir) {if (!found) {if (allElm[i] == elm) {if (_looped) return null;found = true;jslog.info('i: ' + i );continue;}}if (found || (restart && _looped)) {if (allElm[i].focus && allElm[i] != elm) {if (isInList(allElm[i].nodeName.toLowerCase(),allowedElements)&& isVisible(allElm[i]))jslog.info('i: ' + i);return allElm[i];}}}jslog.info('starting next loop...');return (_looped) ? null : getNextFocusElement(elm,true,dir,true);}function isVisible(elm) {if (elm.style.visibility == 'hidden' || elm.style.display == 'none')return falseelsereturn elm.parentNode && elm.parentNode.style ?isVisible(elm.parentNode) : true;}function isInList(aItem,aList,aSep,aCaseSensitive) {if (typeof aItem == 'undefined' || typeof aList == 'undefined')return false;// make sure, the element is a string.aItem = String(aItem);if (aCaseSensitive !== true && typeof aItem == 'string') {if (typeof aList == 'string')aList = aList.toLowerCase();aItem = aItem.toLowerCase();}if (!aSep) aSep = ',';var lString = aSep+aList+aSep;return (lString.indexOf(aSep+aItem+aSep) > -1);}