﻿// SearchControl JScript File
function KGSearchControl1_GetSearchBox_Wrapped(_tbSearchClientID)
{
    var searchBox = document.getElementById(_tbSearchClientID);
    return searchBox;
}

// KGSearchControlButtonOnClick
function KGSearchControl_CallbackTrigger_Wrapped(_tbSearchClientID,
                                                 _cbreference,
                                                 _ConceptTabbedViewCallBack,
                                                 _srmIndex,
                                                 _redirectUrl)
{
  //debugger;
  var keys = 'SuggestionClick|KGSuggestion_SearchKeywords';
	var values = 'true|';
  SetSessionProperty(keys, values, true);

  //SetSessionProperty("SuggestionClick", "true");
  //SetSessionProperty('KGSuggestion_SearchKeywords','');
  
  var itemKey = '-1';
  var itemType = 0;
  var searchBox = document.getElementById(_tbSearchClientID);
  if(searchBox != null && searchBox.value!=null && searchBox.value!='' && searchBox.value.replace(/'/g, '').replace(/^\s*/, '').replace(/\s*$/, '') != '')
  {
    // KGSearchServer will query QuerySearchResults when itemID = '-1'
    var itemName = searchBox.value; //.replace(/'/g, "\\" + "\'")
    
    var keys = 'SearchResultItemClickInfo|HideSearchResultItemInfo|SelectedKnowdeSiblingInfo|SearchResult_ClickedItemID|SearchResult_NewRootConceptID|KGShowResultsTab|KGHideSemanticTreeCheckBox';
	  var values = '|false||-1|-1|true|false';
    SetSessionProperty(keys, values, true);

    //SetSessionProperty('SearchResultItemClickInfo', '');
    //SetSessionProperty('HideSearchResultItemInfo', 'false');
    // store in session as root phrase name
    SetSessionProperty('SearchResult_SearchRootPhraseName', itemName); 
    // new default map will be load, clear old session
    //SetSessionProperty('SelectedKnowdeSiblingInfo', '');	
    
    global_SearchResult_ClickedItemID = '-1';
    //SetSessionProperty('SearchResult_ClickedItemID', '-1');
      
    var currentLocation = window.location.href;
    var currentPage = currentLocation.substring(currentLocation.lastIndexOf('/')+1, currentLocation.length).toLowerCase();
    var hostURL = currentLocation.substring(0, currentLocation.lastIndexOf('/') + 1);
    try
    {
      if (m_srmGetTimes!= null && m_srmGetTimes == "True" && _srmIndex != null)
        m_srmAction[_srmIndex] += ',' + currentPage + ',' + itemName;
    }
    catch(e){}
    
    //SetSessionProperty('PageSession_HighlightedConceptId', hightlightedConceptId);
    // Set the highlighted concept as the selected concept
    //SetSessionProperty('PageSession_KGSelectedConceptID', hightlightedConceptId);  
    /*SetSessionProperty('SearchResult_NewRootConceptID', -1); // clear tree focus status
    SetSessionProperty('KGShowResultsTab', 'true');
    SetSessionProperty('KGHideSemanticTreeCheckBox',false);*/
    //SetSessionProperty('ConceptTabbedView_SelectedTab', 'Results');

    // switch tab
    if (window.KGNavigationControl_SwitchTabs)
			KGNavigationControl_SwitchTabs('Results', itemName);
    else
    {
      SetSessionProperty('ConceptTabbedView_SelectedTab', 'Results');
      global_ConceptTabbedControl_CurrentlySelectedTab = 'Results';
      SetSessionProperty('KGNavigationControl_SemanticTreeTitleSubFix', itemName.replace('&','%26'));
    }
			
	  var keys = 'IndexedDocListMode|KGMapViewCollapsed|PageSession_SearchButtonClick';
	  var values = 'IndexedDocuments|true|true';
    SetSessionProperty(keys, values, true);

    /*SetSessionProperty('IndexedDocListMode', 'IndexedDocuments');
    SetSessionProperty('KGMapViewCollapsed',true);
    SetSessionProperty('PageSession_SearchButtonClick', true);*/
       
    if (_redirectUrl != "" && _redirectUrl != null)
    {
      // need redirection
      // NOTE: temporary hide the concept tree for now
      //SetSessionProperty('KGNavigationControl_SemanticTreeVisible', true);
      SetSessionProperty('PageSession_LibrarySelectedItemID', itemKey);
      SetSessionProperty('PageSession_LibrarySelectedItemTypeID', itemType);
      SetSessionProperty('PageSession_LibrarySelectedItemName', itemName.replace(/'/g, "\\" + "\'"));
      try
      {
        if (m_srmGetTimes!= null && m_srmGetTimes == "True" && _srmIndex != null)
        {
          SetSessionProperty('Srm_T0', m_srmT0[_srmIndex]);
          SetSessionProperty('Srm_Action', m_srmAction[_srmIndex]);
          SetSessionProperty('Srm_Callbacks', 1);
        }
      } catch(e){}
      window.location = hostURL+_redirectUrl;    
    }
    else
    {
      var args = "SrmIndex=" + _srmIndex;
      try
      {
        if (m_srmGetTimes!= null && m_srmGetTimes == "True" && _srmIndex != null)
          m_srmCallbacks[_srmIndex] = 3; //2 for CTV, 1 for Search control
      }
      catch(e){}
      // callback
      eval(_cbreference);
      eval(_ConceptTabbedViewCallBack + '(\'' + itemKey + '\', \'' + itemType + '\', \'' + itemName + '\', \'' + itemName + '\',' + _srmIndex + ')');
      // Hide Suggestion Control
      HideSuggestionsControl();
    }
   
    return true;
  }
  else 
  {
    if(searchBox != null && searchBox.value!=null && searchBox.value!='' && searchBox.value.replace(/^\s*/, '').replace(/\s*$/, '') == '')
    {
      searchBox.value = '';
    }
    
    return false;
  }
}

// Callback Done
function KGSearchControl_CallbackDone(args, context)
{
  try
  {
    // Check for a session time out before completing the callback
    if (CheckSessionTimeout())
      return;
    
    var argsSplit = args.split('[@|@]'); 
    var srmIndex = argsSplit[1]
    SrmControl_RecordTimes(srmIndex);
  }
  catch(e){}
}

// ValidateSearchkeywords
function ValidateSearchkeywords_Wrapped(_tbSearchClientID)
{
  //debugger;
  if(document.getElementById(_tbSearchClientID)!= null)
  {
    if(document.getElementById(_tbSearchClientID).value.replace(/^\s+/g, '').replace(/\s+$/g, '').length < 1)
    {
      alert('Please enter the keyword(s) to search for');
      document.getElementById(_tbSearchClientID).value = '';
      return false;
    }
    else
    {
      document.getElementById(_tbSearchClientID).value = document.getElementById(_tbSearchClientID).value.replace(/^\s+/g, '').replace(/\s+$/g, '');
      return true;
    }
  }
  else
  {
    return false;
  }
}

// UpdateSearchBox
function KGSearchControl_UpdateSearchBox_Wrapped(searchPhrase,
                                                 _SearchBoxID,
                                                 _KGOVERLAYCONTROL_DIV_ID,
                                                 _OVERLAYPOSITIONHIDDEN_CSSCLASS,
                                                 _SearchBoxIdFloatingToolbar,
                                                 _PaneExpandHandler)
{
  //debugger;
  var holder = document.getElementById(_SearchBoxID);
  if(holder != null)
  {
    holder.value = searchPhrase;
    if(document.getElementById(_KGOVERLAYCONTROL_DIV_ID) != null)
    {
      document.getElementById(_KGOVERLAYCONTROL_DIV_ID).className=_OVERLAYPOSITIONHIDDEN_CSSCLASS;
    }
  }
  // Set SearchPhrase of KGFloatingToolbar by updated value
  var searchPhraseKGFloatingToolbar = '';
  var SearchBoxIdFloatingToolbar = _SearchBoxIdFloatingToolbar;
  if( SearchBoxIdFloatingToolbar != null )
  {
    searchPhraseKGFloatingToolbar = document.getElementById(_SearchBoxIdFloatingToolbar);
    if(searchPhraseKGFloatingToolbar != null)
    {
      searchPhraseKGFloatingToolbar.value = '';
    }
  }
  // Expand the left Pane
//  if((document.getElementById('Splitter1')) != null)
//  {
//    eval(_PaneExpandHandler);
//  }
}

// Search Box - KeyUpHandler_NoDelay
function KGSearchControl_KeyUpHandler_NoDelay_Wrapped(_searchBoxID,
                                                      _SearchBoxIdFloatingToolbar,
                                                      _OverlayControlPosType,
                                                      _OverlayControlPosTop,
                                                      _OverlayControlPosLeft,
                                                      _KGSuggestionControlName,
                                                      _CallBackMethod,
                                                      _KGOVERLAYCONTROL_DIV_ID,
                                                      _OVERLAYPOSITIONHIDDEN_CSSCLASS,
                                                      _srmIndex)
{
  //debugger;
  //SetSessionProperty('KGSearchKeywords','');
  
  var searchPhrase = document.getElementById(_searchBoxID);
  // perform a left trim to prevent exception when only a 'space' is entered in suggestions. OM 20/11/07
  if(searchPhrase.value.replace(/^\s+/,"") != "")
  { 
  
   if(searchPhrase.value == "'")
     searchPhrase.value.replace("'", "\\" + "\'");
  
    var searchPhraseKGFloatingToolbar = '';
    var SearchBoxIdFloatingToolbar = _SearchBoxIdFloatingToolbar;
    if(SearchBoxIdFloatingToolbar != null && SearchBoxIdFloatingToolbar !='')
    {
      searchPhraseKGFloatingToolbar = document.getElementById(_SearchBoxIdFloatingToolbar);
      if(searchPhraseKGFloatingToolbar != null && searchPhraseKGFloatingToolbar.value!=null && searchPhraseKGFloatingToolbar.value!='')
      {
        searchPhrase.value = searchPhraseKGFloatingToolbar.value;
      }
    }
    if(searchPhrase!=null && searchPhrase.value!=null && searchPhrase.value!='')
    {
      var coords = findPosNoAdjust(searchPhrase);
      var posTop = coords[1] + 21 + 5; // + 76;  // + 76 is to leave room for the ticker and categories control so you can read them
      var posLeft = coords[0];
      posLeft = posLeft - 245;
    
      var posType = _OverlayControlPosType;
      if(posType != null && posTop!=null && posLeft!=null) 
      {
        eval('Update' + _KGSuggestionControlName + 'Pos(\'' + posType + '\',\'' + posLeft + '\',\'' + posTop + '\');');
      }
      
      if (m_srmGetTimes!= null && m_srmGetTimes == "True" && _srmIndex != null)
        m_srmAction[_srmIndex] += ',' + searchPhrase.value;
      
      // the searchphrase.replace(/\'/g, "\\" + "\'") replaces an apostrophe, with it's escape character ("'" becomes "\'")
      // the "g" ensures all occurances of the apostrophe are replaced rather than just the first.
      eval(_CallBackMethod + '(\'' + searchPhrase.value.replace(/'/g, "\\" + "\'") + '\', ' + _srmIndex + ');');
    }
    else
    {
      if(document.getElementById(_KGOVERLAYCONTROL_DIV_ID) != null)
      {
        document.getElementById(_KGOVERLAYCONTROL_DIV_ID).className=_OVERLAYPOSITIONHIDDEN_CSSCLASS;
      }
      try
      {
        if (m_srmGetTimes!= null && m_srmGetTimes == "True" && _srmIndex != null)
        {    
          m_srmAction[_srmIndex] = null;
          m_srmT0[_srmIndex] = null;
        }
      }
      catch(e){}
    }
  }
}

// Search Box - KeyUpHandler With Delay - keyup event handler
var keyUpTimer = null;
function KGSearchControl_KeyUpHandler_Wrapped(_KEYUP_EVENT_DELAY_MS)
{
  //debugger;
  var globalKeyUpDelay = eval(_KEYUP_EVENT_DELAY_MS);
  if(keyUpTimer != null)
  {
    window.clearTimeout(keyUpTimer);
    delete keyUpTimer;
    keyUpTimer = null;
  }
  if(keyUpTimer == null)
  {
    keyUpTimer = window.setTimeout('KeyUpHandler_NoDelay()', globalKeyUpDelay);
  }
}
