function addEvent( obj, type, fn )
{
  if ( obj.addEventListener ) {
    obj.addEventListener( type, fn, false );
  }
  else if ( obj.attachEvent ) {
    obj["e" + type + fn] = fn;
    obj[type + fn] = function() { obj["e" + type + fn]( window.event ); };
    obj.attachEvent( "on" + type, obj[type + fn] );
  }
}

function prepopulate_input_field( field_id, initial_value, class_name )
{
  initial_value = initial_value || 'Search'; // set a default initial val
  class_name = class_name || 'prefilled'; // set default class for filled field
  var the_field = document.getElementById( field_id );
  // die if field not found or if field already filled
  if ( !the_field || the_field.value !== '' ) return false;
  the_field.className = class_name; // for giving hint text a unique style
  the_field.defaultValue = initial_value;
  the_field.form.reset();
  return true;
}

function clear_input_field( field_id, initial_value )
{
  initial_value = initial_value || 'Search'; // set a default initial val
  var the_field = document.getElementById( field_id );
  // die if field not found
  if ( !the_field ) return false;
  the_field.onfocus = function( e )
  {
    if ( this.value === initial_value ) {
      this.value = '';
      this.className = ''; // clear hint text style
    }
  };
  return true;
}

addEvent( window, 'load', function()
{
  prepopulate_input_field('textboxTerm','search our catalog');
} );
addEvent( window, 'load', function( e )
{
  clear_input_field('textboxTerm','search our catalog');
} );


/** BELOW THIS LINE: search-related script copied from Polaris catalog **/

String.prototype.trim = function() { return this.replace(/^\s*|\s*$/g,''); }

function ProcessInput(e)
{
    if (window.event) // IE
        keyNum = e.keyCode;
    else if (e.which) // Netscape/Firefox/Opera
        keyNum = e.which;

  if (keyNum == 13)
  {
      e.returnValue = false;
      e.cancelBubble = true;
      DoKeywordSearch('KW');
      return false;
  }

  return true;
}

function DoBrowseSearch(strBy)
{
    if (document.getElementById('textboxTerm').value.trim() != '')
    {
        var strURL = 'http://catalog.wpl.org/polaris/search/browse.aspx?ctx=1.1033.0.0.3';
        window.location = strURL + "&type=Browse&Page=0&by=" + strBy + "&term=" + escape(document.getElementById('textboxTerm').value);
    }
    else
        document.getElementById('textboxTerm').focus();
}

function DoKeywordSearch(strBy)
{
    if (document.getElementById('textboxTerm').value.trim() != '')
    {
        var strURL = 'http://catalog.wpl.org/polaris/search/searchresults.aspx?ctx=1.1033.0.0.3';
        window.location = strURL + "&type=Keyword&limit=TOM=*&sort=RELEVENCE&Page=0&by=" + strBy + "&term=" + escape(document.getElementById('textboxTerm').value);
    }
    else
        document.getElementById('textboxTerm').focus();
}
