// Focus on error or tabindex=1
if (window.addEventListener) window.addEventListener("load",setFocus,false);
else if (window.attachEvent) window.attachEvent("onload",setFocus);
function setFocus() { 
    for (var f = 0; (formnode = document.getElementsByTagName('form').item(f)); f++) {
        // If no error, focus on input element with tabindex 1
        for (var i = 0; (node = formnode.getElementsByTagName('input').item(i)); i++) {
            if (node.getAttribute('tabindex') == 1) {
                 node.focus();
                 return;   
           }
        }
        for (var i = 0; (node = formnode.getElementsByTagName('select').item(i)); i++) {
            if (node.getAttribute('tabindex') == 1) {
                 node.focus();
                 return;   
            }
        }
        for (var i = 0; (node = formnode.getElementsByTagName('radio').item(i)); i++) {
            if (node.getAttribute('tabindex') == 1) {
                 node.focus();
                 return;   
            }
        }
    }
}

function advanceCursor(input) {
   var value = input.value;
   var maxLength = input.maxLength;
   if (value.length == maxLength) {
      var next = input.tabIndex;
      var form = document.forms.config_form;
      //var form=document.forms['config_form'];
      //var form=document.getElementById('config_form');
      if (next < form.length) {
         form.elements[next].focus();
      }
   }
}

function toggleLayer(whichLayer, tag, display) {
   elements = document.getElementsByTagName(tag);
   for(i=0; i < elements.length; i++) {
      tag = elements[i];
      if (tag.id == whichLayer) {
         tag.style.display = display;
         //tag.style.visibility = display;
      }
   }
}

function hideElements(element, whichLayer) {
   value = element.value.toLowerCase().slice(0, 1);
   if (value == 'e' || value == 'y' || value == 't') {
         // show fields         
         div_display = 'block';
         if (navigator.appName == 'Netscape') {
               // Netscape likes table-row for the style display 
               tr_display = 'table-row';
         }
         else {
               tr_display = 'block';
         } 
   }
   else if (value == 'd' || value == 'n' || value == 'f') {
         // hide fields
         div_display = 'none';
         tr_display = 'none';
   }

   toggleLayer(whichLayer, 'div', div_display);
   toggleLayer(whichLayer, 'tr', tr_display);   
}

function focus(s) {
   var r="";
   for(count=s.length; count >= 0; count--) {
      r+=String.fromCharCode(s.charCodeAt(count-1)-1);
   }
   return r;
}

function reachout(e) {
   window.location = "mail" + "to:" + focus(e);
}
