/*
 *  File:  quickSearch.js
 *
 *  Write a BGSU QuickSearch form into the current document
 *
 *  Usage:
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/quickSearch.js">
 *    </script>
 *
 *  Insert the <script> element into the <head> or <body> of your document.
 *
 *  Constructors: QuickSearch()              create a QuickSearch object
 *                QuickSearch( url, name )   employ a special search option
 *
 *  Methods:      setColumns( cols )         set the width of the text field
 *                setLabel( label )          set the label for the search button
 *                useImage( img )            use an image as the search button
 *                useImages( img1, img2 )    use dynamic images (rollovers)
 *                setBorder( border )        set border of image (in pixels)
 *                setAlign( align )          set alignment of image
 *                setTarget( target )        set the target of search results page
 *                                             (default: "_self")
 *                write()                    write the search tool into the doc
 *
 *  Update History:
 *
 *  Dong Chen (7/13/2009)
 *	Change search engine, and update parameters
 *  
 *  Dong Chen (2/24/2005)
 *	Changed form name from "search" to "search_js" in order to avoid conflict with global search footer
 *  
 *  Example 1:  quickSearchTest1.html
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/quickSearch.js">
 *    </script>
 *    <script type="text/javascript">
 *      var quicksearch = new QuickSearch();
 *      quicksearch.write();
 *    </script>
 *
 *  Example 2:  quickSearchTest2.html
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/quickSearch.js">
 *    </script>
 *    <script type="text/javascript">
 *      var quicksearch = new QuickSearch();
 *      quicksearch.setColumns( 12 );
 *      quicksearch.setLabel( "Go!" );
 *      quicksearch.write();
 *    </script>
 *
 *  Example 3:  quickSearchTest3.html
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/quickSearch.js">
 *    </script>
 *    <script type="text/javascript">
 *      var url = "http://www.bgsu.edu/offices/execvp/";
 *      var name = "Exec VP";
 *      var quicksearch = new QuickSearch( url, name );
 *      quicksearch.setColumns( 10 );
 *      quicksearch.write();
 *    </script>
 *
 *  Example 4:  quickSearchTest4.html
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/quickSearch.js">
 *    </script>
 *    <script type="text/javascript">
 *      var button = new Image();
 *      button.src = "http://www.bgsu.edu/scripts/quickSearchGo.gif";
 *      var quicksearch = new QuickSearch();
 *      quicksearch.setColumns( 10 );
 *      quicksearch.useImage( button );
 *      quicksearch.write();
 *    </script>
 *
 *  Example 5:  quickSearchTest5.html
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/quickSearch.js">
 *    </script>
 *    <script type="text/javascript">
 *      var buttonUp = new Image();
 *      buttonUp.src = "http://www.bgsu.edu/scripts/quickSearchUp.gif";
 *      var buttonDown = new Image();
 *      buttonDown.src = "http://www.bgsu.edu/scripts/quickSearchDown.gif";
 *      var quicksearch = new QuickSearch();
 *      quicksearch.setColumns( 12 );
 *      quicksearch.useImages( buttonUp, buttonDown );
 *      quicksearch.setBorder( 0 );
 *      quicksearch.setAlign( "top" );
 *      quicksearch.setTarget( "_blank" );
 *      quicksearch.write();
 *    </script>
 *
 */

// The QuickSearch constructor takes zero or two arguments:
function QuickSearch( url, name ) {
  this.cols = null;
  this.label = "Search";
  if ( arguments.length == 2 ) {
    this.searchURL = url;
    this.searchName = name;
  } else {
    this.searchURL = null;
    this.searchName = null;
  }
  this.buttonUp = null;
  this.buttonDown = null;
  this.border = null;
  this.align = null;
  this.target = "_self";
}

// Method to set the width of the text field:
function QS_setColumns( cols ) {
  // Try to parse the argument as an integer:
  cols = parseInt( cols );
  // Test for a non-negative integer:
  if ( !isNaN( cols ) && cols >= 0 ) this.cols = cols;  
}
QuickSearch.prototype.setColumns = QS_setColumns;

// Method to set the label for the search button:
function QS_setLabel( label ) {
  if ( label != null ) this.label = label;
}
QuickSearch.prototype.setLabel = QS_setLabel;

// Method to specify image to use as the search button:
function QS_useImage( image ) {
  this.useImages( image );
}
QuickSearch.prototype.useImage = QS_useImage;

// Method to specify rollover images to use as the search button:
function QS_useImages( image1, image2 ) {
  if ( image1 != null ) {
    this.buttonUp = new Image();
    this.buttonUp.src = image1.src;
    if ( image2 != null ) {
      this.buttonDown = new Image();
      this.buttonDown.src = image2.src;
    }
  }
}
QuickSearch.prototype.useImages = QS_useImages;

// Method to set the border of the image button (in pixels):
function QS_setBorder( border ) {
  // Try to parse the argument as an integer:
  border = parseInt( border );
  // Test for a non-negative integer:
  if ( !isNaN( border ) && border >= 0 ) this.border = border;  
}
QuickSearch.prototype.setBorder = QS_setBorder;

// Method to set the alignment of the image button:
function QS_setAlign( align ) {
  if ( align != null ) {
    if ( align == "bottom" || align == "middle" || align == "top" ) {
      this.align = align;
    }
  }
}
QuickSearch.prototype.setAlign = QS_setAlign;

// Method to set the target of the search results:
function QS_setTarget( target ) {
  if ( target != null ) {
    this.target = target;
  }
}
QuickSearch.prototype.setTarget = QS_setTarget;

// Method to write the search tool into the current document:
function QS_write() {

  // Local variables:
  var cols = this.cols;
  var label = this.label;
  var searchName = this.searchName;
  var searchURL = this.searchURL;
  var buttonUp = this.buttonUp;
  var buttonDown = this.buttonDown;
  var border = this.border;
  var align = this.align;

  // Write the form into the document:
  document.writeln( '' );
  document.writeln( '<form name="search_js" method="get" target="' + this.target + '"' );
  document.writeln( '      onsubmit="submitForm()"' );
  document.writeln( '      action="http://search1.bgsu.edu/search">' );
  // This anonymous text field must be the *first* field in the form:
  document.writeln( '  <input type="text"' );
  if ( cols != null ) {
    document.writeln( '         size="' + cols + '"' );
  }
  document.writeln( '         maxlength="2033">' );
  // Write the submit button:
  if ( buttonUp != null ) {
    document.writeln( '  <a href="javascript:submitForm()"' );
    document.writeln( '     onmouseover="return doMouseover()"' );
    document.writeln( '     onmouseout ="doMouseout()">' );
    document.writeln( '     <img src="' + buttonUp.src + '"' );
    document.writeln( '          width="' + buttonUp.width + '"' );
    document.writeln( '          height="' + buttonUp.height + '"' );
    if ( align != null ) {
      document.writeln( '          align="' + align + '"' );
    }
    if ( border != null ) {
      document.writeln( '          border="' + border + '"' );
    }
    document.writeln( '          alt="Search" name="submitButton"><\/a>' );
  } else {
    document.writeln( '  <input type="button" value="' + label + '"' );
    document.writeln( '         onclick="submitForm()"' );
    document.writeln( '         onmouseover="return doMouseover()"' );
    document.writeln( '         onmouseout ="doMouseout()">' );
  }
  // Write a select object:
  document.writeln( '  <select name="rq" size="1">' );
  // The special search option must be the *first* option listed, if at all:
  if ( searchName != null ) {
    document.writeln( '    <option value="0">' + searchName + '<\/option>' );
  }
  document.writeln( '    <option value="0">BGSU Web<\/option>' );
  document.writeln( '    <option value="2">The Internet<\/option>' );
  document.writeln( '  <\/select>' );

  // Write numerous hidden fields:
  // New search parameters
  document.writeln( ' <input type="hidden" name="q" value="">' );
  document.writeln( ' <input type="hidden" name="site" value="default_collection">' );
  document.writeln( ' <input type="hidden" name="client" value="default_frontend">' );
  document.writeln( ' <input type="hidden" name="output" value="xml_no_dtd">' );
  document.writeln( ' <input type="hidden" name="oe" value="UTF-8">' );
  document.writeln( ' <input type="hidden" name="ie" value="UTF-8">' );
  document.writeln( ' <input type="hidden" name="proxystylesheet" value="default_frontend">' );

  document.writeln( '<\/form>' );

  // Store data so that handlers have access (a kludge):
  document.search_js.searchURL = searchURL;
  document.search_js.searchName = searchName;
  document.search_js.buttonUp = buttonUp;
  document.search_js.buttonDown = buttonDown;
 
}
QuickSearch.prototype.write = QS_write;

// Check the form before submitting:
function submitForm() {
  var form = document.search_js;
  // Store the search string in a hidden field called 'qt':
  form.q.value = form.elements[0].value;
  // Process special search option, if it exists:
  if ( form.searchName != null ) {
    if ( form.rq.selectedIndex == 0 && form.searchURL != null ) {
      form.q.value += " site:" + form.searchURL;
    } else if ( form.rq.selectedIndex == 2 ) {
      window.location = "http://www.google.com/search?hl=en&ie=ISO-8859-1&q=" + form.q.value;

      return;
    }
  } else {
    window.location = "http://www.google.com/search?hl=en&ie=ISO-8859-1&q=" + form.q.value;

    return;
  }
  form.submit();
}

function doMouseover() {
  window.status = "Press button to search the web";
  var form = document.search_js;
  if ( form.buttonDown != null ) {
    document.submitButton.src = form.buttonDown.src;
  }
  return true;
}

function doMouseout() {
  window.status = "";
  var form = document.search_js;

  if ( form.buttonDown != null ) {
    document.submitButton.src = form.buttonUp.src;
  }
}
