//Javascript

/*
Function: buildSearchString
Description: Examines strInput, removes illegal characters and selected keywords.
						 Builds and returns a Verity formatted search string.
*/
function buildSearchString(searchString, keywordsToRemove) {
  var keyWords;
  var veritySearchString;

	// Remove illegal characters
	// Feb 09 2009 - BOBMA - swapped out an iterative function used in version 1 for this replace call.
	searchString = searchString.replace(/['=().><!`@{}*,_~;:^+/\\|&#[\]]/gi, "");

  // Removes any unmatching double quotes
  searchString = removeExtraQuotes(searchString);

  // Returns an array containing keyWords and quoted phrases
  // Feb 09 2009 - BOBMA - swapped out a very iterative function for this regular expression use.
  keyWords = searchString.match(/\b\S+\b|"[^"]*"/gi);

  // Builds a Verity based search string based upon an
  // array of keywords
  veritySearchString = buildVeritySearchString(keyWords, keywordsToRemove);

  return veritySearchString;
}

/*
Function : buildVeritySearchString
Description : Builds a Verity search string based upon an array of keywords.
*/
function buildVeritySearchString(keyWords, keywordsToRemove) {
	var basicVerityClause = "<MANY><THESAURUS>xxxx <ACCRUE> <MANY><STEM>yyyy"
  var newClause = "";
  var newSearchString = "";
  var quote = "\"";
  var keyWord = "";
  var ignoredRegExp = GetIgnoredKeywordsRegExp(keywordsToRemove);

	// Loop through the keyWords array and build a verity search clause for each
  // keyWord that is not enclosed in double quotes.
  // If the keyWord is enclosed in double quotes, just append the keyWord to the verity search string.
  for (i = 0 ; i < keyWords.length ; i++) {
  	keyWord = keyWords[i];

  	if ( keyWord.indexOf(quote) == -1 ) {

			//Feb 09 2009 - BOBMA - do not include specified words in verity search string
      if (!ignoredRegExp.test(keyWord)) {
      	// Special case if keyWord is 'and', 'or', or 'not'. It must be wrapped in single quotes to cooperate with <accrue>.
        if (keyWord == "and" || keyWord == "or" || keyWord == "not") {
        	keyWord = "'" + keyWord + "'";
        }
        newClause = basicVerityClause.replace("xxxx", keyWord);
        newClause = newClause.replace("yyyy", keyWord);
        newSearchString = AppendSearchString(newSearchString, newClause);
			}
    } else {
    	//this is a quoted phrase so add it as is.
      newSearchString = AppendSearchString(newSearchString, keyWord);
    }
  }

  return newSearchString;
}

/*
Function : AppendSearchString
Description : returns the search string with the new clause joined by the "ACCRUE" verity opperator.
*/
function AppendSearchString(searchString, newClause) {
	if (searchString.length > 0)
		searchString += " <ACCRUE> ";
					
	return searchString += newClause;
}


/*
Feb 09 2009 - BOBMA
Function : GetIgnoredKeywordsRegExp
Description : Creates the regular expression for matchcing against supplied keywords
Parameters: Comma seperated list of words
*/
function GetIgnoredKeywordsRegExp(keywords) {
  var pattern = "";

  if (keywords == null || keywords.length <= 0)
		return new RegExp("^$");	//we don't want to match anything in this case.

	//loop through each of the keywords and create the appropriate reg exp match
  var words = keywords.split(",");
  for (i=0; i<words.length; i++) {
  	pattern += (i == 0 ? "" : "|") + "\\b" + words[i].trim() + "\\b";
  }

  return new RegExp(pattern, "gi");
}


/*
Function : removeExtraQuotes
Description: Removes unmatched double quotes within a string. If strInput contains 
an odd number of double quotes ("), this function will remove the last double quote 
from the string.
*/
function removeExtraQuotes(strInput) {

  var ds = new Array();
  var di = 0;
  var quote = "\"";
  var pos = 0;
  var start = 0;
  var numberOfElements = 0;
  var newString = "";

  // Get the position of the first double quote
  pos = strInput.indexOf(quote, start);

  // Add an element to array ds that contains the position of every double quote
  // in the string strInput
  while (pos != -1) {
    ds[di++] = pos;
    start = pos + 1;
    pos = strInput.indexOf(quote, start);
  }

  numberOfElements = ds.length;

  // If there is an odd number of elements in ds, then remove the last double quote
  // from string strInput, otherwise return the string unchanged
  if ( (numberOfElements % 2) != 0 ) {
    pos = ds[numberOfElements - 1];
    newString = strInput.substring(0, pos) + strInput.substring(pos + 1);
  } else {
    newString = strInput;
  }

  return newString;
}


/*
Function : searchHelp
Description: Opens a new browser window with a URL based on the incoming value.
*/
function searchHelp(location) {
  var helpWindow = null;

  helpWindow = window.open(''+location+'','', 'width=575,height=200,left=250,top=250,toolbar=0,location=0,directories=0,status=0,resizable=1,scrollbars=1,menubar=0')
}


/*
Function : trim
Description: Helper function to remove spaces around a string
*/
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, "");
}