/***********************************************************************

 $Id: search.js,v 1.1.1.1 2003/10/06 10:57:31 www Exp $

Copyright Quadstone (c) 2001, 2002

************************************************************************

Simple validator for external search form. Checks the following:

* someone's put something in the box.
* are the words "and", "or" and "not" somewhere in there?
* - If so, it's a boolean query.
* - If not, are there "+" and "-"'s? 
* - - If so, make an appopriate boolean query.
* If none of the above, it'll be a simple search

***********************************************************************/

/* NB: Need to check the string stuff works on Opera */
    
function validate(formname) {
  var wd,lc,i;
  var isbool = 0;
  var ispm = 0;
  var words = new Array();
  var pluswords = new Array();
  var minuswords = new Array();
  var optwords = new Array();
  var form;
  if ( formname == "dave1" ) {
    form = document.forms[0];
  }
  else { form = document.forms[1]; } 
  var text = form.rawwords.value;
  if (text.length==0 || !text.match(/\S/)) { // Something worthwhile?
    alert("You should type something in the search box first!");
    form.rawwords.focus();
    return false;
  }
  // Trim leading and trailing space
  text = text.replace(/^\s+/, "");
  text = text.replace(/\s+$/, "");
  // Split into words
  words = text.split(/\s+/);

  // Is there "and", "or" or "not" in there?
  for (i in words) {
    lc = words[i].toUpperCase();
    if (lc=='AND' || lc=='OR' || lc=='NOT') {
      words[i] = lc;
      isbool=1;
      break;
    }
  }
  // If not boolean, are there '+' and '-'s in there?
  for (i in words) {
    wd = words[i];
    var c = wd.substr(0,1);
    if (c=='+' || c=='-') {
      ispm = 1;
      wd = wd.substr(1).replace(/\s+/, "");
      if (c=='+') {
        pluswords[pluswords.length] = wd;
      }
      else {
        minuswords[minuswords.length] = wd;
      }
    }
    else { // Treat it as if it were optional
      optwords[optwords.length] = wd;
    }
  }
  if (ispm && isbool) {
    alert("Please don't use a boolean (AND, OR, NOT) search at the same time as an inclusion/exclusion (+/-) search!");
    return false;
  }
  // Store away the "raw" words so they can be returned after the search
  form.rawwords.value = words.join(" ");
  if (ispm) {
    var query = "";
    var s = "";
    for (i in pluswords) {
      if (query) query += " AND ";
      query += pluswords[i];
    }
    if (optwords.length) {
      for (i in pluswords) {
        if (s) s += " OR ";
        s += pluswords[i];
      }
      for (i in optwords) {
        if (s) s += " OR ";
        s += optwords[i];
      }
      if (query) query += " AND ";
      query += "(" + s + ")";
    }
    if (minuswords.length) {
      s = "";
      for (i in minuswords) {
        if (s) s += " OR ";
        s += minuswords[i];
      }
      if (query) query += " ";
      query += "NOT (" + s + ")";
    }
    form.words.value = query;
  }
  else {
    form.words.value = words.join(" ");
  }
  // Set correct search method
  form.method.value = (isbool || ispm) ? "boolean" : "and";

  // That's it - allow form to be submitted
  return true;
}

/* searchhelp() just launches a pop-up window containing some hints
   for searching and things. */

function searchhelp() {
  window.open("/info/search/searchhelp.html", "",
    "directories=no,hotkeys=no,menubar=no,location=no,toolbar=no,width=500,height=500,scrollbars=yes");
}

