function firstInput () {
  if($$('div.hasError').length > 0) {
    return $$('div.hasError input').detect(function(input) {
      return (input.type != 'hidden');
    });
  } else if($$('div.formEntry input').length > 0) {
    // Doesn't currently account for selects coming first
    return $$('div.formEntry input').first();
  } else {
    return $$('input').findAll(function (i) { return i.type == "text"; }).first();
  }
}

// Called again from error_hide.js
function selectField () {
  var input = firstInput();
  if((input && !$('errorBox')) || (input && !$('errorBox').visible())) {
    input.focus();
  }
}

Event.observe(window, 'load', selectField);
