/**
 * File: kwSearch.SearchForm.js
 *
 * Author: Chris Boget
 * Copyright 2008 Yakabod, inc.
 *
 * $Revision: 1.16 $
 * $Date: 2011/06/01 14:13:21 $
 * Last edited by: $Author: jauldrid $
 **/

// Set up the namespace
var kwSearch = window.kwSearch || {};
kwSearch.SearchForm = {

  sSearchFormId : '',
  oSearchForm   : null,

  setSearchFormId : function( sSearchFormId )
  {
    kwSearch.SearchForm.sSearchFormId = sSearchFormId;
    kwSearch.SearchForm.oSearchForm   = null;
    kwSearch.SearchForm.getSearchForm();

    /**
     * Defaulting search type to be whatever is set up in the form.
     * This is typically the result of a a POST from some other page
     */
    var sSearchType = kwSearch.SearchForm.getSearchType();
    if( sSearchType != '' )
    {
      kwSearch.SearchForm.setSearchType( sSearchType );
    }
  },

  getSearchFormId : function()
  {
    return kwSearch.SearchForm.sSearchFormId;
  },

  getSearchForm : function()
  {
    if( kwSearch.SearchForm.oSearchForm === null )
    {
      kwSearch.SearchForm.oSearchForm = document.getElementById( kwSearch.SearchForm.sSearchFormId );
      if( !kwSearch.SearchForm.oSearchForm )
      {
        kwSearch.SearchForm.oSearchForm = kwSearch.SearchForm.createFormInDOM();
      }
    }
    return kwSearch.SearchForm.oSearchForm;
  },

  /**
   * Sets the search type to sSearchType.
   * If sIfNotSearchType is defined, the search type will only be changed if it
   * isn't already that value.
   *
   * e.g. setSearchType( 'Query', 'Search' ) changes the search type to 'Query'
   * only if it hasn't been set to 'Serach'
   */
  setSearchType : function( sSearchType, sIfNotSearchType )
  {
    if( sIfNotSearchType )
    {
      if( kwSearch.SearchForm.oSearchForm.SearchData_SearchType.value != sIfNotSearchType )
      {
        kwSearch.SearchForm.oSearchForm.SearchData_SearchType.value = sSearchType;
      }
    }
    else
    {
      kwSearch.SearchForm.oSearchForm.SearchData_SearchType.value = sSearchType;
    }
  },

  getSearchType : function()
  {
    var retval = '';
    if( kwSearch.SearchForm.oSearchForm && kwSearch.SearchForm.oSearchForm.SearchData_SearchType )
    {
      retval = kwSearch.SearchForm.oSearchForm.SearchData_SearchType.value;
    }
    return retval;
  },

  setSearchSort : function( sSortField, sSortDirection )
  {
    kwSearch.SearchForm.setSearchSortField( sSortField );
    kwSearch.SearchForm.setSearchSortDirection( sSortDirection );
  },

  setSearchSortField : function( sSortField )
  {
    if( typeof sSortField === 'undefined' )
    {
      sSortField = 'Date';
    }
    kwSearch.SearchForm.oSearchForm.SearchData_Sort_Field.value = sSortField;
  },

  getSearchSortField : function()
  {
    var retval = '';
    if( kwSearch.SearchForm.oSearchForm.SearchData_Sort_Field )
    {
      retval = kwSearch.SearchForm.oSearchForm.SearchData_Sort_Field.value;
    }
    return retval;
  },

  setSearchSortDirection : function( sSortDirection )
  {
    if( typeof sSortDirection === 'undefined' )
    {
      sSortDirection = 'DESC';
    }
    kwSearch.SearchForm.oSearchForm.SearchData_Sort_Direction.value = sSortDirection;
  },

  getSearchSortDirection : function()
  {
    var retval = '';
    if( kwSearch.SearchForm.oSearchForm.SearchData_Sort_Direction )
    {
      retval = kwSearch.SearchForm.oSearchForm.SearchData_Sort_Direction.value;
    }
    return retval;
  },

  setSearchTypeForSearch : function()
  {
    var sSearchType = '';
    var oBaseSearchRequirements = JSON.parse( kwSearch.SearchForm.getBaseSearchRequirements());
    if( typeof oBaseSearchRequirements != 'undefined' )
    {
      for( var sWidgetType in oBaseSearchRequirements )
      {
        if(( typeof kwSearch.Widgets[sWidgetType]['sDefaultSearchType'] != 'undefined' ) &&
           ( kwSearch.Widgets[sWidgetType]['sDefaultSearchType'] != '' ))
        {
          sSearchType = kwSearch.Widgets[sWidgetType]['sDefaultSearchType'];
        }

        // If it's set to "Search", we can get out of dodge.
        if( sSearchType == 'Search' )
        {
          break;
        }
      }
    }

    /**
     * If it's still not set, default to "Search"
     */
    if( sSearchType == '' )
    {
      sSearchType = 'Search';
    }

    kwSearch.SearchForm.setSearchType( sSearchType );
  },

  setBaseSearchRequirements : function( oBaseSearchRequirements )
  {
    kwSearch.SearchForm.getSearchForm().SearchData_BaseSearchRequirements.value = JSON.stringify( oBaseSearchRequirements );
  },

  getBaseSearchRequirements : function()
  {
    ;
    return kwSearch.SearchForm.getSearchForm().SearchData_BaseSearchRequirements.value;
  },

  /**
   * The following 4 methods are to interact with the legacy form
   * elements.  Once those elements are no longer needed, these
   * methods should be removed
   */
  setQIDData : function( oQIDData )
  {
    try
    {
      if(( oQIDData ) && ( typeof oQIDData == 'object' ))
      {
        var aQIDData = [];
        for( sKey in oQIDData )
        {
          aQIDData.push( oQIDData[sKey].QueryId );
        }
        if( aQIDData.length > 0 )
        {
          kwSearch.SearchForm.getSearchForm().QID.value = aQIDData.join( ',' );
        }
        else
        {
          kwSearch.SearchForm.getSearchForm().QID.value = '';
        }
      }
      else
      {
        kwSearch.SearchForm.getSearchForm().QID.value = '';
      }
    }
    catch( e ) {}
  },

  setFBData : function( oFBData )
  {
    try
    {
      if(( oFBData ) && ( typeof oFBData == 'object' ) && ( typeof oFBData.FbId != 'undefined' ))
      {
        kwSearch.SearchForm.getSearchForm().FB.value = oFBData.FbId;
      }
      else
      {
        kwSearch.SearchForm.getSearchForm().FB.value = '';
      }
    }
    catch( e ) {}
  },

  setVLogData : function( oVLogData )
  {
    try
    {
      if(( oVLogData ) && ( typeof oVLogData == 'object' ) && ( typeof oVLogData.VLogId != 'undefined' ))
      {
        kwSearch.SearchForm.getSearchForm().VLog.value = oVLogData.VLogId;
      }
      else
      {
        kwSearch.SearchForm.getSearchForm().VLog.value = '';
      }
    }
    catch( e ) {}
  },

  setTeamIdData : function( oTeamIdData )
  {
    try
    {
      if(( oTeamIdData ) && ( typeof oTeamIdData == 'object' ) && ( typeof oTeamIdData.TeamId != 'undefined' ))
      {
        kwSearch.SearchForm.getSearchForm().TeamId.value = oTeamIdData.TeamId;
      }
      else
      {
        kwSearch.SearchForm.getSearchForm().TeamId.value = '';
      }
    }
    catch( e ) {}
  },

  setNextStartRecord : function( iStartRecord )
  {
    try
    {
      kwSearch.SearchForm.getSearchForm().SearchData_StartRecord.value = iStartRecord;
    }
    catch( e ) {}
  },

  setRecordLimit : function( iRecordLimit )
  {
    if( typeof iRecordLimit !== 'number' || iRecordLimit < 1 )
    {
      iRecordLimit = '';
    }

    try
    {
      kwSearch.SearchForm.getSearchForm().SearchData_RecordLimit.value = iRecordLimit;
    }
    catch( e ) {}
  },

  getRecordLimit : function()
  {
    var iReturnValue;
    try
    {
      iReturnValue = parseInt( kwSearch.SearchForm.getSearchForm().SearchData_RecordLimit.value );
      if( isNaN( iReturnValue ) || iReturnValue < 1 )
      {
        throw 'kwSearch.SearchForm.getRecordLimit() did not get number from kwSearch.SearchForm.oSearchForm.SearchData_RecordLimit.value';
      }
    }
    catch( e )
    {
      iReturnValue = null;
    }

    return iReturnValue;
  },

  createFormInDOM : function()
  {
    var oForm = document.createElement( 'form' );
    if( oForm )
    {
      document.body.appendChild(oForm);
      oForm.setAttribute( 'id', 'SearchForm' );
      oForm.setAttribute( 'name', 'SearchForm' );
      oForm.setAttribute( 'method', 'POST' );
      oForm.setAttribute( 'action', kwSearch.ContentSearch.StreamContent.sRequestPage );

      var oEl = document.createElement( 'input' );
      oEl.setAttribute( 'type', 'hidden' );
      oEl.setAttribute( 'id', 'SearchData_BaseSearchRequirements' );
      oEl.setAttribute( 'name', 'SearchData[BaseSearchRequirements]' );
      oEl.setAttribute( 'value', '' );
      oForm.appendChild( oEl );

      oEl = document.createElement( 'input' );
      oEl.setAttribute( 'type', 'hidden' );
      oEl.setAttribute( 'id', 'SearchData_SearchType' );
      oEl.setAttribute( 'name', 'SearchData[SearchType]' );
      oEl.setAttribute( 'value', '' );
      oForm.appendChild( oEl );

      oEl = document.createElement( 'input' );
      oEl.setAttribute( 'type', 'hidden' );
      oEl.setAttribute( 'id', 'SearchData_RecordLimit' );
      oEl.setAttribute( 'name', 'SearchData[RecordLimit]' );
      oEl.setAttribute( 'value', '' );
      oForm.appendChild( oEl );

      oEl = document.createElement( 'input' );
      oEl.setAttribute( 'type', 'hidden' );
      oEl.setAttribute( 'id', 'SearchData_StartRecord' );
      oEl.setAttribute( 'name', 'SearchData[StartRecord]' );
      oEl.setAttribute( 'value', '' );
      oForm.appendChild( oEl );

    }
    return oForm;
  },

  submitTo : function( sTargetPage )
  {
    $( kwSearch.SearchForm.getSearchForm()).attr( 'action', sTargetPage ).submit();
  },

  submit : function()
  {
    if( kwSearch.isSearchAllowed())
    {
      var oSearchForm = kwSearch.SearchForm.getSearchForm();
      if( oSearchForm )
      {
        if( oSearchForm.action != kwSearch.ContentSearch.StreamContent.sRequestPage )
        {
          oSearchForm.action = kwSearch.ContentSearch.StreamContent.sRequestPage;
        }
        if( oSearchForm.method.toLowerCase() != 'post' )
        {
          oSearchForm.method = 'POST';
        }
        oSearchForm.submit();
      }
    }
  },

  init : function( iAttempts )
  {
    var retval = null;

    iAttempts = iAttempts || 0;
    if( typeof kwSearch.ContentSearch != 'undefined' )
    {
      kwSearch.ContentSearch.Util.registerSearchCallback( 'Pre', kwSearch.SearchForm.setSearchTypeForSearch );

      /**
       * Rewriting the function as it has already filled it's purpose.
       */
      kwSearch.SearchForm.init = function() {};
    }
    else
    {
      if( iAttempts < 15 )
      {
        setTimeout( function() { kwSearch.SearchForm.init( ++iAttempts ); }, 500 );
      }
    }
    return retval;
  }
}

kwSearchUtil.registerOnloadBehavior( kwSearch.SearchForm.init, 15 );

