// ******************************************************************
// *
// * LsMain.js
// *
// * Copyright (C) 1999 by LexiQuest SA, France. All rights reserved.
// *
// ******************************************************************/

//
// Small function which ensures that this.lastError is always properly set.
//   (if it is called, of course...)
//
function _lsReturnCode(pReturnCode)
{
  if (pReturnCode != LS_ERROR.SERVER_ERROR)
  {
    this.lastServerError  = 0;
    this.lastServerMessage = "";
  }
  this.lastError = pReturnCode;
  return pReturnCode;
}


//
// Initialization of LsMain object.
// Done at least in beginning of LsMain::answer().
//
function _lsMainInitAnswer()
{
  // "Public" properties.
  this.currentQuery = "";
  this.answerArray  = null;

  this.lastError    = LS_ERROR.SUCCESSFUL;
  this.lastServerError  = 0;     // O == no error.
  this.lastServerMessage = "";   // Message from the LexiSez server.
}


//
// Temporary window management.
//
function _lsMainManageTemporaryWindow(pUrl)
{
  this.temporaryWindow.location = pUrl;
}


//
// Making of Tacsy URL (standard CGI or shadow) common parameters.
//
function _lsMainMakeServerUrl(pAction)
{
  var url;
  if (this.useShadow)
  {
    url = this.lsServerShadowUrl;
  }
  else
  {
    url = this.lsServerUrl;
  }

  url +=
    "?_action="      + pAction +
    "&_host="        + this.lsServerHost +
    "&_port="        + this.lsServerPort +
    "&_application=" + this.currentApplication +
    "&_user="        + this.user;

  if (this.lrAlias != this._lrAliasDefault)
  {
    url += "&_lrAlias=" + this.lrAlias;
  }
  if (this.lrApiAlias != this._lrApiAliasDefault)
  {
    url += "&_lrApiAlias=" + this.lrApiAlias;
  }
  if (this.lrCgiAlias != this._lrCgiAliasDefault)
  {
    url += "&_lrCgiAlias=" + this.lrCgiAlias;
  }

  if (this.useShadow)
  {
    url +=
      "&_proxy="    + this.proxy +
      "&_baseUrl="  + this.lsServerBaseUrl +
      "&_cgiUrl="   + this.lsServerUrl;
  }

  return url;
}


//
// Effective call to search-engine.
//
function _lsMainCallSearchEngine(pFaqBases, pSearchEngineAlias)
{
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  if (this.currentApplication == null || this.currentApplication == "")
  {
    return this._returnCode(LS_ERROR.MISSED_APPLICATION);
  }

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsQuery") +
    "&_callBack="             + this.answerCallBack +
    "&_callBackData="         + this.answerData +
    "&_getQueryAnalysis="     + (this.getQueryAnalysis ? 1 : 0) +
    "&_searchEngineAlias="    + pSearchEngineAlias +
    "&_maxAnswer="            + this.maxAnswer +
    "&_insensitiveCategory="  + this.insensitiveCategory +
    "&_query="                + escape(this.currentQuery);

  if (this.currentQueryLanguage && this.currentQueryLanguage != "")
  {
    url += "&_langue="  + this.currentQueryLanguage;
  }

  // We do not generate trace only if explicitly required.
  if (!this.lsServerTrace)
  {
    url += "&_trace=0";
  }

  if (pFaqBases != null)
  {
    // FaqBases specification.
    for (currentFaqBase in pFaqBases)
    {
      url += "&faqBase=" + pFaqBases[currentFaqBase];
    }
  }

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}


//
// Function which displays an empty page, with the application-specific stuff.
//
function _lsMainGetEmptyPage(pWindow)
{
  var url = this._makeServerUrl("lsEmptyPage");
  pWindow.location = url;
}


//
// Main function to call to have a query answered.
//
function _lsMainAnswer(pQuery, pFaqBases, pSearchEngineAlias,
                       pTemporaryWindow, pAnswerCallBack, pAnswerData)
{
  this.initializeAnswer();

  // User CallBack and Data definition.
  if (pAnswerCallBack != null && pAnswerCallBack != "")
  {
    this.answerCallBack = pAnswerCallBack;
  }
  if (this.answerCallBack == null || this.answerCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pAnswerData != null)
  {
    this.answerData = pAnswerData;
  }

  if (pQuery == null || pQuery == "")
  {
    return this._returnCode(LS_ERROR.MISSED_QUERY);
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  this.currentQuery = pQuery;

  return this._callSearchEngine(pFaqBases, pSearchEngineAlias);
}


// Function which returns the linguistic analysis (index) of a query.
function _lsMainAnalyze(pQuery, pQueryMode, pUseClauses,
                        pTemporaryWindow, pAnalyzisCallBack, pAnalyzisData)
{
  // User CallBack and Data definition.
  if (pAnalyzisCallBack != null && pAnalyzisCallBack != "")
  {
    this.analyzisCallBack = pAnalyzisCallBack;
  }
  if (this.analyzisCallBack == null || this.analyzisCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pAnalyzisData != null)
  {
    this.analyzisData = pAnalyzisData;
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  var actualQuery = pQuery.replace(/\’/g, "'");    // Char #146 by char #39
  var useClauses  = (pUseClauses) ? 1 : 0;

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsQueryAnalysis") +
    "&_callBack="     + this.analyzisCallBack +
    "&_callBackData=" + this.analyzisData +
    "&_query="        + escape(actualQuery) +
    "&_queryMode="    + pQueryMode +
    "&_useClauses="   + useClauses;

  if (this.currentQueryLanguage && this.currentQueryLanguage != "")
  {
    url += "&_langue="  + this.currentQueryLanguage;
  }

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}


// Function which retrieves available FaqBases.
function _lsMainGetFaqBases(pTemporaryWindow, pGetFaqBasesCallBack, pGetFaqBasesData)
{
  if (this.currentApplication == null || this.currentApplication == "")
  {
    return this._returnCode(LS_ERROR.MISSED_APPLICATION);
  }

  // User CallBack and Data definition.
  if (pGetFaqBasesCallBack != null && pGetFaqBasesCallBack != "")
  {
    this.getFaqBasesCallBack = pGetFaqBasesCallBack;
  }
  if (this.getFaqBasesCallBack == null || this.getFaqBasesCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pGetFaqBasesData != null)
  {
    this.getFaqBasesData = pGetFaqBasesData;
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsGetFaqBases") +
    "&_callBack="     + this.getFaqBasesCallBack +
    "&_callBackData=" + this.getFaqBasesData;

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}


// Function which retrieves main informations from Runtime Server.
function _lsMainGetMainInformations(pTemporaryWindow, pGetMainInformationsCallBack, pGetMainInformationsData)
{
  // User CallBack and Data definition.
  if (pGetMainInformationsCallBack != null && pGetMainInformationsCallBack != "")
  {
    this.getMainInformationsCallBack = pGetMainInformationsCallBack;
  }
  if (this.getMainInformationsCallBack == null || this.getMainInformationsCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pGetMainInformationsData != null)
  {
    this.getMainInformationsData = pGetMainInformationsData;
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsGetMainInformations") +
    "&_callBack="     + this.getMainInformationsCallBack +
    "&_callBackData=" + this.getMainInformationsData;

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}


// Function which retrieves available search engines configurations
function _lsMainGetSearchEngines(pTemporaryWindow, pGetSearchEnginesCallBack, pGetSearchEnginesData)
{
  // User CallBack and Data definition.
  if (pGetSearchEnginesCallBack != null && pGetSearchEnginesCallBack != "")
  {
    this.getSearchEnginesCallBack = pGetSearchEnginesCallBack;
  }
  if (this.getSearchEnginesCallBack == null || this.getSearchEnginesCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pGetSearchEnginesData != null)
  {
    this.getSearchEnginesData = pGetSearchEnginesData;
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsGetSearchEngines") +
    "&_callBack="     + this.getSearchEnginesCallBack +
    "&_callBackData=" + this.getSearchEnginesData;

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}

// Function which retrieves available FaqBases.
function _lsMainGetApplications(pTemporaryWindow, pGetApplicationsCallBack, pGetApplicationsData)
{
  // User CallBack and Data definition.
  if (pGetApplicationsCallBack != null && pGetApplicationsCallBack != "")
  {
    this.getApplicationsCallBack = pGetApplicationsCallBack;
  }
  if (this.getApplicationsCallBack == null || this.getApplicationsCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pGetApplicationsData != null)
  {
    this.getApplicationsData = pGetApplicationsData;
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsGetApplications") +
    "&_callBack="     + this.getApplicationsCallBack +
    "&_callBackData=" + this.getApplicationsData;

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}


// Function which retrieves protection data associated to an application.
function _lsMainGetApplicationProtect(pTemporaryWindow,
                                      pGetApplicationProtectCallBack, pGetApplicationProtectData)
{
  // User CallBack and Data definition.
  if (pGetApplicationProtectCallBack != null && pGetApplicationProtectCallBack != "")
  {
    this.getApplicationProtectCallBack = pGetApplicationProtectCallBack;
  }
  if (this.getApplicationProtectCallBack == null || this.getApplicationProtectCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pGetApplicationProtectData != null)
  {
    this.getApplicationProtectData = pGetApplicationProtectData;
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsGetApplicationProtect") +
    "&_callBack="     + this.getApplicationProtectCallBack +
    "&_callBackData=" + this.getApplicationProtectData;

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}


// Function which retrieves path informations about a folder
function _lsMainGetPathFolder(pFolderPath, pFolderCode,
                              pTemporaryWindow, pGetPathFolderCallBack, pGetPathFolderData)
{
  // User CallBack and Data definition.
  if (pGetPathFolderCallBack != null && pGetPathFolderCallBack != "")
  {
    this.getPathFolderCallBack = pGetPathFolderCallBack;
  }
  if (this.getPathFolderCallBack == null || this.getPathFolderCallBack == "")
  {
    return this._returnCode(LS_ERROR.MISSED_CALLBACK);
  }

  if (pGetPathFolderData != null)
  {
    this.getPathFolderData = pGetPathFolderData;
  }

  // Temporary window definition.
  if (pTemporaryWindow != null)
  {
    this.temporaryWindow = pTemporaryWindow;
  }
  if (this.temporaryWindow == null)
  {
    return this._returnCode(LS_ERROR.MISSED_TEMPORARY_WINDOW);
  }

  // Call Tacsy server, which in turn will display an HTML page
  //   in temporary window, from which we will get answers...
  var url = this._makeServerUrl("lsGetPathFolder") +
    "&_callBack="     + this.getPathFolderCallBack +
    "&_callBackData=" + this.getPathFolderData +
    "&_path="         + escape(pFolderPath) +
    "&_code="         + escape(pFolderCode);

  this._manageTemporaryWindow(url);
  return this._returnCode(LS_ERROR.SUCCESSFUL);
}


function _lsMainSanitize(pString)
{
  var resultString = "";

  for (stringIndex = 0; stringIndex < pString.length; ++stringIndex)
  {
    // Non-printable characters.
    var currentCode = pString.charCodeAt(stringIndex);
    if (currentCode <= 31)
    {
      resultString += ' ';
    }
    else
    {
      var currentChar   = pString.charAt(stringIndex);
      var replaceString = this._specialChar[escape(currentChar)];
      if (replaceString)
      {
        resultString += replaceString;
      }
      else
      {
        resultString += currentChar;
      }
    }
  }

  return resultString;
}


//
// LsMain Constructor.
//
function LsMain()
{
  // "Public" methods.
  this.initializeAnswer      = _lsMainInitAnswer;
  this.answer                = _lsMainAnswer;
  this.analyze               = _lsMainAnalyze;
  this.getMainInformations   = _lsMainGetMainInformations;
  this.getApplications       = _lsMainGetApplications;
  this.getApplicationProtect = _lsMainGetApplicationProtect;
  this.getSearchEngines      = _lsMainGetSearchEngines;
  this.getFaqBases           = _lsMainGetFaqBases;
  this.getPathFolder         = _lsMainGetPathFolder;
  this.getEmptyPage          = _lsMainGetEmptyPage;

  this.sanitize            = _lsMainSanitize;

  // Contextual properties.
  // They can be modified as usual properties, or by calling answer() method.
  this.temporaryWindow               = null;
  this.answerCallBack                = "";
  this.answerData                    = null;
  this.analyzisCallBack              = "";
  this.analyzisData                  = null;
  this.getFaqBasesCallBack           = "";
  this.getFaqBasesData               = null;
  this.getMainInformationsCallBack   = "";
  this.getMainInformationsData       = null;
  this.getSearchEnginesCallBack      = "";
  this.getSearchEnginesData          = null;
  this.getApplicationsCallBack       = "";
  this.getApplicationsData           = null;
  this.getApplicationProtectCallBack = "";
  this.getApplicationProtectData     = null;
  this.getPathFolderCallBack         = "";
  this.getPathFolderData             = null;
  this.user                          = "";
  this.getQueryAnalysis              = false;    // Must the query analysis be returned with the answers ?

  // Aliases properties.
  this._lrAliasDefault    = "/lr21";
  this._lrApiAliasDefault = "/lrApi21";
  this._lrCgiAliasDefault = "/lrCgi21";

  this.lrAlias    = this._lrAliasDefault;
  this.lrApiAlias = this._lrApiAliasDefault;
  this.lrCgiAlias = this._lrCgiAliasDefault;

  // Tacsy server properties.
  this.lsServerUrl        = this.lrCgiAlias + "/lrAdminCgi";
  this.lsServerHost       = "localhost";
  this.lsServerPort       = "12221";
  this.lsServerTrace      = 1;

  // Tacsy shadow properties.
  this.lsServerShadowUrl  = this.lrCgiAlias + "/lsShadowCgi";
  this.lsServerBaseUrl    = "";
  this.proxy              = "";
  this.useShadow          = false;

  // Application properties.
  this.currentApplication  = "";
  this.maxAnswer           = -1;   // -1 == application default's value, 0 == no limit, >0 == limit's specification.
  this.insensitiveCategory = -1;   // -1 == application default's value, 0 == sensitive categories, >0 == insensitive categories.

  this.crossLanguageMessage = "";  // Default : server message when cross language is not allowed.

  // "Private" methods.
  this._returnCode             = _lsReturnCode;
  this._manageTemporaryWindow  = _lsMainManageTemporaryWindow;
  this._callSearchEngine       = _lsMainCallSearchEngine;
  this._makeServerUrl          = _lsMainMakeServerUrl;

  // Initialization of special characters, and their ANSI "equivalents".
  this._specialChar = new Array();

  // Initialization of current query properties.
  this.initializeAnswer();
}

