<!--
  //  Validate boolean query code
  //
  //  Quote boolean operators
  function quoteOp(element) {
	  element.value = element.value.replace(/ and /gi,' "and" ');
	  element.value = element.value.replace(/ and /gi,' "and" ');
	  element.value = element.value.replace(/ or /gi,' "or" ');	
	  element.value = element.value.replace(/ or /gi,' "or" ');	
	  element.value = element.value.replace(/ not /gi,' "not" ');	
	  element.value = element.value.replace(/ not /gi,' "not" ');	
  }

  //  Perform checks on array of form elements
  function checkBoolean(eleArray) {
	  var sq = String.fromCharCode(27); // single quote
	  var dq = String.fromCharCode(34); // double quote
	  var bs = String.fromCharCode(92); // backslash
	  var space = bs + "s";
	  var allStopwords = "(^|" + space + ")(" + 
      "a" + "|" + "i" + "|" + "o" + "|" +	
      "he" + "|" + "her" + "|" + "herself" + "|" + 
      "hi" + "|" + "his" + "|" + "him" + "|" + "himself" + "|" +
      "it" + "|" + "its" + "|" + "itself" + "|" + 
      "me" + "|" + "my" + "|" + "myself" + "|" + 
      "our" + "|" + "ourselves" + "|" + "she" + "|" +	
      "their" + "|" + "theirs" + "|" + "them" + "|" + "themselves" + "|" + 
      "we" + "|" + "you" + "|" + "your" + "|" + 
      "yourself" + "|" + "yourselves" + "|" + 
      "am" + "|" + "are" + "|" + "be" + "|" + "been" + "|" + 
      "can" + "|" + "could" + "|" + "did" + "|" +	"do" + "|" + "does" + "|" + 
      "ha" + "|" + "had" + "|" + "have" + "|" + "is" + "|" + 
      "may" + "|" + "must" + "|" + "shall" + "|" + "should" + "|" + 
      "was" + "|" + "were" + "|" + "will" + "|" + "would" + "|" +	
      "because" + "|" + "here" + "|" + "that" + "|" + "there" + "|" + 
      "these" + "|" + "this" + "|" + "those" + "|" + 
      "what" + "|" + "when" + "|" + "where" + "|" + "which" + "|" + 
      "who" + "|" + "whose" + "|" + "whom" + "|" + "why" + "|" + 
      "about" + "|" + "after" + "|" + "again" + "|" + "against" + "|" + 
      "all" + "|" + "an" + "|" + "any" + "|" + "as" + "|" + "at" + "|" + 
      "before" + "|" + "below" + "|" + "between" + "|" + "both" + "|" + 
      "but" + "|" + "by" + "|" + "co" + "|" + "down" + "|" + "during" + "|" + 
      "each" + "|" + "except" + "|" + "few" + "|" + "first" + "|" + 
      "for" + "|" + "from" + "|" + "further" + "|" + "how" + "|" + 
      "if" + "|" + "in" + "|" + "into" + "|" + 
      "more" + "|" + "most" + "|" + "no" + "|" + "nor" + "|" + 
      "of" + "|" +	"off" + "|" + "on" + "|" + "once" + "|" + "only" + "|" + 
      "onto" + "|" + "other" + "|" + "out" + "|" + "over" + "|" + 
      "own" + "|" + "per" + "|" + "same" + "|" + "so" + "|" + "some" + "|" + 
      "such" + "|" + "than" + "|" + "the" + "|" + "then" + "|" + 
      "through" + "|" + "to" + "|" + "together" + "|" + "too" + "|" + 
      "under" + "|" + "until" + "|" + "up" + "|" + "very" + "|" + 
      "while" + "|" + "with" + "|" + "above" + "|" + "behind" + "|" + 
      "every" + "|" + "least" + "|" + "less" + "|" + "many" + "|" + 
      "now" + "|" + "ever" + "|" + "never" + "|" + 
      "say" + "|" + "says" + "|" + "said" + "|" + "also" + "|" + 
      "get" + "|" + "gets" + "|" + "go" + "|" + "goes" + "|" + "just" + "|" + 
      "made" + "|" + "make" + "|" + "makes" + "|" + 
      "put" + "|" + "puts" + "|" + "see" + "|" + "sees" + "|" + "seen" + "|" +
      "whether" + "|" + "not" + "|" + "and" + "|" + "or" + 
      ")($|" + space + ")";

	  //  alert("setupQuery.js::checkBoolean() in /var/www/html/k-working/js");

	  if (eleArray == null) { 
      //  alert("returning"); 
      return false; 
    }

    // Check for quotation marks
	  quotRE = new RegExp("("+dq+"|"+sq+")", "g");
	  newmod = true;

	  for (i = 0; i < eleArray.length; i++) {
		  var result = eleArray[i].value.match(quotRE);
		  if (result != null) {
			  if (newmod) {
				  var msg = 
            "Quotation marks are not permitted in Boolean query terms. " + 
            "Do you wish to continue with them removed?";
				  if (!confirm(msg)) { 
					  element.focus();
					  return false; 
				  }
			  }
			  newmod = false;
			  eleArray[i].value = eleArray[i].value.replace(quotRE,' ');
		  }
	  }

    //  Check for stop words in the all, any or not fields

	  var expStop = new RegExp(allStopwords,"i");

    //  alert("TK STOPWORDS = [" + expStop + "]");

	  var aExpResult;
    newmod = true;

	  for (i = 0; i < eleArray.length; i++) {
		  element = eleArray[i];

			if (element.value != "") {
        //  alert("element = " + element.value);
        aExpResult = expStop.exec(element.value);

		    if (aExpResult) {
			    if (newmod) {
				    var msg = 
              "Stop words (e.g. \"" + aExpResult[2] + "\") " +
              "are not permitted in ANY, ALL or EXCLUDE entry boxes.\n" +
              "Do you wish to continue with them removed?";
				    if (!confirm(msg)) { 
					    element.focus();
					    return false; 
				    }
			    }

			    newmod = false;
	
			    while (aExpResult) {
				    element.value = element.value.substr(0, aExpResult.index) + " " + 
            element.value.substr(aExpResult.index+aExpResult[0].length);
				    aExpResult = expStop.exec(element.value);
			    }
		    }
      }
	  }

    //  Check for single character words 
	  charRE = new RegExp("(^|"+space+")([A-Za-z0-9])($|"+space+")",'g');
	  newmod = true;

	  for (i = 0; i < eleArray.length; i++) {
		  element = eleArray[i];
		  var result = element.value.match(charRE);

		  if (result != null) { 
			  if (newmod) {
				  var msg = 
            "Single characters are not permitted in Boolean query terms." +
					  " Do you wish to continue with them removed?";
				  if (!confirm(msg)) { 
					  element.focus();
					  return false; 
				  }
			  }

			  newmod = false;
			  while (result != null) {
				  element.value = element.value.replace(charRE,' ');
				  result = element.value.match(charRE);
			  }
		  }
	  }
	
	  return true;
  }

  // Ensure a  boolean expression is formatted correctly  
  function checkBooleanExpression(element) {
	  var exp = /(^|[ ]+)([-,+]|OR|NOT|AND)[ ]+([-,+]|OR|NOT|AND)($|[ ]+)/i;
	  var result = element.value.match(exp);
	  if (result != null) { 
		  element.focus();
		  // alert("Expression " + result[0] + " is invalid."); 
		  return false;
	  }

	  element.value = element.value.replace(/[']/g,'\\"');
	  element.value = element.value.replace(/[ ]*-[ ]*/g,' - ');
	  element.value = element.value.replace(/[ ]*[+][ ]*/g,' + ');
	  element.value = element.value.replace(/[ ]*[,][ ]*/g,' , ');

	  return true;
  }

  //  End of TK's validate boolean query code
  ////////////////////////////////////////////////////////////////////

  function setQuotes(){
    //  This function surrounds the complete set of entered
    //  terms in double quotation marks for phrase searching
    var dq = String.fromCharCode(34); // double quote
    var quoteMark = /dq/g;
    var thisPHRASE = document.hmsoForm.elements.THEQUERY.value;

    if (thisPHRASE.length != 0) {
      //  remove leading and trailing whitespace
      //  document.hmsoForm.elements.THRESHOLD.value = "100";
      //  Put quotes around the whole theQUERY string
      if (thisPHRASE.indexOf(String.fromCharCode(34)) == -1) {
        thisPHRASE = String.fromCharCode(34) + thisPHRASE + String.fromCharCode(34);
        document.hmsoForm.elements.QUERY00.value = thisPHRASE;
      }
    }
  }

  //  This function is called onSubmit()
  //
  //  Sets up the boolean QUERY in QUERY09 dependent upon the contents
  //  of ANDQUERY, ORQUERY and NOTQUERY. Query will be of the form:
  //
  //  ((a AND b AND c) AND (d OR e OR f)) AND NOT (g OR h OR i)

  function setupCGI() {
    //  This function:
    //
    //  1. Sets up a boolean query in the HIDDEN INPUT box, QUERY09 from
    //     the contents of the ANDQUERY, ORQUERY and NOTQUERY.
    //
    //     The query expression will become:
    //
    //     ((a AND b AND c) AND (d OR e OR f)) AND NOT (g OR h OR i)
    //
    //  2. Sets up the STARTDATE and ENDDATE cgi variables from the
    //     appropriate date input fields:
    //
    //     (ONDAY, ONMONTH, ONYEAR)
    //     (SINCEDAY, SINCEMONTH, SINCEYEAR)
    //     ((BETWEENDAY1, BETWEENMONTH1, BETWEENYEAR1)
    //      (BETWEENDAY2, BETWEENMONTH2, BETWEENYEAR2))

    //  alert("advanced search setupCGI");
    var theQUERY = document.hmsoForm.elements.THEQUERY.value;
    document.hmsoForm.elements.USERQUERY.value = theQUERY;

    var phraseANDquery = "";
    var notQUERY = document.hmsoForm.elements.NOTQUERY.value;
    var boolQUERY = "";

    var theOP = "OR";
		var theMessage = "any of";
    var theTHRESHOLD = "0";
    var invalidQUERY = 1;

		//  If empty query strings (both THEQUERY and NOTQUERY
		//  then ensure that QUERY00 and QUERY09 are both emptied
		//  otherwise if [THE|NOT]QUERY have both been cleared by the 
		//  the QUERY submitted to dialogserver will still exist.

    if (theQUERY == "" && notQUERY == "") {
       document.hmsoForm.elements.QUERY00.value = "";
       document.hmsoForm.elements.QUERY09.value = ""; 
       return true;
    }

		//  Determine Probabilistic/Boolean query operator.
		//
    //  theOP = document.hmsoForm.elements.BORP[<index>].value
    //
    //  <index> will be [0, 1, 2] = [OR, AND, PHRASE]

    if (document.hmsoForm.elements.BORP[0].checked) {
      theOP = document.hmsoForm.elements.BORP[0].value;
    } else if (document.hmsoForm.elements.BORP[1].checked) {
      theOP = document.hmsoForm.elements.BORP[1].value;
    } else if (document.hmsoForm.elements.BORP[2].checked) {
      theOP = document.hmsoForm.elements.BORP[2].value;
    }

    // alert("theOP: " + theOP);
		//
    //  First validate boolean queries to make sure they don't
    //  contain any stopwords (excluding and, or & not)

    theTHRESHOLD = 100;

		if (theOP != "PHRASE") {
      if (!checkBoolean([document.hmsoForm.THEQUERY, 
                         document.hmsoForm.NOTQUERY])) { 
        return false; 
      }

      theTHRESHOLD = "0";
    }

    theQUERY = document.hmsoForm.elements.THEQUERY.value;
    notQUERY = document.hmsoForm.elements.NOTQUERY.value;

    var leadingWS = /^[ ]+/;
    var trailingWS = /[ ]+$/;
    var middleWS = /[ ]+/g;

		//  Remove leading and trailing whitespace from theQUERY

    if (theQUERY.length != 0) {
      //  remove leading and trailing whitespace
      theQUERY = theQUERY.replace(leadingWS, "");
      theQUERY = theQUERY.replace(trailingWS, "");
      phraseANDquery = "(" + theQUERY + ")";
      //  alert("PAQ(0): " + phraseANDquery);

      document.hmsoForm.elements.THEQUERY.value = theQUERY;
    }

    //  Need to account for situation where there are multiple
    //  whitespace characters separating query terms since Muscat
    //  wont accept expressions like (a AND AND b)
    //
    //  if theOP isn't PHRASE then replace multiple spaces by 
    //  " AND " or " OR " accordingly. Otherwise enclose theQUERY in quotes

    //  alert("TQ(1) = " + theQUERY + " (" + theOP + ")");

    if (theQUERY.length != 0) {
      if (theOP == "AND") { 
        theQUERY = theQUERY.replace(middleWS, " AND ");
        document.hmsoForm.elements.QUERY00.value = "";
				theMessage = "all of";
      } else if (theOP == "OR") { 
        theQUERY = theQUERY.replace(middleWS, " OR ");
        document.hmsoForm.elements.QUERY00.value = "";
				theMessage = "any of";
      } else if (theOP == "PHRASE") {
        phraseANDquery = phraseANDquery.replace(middleWS, " AND ");
        //  alert("PAQ(1): " + phraseANDquery);

        setQuotes();
				//  document.hmsoForm.elements.QUERY00.value is set to
				//  theQUERY in setQuotes()
				//  Set relevance threshold to 100

        theTHRESHOLD = "100";
				//  Clear boolQUERY
        //  boolQUERY = phraseANDquery;
        //  document.hmsoForm.elements.QUERY09.value = boolQUERY;
        boolQUERY = "";
				theMessage = "exact phrase";
      }
			document.hmsoForm.elements.THEMESSAGE.value = theMessage;
    }
    // else {
		//	document.hmsoForm.elements.THEMESSAGE.value = "";theMessage;
		//}


    //  alert("TQ(2) = " + theQUERY);
    //  alert("TQ(3) = (" + theOP + ")");

    if (theQUERY.length > 0) {
      //  A valid ALL, ANY or PHRASE query. 
      //
      //  Check for non-empty NOTQUERY. Convert to form:
      //
      //    AND NOT NOTQUERY.
      //
      //  e.g. "child support" to "AND NOT (child OR support)"
      //
      //  Remove leading and trailing whitespace
      //
      var notQUERY = document.hmsoForm.elements.NOTQUERY.value;

      if (notQUERY.length != 0) {
        //  remove leading and trailing whitespace
        notQUERY = notQUERY.replace(leadingWS, "");
        notQUERY = notQUERY.replace(trailingWS, "");
        document.hmsoForm.elements.NOTQUERY.value = notQUERY;
        notQUERY = notQUERY.replace(middleWS, " OR ");
        notQUERY = "AND NOT (" + notQUERY + ")";

        if (theOP == "PHRASE") {
          //  ensure that boolQUERY is 
          //  (T1 AND T2) etc., i.e. not (T1 OR T2)
          alert("You may only exclude words with 'ANY' or 'ALL', not 'EXACT PHRASE'");
          return false;
          //  boolQUERY = phraseANDquery + " " + notQUERY;
          //  theTHRESHOLD = 0;
          //  alert("PBN: " + boolQUERY);
          invalidQUERY = 0;
        } else {
          boolQUERY = theQUERY;
          //  alert("BQ1: " + boolQUERY);
          //  boolQUERY = boolQUERY.replace(middleWS, " AND ");
          //  alert("BQ2: " + boolQUERY);
          boolQUERY = "(" + boolQUERY + ") " + notQUERY;
          //  alert("BQ3: " + boolQUERY);
        }
      } else {
        boolQUERY = theQUERY;
      }

      //  alert("BQ3: " + boolQUERY);


      if (theOP != "PHRASE") {
        theTHRESHOLD = 0;
      }
		} else if (notQUERY.length != 0) {
      alert("You can't have an empty key word query with a NOT query");
		  return false;
		}

    if (theOP == "PHRASE") {
      boolQUERY = "";
    }

    document.hmsoForm.elements.QUERY09.value = boolQUERY;
    document.hmsoForm.elements.THRESHOLD.value = theTHRESHOLD;

    // alert("THEQUERY = " + theQUERY + "\nQUERY00 = " + document.hmsoForm.elements.QUERY00.value);
    // alert("boolQUERY = " + boolQUERY + "\nQUERY09 = " + document.hmsoForm.elements.QUERY09.value);
    // alert("theTHRESHOLD = " + theTHRESHOLD + "\nTHRESHOLD = " + document.hmsoForm.elements.THRESHOLD.value);

    return true;
  }

  function setMHQuotes() {
    //  This function surrounds the complete set of entered
    //  terms in double quotation marks for phrase searching
    var dq = String.fromCharCode(34); // double quote
    var quoteMark = /dq/g;
    var thisPHRASE = document.quickSearch.elements.QUICKQUERY.value;

    if (thisPHRASE.length != 0) {
      //  remove leading and trailing whitespace
      //  document.quickSearch.elements.THRESHOLD.value = "100";
      //  Put quotes around the whole theQUERY string
      if (thisPHRASE.indexOf(String.fromCharCode(34)) == -1) {
        thisPHRASE = String.fromCharCode(34) + thisPHRASE + String.fromCharCode(34);
        document.quickSearch.elements.QUERY01.value = thisPHRASE;
      }
    }
  }

  //  This function is called onSubmit() of quickForm
  //
  //  Sets up the boolean QUERY in QUERY02 dependent upon the contents
  //  of ANDQUERY, ORQUERY and NOTQUERY. Query will be of the form:
  //
  //  ((a AND b AND c) AND (d OR e OR f)) AND NOT (g OR h OR i)

  function setupMHCGI() {
    //  This function:
    //   //  1. Sets up a boolean query in the HIDDEN INPUT box, QUERY02 from
    //     the contents of the ANDQUERY, ORQUERY and NOTQUERY.
    //
    //     The query expression will become:
    //
    //     ((a AND b AND c) AND (d OR e OR f)) AND NOT (g OR h OR i)
    //
    //  2. Sets up the STARTDATE and ENDDATE cgi variables from the
    //     appropriate date input fields:
    //
    //     (ONDAY, ONMONTH, ONYEAR)
    //     (SINCEDAY, SINCEMONTH, SINCEYEAR)
    //     ((BETWEENDAY1, BETWEENMONTH1, BETWEENYEAR1)
    //      (BETWEENDAY2, BETWEENMONTH2, BETWEENYEAR2))

    //  alert("masthead setupMHCGI");
    var quickQUERY = document.quickSearch.elements.QUICKQUERY.value;
    document.quickSearch.elements.USERQUERY.value = quickQUERY;

    var phraseANDquery = "";
    var boolQUERY = "";

    var theOP = "OR";
		var theMessage = "any of";
    var theTHRESHOLD = "0";
    var invalidQUERY = 1;

		//  If empty query strings (both QUICKQUERY and NOTQUERY
		//  then ensure that QUERY01 and QUERY02 are both emptied
		//  otherwise if [THE|NOT]QUERY have both been cleared by the 
		//  the QUERY submitted to dialogserver will still exist.

    if (quickQUERY == "") {
       document.quickSearch.elements.QUERY01.value = "";
       return true;
    }

		//  Determine Probabilistic/Boolean query operator.
		//
    //  theOP = document.quickSearch.elements.QBORP[<index>].value
    //
    //  <index> will be [0, 1, 2] = [OR, AND, PHRASE]

    if (document.quickSearch.elements.QBORP[0].checked) {
      theOP = document.quickSearch.elements.QBORP[0].value;
    } else if (document.quickSearch.elements.QBORP[1].checked) {
      theOP = document.quickSearch.elements.QBORP[1].value;
    } else if (document.quickSearch.elements.QBORP[2].checked) {
      theOP = document.quickSearch.elements.QBORP[2].value;
    }

    if (theOP == "OR") {
      document.quickSearch.elements.QBORP.value = "OR";
    }

    //  alert("theOP: " + theOP);
		//
    //  First validate boolean queries to make sure they don't
    //  contain any stopwords (excluding and, or & not)

    theTHRESHOLD = 100;

		if (theOP != "PHRASE") {
      if (!checkBoolean([document.quickSearch.QUICKQUERY])) { 
        return false; 
      }

      theTHRESHOLD = "0";
    }

    quickQUERY = document.quickSearch.elements.QUICKQUERY.value;

    var leadingWS = /^[ ]+/;
    var trailingWS = /[ ]+$/;
    var middleWS = /[ ]+/g;

		//  Remove leading and trailing whitespace from quickQUERY

    if (quickQUERY.length != 0) {
      //  remove leading and trailing whitespace
      quickQUERY = quickQUERY.replace(leadingWS, "");
      quickQUERY = quickQUERY.replace(trailingWS, "");
      phraseANDquery = "(" + quickQUERY + ")";
      //  alert("PAQ(0): " + phraseANDquery);

      document.quickSearch.elements.QUICKQUERY.value = quickQUERY;
    }

    //  Need to account for situation where there are multiple
    //  whitespace characters separating query terms since Muscat
    //  wont accept expressions like (a AND AND b)
    //
    //  if theOP isn't PHRASE then replace multiple spaces by 
    //  " AND " or " OR " accordingly. Otherwise enclose quickQUERY in quotes

    //  alert("TQ(1) = " + quickQUERY + " (" + theOP + ")");

    if (quickQUERY.length != 0) {
      if (theOP == "AND") { 
        quickQUERY = quickQUERY.replace(middleWS, " AND ");
        document.quickSearch.elements.QUERY01.value = "";
				theMessage = "all of";
      } else if (theOP == "OR") { 
        quickQUERY = quickQUERY.replace(middleWS, " OR ");
        document.quickSearch.elements.QUERY01.value = "";
				theMessage = "any of";
      } else if (theOP == "PHRASE") {
        phraseANDquery = phraseANDquery.replace(middleWS, " AND ");
        //  alert("PAQ(1): " + phraseANDquery);

        setMHQuotes();
				//  document.quickSearch.elements.QUERY01.value is set to
				//  quickQUERY in setQuotes()
				//  Set relevance threshold to 100

        theTHRESHOLD = "100";
				//  Clear boolQUERY
        //  boolQUERY = phraseANDquery;
        //  document.quickSearch.elements.QUERY02.value = boolQUERY;
        boolQUERY = "";
				theMessage = "exact phrase";
      }
			document.quickSearch.elements.THEMESSAGE.value = theMessage;
    }
    // else {
		//	document.quickSearch.elements.THEMESSAGE.value = "";theMessage;
		//}


    //  alert("theMessage = " + theMessage);
    //  alert("theMessage = " + document.quickSearch.elements.THEMESSAGE.value);

    //  alert("TQ(2) = " + quickQUERY);
    //  alert("TQ(3) = (" + theOP + ")");

    if (quickQUERY.length > 0) {
      //  A valid ALL, ANY or PHRASE query. 
      //
      boolQUERY = quickQUERY;

      //  alert("BQ3: " + boolQUERY);


      if (theOP != "PHRASE") {
        theTHRESHOLD = 0;
      }
		}

    if (theOP == "PHRASE") {
      boolQUERY = "";
    }

    document.quickSearch.elements.QUERY02.value = boolQUERY;
    document.quickSearch.elements.THRESHOLD.value = theTHRESHOLD;

    //  alert("QUICKQUERY = " + quickQUERY + "\nQUERY01 = " + document.quickSearch.elements.QUERY01.value);
    //  alert("boolQUERY = " + boolQUERY + "\nQUERY02 = " + document.quickSearch.elements.QUERY02.value);
    //  alert("theTHRESHOLD = " + theTHRESHOLD + "\nTHRESHOLD = " + document.quickSearch.elements.THRESHOLD.value);

    return true;
  }
// -->


