function validateSaveSearch()
{
  if( !validateString( 'searchName', 'Search Name', 1, 250 ) )
  {
    return false;
  }
  var needsoneVal = $( 'needsOneValue' ).value;
  var userVal = document.getElementById( 'createUserStreamNumResults' );
  var userCheckbox = document.getElementById( 'createUserStream' );
  var courseCheckbox = document.getElementById( 'createCourseStream' );
  var userIsChecked = userCheckbox && userCheckbox.checked;
  var courseIsChecked = courseCheckbox && courseCheckbox.checked;
  var oneValid = false;
  if( userIsChecked )
  {
    if( !validateInteger( 'createUserStreamNumResults', 'Number of Results', 1, 25 ) )
    {
      return false;
    }
    oneValid = true;
  }
  if( courseIsChecked )
  {
    if( !validateInteger( 'createCourseStreamNumResults', 'Number of Results', 1, 25 ) )
    {
      return false;
    }
    oneValid = true;
  }
  if( needsoneVal == 'true' && !oneValid )
  {
    return invalidate( userVal, 'At least one stream must be created' );
  }
  var newNameElem = $( 'searchName' );
  var value = trim( newNameElem.value );
  return doSearchNameCheck( $('savedSearch').value.strip(), value );
}
function saveSearch()
{
  //trim leading/ending spaces from the name
  var newNameElem = $( 'searchName' );
  newNameElem.value = newNameElem.value.strip();
  var params = marshallParams( document.forms['saveSearchForm'] );
  if( !validateSaveSearch() )
  {
    return false;
  }
  var savedSearches = $( 'savedSearchesInnerListDiv' );
  if( savedSearches )
  {
    ajaxUpdate( savedSearches, 'saveSearch.dobbb', params, saveSearchSuccess, saveSearchError );
  }
  else
  {
    ajaxRequest( 'saveSearch.dobbb', params, saveSearchSuccess, saveSearchError );
  }
  return false;
}
//if there's already a save search form loaded, hide it and clear out its div, lest we get confused by multiple forms
function removeOldSearchForm()
{
  var saveSearchForm = document.forms['saveSearchForm'];
  if( saveSearchForm != null )
  {
    var parentDiv = saveSearchForm.parentNode;
    if( parentDiv != null )
    {
      parentDiv.innerHTML = '';
    }
    else
    {
      alert( "why isn't there a parent div for saveSearchForm?");
    }
  }
}
var gDivName;
var gButtonId;
function loadSaveSearchForm( criteria, resultMode, searchScope, divName, buttonId, isView, showStreamCheckboxes )
{
  removeOldSearchForm();
  gDivName = divName;
  gButtonId = buttonId;
  var params = 'criteria=' + criteria + '&resultMode=' + resultMode + '&searchScope=' + searchScope + '&isView=' + isView + '&showStreamCheckboxes=' + showStreamCheckboxes;
  ajaxUpdate( divName, 'saveSearch.dobbb', params, onLoadSuccess, defaultOnFailure, 'get' );
}
function onLoadSuccess( )
{
  showBox( gDivName, gButtonId, 'true' )
}
function saveSearchSuccess()
{
  hideContextMenu();
  reportAjaxSuccessInline( 'The search was saved successfully' );
}
function saveSearchError( request )
{
  if( getStatusText(request) == 'NAME_EXISTS' )
  {
    var confirmed = confirm( 'You already have a saved search with this name.  Would you like to overwrite the existing search?' );
    if( confirmed )
    {
      var theForm = document.forms['saveSearchForm'];
      var overwrite = theForm.elements['overwriteExisting'];
      overwrite.value = 'true';
      saveSearch();
    }
  }
  else
  {
    defaultOnFailure( request );
  }
}
function toggleNumResults( field, numResultsId )
{
  var numResultsElem = document.getElementById( numResultsId );
  var numResultsElemLabel = document.getElementById( numResultsId + 'label' );
  if( field.checked )
  {
    numResultsElem.style.display = 'block';
    numResultsElemLabel.style.display = 'block';
  }
  else
  {
    numResultsElem.style.display = 'none';
    numResultsElemLabel.style.display = 'none';
  }
}
function showRenamePopup( ssId, searchName, savedSearchButtonId )
{
  removeOldSearchForm();
  gButtonId = savedSearchButtonId;
  var params = 'savedSearch=' + ssId;
  ajaxUpdate( 'rename_search_form', 'saveSearch.dobbb', params, onSaveSearchFormLoaded, defaultOnFailure, 'get' )
}
function onSaveSearchFormLoaded()
{
  showBox( 'rename_search_form', gButtonId, 'true' );
}
function confirmRemoveSearch( hasStreams )
{
  if( hasStreams == 'true' )
  {
    return confirm( 'This saved search has streams that are based on it.  Removing this search will remove those streams.  Do you want to continue?' )
  }
  else
  {
    return confirm( 'Are you sure?' );
  }
}
var saveSearchNameMessage = null;
function checkNameStatus( transport )
{
  var result = transport.responseText.strip();
  if( result == 'ok' )
  {
    saveSearchNameMessage = null;
  }
  if( result == 'streams' )
  {
    saveSearchNameMessage = "That name is already in use for another Saved Search. If you continue, you will overwrite the existing Saved Search and affect the associated Streams based on it.";
  }
  if( result == 'ss_streams' )
  {
    saveSearchNameMessage = "Editing this Saved Search will affect streams based on this Saved Search. Do you wish to continue?"
  }
  if( result == 'exists' )
  {
    saveSearchNameMessage = "That name is already in use for another Saved Search. Do you want to overwrite the existing saved search?";
  }
}
function doSearchNameCheck( savedSearchId, newName )
{
  //make an AJAX call to check to see if the search name has been used for another search
  ajaxRequest( 'validateSearchName.dobbb', 
               'savedSearchId=' + savedSearchId + '&savedSearchName=' + encodeURIComponent( newName ),
               checkNameStatus,
               defaultOnFailure,
               'get',
               false ); //synchronous
  if( saveSearchNameMessage != null )
  {
    var goAhead = confirm( saveSearchNameMessage );
    return goAhead;
  }
  else
  {
    return true;
  }
}
