/*
 File:             csconfig.js
 Version:          1.0
 Last updated:     11-11-2003
 Last updated by:  NE
 Copyright:        © 2003 C-CONTENT b.v. 
 Description:      client site configuration storage
 Required modules: -
 Output:           -
*/

function _getById(anArray, sId, bGetFirstOnEmptyId)
{
  if ((sId == "") && bGetFirstOnEmptyId && (anArray.length > 0))
    return(anArray[0]);
    
  var oArrayElement;
  var iArrayElement = 0;
  while(!oArrayElement && (iArrayElement<anArray.length))
  {
    if (anArray[iArrayElement].id && (anArray[iArrayElement].id == sId))
      oArrayElement = anArray[iArrayElement];
    else
      iArrayElement++;
  }
  return(oArrayElement);
}

/* -----------------------------------------------------------------------
   Object:  CsHitlistElement
   Purpose: Wraps a single hit list (shortinfo) field of an eXtrect index 
            set
   ----------------------------------------------------------------------- */
function CsHitlistElement(sHleId, sHleName, sHleXPath)
{
  this.hitlist;
  this.id = sHleId;
  this.index = -1;
  this.name = sHleName;
  this.xpath = sHleXPath;
  this.selected = true;
}

/* -----------------------------------------------------------------------
   Object:  CsHitlist
   Purpose: Wraps a hit list (shortinfo) of an eXtrect index set
   ----------------------------------------------------------------------- */
function CsHitlistAddElement(oHitlistElement)
{
  oHitlistElement.index = this.elements.push(oHitlistElement)-1;
  oHitlistElement.hitlist = this;
}

function CsHitlistGetElementById(sHleId)
{
  return(_getById(this.elements, sHleId, false));
}

function CsHitlistGetSelected(sGetType)
{
  if ((sGetType == "xpath") || (sGetType == "name"))
  {
    sRes = "";
    for (var i = 0; i < this.elements.length; i++)
    { 
      if (this.elements[i].selected)
        sRes += ((sRes)?String.fromCharCode(254):"")+eval("this.elements[i]."+sGetType);
    }
    return(sRes);
  }
  else if (sGetType == "object")
  {
    aRes = new Array();
    for (var i = 0; i < this.elements.length; i++)
    { 
      if (this.elements[i].selected)
        aRes.push(this.elements[i]);
    }
    return(aRes);
  }
  
}

function CsHitlist(sHlId, sHlName, bColumnMode)
{
  //methods
  this.addElement = CsHitlistAddElement;
  this.getElementById = CsHitlistGetElementById;
  this.getSelected = CsHitlistGetSelected;
  //properties
  this.id = sHlId;
  this.name = sHlName;
  this.elements = new Array();
  this.columnMode = bColumnMode;
}

/* -----------------------------------------------------------------------
   Object:  CsSearchGroup
   Purpose: Wraps a search group of an eXtrect index set
   ----------------------------------------------------------------------- */
function CsSearchGroup(sSGId, sSGName, sSGType, bHasWordlist, bHasThesaurus)
{
  //private
  //public
  this.id = sSGId;
  this.index = -1;
  this.name = sSGName;
  this.type = sSGType;
  this.wordlist = ((sSGType == 'text') && bHasWordlist);
  this.thesaurus = bHasThesaurus;
  this.searchForm;
}

/* -----------------------------------------------------------------------
   Object:  CsSearchForm
   Purpose: Wraps a search form of an eXtrect index set
   ----------------------------------------------------------------------- */

function CsSearchFormAddSearchGroup(oSearchGroup)
{
  oSearchGroup.index = this.searchGroups.push(oSearchGroup)-1;
  oSearchGroup.searchForm = this;
}

function CsSearchFormGetSearchGroupById(sSGId)
{
  return(_getById(this.searchGroups, sSGId, false));
}

function CsSearchForm(sSFId, sSFName, sTransType)
{
  //private
  //public
  this.database;
  this.id = sSFId;
  this.index = -1;
  this.name = sSFName;
  this.searchGroups = new Array();
  this.addSearchGroup = CsSearchFormAddSearchGroup;
  this.getSearchGroupById = CsSearchFormGetSearchGroupById;
  this.operators = true;
  this.transtype = (sTransType)?sTransType:"advanced;normal;"; 
}

/* -----------------------------------------------------------------------
   Object:  CsToc, CsTocNode
   Purpose: Contains a tree of the opened nodes
   ----------------------------------------------------------------------- */
function _createId(aSplittedId, iLevel)
{
  var sId = "";
  for(i=0;i<=iLevel;i++)
    sId += ((sId!="")?".":"")+aSplittedId[i];
  return(sId);
}

function CsTocNodeToString()
{
  var sStr = this.id;
  var sStrChilds = "";
  for (var i in this.childNodes)
  {
    if (this.childNodes[i])
      sStrChilds += ((sStrChilds != "")?",":"")+this.childNodes[i].toString();
  }
  if (sStrChilds != "")
    sStr = sStr + "{" + sStrChilds + "}";
  return(sStr);
}

function CsTocNode(sId, oParentNode)
{
  //methods
  this.toString = CsTocNodeToString;
  //properties
  this.id = sId;
  this.childNodes = new Array();
  this.parentNode = oParentNode;
}

function CsTocAddNode(sId)
{
  return(this.getNode(sId, true));
}

function CsTocDeleteNode(sId)
{
  var oTocNode = this.getNode(sId);
  if (oTocNode)
  {
    var oParentNode = oTocNode.parentNode;
    if (oParentNode)
      oParentNode.childNodes[oTocNode.id] = null;
  }
}

function CsTocGetNode(sId, bCreate)
{
  var i = 0;
  var aSplittedId = sId.split(".");
  var oChildNode = this.childNodes[aSplittedId[i]];
  if (!oChildNode && bCreate)
  {
    oChildNode = this.childNodes[aSplittedId[i]] = new CsTocNode(aSplittedId[i], this);
  }
  i+=1;
  while (oChildNode && i<aSplittedId.length)
  {
    oParentNode = oChildNode;
    oChildNode = oChildNode.childNodes[aSplittedId[i]];
    if (!oChildNode && bCreate)
      oChildNode = oParentNode.childNodes[aSplittedId[i]] = new CsTocNode(aSplittedId[i], oParentNode);
    i+=1;
  }
  return(oChildNode);
}

function CsTocToString()
{
  var sStr = "";
  var sStrChilds = "";
  for (var i in this.childNodes)
  {
    if (this.childNodes[i])
      sStrChilds += ((sStrChilds != "")?",":"")+this.childNodes[i].toString();
  }
  if (sStrChilds != "")
    sStr = sStr + "{" + sStrChilds + "}";
  return(sStr);
}


function CsTocClear()
{
  this.childNodes = new Array();
}

function CsToc()
{
  //methods
  this.addNode = CsTocAddNode;
  this.deleteNode = CsTocDeleteNode;
  this.getNode = CsTocGetNode;
  this.toString = CsTocToString;
  this.clear = CsTocClear; //CT.2004.05.26
  
  //properties
  this.childNodes = new Array();
  this.fileUrl = "";

  //alert(CsToc);
}
    
/* -----------------------------------------------------------------------
   Object:  CsDatabase
   Purpose: Wraps an eXtrect index set
   ----------------------------------------------------------------------- */
function CsDatabaseAddHitlist(oHitlist)
{
  oHitlist.index = this.hitlists.push(oHitlist)-1;
  oHitlist.database = this;
}

function CsDatabaseAddSearchForm(oSearchForm)
{
  oSearchForm.index = this.searchForms.push(oSearchForm)-1;
  oSearchForm.database = this;
}

function CsDatabaseGetHitlistById(sHlId)
{
  return(_getById(this.hitlists, sHlId, true));
}

function CsDatabaseGetSearchFormById(sSFId)
{
  return(_getById(this.searchForms, sSFId, true));
}

function CsDatabase(sDtbId, sDtbLocation, sDtbName)
{
  //methods
  this.addHitlist = CsDatabaseAddHitlist;
  this.getHitlistById = CsDatabaseGetHitlistById; 
  this.addSearchForm = CsDatabaseAddSearchForm;
  this.getSearchFormById = CsDatabaseGetSearchFormById;
  //properties
  this.id = sDtbId;
  this.hasRelevantWords = false;
  this.hitlists = new Array();
  this.location = sDtbLocation;
  this.name = sDtbName;
  this.searchForms = new Array();
  this.toc = new CsToc();
}

/* -----------------------------------------------------------------------
   Object:  CsConfig
   Purpose: Implement the client site config functionality
   ----------------------------------------------------------------------- */

function CsConfigAddDatabase(oDatabase)
{
  this.databases.push(oDatabase);
  //this.databases[oDatabase.id] = oDatabase;
}

function CsConfigGetDatabaseById(sDtbId)
{
  return(_getById(this.databases, sDtbId, true));
}

function CsConfig()
{
  //methods
  this.addDatabase = CsConfigAddDatabase;
  this.getDatabaseById = CsConfigGetDatabaseById;
  //properties
  this.currentDtb;
  this.currentDtbId = "";
  this.currentSearchFormId = "";
  this.currentLang = "";
  this.databases = new Array();
  this.operators = Array();
  this.operators["and"] = "and";
  this.operators["or"] = "or";
  this.operators["not"] = "not";
  this.operators["near"] = "near";
  this.operators["till"] = "till";
  this.text = new Array();
  this.text["range"] = Array();
  this.text["range"]["from"] = "from";
  this.text["range"]["untill"] = "untill";
  this.frames = new Array();
  this.frames["search"] = "frameRight";
  this.frames["hitlist"] = "frameRight";
  this.frames["document"] = "frameRight";
  this.frames["toc"] = "frameLeft";
}

var csconfig = new CsConfig();

