/****************************************************************************
 * Copyright (c) 1998-2007 Luna Imaging, Inc.  All Rights Reserved.
 *
 * This software is confidential and proprietary information of
 * Luna Imaging, Inc.  ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Luna Imaging, Inc.
 *
 * The software may not be copied, reproduced, translated or reduced to
 * any electronic medium or machine-readable form without
 * the prior written consent of Luna Imaging.
 *
 * You are not allowed to distribute the binary and source code
 * (if released) to third parties, without the prior written consent from
 * Luna Imaging.
 *
 * You are not allowed to reverse engineer, disassemble or decompile
 * code, or make any modifications of the binary or source code, remove
 * or alter any trademark, logo, copyright or other proprietary notices,
 * legends, symbols, or labels in the Software.
 *
 * You are not allowed to sub-license the Software or any derivative
 * work based on or derived from the Software.
 *
 * LUNA IMAGING MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, LUNA IMAGING SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 *
 *  EnhancedAdvancedSearch.js
 *
 *  Description:
 *    Dynamically renders a new advanced search form.
 *
 *  Structure:
 *
 *
 *  Requires:
 *    prototype.js - http://www.prototypejs.org/
 *
 *  Development History:
 *    4-6-2009       - created
 *
 *******************************************************************************/



function EnhancedAdvancedSearch()
{
  // html elements
  this.mContainer = null;
  this.mPositionRefernceElement = null;
  this.mPanel = null;
  this.mMediaCollectionsContainer = null;
  this.mMediaCollectionsListContainer = null;
  this.mSearchOptionsContainer = null;
  this.mSearchValuesContainer = null;
  this.mQueryStringContainer = null;
  this.mSearchHeaderContainer = null;
  this.mClearSearchButtonContainer = null;
  this.mToggleMediaCollectionsButton = null;
  this.mSelectAllMediaCollectionsButton = null;
  this.mClearMediaCollectionSelectionButton = null;
  this.mMediaCollectionsText = null;
  this.mNewSearchTermButton = null;
  
  this.mNewSearchAllTermButton = null;
  this.mNewSearchAnyTermButton = null;  
  this.mNewSearchExactTermButton = null;
  
  this.mHelpButton = null;
  this.mCloseButton = null;
  this.mTitleButton = null;
  this.mSelectedTitleButton = null;
  this.mSearchValueContainer = null;
  this.mLoadingContainer = null;
  this.mAdvanceSearchInstructionText = null;
  this.mSubmitSearchButton = null;
  this.mClearSearchButton = null;

  // js elements
  this.mMediaCollections = null;
  this.mSearchTermTemplate = null;
  this.mMediaSearchOperators = null;
  this.mMediaSearchDateOperators = null;
  this.mMediaFields = null;
  this.mCurrentSearchOption = null;
  this.mTermConditions = $( [ {displayName:'And', value:' AND ', className:'matchAll'}, {displayName:'Or', value:' OR ', className:'matchAny'} ] );

  this.mSearchTypeOverwriteOp = $( ['#', '^']);
  this.mDefaultOp = '=';
  
  this.mCurrentTermCondition = this.mTermConditions[0];
  this.mSuggestionCache = $( new Array() );
  this.helpTipPopUp = null;
  this.helpTip = null;
  this.advancedSearchShowTip = null;
  this.showTip = null;

  // flags
  this.mAnimate = true;
  this.mIsHidden = false;
  this.mLoading = false;
  this.mIsRequestSuggestionMode = true;
  this.mBatchSetSearchValuesTimeout = null;

  // functions
  this.mRequestMediaFieldsFunction = null;
  this.mRequestSuggestionFunction = null;
  this.mToggleSearchFunction = null;
  this.mAlternateSearchEnabledFunction = null;

  this.mSort = null;
  // urls
  this.mSearchUrl = null;

  this.mHelpUrl = null;
  this.mAdvancedHelpUrl = null;
  
  // global variable
  this.mMaxSelectFieldLength = 0;

  this.CLASSNAME = 'advanceMediaSearchContainer';
  this.MEDIA_COLLECTION_CLASSNAME = 'esMediaCollectionsContainer';
  this.MEDIA_COLLECTION_HEADER_CLASSNAME = 'mediaCollectionsHeaderContainer';
  this.SEARCH_OPTIONS_CONTAINER_CLASSNAME = 'searchOptionsContainer';
  this.SEARCH_VALUES_CONTAINER_CLASSNAME = 'esSearchValuesContainer';
  this.SEARCH_TERMS_CONTAINER_CLASSNAME = 'searchTermsContainer';
  this.SEARCH_ALL_TERMS_CONTAINER_CLASSNAME = 'searchAllTermsContainer';
  this.SEARCH_ANY_TERMS_CONTAINER_CLASSNAME = 'searchAnyTermsContainer';
  this.SEARCH_EXACT_TERMS_CONTAINER_CLASSNAME = 'searchExactTermsContainer';
  this.SEARCH_TERM_CONTAINER_CLASSNAME = 'searchTermContainer';
  this.SEARCH_ALL_TERM_CONTAINER_CLASSNAME = 'searchAllTermContainer';
  this.SEARCH_ANY_TERM_CONTAINER_CLASSNAME = 'searchAnyTermContainer';
  this.SEARCH_EXACT_TERM_CONTAINER_CLASSNAME = 'searchExactTermContainer';
  this.SEARCH_SUBMIT_CONTAINER_CLASSNAME = 'searchSubmitContainer';
  this.SEARCH_QUERY_STRING_CONTAINER_CLASSNAME = 'queryStringContainer';
  this.SEARCH_QUERY_DISPLAY_CONTAINER_CLASSNAME = 'queryDisplayContainer';
  this.SEARCH_CLEAR_SEARCH_BUTTON_CONTAINER_CLASSNAME = 'clearSearchButtonContainer';
  this.SEARCH_HEADER_CONTAINER_CLASSNAME = 'searchHeaderContainer'
  this.SEARCH_CLEAR_SEARCH_BUTTON_CLASSNAME = 'clearSearchButtonContainer';
  this.EXPANDED_CLASSNAME = 'expanded';
  this.REMOVE_BUTTON_CLASSNAME = 'removeButton';
  this.CLOSE_BUTTON_CLASSNAME = 'closeButton';
  this.HELP_BUTTON_CLASSNAME = 'helpButton';
  this.TERM_CONDITION_BUTTON_CLASSNAME = 'termConditionButton';
  this.NEW_SEARCH_TERM_BUTTON_CLASSNAME = 'newSearchTermButton';
  this.NEW_SEARCH_ALL_TERM_BUTTON_CLASSNAME = 'newSearchAllTermButton';
  this.NEW_SEARCH_ANY_TERM_BUTTON_CLASSNAME = 'newSearchAnyTermButton';
  this.NEW_SEARCH_EXACT_TERM_BUTTON_CLASSNAME = 'newSearchExactTermButton';
  this.CURRENT_SEARCH_TERM_CLASSNAME = 'currentSearchTerm';
  this.LOADING_CONTAINER_CLASSNAME = 'loadingContainer';
  this.SEARCH_VALUE_CONTAINER_CLASSNAME = 'searchValue';
  this.SUBMIT_SEARCH_BUTTON_CLASSNAME = 'submitSearchButton';
  this.CLEAR_SEARCH_BUTTON_CLASSNAME = 'clearSearchButton';
  this.TOGGLE_MEDIA_COLLECTION_BUTTON_CLASSNAME = 'toggleMediaCollectionButton';
  this.SEARCH_DATE_CONTAINER_CLASSNAME = 'searchDateContainer';
  this.FIRST_SEARCH_TERM_VALUE_CLASSNAME = 'firstSearchTermValue';
  this.TOOL_TIP_CLASSNAME = 'tooltip';
  this.TOOL_TIP_BOTTOM_CLASSNAME = 'tooltip_bottom';
  this.TOOL_TIP_TOP_CLASSNAME = 'tooltip_top';
  this.TOOL_TIP_MIDDLE_CLASSNAME = 'tooltip_middle';
  this.SEARCH_TITLE_CLASSNAME = 'searchTitle';
  this.SEARCH_SELECTED_TITLE_CLASSNAME = 'searchSelectedTitle';

  this.ADVANCE_SEARCH_SUGGESTIONS_TEXT = 'Enter a letter or phrase to see a list of known values:'
  this.ADVANCE_SEARCH_VALUES_TEXT = 'Enter search values and click add:';
  this.ADVANCE_SEARCH_DATE_VALUE_TEXT = 'Enter a start and/or end year';
  this.ADVANCED_SEARCH_TEXT = 'Advanced Search';
  this.ALTERNATE_ADVANCED_SEARCH_TEXT = 'Alternate Advanced Search';

  this.SEARCH_INTRO_TEXT = 'Use the following form to construct an advanced search query.';
  this.SEARCH_ALL_TERM_TEXT = 'find all these words:';
  this.SEARCH_ANY_TERM_TEXT = 'find any of these words:';
  this.SEARCH_EXACT_TERM_TEXT = 'find this exact wording:';
  this.SEARCH_IN_TEXT = 'In';
  this.SEARCH_HINT_TEXT = 'Hint'
  this.SEARCH_QUERY_INVALID = 'Search query is invalid';

  this.ERROR_SELECT_MEDIA_FIELD_TEXT = 'Please select a media field first.'
  this.ERROR_SELECT_MEDIA_COLLECTION_TEXT = 'Please select at least one media collection.'
  this.MEDIA_COLLECTION_TEXT = 'Collections: ';
  this.SELECT_MEDIA_FIELD_DEFAULT_TEXT = 'Select a field';
  this.HELP_BUTTON_TEXT = 'Help';
  this.CLOSE_BUTTON_TEXT = 'Close';
  this.ADD_SEARCH_VALUE_BUTTON_TEXT = 'Add';
  this.LIST_ALL_BUTTON_TEXT = '%';
  this.SUBMIT_SEARCH_BUTTON_TEXT = 'Search';
  this.CLEAR_SEARCH_BUTTON_TEXT = 'Clear Search';
  this.CLEAR_SELECTION_TEXT = 'Clear Selection';
  this.SELECT_ALL_TEXT = 'Select All'
  this.HELP_TARGET = 'help';

  this.REMOVE_SEARCH_TERM_INSTRCUTION = 'Click to remove.';

  this.SEARCH_TERM_REMOVE_BUTTON_ELEMENT_INDEX = 7;
  this.SEARCH_TERM_TOOLTIP_ELEMENT_INDEX = 6;
  this.SEARCH_TERM_INPUT_INDEX = 5;
  this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX = 3;
  this.SEARCH_TERM_OPERATOR_ELEMENT_INDEX = -1; //not used for now
  this.SEARCH_TERM_VALUE_ELEMENT_INDEX = 5;
  this.SEARCH_TERM_CONDITION_ELEMENT_INDEX = 1;
  this.SEARCH_TERM_HINT_ELEMENT_INDEX = 0;

  this.SEARCH_VALUES_WIDTH = 200;
  this.SEARCH_VALUE_INPUT_WIDTH = this.SEARCH_VALUES_WIDTH - 10;
  this.SEARCH_DATE_INPUT_WIDTH = 40;
  this.SEARCH_TERM_MIN_HEIGHT = 20; //30;
  this.SEARCH_TERMS_CONTAINER_GAP = 5;
  this.INNER_ELEMENT_SIZE_PERCENTAGE = .97;
  this.REMOVE_BUTTON_WIDTH = 20;
  this.MAX_LOADING_TIME = 46000; // 46 sec, yes that long
  this.MIN_LOADING_TIME = 800;
  this.SEARCH_TERM_CONDITION_WIDTH = 52;
  this.SEARCH_INPUT_WIDTH = 348;
  this.SEARCH_COMBINED_WIDTH = 450;

  this.SUGGESTION_BATCH_SIZE = 50;
  this.SUGGESTION_BATCH_DELAY = 300;

  this.SELECT_NO_VALUE = '-';

  this.SUGGESTION_KEYDOWN_PAUSE = 500; // in milliseconds
  this.COLLECTION_NAME_LENGTH = 26;

  this.init = function()
  {
    this.mPanel = $( document.createElement( 'div' ) );
    this.mLoadingContainer = $( document.createElement( 'div' ) );
	this.mSearchHeaderContainer = $( document.createElement( 'div' ) );
    this.mSearchOptionsContainer = $( document.createElement( 'div' ) );
    this.mMediaCollectionsContainer = $( document.createElement( 'div' ) );
	this.mMediaCollectionsHeaderContainer = $( document.createElement( 'div' ) );
    this.mMediaCollectionsListContainer = $( document.createElement( 'div' ) );
    this.mSearchValuesContainer = $( document.createElement( 'div' ) );
    this.mAddSearchDateValueButton = $( document.createElement( 'a' ) );
    //this.mSearchStartDate = $( document.createElement( 'input' ) );
    //this.mSearchEndDate = $( document.createElement( 'input' ) );
    this.mSearchValueContainer = $( document.createElement( 'div' ) );
	this.mSearchValueFillerContainer = $( document.createElement( 'div' ) );
	this.mClearSearchButtonContainer = $( document.createElement( 'div' ) );
    this.mSearchSubmitContainer = $( document.createElement( 'div' ) );   
	this.mQueryStringContainer =  $( document.createElement( 'div' ) );
	this.mQueryDisplayContainer =  $( document.createElement( 'div' ) );	  
    this.mSearchAllTermsContainer = $( document.createElement( 'div' ) );
    this.mSearchAnyTermsContainer = $( document.createElement( 'div' ) );
    this.mSearchExactTermsContainer = $( document.createElement( 'div' ) );
    
    this.mToggleMediaCollectionsButton = $( document.createElement( 'a' ) );
	this.mClearMediaCollectionSelectionButton = $( document.createElement( 'span' ) );
	this.mSelectAllMediaCollectionsButton = $( document.createElement( 'span' ) );    
    this.mNewSearchAllTermButton = $( document.createElement( 'a' ) );
    this.mNewSearchAnyTermButton = $( document.createElement( 'a' ) );
	this.mNewSearchExactTermButton = $( document.createElement( 'a' ) );    
    this.mHelpButton = $( document.createElement( 'a' ) );
    this.mCloseButton = $( document.createElement( 'a' ) );    
    this.mTitleButton = $( document.createElement( 'span' ) );
    this.mSelectedTitleButton = $( document.createElement( 'span' ) );
    this.mSubmitSearchButton = $( document.createElement( 'a' ) );
    this.mClearSearchButton = $( document.createElement( 'a' ) );
	
    this.mMediaCollectionsText = $( document.createElement( 'span' ) );
    this.mAdvanceSearchInstructionText = $( document.createElement( 'span' ) );

    // to avoid flicker if brower is a bit busy doing other stuff
    this.mPanel.style.visibility = 'hidden';

    this.mPanel.addClassName( this.CLASSNAME );
    this.mLoadingContainer.addClassName( this.LOADING_CONTAINER_CLASSNAME );
    this.mMediaCollectionsContainer.addClassName( this.MEDIA_COLLECTION_CLASSNAME );
	this.mMediaCollectionsHeaderContainer.addClassName( this.MEDIA_COLLECTION_HEADER_CLASSNAME );
    this.mSearchValuesContainer.addClassName( this.SEARCH_VALUES_CONTAINER_CLASSNAME );
	this.mQueryStringContainer.addClassName( this.SEARCH_QUERY_STRING_CONTAINER_CLASSNAME );
	this.mQueryDisplayContainer.addClassName( this.SEARCH_QUERY_DISPLAY_CONTAINER_CLASSNAME );
	this.mClearSearchButtonContainer.addClassName( this.SEARCH_CLEAR_SEARCH_BUTTON_CONTAINER_CLASSNAME );
	this.mSearchSubmitContainer.addClassName( this.SEARCH_SUBMIT_CONTAINER_CLASSNAME );
    this.mSearchAllTermsContainer.addClassName( this.SEARCH_ALL_TERMS_CONTAINER_CLASSNAME );
    this.mSearchAnyTermsContainer.addClassName( this.SEARCH_ANY_TERMS_CONTAINER_CLASSNAME );
    this.mSearchExactTermsContainer.addClassName( this.SEARCH_EXACT_TERMS_CONTAINER_CLASSNAME );
	this.mSearchHeaderContainer.addClassName( this.SEARCH_HEADER_CONTAINER_CLASSNAME );    
    this.mSearchOptionsContainer.addClassName( this.SEARCH_OPTIONS_CONTAINER_CLASSNAME );    
    this.mNewSearchAllTermButton.addClassName( this.NEW_SEARCH_ALL_TERM_BUTTON_CLASSNAME );
    this.mNewSearchAnyTermButton.addClassName( this.NEW_SEARCH_ANY_TERM_BUTTON_CLASSNAME );
    this.mNewSearchExactTermButton.addClassName( this.NEW_SEARCH_EXACT_TERM_BUTTON_CLASSNAME );
	this.mToggleMediaCollectionsButton.addClassName( this.TOGGLE_MEDIA_COLLECTION_BUTTON_CLASSNAME );        
    this.mSubmitSearchButton.addClassName( this.SUBMIT_SEARCH_BUTTON_CLASSNAME );
	this.mCloseButton.addClassName( this.CLOSE_BUTTON_CLASSNAME );
	this.mHelpButton.addClassName( this.HELP_BUTTON_CLASSNAME );
	this.mTitleButton.addClassName( this.SEARCH_TITLE_CLASSNAME );
	this.mSelectedTitleButton.addClassName( this.SEARCH_SELECTED_TITLE_CLASSNAME );

    this.mPanel.appendChild( this.mLoadingContainer );
    this.mSearchHeaderContainer.appendChild( this.mSelectedTitleButton );
	var ams = this;
    if (ams.mAlternateSearchEnabledFunction()) {
	  this.mSearchHeaderContainer.appendChild( this.mTitleButton );
	}
    this.mSearchHeaderContainer.appendChild( this.mCloseButton );
    this.mSearchHeaderContainer.appendChild( this.mHelpButton );
	this.mPanel.appendChild( this.mSearchHeaderContainer );	
    this.mSearchValuesContainer.appendChild( this.mSearchValueFillerContainer );
    this.mSearchValuesContainer.appendChild( this.mSearchValueContainer );
    this.mSearchSubmitContainer.appendChild( this.mSubmitSearchButton );
    this.mSearchAllTermsContainer.appendChild( this.mNewSearchAllTermButton );
    this.mSearchAnyTermsContainer.appendChild( this.mNewSearchAnyTermButton );
    this.mSearchExactTermsContainer.appendChild( this.mNewSearchExactTermButton );
    this.mSearchOptionsContainer.appendChild( this.mSearchAllTermsContainer );
    this.mSearchOptionsContainer.appendChild( this.mSearchAnyTermsContainer );
    this.mSearchOptionsContainer.appendChild( this.mSearchExactTermsContainer );    
	this.mClearSearchButtonContainer.appendChild( this.mClearSearchButton );
	this.mQueryDisplayContainer.appendChild( this.mQueryStringContainer );
	this.mQueryDisplayContainer.appendChild( this.mClearSearchButtonContainer );
	
	this.mPanel.appendChild( this.mQueryDisplayContainer );
    this.mPanel.appendChild( this.mSearchOptionsContainer );
	this.mPanel.appendChild( this.mSearchSubmitContainer );
    this.mPanel.appendChild( this.mMediaCollectionsContainer );
    this.mContainer.appendChild( this.mPanel );

    // defaults
    this.mDimensions = $( [ this.mPanel.getWidth(), this.mPanel.getHeight() ] );
    this.mToggleMediaCollectionsButton.href = 'javascript: var aButton;';
	this.mClearMediaCollectionSelectionButton.update( this.CLEAR_SELECTION_TEXT );
	this.mSelectAllMediaCollectionsButton.update( this.SELECT_ALL_TEXT );
    this.mNewSearchAllTermButton.href = 'javascript: var aButton;';
    this.mNewSearchAnyTermButton.href = 'javascript: var aButton;';
    this.mNewSearchExactTermButton.href = 'javascript: var aButton;';
    //this.mHelpButton.update( this.HELP_BUTTON_TEXT );
    //this.mHelpButton.href = this.mHelpUrl;
    //this.mHelpButton.target = this.HELP_TARGET;
    this.mHelpButton.id = 'AdvancedSearchHelp';
    //this.mCloseButton.update( this.CLOSE_BUTTON_TEXT );
    this.mCloseButton.href = 'javascript: var aButton;';
    this.mAddSearchDateValueButton.href = 'javascript: var aButton;';
    this.mAddSearchDateValueButton.update( this.ADD_SEARCH_VALUE_BUTTON_TEXT );
    this.mAdvanceSearchInstructionText.update( this.ADVANCE_SEARCH_SUGGESTIONS_TEXT );
    this.mSubmitSearchButton.href = 'javascript: var aButton;';
    this.mSubmitSearchButton.update( this.SUBMIT_SEARCH_BUTTON_TEXT );
    this.mClearSearchButton.href = 'javascript: var aButton;';
    this.mClearSearchButton.update( this.CLEAR_SEARCH_BUTTON_TEXT );
	this.mTitleButton.update( this.ALTERNATE_ADVANCED_SEARCH_TEXT );
	this.mSelectedTitleButton.update( this.ADVANCED_SEARCH_TEXT );

    // events
    var ams = this;
    this.mToggleMediaCollectionsButton.onclick = function(){ ams.mToggleMediaCollectionsButton.blur(); ams.toggleMediaCollections() };
    this.mMediaCollectionsText.onclick = function(){ ams.mToggleMediaCollectionsButton.blur(); ams.toggleMediaCollections() };
    this.mNewSearchAllTermButton.onclick = function(){ ams.mNewSearchAllTermButton.blur(); ams.appendSearchAllTerm(); ams.updateDimensions();};
    this.mNewSearchAnyTermButton.onclick = function(){ ams.mNewSearchAnyTermButton.blur(); ams.appendSearchAnyTerm(); ams.updateDimensions();};
    this.mNewSearchExactTermButton.onclick = function(){ ams.mNewSearchExactTermButton.blur(); ams.clearSearchValues(); ams.appendSearchExactTerm(); ams.updateDimensions();};
    this.mCloseButton.onclick = function(){ ams.mCloseButton.blur(); ams.toggle(); };
    this.mHelpButton.onclick = function(){ ams.mHelpButton.blur(); ams.toggleHelpControl(); };
    this.mSubmitSearchButton.onclick = function(){ ams.mSubmitSearchButton.blur(); return ams.submitSearch(); };
	this.mClearSearchButton.onclick = function(){ ams.clearAllSearchFields();ams.updateQueryStringContainer(); };
	this.mTitleButton.onclick = function() { ams.mToggleSearchFunction(); };

    // stylings
    this.mPanel.style.position = 'absolute';
    this.mPanel.style.top = 0 + 'px';
    this.mPanel.style.right = 0 + 'px';
    this.mPanel.style.overflowX = 'hidden';
    this.mPanel.style.overflowY = 'hidden';

    this.mLoadingContainer.hide();
    this.mLoadingContainer.style.position = 'absolute';
    this.mLoadingContainer.style.left = 1 + 'px';
    this.mLoadingContainer.style.top = 1 + 'px';
    this.mLoadingContainer.style.width = 150 + 'px';
    this.mLoadingContainer.style.height = 500 + 'px';
    this.mLoadingContainer.style.zIndex = 99999;
    jshSetOpacity( this.mLoadingContainer, .75 );

    var innerElementWidth = Math.round( this.INNER_ELEMENT_SIZE_PERCENTAGE * this.mDimensions[0] );
    var innerElementHeight = Math.round( this.INNER_ELEMENT_SIZE_PERCENTAGE * this.mDimensions[1] );
	
	this.mSearchHeaderContainer.style.cssFloat = 'left';
	this.mSearchHeaderContainer.style.styleFloat = 'left';
	this.mSearchHeaderContainer.style.width = innerElementWidth - 10 + 'px';
	this.mSearchHeaderContainer.style.marginBottom = 20 + 'px';

    this.mMediaCollectionsContainer.style.width = innerElementWidth + 'px';
    this.mMediaCollectionsContainer.style.overflow = 'auto';
	this.mMediaCollectionsContainer.style.cssFloat = 'left';
	this.mMediaCollectionsContainer.style.styleFloat = 'left';
	this.mMediaCollectionsContainer.style.marginLeft = 12 + 'px';
	
	this.mSelectAllMediaCollectionsButton.style.cssFloat = 'left';
	this.mSelectAllMediaCollectionsButton.style.styleFloat = 'left';
	this.mSelectAllMediaCollectionsButton.style.cursor = 'pointer';
	this.mClearMediaCollectionSelectionButton.style.cssFloat = 'right';
	this.mClearMediaCollectionSelectionButton.style.styleFloat = 'right';
	this.mClearMediaCollectionSelectionButton.style.marginRight = 5 + 'px';
	this.mClearMediaCollectionSelectionButton.style.cursor = 'pointer';
	this.mMediaCollectionsText.style.cssFloat = 'left';
	this.mMediaCollectionsText.style.styleFloat = 'left';
	this.mMediaCollectionsText.style.marginLeft = '20px';
	
	this.mSearchSubmitContainer.style.width = innerElementWidth + 'px';
    this.mSearchSubmitContainer.style.overflow = 'hidden';
	this.mSearchSubmitContainer.style.cssFloat = 'left';
	this.mSearchSubmitContainer.style.styleFloat = 'left';

    this.mMediaCollectionsListContainer.style.width = ( innerElementWidth - 20 ) + 'px';
    this.mMediaCollectionsListContainer.style.overflow = 'hidden';
    this.mMediaCollectionsListContainer.style.display = 'none';
	this.mMediaCollectionsListContainer.style.cssFloat = 'left';
	this.mMediaCollectionsListContainer.style.styleFloat = 'left';

    this.mSearchOptionsContainer.style.width = innerElementWidth + 'px';
	
    var offset = [ 13, 0 ];
    var borderOffset = jshBorderOffset( this.mNewSearchTermButton, [0,0] );
    this.mSearchExactTermsContainer.style.width = innerElementWidth - 40 + 'px';
	this.mSearchExactTermsContainer.style.cssFloat = 'left';
	this.mSearchExactTermsContainer.style.styleFloat = 'left';
    this.mSearchExactTermsContainer.style.left = ( offset[0] ) + 'px';
    
    this.mSearchAllTermsContainer.style.width = innerElementWidth - 40 + 'px';
	this.mSearchAllTermsContainer.style.cssFloat = 'left';
	this.mSearchAllTermsContainer.style.styleFloat = 'left';
    this.mSearchAllTermsContainer.style.left = ( offset[0] ) + 'px';
    
    this.mSearchAnyTermsContainer.style.width = innerElementWidth - 40 + 'px';
	this.mSearchAnyTermsContainer.style.cssFloat = 'left';
	this.mSearchAnyTermsContainer.style.styleFloat = 'left';
    this.mSearchAnyTermsContainer.style.left = ( offset[0] ) + 'px';
	
	this.mQueryDisplayContainer.style.width = (innerElementWidth - 20) + 'px';	
    this.mQueryStringContainer.style.width = innerElementWidth + 'px';
    this.mQueryStringContainer.style.left = ( offset[0] ) + 'px';
	this.mQueryStringContainer.style.textAlign = 'left';
	this.mQueryStringContainer.style.cssFloat = 'left';
	this.mQueryStringContainer.style.styleFloat = 'left';
	this.mQueryStringContainer.style.width = '89%';
	
	this.mClearSearchButtonContainer.style.width = '10%';

	this.mSubmitSearchButton.style.cssFloat = 'right';
	this.mSubmitSearchButton.style.styleFloat = 'right';
    this.mSubmitSearchButton.style.textDecoration = 'none';
	
    this.mNewSearchAllTermButton.style.display = 'block';
    this.mNewSearchAllTermButton.style.width = ( this.REMOVE_BUTTON_WIDTH ) + 'px';
    this.mNewSearchAllTermButton.style.height = ( this.SEARCH_TERM_MIN_HEIGHT ) + 'px';
    this.mNewSearchAllTermButton.style.height = ( this.SEARCH_TERM_MIN_HEIGHT ) + 'px';
    this.mNewSearchAllTermButton.style.marginLeft = 5 + 'px';
    this.mNewSearchAnyTermButton.style.width = ( this.REMOVE_BUTTON_WIDTH ) + 'px';
    this.mNewSearchAnyTermButton.style.height = ( this.SEARCH_TERM_MIN_HEIGHT ) + 'px';
    this.mNewSearchAnyTermButton.style.marginLeft = 5 + 'px';
	this.mNewSearchExactTermButton.style.display = 'block';
    this.mNewSearchExactTermButton.style.width = ( this.REMOVE_BUTTON_WIDTH ) + 'px';
    this.mNewSearchExactTermButton.style.height = ( this.SEARCH_TERM_MIN_HEIGHT ) + 'px';
    this.mNewSearchExactTermButton.style.marginLeft = 5 + 'px';

    this.constructMediaCollectionContainer();
	this.toggleMediaCollections();
    this.constructQueryStringContainer();
	
    this.appendSearchAllTerm();
    this.appendSearchAnyTerm();
    this.appendSearchExactTerm();
	
	this.mSearchValuesContainer.style.clear = 'none';
	
    this.constructSearchValues();

    this.mPanel.hide();
    this.mIsHidden = true;

    // bring back visibility, now that we are all preped and hidden
    this.mPanel.style.visibility = '';

  };

  this.onEnterSubmit = function(e){ 
    e = e?e:window.event;
    if(e.KeyCode == 13)
	{
      this.submitSearch();
    }
  };

  this.constructSearchValues = function()
  {
    //this.mSearchStartDate.style.width = ( this.SEARCH_DATE_INPUT_WIDTH ) + 'px';
    //this.mSearchEndDate.style.width = ( this.SEARCH_DATE_INPUT_WIDTH ) + 'px';
    //this.mSearchDateContainer.hide();
	this.mSearchValueFillerContainer.appendChild(document.createTextNode("\u00a0"));
	
	var offset = Position.positionedOffset( this.mSearchExactTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX] );
    this.mSearchValueFillerContainer.style.width = '387px';
    this.mSearchValueFillerContainer.style.clear = 'left';	
	this.mSearchValueFillerContainer.style.cssFloat = 'left';
	this.mSearchValueFillerContainer.style.styleFloat = 'left';
	
	
    this.mSearchValueContainer.style.overflow = 'auto';
    this.mSearchValueContainer.style.width = ( this.SEARCH_VALUE_INPUT_WIDTH ) + 'px';
    this.mSearchValueContainer.style.height = 150 + 'px';
	
	offset = Position.positionedOffset( this.mSearchExactTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX] );
    offset[1] += this.mSearchExactTermsContainer.getHeight();

	this.mSearchValueContainer.style.cssFloat = 'left';
	this.mSearchValueContainer.style.styleFloat = 'left';

    // events
    //var ams = this;
    //this.mSearchValueInput.advanceMediaSearch = this;
	//this.mCurrentSearchOption.advanceMediaSearch = this;
    //this.mAddSearchValueButton.onclick = function(){ ams.mAddSearchValueButton.blur(); ams.addSearchValueToCurrentSearchOption( ams.mSearchValueInput.value ); ams.mSearchValueInput.value = ''; };
    //this.mAddSearchDateValueButton.onclick = function(){ ams.mAddSearchDateValueButton.blur(); ams.addSearchValueToCurrentSearchOption( ams.mSearchStartDate.value + '...' + ams.mSearchEndDate.value ); ams.mSearchStartDate.value = ''; ams.mSearchEndDate.value = ''; };
    //this.mListAllButton.onclick = function(){ ams.mListAllButton.blur(); ams.addSearchValueToCurrentSearchOption( '' ); ams.mSearchValueInput.value = '%'; ams.requestSuggestions( '%' );};

    // start out with suggestions on
    this.setSuggestionMode( true );
  };

  this.constructQueryStringContainer = function()
  {
	var container = $( document.createElement( 'p' ) );
    this.mQueryStringContainer.appendChild( container );
  };
  
  this.updateQueryStringContainer = function()
  {
	this.mQueryStringContainer.update( this.generateQueryString(false) );
  };
  
  this.constructMediaCollectionContainer = function()
  {
    this.mMediaCollectionsHeaderContainer.appendChild( this.mToggleMediaCollectionsButton );
	this.mMediaCollectionsHeaderContainer.appendChild( this.mSelectAllMediaCollectionsButton );
    this.mMediaCollectionsHeaderContainer.appendChild( this.mMediaCollectionsText );
    this.mMediaCollectionsHeaderContainer.appendChild( this.mClearMediaCollectionSelectionButton );
		
    this.mMediaCollectionsContainer.appendChild( this.mMediaCollectionsHeaderContainer );
    this.mMediaCollectionsContainer.appendChild( this.mMediaCollectionsListContainer );
	
	var ams = this;
	this.mSelectAllMediaCollectionsButton.onclick = function(){ams.selectAllMediaCollections(); ams.updateMediaCollectionsUI(); ams.requestMediaFields(); ams.clearAllSearchFields(); ams.updateQueryStringContainer();};
	this.mClearMediaCollectionSelectionButton.onclick = function(){ams.clearAllMediaCollections(); ams.updateMediaCollectionsUI(); ams.requestMediaFields(); ams.clearAllSearchFields(); ams.updateQueryStringContainer();};
	
    if( this.mMediaCollections )
    {
      var mc = null;
      var container = null;
      var checkbox = null;
      var cbLabel = null;
      for( var i = 0; i < this.mMediaCollections.length; i++ )
      {
        mc = this.mMediaCollections[i];
        container = $( document.createElement( 'div' ) );
        checkbox = $( document.createElement( 'input' ) );
        cbLabel = $( document.createElement( 'label' ) );

        checkbox.type = 'checkbox';
        checkbox.id = mc.abbreviatedName;
        checkbox.name = mc.abbreviatedName;
        checkbox.value = mc.abbreviatedName;

        var ams = this;
        checkbox.onclick = function(){ ams.updateMediaCollectionsUI(); ams.requestMediaFields(); ams.updateQueryStringContainer();};
		
        cbLabel.htmlFor = mc.abbreviatedName;

        var collNameToUse = mc.collectionName;
        if( mc.collectionName.length > this.COLLECTION_NAME_LENGTH )
        {
          collNameToUse = mc.collectionName.truncate( this.COLLECTION_NAME_LENGTH, '...' );
        }
        cbLabel.update( collNameToUse);

        container.appendChild( checkbox );
        container.appendChild( cbLabel );
        this.mMediaCollectionsListContainer.appendChild( container );
      }
    }
  };
  
  this.selectAllMediaCollections = function()
  {
  	var numCollections = this.mMediaCollectionsContainer.childNodes[1].childNodes.length;
	for( var i = 0; i < numCollections; i++ )
	{
	  this.mMediaCollectionsContainer.childNodes[1].childNodes[i].childNodes[0].checked = true;
	}
  };
  
  this.clearAllMediaCollections = function()
  {
  	var numCollections = this.mMediaCollectionsContainer.childNodes[1].childNodes.length;
	for( var i = 0; i < numCollections; i++ )
	{
	  this.mMediaCollectionsContainer.childNodes[1].childNodes[i].childNodes[0].checked = false;
	}
  };

  this.showTooltip = function (e)
  {
    if(window.event) // IE check
      el = window.event.srcElement;
    if(e && e.target) // standard-compliant browsers
      el = e.target;	
	
    var parent = el.parentNode || el.parentElement;
    offset = Position.positionedOffset(parent);
    posx = offset[0];
    posy = offset[1];
    parent.childNodes[6].style.left = posx + 10 + 'px'; 
	parent.childNodes[6].style.top = posy + 10 + 'px';
    parent.childNodes[6].style.display = 'block';
	
  };
  
  this.hideTooltip = function (e){
    if(window.event) // IE check
      el = window.event.srcElement;
    if(e && e.target) // standard-compliant browsers
      el = e.target
    var parent = el.parentNode || el.parentElement;  
    parent.childNodes[6].style.display = 'none';
  };
  
  this.appendSearchAllTerm = function()
  {
    var container = $( document.createElement( 'div' ) );
	var termCondition = this.constructTermConditionsSelect();
    var mediaField = this.constructMediaFieldsSelect();
    var operators = this.constructMediaSearchOperatorsSelect();
	var searchAllTermInput = $( document.createElement( 'input' ) );
	var searchInText = $( document.createElement( 'p' ) );
	var searchAllTermText = $( document.createElement( 'p' ) );
	var searchAllHint = $( document.createElement( 'a' ) );	
	var tooltip = this.constructToolTip('Use this search box to find results that contain all of the words you have typed in. <br><br>Wildcard searching: Use an asterisk in place of many letters. Use a question mark in place of a single letter. <br><br>Select a single collection for the list of fields unique to that collection. Select more than one collection for list of categories to choose from.');
	
	searchInText.style.paddingLeft = 10 + 'px';
	searchInText.style.paddingRight = 10 + 'px';
	
	searchAllHint.style.paddingRight = 10 + 'px';
	searchAllHint.style.width = 20 + 'px';
	searchAllHint.onmouseover=this.showTooltip;
    searchAllHint.onmouseout=this.hideTooltip;
	searchAllHint.style.textDecoration = 'none';
	searchAllHint.style.cursor = 'pointer';
	
	searchAllTermText.style.width = 150 + 'px';
    searchAllTermText.style.overflow = 'hidden';
	searchAllTermText.style.cssFloat = 'left';
	searchAllTermText.style.paddingLeft = 10 + 'px';
	searchAllTermText.update( this.SEARCH_ALL_TERM_TEXT );
	  
	searchAllHint.addClassName( this.TOOL_TIP_CLASSNAME );
	//searchAllHint.href( '#' );
	
	searchAllTermInput.addClassName( this.NEW_SEARCH_ALL_TERM_INPUT_CLASSNAME );
    searchAllTermInput.type = 'text';
	
	searchInText.update( this.SEARCH_IN_TEXT );
	searchAllHint.update( this.SEARCH_HINT_TEXT );
	
	container.appendChild( searchAllHint );
    container.appendChild( termCondition );
	container.appendChild( searchInText );
    container.appendChild( mediaField );
	container.appendChild( searchAllTermText );
	container.appendChild( searchAllTermInput );
	container.appendChild( tooltip );
	container.appendChild( this.mNewSearchAllTermButton );
	
	var len = this.mSearchAllTermsContainer.childNodes.length;

	if(len>0)
	{
	    searchAllHint.update( '&nbsp;' );
	    var removeButton = $( document.createElement( 'a' ) );	
	    removeButton.href = 'javascript: var aButton;';
	    removeButton.title = this.REMOVE_SEARCH_TERM_INSTRCUTION;
	    removeButton.addClassName( this.REMOVE_BUTTON_CLASSNAME );
    	removeButton.style.cssFloat = 'left';
		removeButton.style.styleFloat = 'left';
	    removeButton.mRemovalElement = this.mSearchAllTermsContainer.childNodes[len-1];
	    removeButton.onclick = function(){ ams.removeSearchAllTerm( removeButton.mRemovalElement ); ams.resetSearchHint(); ams.updateQueryStringContainer();};	
		this.mSearchAllTermsContainer.childNodes[len-1].appendChild( removeButton );										
	}	
    
    // add search term
    this.mSearchAllTermsContainer.appendChild( container );

    var borderOffset = jshBorderOffset( this.mNewSearchAllTermButton, [0,0] );
    container.addClassName( this.SEARCH_ALL_TERM_CONTAINER_CLASSNAME );
    container.style.overflow = 'hidden';
    container.style.width = '100%';
    container.style.height = this.SEARCH_TERM_MIN_HEIGHT + 'px';
	
    // need to float all so they are all on one line
    for( var i = 0; i <  container.childNodes.length; i++ )
    {
      if( container.childNodes[i] != removeButton )
      {
        container.childNodes[i].style.cssFloat = 'left';
        container.childNodes[i].style.styleFloat = 'left';
      }
	  if( i == this.SEARCH_TERM_CONDITION_ELEMENT_INDEX )
	  {
	  	container.childNodes[i].style.width = this.SEARCH_TERM_CONDITION_WIDTH + 'px';
	  }
	  if( i == this.SEARCH_TERM_INPUT_INDEX )
	  {
	  	container.childNodes[i].style.width = this.SEARCH_INPUT_WIDTH + 'px';
	  }
    }

    // events
    var ams = this;

    mediaField.onchange = function(){ ams.clearSearchTermValues( searchAllTermInput ); ams.updateSearchAllTermUI(); ams.updateQueryStringContainer();};
    termCondition.onchange = function(){ ams.updateTermCondition( termCondition.selectedIndex ); ams.updateQueryStringContainer();};
	searchAllTermInput.onkeyup = function(){ams.updateQueryStringContainer();};
	
    // update buttons
    this.updateSearchAllTermUI( container );
  };
  
  this.appendSearchExactTerm = function()
  {
    var container = $( document.createElement( 'div' ) );
	var termCondition = this.constructTermConditionsSelect();
    var mediaField = this.constructMediaFieldsSelect();
    var operators = this.constructMediaSearchOperatorsSelect();
	var searchExactTermInput = $( document.createElement( 'input' ) );
	var searchInText = $( document.createElement( 'p' ) );
	var searchExactTermText = $( document.createElement( 'p' ) );
	var searchExactHint = $( document.createElement( 'a' ) );
	var tooltip = this.constructToolTip('Use this search box to find results that contain the exact word or phrase you are looking for.  As you begin typing, a list of values will display. Continue typing to narrow down the list of available values.  Click on the \'+\' sign to enter a new value.<br><br>Select a single collection for the list of fields unique to that collection. Select more than one collection for list of categories to choose from.');
	
	searchInText.style.paddingLeft = 10 + 'px';
	searchInText.style.paddingRight = 10 + 'px';
	
	searchExactHint.style.paddingRight = 10 + 'px';
	searchExactHint.style.width = 20 + 'px';
	searchExactHint.style.textDecoration = 'none';
	searchExactHint.style.cursor = 'pointer';
	
	searchExactHint.onmouseover=this.showTooltip;
    searchExactHint.onmouseout=this.hideTooltip;
	
	searchExactTermText.style.width = 150 + 'px';
    searchExactTermText.style.overflow = 'hidden';
	searchExactTermText.style.cssFloat = 'left';
	searchExactTermText.style.paddingLeft = 10 + 'px';
	searchExactTermText.update( this.SEARCH_EXACT_TERM_TEXT );

	searchExactTermInput.addClassName( this.NEW_SEARCH_EXACT_TERM_INPUT_CLASSNAME );
    searchExactTermInput.type = 'text';
		
	searchInText.update( this.SEARCH_IN_TEXT );
	searchExactHint.update( this.SEARCH_HINT_TEXT );
	
	container.appendChild( searchExactHint );
    container.appendChild( termCondition );
    container.appendChild( searchInText );
    container.appendChild( mediaField );
    container.appendChild( searchExactTermText );
    container.appendChild( searchExactTermInput );
	container.appendChild( tooltip );
	container.appendChild( this.mNewSearchExactTermButton );
	container.appendChild( this.mSearchValuesContainer );
	
	//first add the remove button to the last field
	var len = this.mSearchExactTermsContainer.childNodes.length;	
	if(len>0)
	{
		searchExactHint.update( '&nbsp;' );
	    var removeButton = $( document.createElement( 'a' ) );	
	    removeButton.href = 'javascript: var aButton;';
	    removeButton.title = this.REMOVE_SEARCH_TERM_INSTRCUTION;
	    removeButton.addClassName( this.REMOVE_BUTTON_CLASSNAME );
    	removeButton.style.cssFloat = 'left';
		removeButton.style.styleFloat = 'left';
	    removeButton.mRemovalElement = this.mSearchExactTermsContainer.childNodes[len-1];
	    removeButton.onclick = function(){ ams.removeSearchExactTerm( removeButton.mRemovalElement ); ams.resetSearchHint(); ams.updateQueryStringContainer();};	
		this.mSearchExactTermsContainer.childNodes[len-1].appendChild( removeButton );
		// remove onchange event on the last field
		this.mSearchExactTermsContainer.childNodes[len-1].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].onchange = '';
	}
	
    // add search term
    this.mSearchExactTermsContainer.appendChild( container );
	
	this.mCurrentSearchOption = container;

    var borderOffset = jshBorderOffset( this.mNewSearchExactTermButton, [0,0] );
    container.addClassName( this.SEARCH_EXACT_TERM_CONTAINER_CLASSNAME );
    container.style.overflow = 'hidden';
    container.style.width = '100%';
    container.style.height = '99%'; 
	
    // need to float all so they are all on one line
    for( var i = 0; i <  container.childNodes.length; i++ )
    {
      if(( container.childNodes[i] != removeButton ) && ( container.childNodes[i] != this.mSearchValuesContainer ))
      {
        container.childNodes[i].style.cssFloat = 'left';
        container.childNodes[i].style.styleFloat = 'left';
      }
	  if( i == this.SEARCH_TERM_CONDITION_ELEMENT_INDEX )
	  {
	  	container.childNodes[i].style.width = this.SEARCH_TERM_CONDITION_WIDTH + 'px';
	  }
	  if( i == this.SEARCH_TERM_INPUT_INDEX )
	  {
	  	container.childNodes[i].style.width = this.SEARCH_INPUT_WIDTH + 'px';
	  }
    }

    // events
    var ams = this;
    mediaField.onchange = function(){ ams.clearSearchValues(); ams.clearSearchTermValues( searchExactTermInput ); ams.updateSearchValuesUI(); ams.updateQueryStringContainer();};
    termCondition.onchange = function(){ ams.updateTermCondition( termCondition.selectedIndex ); ams.updateQueryStringContainer();};
	searchExactTermInput.onkeyup = function(){ams.updateQueryStringContainer();};
	
    // update buttons
    this.updateSearchExactTermUI( container );
  };
  
  this.appendSearchAnyTerm = function()
  {
    var container = $( document.createElement( 'div' ) );
	var termCondition = this.constructTermConditionsSelect();
    var mediaField = this.constructMediaFieldsSelect();
    var operators = this.constructMediaSearchOperatorsSelect();
	var searchAnyTermInput = $( document.createElement( 'input' ) );
	var searchInText = $( document.createElement( 'p' ) );
	var searchAnyTermText = $( document.createElement( 'p' ) );
	var searchAnyHint = $( document.createElement( 'a' ) );
	var tooltip = this.constructToolTip('Use this search box to find results that contain at least one of the words you have typed in.<br><br>Wildcard searching: Use an asterisk in place of many letters. Use a question mark in place of a single letter.<br><br>Select a single collection for the list of fields unique to that collection. Select more than one collection for list of categories to choose from.');
	
	searchInText.style.paddingLeft = 10 + 'px';
	searchInText.style.paddingRight = 10 + 'px';
	
	searchAnyHint.style.paddingRight = 10 + 'px';
	searchAnyHint.style.width = 20 + 'px';
	searchAnyHint.style.textDecoration = 'none';
	searchAnyHint.style.cursor = 'pointer';
	
	searchAnyHint.onmouseover = this.showTooltip;
	searchAnyHint.onmouseout = this.hideTooltip;
	
	searchAnyTermText.style.width = 150 + 'px';
    searchAnyTermText.style.overflow = 'hidden';
	searchAnyTermText.style.cssFloat = 'left';
	searchAnyTermText.style.paddingLeft = 10 + 'px';
	searchAnyTermText.update( this.SEARCH_ANY_TERM_TEXT );

	searchAnyTermInput.addClassName( this.NEW_SEARCH_ANY_TERM_INPUT_CLASSNAME );
    searchAnyTermInput.type = 'text';
	
	searchInText.update( this.SEARCH_IN_TEXT );
	searchAnyHint.update( this.SEARCH_HINT_TEXT );
	
	container.appendChild( searchAnyHint );
    container.appendChild( termCondition );
    container.appendChild( searchInText );
    container.appendChild( mediaField );
    container.appendChild( searchAnyTermText );
    container.appendChild( searchAnyTermInput );
    //container.appendChild( termContainer );
	container.appendChild( tooltip );
	container.appendChild( this.mNewSearchAnyTermButton );
	
	var len = this.mSearchAnyTermsContainer.childNodes.length;	
	if(len>0)
	{
		searchAnyHint.update( '&nbsp;' );
	    var removeButton = $( document.createElement( 'a' ) );	
	    removeButton.href = 'javascript: var aButton;';
	    removeButton.title = this.REMOVE_SEARCH_TERM_INSTRCUTION;
	    removeButton.addClassName( this.REMOVE_BUTTON_CLASSNAME );
    	removeButton.style.cssFloat = 'left';
		removeButton.style.styleFloat = 'left';
	    removeButton.mRemovalElement = this.mSearchAnyTermsContainer.childNodes[len-1];
	    removeButton.onclick = function(){ ams.removeSearchAnyTerm( removeButton.mRemovalElement ); ams.resetSearchHint(); ams.updateQueryStringContainer();};	
		this.mSearchAnyTermsContainer.childNodes[len-1].appendChild( removeButton );										
	}
	
    // add search term
    this.mSearchAnyTermsContainer.appendChild( container );

    var borderOffset = jshBorderOffset( this.mNewSearchAnyTermButton, [0,0] );
    container.addClassName( this.SEARCH_ANY_TERM_CONTAINER_CLASSNAME );
    container.style.overflow = 'hidden';
    container.style.width = '100%';
    container.style.height = this.SEARCH_TERM_MIN_HEIGHT + 'px';
	
    // need to float all so they are all on one line
    for( var i = 0; i <  container.childNodes.length; i++ )
    {
      if( container.childNodes[i] != removeButton )
      {
        container.childNodes[i].style.cssFloat = 'left';
        container.childNodes[i].style.styleFloat = 'left';
      }
	  if( i == this.SEARCH_TERM_CONDITION_ELEMENT_INDEX )
	  {
	  	container.childNodes[i].style.width = this.SEARCH_TERM_CONDITION_WIDTH + 'px';
	  }
	  if( i == this.SEARCH_TERM_INPUT_INDEX )
	  {
	  	container.childNodes[i].style.width = this.SEARCH_INPUT_WIDTH + 'px';
	  }
    }

    // events
    var ams = this;
    mediaField.onchange = function(){ ams.clearSearchValues(); ams.clearSearchTermValues( searchAnyTermInput ); /*ams.updateSearchTermUI(); */
	ams.updateSearchValuesUI();ams.updateQueryStringContainer(); };
    termCondition.onchange = function(){ ams.updateTermCondition( termCondition.selectedIndex );ams.updateQueryStringContainer(); };
    searchAnyTermInput.onkeyup = function(){ams.updateQueryStringContainer();};
	
    // update buttons
    this.updateSearchAnyTermUI( container );
  };


  this.toggleTermCondition = function()
  {
    if( this.mCurrentTermCondition )
    {
      var index = this.mTermConditions.indexOf( this.mCurrentTermCondition );
      index++;
      this.mCurrentTermCondition = this.mTermConditions[ index % this.mTermConditions.length ];
    }
    else
    {
      this.mCurrentTermCondition = this.mTermConditions[0];
    }

    this.updateSearchAllTermUI();
    this.updateSearchAnyTermUI();
    this.updateSearchExactTermUI();
  };
  
  this.updateTermCondition = function( selectedIndex )
  {
    if( selectedIndex >= 0 )
    {
      this.mCurrentTermCondition = this.mTermConditions[selectedIndex];
    }

    this.updateSearchAllTermUI();
    this.updateSearchAnyTermUI();
    this.updateSearchExactTermUI();
  };

	
  this.updateSearchAllTermUI = function( newCurrentSearchOption )
  {
    // update term condition ( and/or ) for all select lists
    if( this.mCurrentTermCondition )
    {
      for( var i = 0; i < this.mSearchAllTermsContainer.childNodes.length; i++ )
      {
	  	if( this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_CONDITION_ELEMENT_INDEX].options )
	  	  this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_CONDITION_ELEMENT_INDEX].options[this.mTermConditions.indexOf(this.mCurrentTermCondition)].selected = "true";
      }
	  var termConditionHolder = $( document.createElement( 'div' ) );
	  termConditionHolder.update('&nbsp;');
	  termConditionHolder.style.width = this.SEARCH_TERM_CONDITION_WIDTH + 'px';
  	  termConditionHolder.style.cssFloat = 'left';
  	  termConditionHolder.style.styleFloat = 'left';
	  this.mSearchAllTermsContainer.childNodes[0].replaceChild(termConditionHolder, this.mSearchAllTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_CONDITION_ELEMENT_INDEX]);
    }
	
	var currentSelectWidth = this.mSearchAllTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].getWidth();
	var currentInputWidth = this.mSearchAllTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_INPUT_INDEX].getWidth();
    	  
	if (/msie|MSIE 6/.test(navigator.userAgent)) //fix the input box width for IE6
	{
		for (var i = 0; i < this.mSearchAllTermsContainer.childNodes.length; i++) 
		{
			if (this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
				this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = 250 + 'px';
		}
	}
	else {
		if ((currentSelectWidth + currentInputWidth) > this.SEARCH_COMBINED_WIDTH) {
			for (var i = 0; i < this.mSearchAllTermsContainer.childNodes.length; i++) {
				if (this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
					this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = currentInputWidth - ((currentSelectWidth + currentInputWidth) - this.SEARCH_COMBINED_WIDTH) + 'px';
			}
		}
		else 
			if (currentSelectWidth == 0 || currentInputWidth == 0) {
				for (var i = 0; i < this.mSearchAllTermsContainer.childNodes.length; i++) {
					if (this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
						this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = this.SEARCH_INPUT_WIDTH + 'px';
				}
			}
			else {
				for (var i = 0; i < this.mSearchAllTermsContainer.childNodes.length; i++) {
					if (this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
						this.mSearchAllTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = currentInputWidth + (this.SEARCH_COMBINED_WIDTH - (currentSelectWidth + currentInputWidth)) + 'px';
				}
			}
	}
	
  };

  this.updateSearchAnyTermUI = function( newCurrentSearchOption )
  {
    // update term condition ( and/or ) for all select lists
    if( this.mCurrentTermCondition )
    {
      for( var i = 0; i < this.mSearchAnyTermsContainer.childNodes.length; i++ )
      {
	  	if( this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_CONDITION_ELEMENT_INDEX].options )
	  	  this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_CONDITION_ELEMENT_INDEX].options[this.mTermConditions.indexOf(this.mCurrentTermCondition)].selected = "true";
      }
    }
	
	var currentSelectWidth = this.mSearchAnyTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].getWidth();
	var currentInputWidth = this.mSearchAnyTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_INPUT_INDEX].getWidth();
    
	if (/msie|MSIE 6/.test(navigator.userAgent)) //fix the input box width for IE6
	{
		for (var i = 0; i < this.mSearchAnyTermsContainer.childNodes.length; i++) 
		{
			if (this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
				this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = 250 + 'px';
		}
	}
	else {
		if ((currentSelectWidth + currentInputWidth) > this.SEARCH_COMBINED_WIDTH) {
			for (var i = 0; i < this.mSearchAnyTermsContainer.childNodes.length; i++) {
				if (this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
					this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = currentInputWidth - ((currentSelectWidth + currentInputWidth) - this.SEARCH_COMBINED_WIDTH) + 'px';
			}
		}
		else 
			if (currentSelectWidth == 0 || currentInputWidth == 0) {
				for (var i = 0; i < this.mSearchAnyTermsContainer.childNodes.length; i++) {
					if (this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
						this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = this.SEARCH_INPUT_WIDTH + 'px';
				}
			}
			else {
				for (var i = 0; i < this.mSearchAnyTermsContainer.childNodes.length; i++) {
					if (this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
						this.mSearchAnyTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = currentInputWidth + (this.SEARCH_COMBINED_WIDTH - (currentSelectWidth + currentInputWidth)) + 'px';
				}
			}
	}
  };

 
 
  this.updateSearchExactTermUI = function( newCurrentSearchOption )
  {
    // update term condition ( and/or ) for all select lists
    if( this.mCurrentTermCondition )
    {
      for( var i = 0; i < this.mSearchExactTermsContainer.childNodes.length; i++ )
      {
	  	if( this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_CONDITION_ELEMENT_INDEX].options )
	  	  this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_CONDITION_ELEMENT_INDEX].options[this.mTermConditions.indexOf(this.mCurrentTermCondition)].selected = "true";
      }
    }
	
	var currentSelectWidth = this.mSearchExactTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].getWidth();
	var currentInputWidth = this.mSearchExactTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_INPUT_INDEX].getWidth();
    
	if (/msie|MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{ 
      var ieversion= new Number(RegExp.$1); // capture x.x portion and store as a number
      if (ieversion < 8)
        for (var i = 0; i < this.mSearchExactTermsContainer.childNodes.length; i++) 
		{   // fix input box height for IE 6,7
			if ( i < this.mSearchExactTermsContainer.childNodes.length - 1 ) 
				this.mSearchExactTermsContainer.childNodes[i].style.height = this.SEARCH_TERM_MIN_HEIGHT + 'px';
		}
    }
	if (/msie|MSIE 6/.test(navigator.userAgent)) //fix the input box width for IE6
	{
		for (var i = 0; i < this.mSearchExactTermsContainer.childNodes.length; i++) 
		{
			if (this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
				this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = 250 + 'px';
		}
	}
	else {
		if ((currentSelectWidth + currentInputWidth) > this.SEARCH_COMBINED_WIDTH) {
			for (var i = 0; i < this.mSearchExactTermsContainer.childNodes.length; i++) {
				if (this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
					this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = currentInputWidth - ((currentSelectWidth + currentInputWidth) - this.SEARCH_COMBINED_WIDTH) + 'px';
			}
		}
		else 
			if (currentSelectWidth == 0 || currentInputWidth == 0) {
				for (var i = 0; i < this.mSearchExactTermsContainer.childNodes.length; i++) {
					if (this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
						this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = this.SEARCH_INPUT_WIDTH + 'px';
				}
			}
			else {
				for (var i = 0; i < this.mSearchExactTermsContainer.childNodes.length; i++) {
					if (this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX]) 
						this.mSearchExactTermsContainer.childNodes[i].childNodes[this.SEARCH_TERM_INPUT_INDEX].style.width = currentInputWidth + (this.SEARCH_COMBINED_WIDTH - (currentSelectWidth + currentInputWidth)) + 'px';
				}
			}
	}
  };
   
/*  
  this.updateSearchOperators = function()
  {
    var searchTerm;
    for( var i = 0; i < this.mSearchTermsContainer.childNodes.length; i++ )
    {
      searchTerm = this.mSearchTermsContainer.childNodes[i];
      this.constructMediaSearchOperatorsSelect( searchTerm.childNodes[this.SEARCH_TERM_OPERATOR_ELEMENT_INDEX], this.isMediaFieldDate( searchTerm ) );
    }
  }
*/

  this.updateSearchValuesUI = function()
  {
    if( this.mIsRequestSuggestionMode == false )
    {
      // is it a date?
      if( this.isMediaFieldDate( this.mCurrentSearchOption ) )
      {
        //this.mAdvanceSearchInstructionText.update( this.ADVANCE_SEARCH_DATE_VALUE_TEXT );
        //this.mSearchDateContainer.show();
        //this.mSearchValueInput.hide();
        //this.mAddSearchValueButton.hide();
        //this.mListAllButton.hide();
      }
      else
      {
        // events
        var ams = this;
        //this.mSearchValueInput.onkeydown = function(e){ var kc = jshGetEvent(e).keyCode; if( kc == 13 ){ ams.addSearchValueToCurrentSearchOption( ams.mSearchValueInput.value ); ams.mSearchValueInput.value = ''; } }
		this.mCurrentSearchOption.childNodes[ams.SEARCH_TERM_VALUE_ELEMENT_INDEX].onkeydown = function(e){ var kc = jshGetEvent(e).keyCode; if( kc == 13 ){ ams.addSearchValueToCurrentSearchOption( ams.mCurrentSearchOption.childNodes[ams.SEARCH_TERM_VALUE_ELEMENT_INDEX].value ); ams.mCurrentSearchOption.childNodes[ams.SEARCH_TERM_VALUE_ELEMENT_INDEX].value = ''; } }
      }
    }
    else
    {
      this.mAdvanceSearchInstructionText.update( this.ADVANCE_SEARCH_SUGGESTIONS_TEXT );
      //events
	  this.mCurrentSearchOption.childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].enhancedAdvancedSearch = this;
	  this.mCurrentSearchOption.childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].onkeydown = Static_EnhancedAdvancedSearchRequest_RequestSuggestions;
    }
  };



  this.updateMediaCollectionsUI = function()
  {
    // update the title
    var selectedCollections = this.getSelectedMediaCollections();
    this.mMediaCollectionsText.update( 'Limit search to the following Collections: Searching ' + selectedCollections.length + ' of ' + this.mMediaCollections.length );

  };



  this.updateDimensions = function()
  {
  	var combinedHeight = this.mSearchAllTermsContainer.getHeight() + this.mSearchAnyTermsContainer.getHeight() 
						+ this.mSearchExactTermsContainer.getHeight() + this.mMediaCollectionsContainer.getHeight();
    if( combinedHeight >= ( this.mPanel.getHeight() - 150 ) )
    {
      var height = ( combinedHeight + 150 )
      this.mPanel.style.height = height + 'px';
    }
  };

  this.removeSearchAllTerm = function( searchTerm )
  {
    if( searchTerm )
    {
      if( this.mSearchAllTermsContainer.childNodes.length > 1 )
      {
        this.mSearchAllTermsContainer.removeChild( searchTerm );

      }
    }
	// make sure doesn't display termCondition select in the 1st row
	this.updateSearchAllTermUI();
  };
  
  this.removeSearchAnyTerm = function( searchTerm )
  {
    if( searchTerm )
    {
      if( this.mSearchAnyTermsContainer.childNodes.length > 1 )
      {
        this.mSearchAnyTermsContainer.removeChild( searchTerm );
      }
    }
  };
  
  this.resetSearchHint = function()
  { // adding back the text
    this.mSearchAnyTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_HINT_ELEMENT_INDEX].update(this.SEARCH_HINT_TEXT);
    this.mSearchExactTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_HINT_ELEMENT_INDEX].update(this.SEARCH_HINT_TEXT);
    this.mSearchAllTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_HINT_ELEMENT_INDEX].update(this.SEARCH_HINT_TEXT);
  }
  
  this.removeSearchExactTerm = function( searchTerm )
  {
    if( searchTerm )
    {
      if( this.mSearchExactTermsContainer.childNodes.length > 1 )
      {
        this.mSearchExactTermsContainer.removeChild( searchTerm );
      }
    }
  };

  this.constructTermConditionsSelect = function( select )
  {
    if( ! select )
    {
      select = $( document.createElement( 'select' ) );
    }

    // clear any existing options
    select.options.length = 0;

    // default value
    //select.options[ 0 ] = new Option( this.SELECT_MEDIA_FIELD_DEFAULT_TEXT, this.SELECT_NO_VALUE );

    if( this.mTermConditions )
    {
      for( var i = 0; i < this.mTermConditions.length ; i++ )
      {
        select.options[ i ] = new Option( this.mTermConditions[ i ].displayName, this.mTermConditions[ i ].value );
        select.options[ i ].title = this.mTermConditions[ i ].displayName;
      }
    }

    return select;
  };
  
  this.constructMediaFieldsSelect = function( select )
  {	
    if( ! select )
    {
      select = $( document.createElement( 'select' ) );
    }

    // clear any existing options
    select.options.length = 0;

    // default value
    select.options[ 0 ] = new Option( this.SELECT_MEDIA_FIELD_DEFAULT_TEXT, this.SELECT_NO_VALUE );

    if( this.mMediaFields )
    {
      for( var i = 1; i <= this.mMediaFields.length ; i++ )
      {		  
        if( ! this.mMediaFields[ i - 1 ].fieldName )
          this.mMediaFields[ i - 1 ].fieldName = '';

        select.options[ i ] = new Option( this.mMediaFields[ i - 1 ].displayName, this.mMediaFields[ i - 1 ].fieldName );
        select.options[ i ].title = this.mMediaFields[ i - 1 ].displayName;

        // if we have more than one collection selected
        // we need to default to the last option ie All Fields
        //var selectCollections = this.getSelectedMediaCollections();
        //if( selectCollections && ( selectCollections.length > 1 ) && ( i == this.mMediaFields.length ) )
		if( i == this.mMediaFields.length ) 
        {
          select.options[ i ].selected = true;
        }
      }
    }
    return select;
  };

  this.constructMediaSearchOperatorsSelect = function( select, isDate )
  {
    if( ! select )
    {
      select = $( document.createElement( 'select' ) );
    }

    // clear any existing options
    select.options.length = 0;

    var operators = ( isDate == true )? this.mMediaSearchDateOperators : this.mMediaSearchOperators;
    if( operators )
    {
      for( var i = 0; i < operators.length ; i++ )
      {
        select.options[ i ] = new Option( operators[ i ].displayName, operators[ i ].value );
        select.options[ i ].title = operators[ i ].value;
      }
    }

    return select;
  };
  
  this.constructToolTip = function( tip )
  {    
	var tooltip = $( document.createElement( 'span' ) );
	var t = $( document.createElement( 'span' ) );
	var m = $( document.createElement( 'span' ) );
	var b = $( document.createElement( 'span' ) );

	tooltip.addClassName( this.TOOL_TIP_CLASSNAME );
	tooltip.id = this.TOOL_TIP_CLASSNAME;
	tooltip.style.position = 'absolute';
	tooltip.style.display = 'none';
	
    t.addClassName(this.TOOL_TIP_TOP_CLASSNAME);
    t.style.display = 'block';	
    m.addClassName(this.TOOL_TIP_MIDDLE_CLASSNAME);
    m.style.display = 'block';
    m.style.color = '#000000';
    m.style.textAlign = 'left';
	m.update( tip );
    b.addClassName(this.TOOL_TIP_BOTTOM_CLASSNAME);
    b.style.display = 'block';
	
	tooltip.appendChild(t);
	tooltip.appendChild(m);
	tooltip.appendChild(b);
	
	return tooltip;
  };


  this.render = function( container, reference )
  {
    if( container && reference )
    {
      this.mContainer = container;
      this.mPositionRefernceElement = reference;
      this.init();
    }
  };


  this.updatePosition = function()
  {
    if( this.mPanel && this.mPositionRefernceElement )
    {
      var referencePosition = Position.cumulativeOffset( this.mPositionRefernceElement );
      var windowSize = jshGetWindowSize();
      var newPosition = $( [ 0, 0 ] );
      newPosition[0] = windowSize[0] - ( referencePosition[0] + this.mPositionRefernceElement.getWidth() );
      newPosition[1] = referencePosition[1] + this.mPositionRefernceElement.getHeight();

      this.mPanel.style.right = newPosition[0] + 'px';
      this.mPanel.style.top = newPosition[1] + 'px';
    }
  };


  this.hide = function()
  {
    this.mIsHidden = true;
    if( this.mAnimate == true )
    {
      this.mPanel.style.overflowY = 'hidden';
      var ams = this;
      var postFunction = function(){ ams.mPanel.hide(); };
      ElementEffects.animateElementGrowth( this.mPanel, [ 0, 0 ], 55, .45, postFunction );
    }
    else
    {
      this.mPanel.hide();
    }
  };

  this.show = function()
  {
    this.mIsHidden = false;
    this.mPanel.show();
    this.updatePosition();

    if( this.mAnimate == true )
    {
      var ams = this;
      var postFunctionss = function(){ ams.mPanel.style.overflowY = 'hidden'; };
      this.mPanel.style.width = 0 + 'px';
      this.mPanel.style.height = 0 + 'px';
      ElementEffects.animateElementGrowth( this.mPanel, this.mDimensions, 55, .45, postFunctionss );
    }
  };

  this.toggle = function()
  {
    if( this.mIsHidden == true )
    {
      this.show();
      this.initiateAdvancedSearchHelp();
      this.showHelpTipPopUp();
	  this.updateSearchAllTermUI();
	  this.updateSearchAnyTermUI();
	  this.updateSearchExactTermUI();
    }
    else
    {
      this.hide();
      this.closeAdvancedSearchHelp();
    }
  };


  this.toggleMediaCollections = function()
  {
    if( this.mMediaCollectionsListContainer.visible() )
    {
      this.mToggleMediaCollectionsButton.className = '';
      this.mMediaCollectionsListContainer.hide();
    }
    else
    {
      this.mToggleMediaCollectionsButton.className = this.EXPANDED_CLASSNAME;
      this.mMediaCollectionsListContainer.show();
    }
  };


  this.setSort = function( sort )
  {
    this.mSort = sort;
  };
  


  this.getSort = function()
  {
    return this.mSort
  };
  

  this.setSearchUrl = function( searchUrl )
  {
    this.mSearchUrl = searchUrl;
  };

  this.setAlternateSearchEnabledFunction = function( alternateSearchEnabledFunction )
  {
    this.mAlternateSearchEnabledFunction = alternateSearchEnabledFunction;
  };

  this.setMediaCollections = function( mediaCollections, selectedMediaCollections )
  {
    this.mMediaCollections = mediaCollections;
  };

  this.setSelectedCollections = function( selectedMediaCollection )
  {
    if( selectedMediaCollection )
    {
      var checkboxes = this.mMediaCollectionsListContainer.getElementsBySelector( 'input' );
      for( var j = 0; j < selectedMediaCollection.length; j++ )
      {
        for( var i = 0; i < checkboxes.length; i++ )
        {
          if( checkboxes[i] && checkboxes[i].value == selectedMediaCollection[j].abbreviatedName )
          {
            checkboxes[i].checked = true;
            break;
          }
        }
      }

      this.requestMediaFields();
      this.updateMediaCollectionsUI();
    }
  };

  this.setRequestMediaFieldsFunction = function( requestMediaFieldsFunction )
  {
    this.mRequestMediaFieldsFunction = requestMediaFieldsFunction;
  };

  this.requestMediaFields = function()
  {
    if( this.mRequestMediaFieldsFunction )
    {
      var selectedCollections = this.getSelectedMediaCollections();
      this.mRequestMediaFieldsFunction( selectedCollections );
    }
	return;
  };


  this.setMediaFields = function( mediaFields )
  {
    this.mMediaFields = mediaFields;

    // update all existing terms with the new media fields.
    var searchExactTermOptions = this.mSearchExactTermsContainer.childNodes;
	var searchAllTermOptions = this.mSearchAllTermsContainer.childNodes;
	var searchAnyTermOptions = this.mSearchAnyTermsContainer.childNodes;
	
	var maxFieldLength = 0;
	
	if( this.mMediaFields )
	{
	  for ( var i = 1; i <= this.mMediaFields.length; i++)
	    if ( this.mMediaFields[i-1].displayName.length > maxFieldLength )
		  maxFieldLength = this.mMediaFields[i-1].displayName.length;
	}

	this.mMaxSelectFieldLength = maxFieldLength;
	
	// update the dropdown menus for all searches
    for( var i = 0; i < searchExactTermOptions.length; i++ )
    {
      if( searchExactTermOptions[i] && ( searchExactTermOptions[i] != this.mNewSearchTermButton ) )
      {
        this.constructMediaFieldsSelect( searchExactTermOptions[i].childNodes[ this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX ] );
        this.clearSearchTermValues( searchExactTermOptions[i].childNodes[ this.SEARCH_TERM_VALUE_ELEMENT_INDEX ] );
      }
    }
	this.updateSearchExactTermUI();
	
    for( var i = 0; i < searchAllTermOptions.length; i++ )
    {
      if( searchAllTermOptions[i] && ( searchAllTermOptions[i] != this.mNewSearchTermButton ) )
      {
        this.constructMediaFieldsSelect( searchAllTermOptions[i].childNodes[ this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX ] );
        this.clearSearchTermValues( searchAllTermOptions[i].childNodes[ this.SEARCH_TERM_VALUE_ELEMENT_INDEX ] );
      }
    }
	this.updateSearchAllTermUI();
	
    for( var i = 0; i < searchAnyTermOptions.length; i++ )
    {
      if( searchAnyTermOptions[i] && ( searchAnyTermOptions[i] != this.mNewSearchTermButton ) )
      {
        this.constructMediaFieldsSelect( searchAnyTermOptions[i].childNodes[ this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX ] );
        this.clearSearchTermValues( searchAnyTermOptions[i].childNodes[ this.SEARCH_TERM_VALUE_ELEMENT_INDEX ] );
      }
    }
	this.updateSearchAnyTermUI();
	
	// move the values container as necessary
	var offset = Position.positionedOffset( this.mSearchExactTermsContainer.childNodes[0].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX] );
	this.mSearchValueFillerContainer.style.width = 387 + ( offset[0] - 420 ) + 'px';
  };


  this.setMediaSearchOperators = function( operators )
  {
    this.mMediaSearchOperators = operators;
  };

  this.setMediaSearchDateOperators = function( mediaSearchDateOperators )
  {
    this.mMediaSearchDateOperators = mediaSearchDateOperators;
  };

  /**
   * If true the panel will animate certain actions like close and open
   */
  this.setAnimate = function( animate )
  {
    this.mAnimate = animate;
  };
  
  this.setToggleSearchFunction = function( toggleSearchFunction )
  {
    this.mToggleSearchFunction = toggleSearchFunction;
  }

  this.setRequestSuggestionFunction = function( requestSuggestionFunction )
  {
    this.mRequestSuggestionFunction = requestSuggestionFunction;
  }

  this.requestSuggestions = function( input )
  {
    if( this.mIsRequestSuggestionMode == true )
    {
      var fieldName = this.mCurrentSearchOption.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value;
      var selectedCollections = this.getSelectedMediaCollections();

      if( (! fieldName || fieldName == this.SELECT_NO_VALUE) && fieldName !="" )
      {
        // no media field, cant continue
        this.mSearchValueContainer.update( this.ERROR_SELECT_MEDIA_FIELD_TEXT );
      }
      else if( !selectedCollections || ( selectedCollections.length < 1 ) )
      {
        // no media collections, cant continue
        this.mSearchValueContainer.update( this.ERROR_SELECT_MEDIA_COLLECTION_TEXT );
      }
      else if( input && this.mRequestSuggestionFunction && this.mCurrentSearchOption )
      {
        // every thing is good lets grab some suggestions
        if( this.mSuggestionCache[ input + '-' + fieldName + '-' +  selectedCollections.join('-') ] )
        {
          // woohoo its cached
          this.setSuggestions( this.mSuggestionCache[ input + '-' + fieldName + '-' +  selectedCollections ] );
        }
        else
        {
          // doh lets request it
          //this.startLoadingState( this.mSearchValuesContainer );
          this.mRequestSuggestionFunction( input, fieldName, selectedCollections );
        }
      }
    }
  };

  this.setSuggestions = function( suggestions, input, fieldName, selectedCollections )
  {
    if( suggestions )
    {
      // cache it if we can
      if( input && fieldName && selectedCollections )
      {
        this.mSuggestionCache[ input + '-' + fieldName + '-' +  selectedCollections.join('-') ] = suggestions;
      }
      this.setSearchValues( suggestions );
      this.endLoadingState();

    }
  };

  this.clearSearchValues = function()
  {
    // stop adding in search values if we are clearing them
    if( this.mBatchSetSearchValuesTimeout )
    {
      clearTimeout( this.mBatchSetSearchValuesTimeout );
      this.mBatchSetSearchValuesTimeout = null;
    }

    this.mSearchValueContainer.update( '' );
  };

  this.clearAllSearchFields = function()
  {
  	// grab all search options
    var searchAllTermsOptions = this.mSearchAllTermsContainer.childNodes;
    var searchAnyTermsOptions = this.mSearchAnyTermsContainer.childNodes;
    var searchExactTermsOptions = this.mSearchExactTermsContainer.childNodes;
	
    if( searchAllTermsOptions )
    {
      for( var i = 0; i < searchAllTermsOptions.length; i++ )
      {
	    //searchAllTermsOptions[i].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].options[0].selected = true;
		searchAllTermsOptions[i].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value = '';
      }
    }
	if( searchAnyTermsOptions )
    {
      for( var i = 0; i < searchAnyTermsOptions.length; i++ )
      {
	    //searchAnyTermsOptions[i].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].options[0].selected = true;
		searchAnyTermsOptions[i].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value = '';
      }
    }
	if( searchExactTermsOptions )
    {
      for( var i = 0; i < searchExactTermsOptions.length; i++ )
      {
	    //searchExactTermsOptions[i].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].options[0].selected = true;
		searchExactTermsOptions[i].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value = '';
      }
    }
  }

  this.appendSearchValue = function( value )
  {
    if( value )
    {
      value = value.replace( /\\"/g, '"' );
    }

	// since we now know value is unique, and blinking is not necessary, don't need the following
    //var searchValue = this.findSearchValue( value );
    if( false )
    {
      // already there, lets blink it
      //ElementEffects.blinkColor( searchValue );
    }
    else
    {
      var ams = this;
      searchValue = $( document.createElement( 'span' ) );
      searchValue.update( value );
      searchValue.value = value;
      searchValue.title = value;
      searchValue.onclick = function(e){ var se = jshGetSourceElement(e); ams.addSearchValueToCurrentSearchOption( se.value ); };

      this.mSearchValueContainer.appendChild( searchValue );
    }
  };



  this.findSearchValue = function( value )
  {
    for( var i = 0; i < this.mSearchValueContainer.childNodes.length; i++ )
    {
      if( this.mSearchValueContainer.childNodes[i].value == value )
      {
        return this.mSearchValueContainer.childNodes[i];
      }
    }

    return null;
  };


  this.setSearchValues = function( values, skipClearSearchValues )
  {
    // clear current values
    if( skipClearSearchValues != true )
    {
      this.clearSearchValues();
    }

	// get only unique values from suggestions
	values = this.getUniqueValues(values);
    if( values )
    {
      for( var i = 0; i < this.SUGGESTION_BATCH_SIZE; i++ )
      {
        if( values.length <= i )
        {
          break;
        }

        this.appendSearchValue( values[i] );
        values[i] = null;
      }

      values = values.compact();
      if( values.length > 0 )
      {
        // next batch in a bit
        var daValues = values;
        var ams = this;
        this.mBatchSetSearchValuesTimeout = setTimeout( function(){ ams.setSearchValues( daValues, true ); }, this.SUGGESTION_BATCH_DELAY );
      }
    }
  };

  // return unique values of an array
  this.getUniqueValues = function ( array ) {
    var hash = new Object();
    for (j = 0; j < array.length; j++) {hash[array[j]] = true}
    var array = new Array();
    for (value in hash) {array.push(value)};
    return array;
  }

  this.addSearchValueToCurrentSearchOption = function( value )
  {
    if( value && ( value != '...' ) && this.mCurrentSearchOption )
    {
      var searchTermValuesContainer = this.mCurrentSearchOption.childNodes[ this.SEARCH_TERM_VALUE_ELEMENT_INDEX ];

      var indexOfSearchValue = searchTermValuesContainer.value.indexOf( value );
      if( indexOfSearchValue < 0 )
      {
        // update actual search value
        var termValue = value;
		searchTermValuesContainer.value = termValue;
		this.updateQueryStringContainer();
      }
      else
      {
        // trying to add it again, show them where its at
      }
    }	
  };

  this.clearSearchTermValues = function( searchTermValuesContainer )
  {
    if( searchTermValuesContainer )
    {
	  searchTermValuesContainer.value = '';
    }
  };


  /**
   * Returns a array of media collection strings that have been marked
   * as active
   */
  this.getSelectedMediaCollections = function()
  {
    var toReturn  = $( new Array() );
    var checkboxes = this.mMediaCollectionsListContainer.getElementsBySelector( 'input' );
    for( var i = 0; i < checkboxes.length; i++ )
    {
      if( checkboxes[i] && checkboxes[i].checked )
      {
        toReturn.push( checkboxes[i].value );
      }
    }

    return toReturn;
  };


  this.setSuggestionMode = function( requestSuggestions, operatorsSelect )
  {
    if( operatorsSelect )
    {
      if( operatorsSelect.value == '=' )
      {
        requestSuggestions = true;
      }
      else
      {
        requestSuggestions = false;
      }
    }

    this.mIsRequestSuggestionMode = ( requestSuggestions == true );

    this.updateSearchValuesUI();
  };


/*
  this.isMediaFieldDate = function( searchOption )
  {
    var isDate = false;
    if( searchOption )
    {
      var mediaField = searchOption.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX];
      if( mediaField && this.mMediaFields )
      {
        for( var i = 0; i < this.mMediaFields.length; i++ )
        {
          if( this.mMediaFields[i].fieldName == mediaField.value )
          {
            isDate = this.mMediaFields[i].dateField;
            break;
          }
        }
      }
    }

    return isDate;
  };
*/

  this.isSearchOptionsValid = function()
  {
    var valid = false;

    // grab all search options
    var searchAllTermsOptions = this.mSearchAllTermsContainer.childNodes;
    var searchAnyTermsOptions = this.mSearchAnyTermsContainer.childNodes;
    var searchExactTermsOptions = this.mSearchExactTermsContainer.childNodes;
	
    if( searchAllTermsOptions )
    {
      valid = true;
      for( var i = 0; i < searchAllTermsOptions.length; i++ )
      {
        // continue if we have one, and its not the new button
          // if any of these are true the search option is not valid
          if( ( searchAllTermsOptions[i] ) && 
		  	  ( searchAllTermsOptions[i].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value == this.SELECT_NO_VALUE ) 
			)
          {
		  	//not valid if entered a value but didn't select a field in the 1st field. Always invalid if didn't select a field.
		  	if ((i != 0) || ((i == 0) && (searchAllTermsOptions[i].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value.length > 0))) {
				valid = false;
				break;
			}
          }
      }
    }
    if( searchAnyTermsOptions )
    {
      valid = true;
      for( var i = 0; i < searchAnyTermsOptions.length; i++ )
      {

          // if any of these are true the search option is not valid
          if( ( searchAnyTermsOptions[i] ) && 
		  	  ( searchAnyTermsOptions[i].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value == this.SELECT_NO_VALUE ) 
			)
          {
		  	if ((i != 0) || ((i == 0) && (searchAnyTermsOptions[i].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value.length > 0)))
			{
				valid = false;
				break;
			}
          }
        
      }
    }
    if( searchExactTermsOptions )
    {
      valid = true;
      for( var i = 0; i < searchExactTermsOptions.length; i++ )
      {
          // if any of these are true the search option is not valid
          if( ( searchExactTermsOptions[i] ) && 
		  	  ( searchExactTermsOptions[i].childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value == this.SELECT_NO_VALUE ) 
			)
          {
		  	if ((i != 0) || ((i == 0) && (searchExactTermsOptions[i].childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value.length > 0)))
			{
				valid = false;
				break;
			}
          }
        
      }
    }

    return valid;
  };



  this.generateQueryString = function( includeMediaCollection )
  {
	var query = '';
	var isFirst = true;
	
    if( this.isSearchOptionsValid() )
    {
	  //check all term search fields
      for( var i = 0; i < this.mSearchAllTermsContainer.childNodes.length; i++ )
      {
        var searchTerm;
        searchTerm =  this.mSearchAllTermsContainer.childNodes[i];
        // var isDate = this.isMediaFieldDate( searchTerm );
        var searchValues = searchTerm.childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value;
		var operator = this.mDefaultOp;		
	    var includeQuotes = true;
		
	  	if(( searchValues.length > 0 ) && ( searchTerm.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value == '-' ))
		{
		  query = this.ERROR_SELECT_MEDIA_FIELD_TEXT;
		  return query;
		}
		// check for escape operators
		if(( searchValues.length > 1 ) && ( this.mSearchTypeOverwriteOp.indexOf(searchValues.substring(0,1)) >= 0 ))
		{
		  operator = searchValues.substring(0,1);
		  searchValues = searchValues.substring(1,searchValues.length);
		  // don't include quotes if using contain or startWith
		  includeQuotes = false;
		}
		searchValues.strip();
		if ( searchValues.length > 0 ) {
	  	  // toss in the condition if its not the first one.
	  	  if (!isFirst){
		    query += this.mCurrentTermCondition.value;
		  }
		  // concat all the parts to build up the query
		  query += searchTerm.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value;
		  query += operator;
		  query += searchValues;
		  //query += this.insertFieldConditionOperator( searchValues, 'AND', searchTerm.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value, operator, includeQuotes );
		  isFirst = false;
		}
      }

	  // check any term search fields
      for( var i = 0; i < this.mSearchAnyTermsContainer.childNodes.length; i++ )
      {
		var searchTerm;
        searchTerm =  this.mSearchAnyTermsContainer.childNodes[i];
        // var isDate = this.isMediaFieldDate( searchTerm );
        var searchValues = searchTerm.childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value;
		var operator = this.mDefaultOp;		
		var includeQuotes = true;
		
		if( ( searchValues.length > 0 ) && ( searchTerm.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value == '-') )
		{
		  query = this.ERROR_SELECT_MEDIA_FIELD_TEXT;
		  return query;
		}
		// check for escape operators
		if(( searchValues.length > 1 ) && ( this.mSearchTypeOverwriteOp.indexOf(searchValues.substring(0,1)) >= 0 ))
		{
		  operator = searchValues.substring(0,1);
		  searchValues = searchValues.substring(1,searchValues.length);
		  includeQuotes = false;
		}
		searchValues.strip();
		if ( searchValues.length > 0 ) {
	  	  // toss in the condition if its not the first one.
	  	  if ( !isFirst ){
		    query += this.mCurrentTermCondition.value;
		  }
		  // concat all the parts to build up the query
		  query += searchTerm.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value;
		  query += operator;
		  //query += this.insertFieldConditionOperator( searchValues, 'OR', searchTerm.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value, operator, includeQuotes );
		  query += this.insertOperator( searchValues, 'OR' );
		  isFirst = false;
		}
      }

	  // check exact term search fields
      for( var i = 0; i < this.mSearchExactTermsContainer.childNodes.length; i++ )
      {
		var searchTerm;
        searchTerm =  this.mSearchExactTermsContainer.childNodes[i];
        // var isDate = this.isMediaFieldDate( searchTerm );
        var searchValues = searchTerm.childNodes[this.SEARCH_TERM_VALUE_ELEMENT_INDEX].value;
		var operator = this.mDefaultOp;

		// check for escape operators
		if(( searchValues.length > 1 ) && ( this.mSearchTypeOverwriteOp.indexOf(searchValues.substring(0,1)) >= 0 ))
		{
		  operator = searchValues.substring(0,1);
		  searchValues = searchValues.substring(1,searchValues.length);
		}
		searchValues.strip();
		if ( searchValues.length > 0 ) {
	  	  // toss in the condition if its not the first one.
	  	  if ( !isFirst ){
		    query += this.mCurrentTermCondition.value;
		  }
		  // concat all the parts to build up the query
		  query += searchTerm.childNodes[this.SEARCH_TERM_MEDIA_FIELD_ELEMENT_INDEX].value;
		  query += operator;
		  query += '"' + searchValues + '"';
		  isFirst = false;
		}
      }

    }else
	{
	  query = this.SEARCH_QUERY_INVALID;
	}

    // add the collection information to the query if query is not null.
    if ( (query) && (includeMediaCollection) )
    {
      query += " LIMIT:";
      var selectedCollections = this.getSelectedMediaCollections();
      for(var i = 0 ; i < selectedCollections.length; i++)
      {
        query += selectedCollections[i] + ",";
      }

      if( query[query.length -1] == "," )
      {
        query = query.substring( 0, query.length-1 );
      }

      // might need a full utf-8 encoding
      // at some point
      query = query.replace( '&', '%26' );
      query = query.replace( '#', '%23' );
    }
    
    return query;
  };



  this.submitSearch = function()
  {
    var query = this.generateQueryString(true);
	
    if( query )
    {
      var url = jshAppendParameter( this.mSearchUrl, 'q', query );
      url = jshAppendParameter( url, 'sort', this.mSort );

      window.location.href = url;

      return false;
    }
    else
    {
      ElementEffects.blinkBorder( this.mCurrentSearchOption );
    }

    return true;
  };



  this.startLoadingState = function( referenceElement )
  {
    if( this.mLoading != true )
    {
      this.mLoading = true;

      //clear any mim timeout currently set
      if( this.mMinimumLoadingTimeout )
      {
        clearTimeout( this.mMinimumLoadingTimeout );
      }

      // some attributes to properly show the loading screen
      var borderOffset = jshBorderOffset( referenceElement );
      var offset = Position.positionedOffset( referenceElement );
      this.mLoadingStartTime = new Date();
      this.mLoadingContainer.style.width = ( referenceElement.getWidth() - borderOffset[0] ) + 'px';
      this.mLoadingContainer.style.height = ( referenceElement.getHeight() - borderOffset[1] ) + 'px';
      this.mLoadingContainer.style.left = ( offset[0] + (borderOffset[0]) ) + 'px';
      this.mLoadingContainer.style.top = ( offset[1] + (borderOffset[1]) ) + 'px';
      this.mLoadingContainer.show();


      // make sure loading state ends
      var ams = this;
      setTimeout( function(){ ams.endLoadingState( true ) }, this.MAX_LOADING_TIME );
    }
  };

  /**
   * Ends the loading state
   */
  this.endLoadingState = function( forceClose )
  {
    // close if enough time has elapsed or if we are told to
    if( ( forceClose == true ) ||
        ( ( this.mLoadingStartTime - new Date() ) > this.MIN_LOADING_TIME ) )
    {
      // stop the loading process
      if( this.mLoading != null )
      {
        this.mLoading = null;
        this.mMinimumLoadingTimeout = null;
        this.mLoadingStartTime = null;
        this.mLoadingContainer.hide();
      }
    }
    else
    {
      // make sure we dont just flash, give us a few before remving our selves
      var timeout = this.MIN_LOADING_TIME - ( this.mLoadingStartTime - new Date() );
      timeout = Math.min( timeout, this.MIN_LOADING_TIME );

      // call our selves in a bit
      var ams = this;
      this.mMinimumLoadingTimeout = setTimeout( function(){ ams.endLoadingState( true ) }, timeout );
    }
  };

  this.setHelpObjects = function (helpTipPopUp, helpTip)
  {
      this.helpTipPopUp = helpTipPopUp;
      this.helpTip = helpTip;
  };

  this.showHelpTipPopUp = function ()
  {
      this.helpTipPopUp.setHelpUrl( this.mAdvancedHelpUrl );
      this.helpTip.setHelpUrl( this.mAdvancedHelpUrl );

      if( this.advancedSearchShowTip=="true" )
      {
        this.helpTipPopUp.loadContentAndShow();
      }
  };

  this.setAdvancedSearchHelpUrl = function( advancedHelpUrl )
  {
    this.mAdvancedHelpUrl = advancedHelpUrl;
  };

  this.setHelpUrl = function( helpUrl )
  {
    this.mHelpUrl = helpUrl;
  };

  this.setAdvancedSearchShowTip = function ( advancedSearchShowTip )
  {
      this.advancedSearchShowTip = advancedSearchShowTip;
  };

  this.setShowTip = function ( showTip )
  {
      this.showTip = showTip;
  };

  this.toggleHelpControl = function()
  {
    this.helpTip.setHelpUrl( this.mAdvancedHelpUrl );

    // the toggle is handled in loadandshow HelpControl
    this.helpTip.loadContentAndShow();
  };

  this.closeAdvancedSearchHelp = function()
  {

    // set the urls back to main help urls
    this.helpTip.setHelpUrl( this.mHelpUrl );
    this.helpTipPopUp.setHelpUrl( this.mHelpUrl );

    // hide all helps
    this.helpTip.hide();
    this.helpTipPopUp.hide();

    this.setShowTip( this.showTip );

    // set the parameter back
    this.helpTipPopUp.setShowTipParameter('showTip=false');

  };

  this.initiateAdvancedSearchHelp = function()
  {

    // hide the already opended helps
    this.helpTip.hide();
    this.helpTipPopUp.hide();

    // set the bool to the advanced search show tip
    this.setShowTip( this.advancedSearchShowTip );
    this.helpTipPopUp.setShowTip( this.advancedSearchShowTip );

    // set the helpfulTip parameter
    this.helpTipPopUp.setShowTipParameter('&advancedSearchShowTip=false');
  };
  
  this.insertFieldConditionOperator = function( str, condition, field, op, includeQuotes )
  {
  	var splitResult = str.split(" ");
	var toReturn = '';
	var quote = '"';
	if( includeQuotes )
	{
	  quote = '"';	
	}
	else
	{
	  quote = '';
	}
	for( i = 0; i < splitResult.length; i++ )
	{
	  if( i > 0 )
	    toReturn += ' ' + condition + ' ' + field + op + quote + splitResult[i] + quote;
      else
	    toReturn += field + op + quote + splitResult[i] + quote;
	}
	return toReturn;
  }
  
  this.insertOperator = function( str, condition )
  {
  	var splitResult = str.split(" ");
	var toReturn = '';
	for( i = 0; i < splitResult.length; i++ )
	{
	  if( i > 0 )
	    toReturn += ' ' + condition + ' ' + splitResult[i];
      else
	    toReturn += splitResult[i];
	}
	return toReturn;
  }
}

function Static_EnhancedAdvancedSearchRequest_RequestSuggestions(e)
{
  var sourceElement = jshGetSourceElement( e );
  if( sourceElement && sourceElement.enhancedAdvancedSearch )
  {
    var ams = sourceElement.enhancedAdvancedSearch;
    if( ams.mLastKeyDownTimeOut )
    {
      clearTimeout( ams.mLastKeyDownTimeOut );
    }

    ams.mLastKeyDownTimeOut = setTimeout( function(){ ams.requestSuggestions( ams.mCurrentSearchOption.childNodes[ams.SEARCH_TERM_VALUE_ELEMENT_INDEX].value /*ams.mSearchValueInput.value*/ ); }, ams.SUGGESTION_KEYDOWN_PAUSE );
  }
}
