
/*
Module: Finder.js
    This Module provides the Finder Explore and Search user interface and functionality
    to the MyBST Verse Display and the Reference Tab.

*/
var referenceLibraryRootId = 136594;  // root id of reference library

function CreateCollection(ClassName)
{
    /*
    Function: CreateCollection
        This Function is used to create the collection class.

    Parameters:
        ClassName - the name of the new class

    Returns:
        A new collection by the name supplied

    */
    var obj=new Array();
    eval("var t=new "+ClassName+"()");
    for(_item in t)
        {
            eval("obj."+_item+"=t."+_item);
        }
    return obj;
}

function LevelCollection()
{
    /*
    Class: LevelCollection
        Contains a collection of Levels.

    */
    this.Container="";
    this.Add=function(obj)
    {
        this.push(obj);
    }
    this.Set=function(number, title, dataaccess, width)
    {
        this[number].Title = title;
        this[number].DataAccess = dataaccess;
    }
}

function Level(title, dataaccess, width)
{
    /*
    Class: Level
        Contains drop down titles and data access function names.

    Parameters:
        title - drop down selection
        dataaccess - data access funtion name to fill the next level.

    */
    this.Title = title;
    this.Usage = "";
    this.Select = "";
    this.DataAccess = dataaccess;
}

function FinderCollection()
{
    /*
    Class: FinderCollection
        Contains a collection of Finders.

    */
    this.Container="";
    this.Add=function(obj)
    {
        this.push(obj);
    }
}

function Finder(index)
{
    /*
    Class: Finder
        Contains all the properties for a Finder Explorer tab in a left or right verse text panel.
        Adds a Level Collection and a Search Collection to itself.

    Parameters:
        index - 1 or 2 for the left or right panel.

    */
    this.Finderopenedflag = 0; //1 when initialized
    this.Index = index; //panel in use (1=left, 2=right)
    this.IsReferenceFinder = 0;
    this.CurrentFind = 0; //current dropdown list level
    this.CurrentSearch = 0; //current dropdown list level

    this.Searchopenedflag = 0;
    this.Searchtranslationoptions = "";

    this.DropdownChoice1 = "";
    this.DropdownChoice2 = "";
    this.DropdownChoice3 = "";
    this.DropdownChoice4 = "";

    //this.Trace = "";

    this.Levels = new CreateCollection("LevelCollection"); //levels of dropdown lists, top is 0
    this.Search = new CreateCollection("LevelCollection"); //levels of dropdown lists, top is 0

    this.FinderSelectLibrary = function(object)
    {//
        /*
        Function: FinderSelectLibrary
            Create titles and decide data access for dropdown lists.
            Used only when the first dropdown list is clicked.
            Decide which library is being used.
            Calls FinderAddList

        Parameters:
            object - the drop down list

        Returns:
            Fills Levels with drop down list information

        */
        var selectedvalue;
        selectedvalue = object[object.selectedIndex].text; //this would be Bibles if the first is clicked
        if (selectedvalue > "")
        {
            //delete all the lists except the first one
            var loopct = this.Levels.length-1;
            for(var i=0;i<loopct;i++)
                this.Levels.pop();
            this.CurrentFind=0;
            //this switch is used to set the current library when the first drop down list is clicked
            switch (selectedvalue) 
            {
                case LocalStrings.ApocryphalWritings :
                    this.Levels.Add(new Level(LocalStrings.ApocryphalWritings, "Apocryphal-Translations", ""));
                    this.Levels.Add(new Level(LocalStrings.Books, "Bibles-Books", ""));
                    this.Levels.Add(new Level(LocalStrings.Chapters, "Bibles-Chapters", ""));
                    break;
                case LocalStrings.Bibles :
                    //this set of levels has a static structure
                    this.Levels.Add(new Level(LocalStrings.Translations, "Bibles-Translations", ""));
                    this.Levels.Add(new Level(LocalStrings.Books, "Bibles-Books", ""));
                    this.Levels.Add(new Level(LocalStrings.Chapters, "Bibles-Chapters", ""));
                    break;
                case LocalStrings.Commentaries :
                    this.Levels.Add(new Level(LocalStrings.Commentaries, "ReferenceLibrary", ""));
                    break;
                case LocalStrings.Concordances :
                    this.Levels.Add(new Level(LocalStrings.Concordances, "ReferenceLibrary", ""));
                    break;
                case LocalStrings.Dictionaries :
                    this.Levels.Add(new Level(LocalStrings.Dictionaries, "ReferenceLibrary", ""));
                    break;
                case LocalStrings.Encyclopedias :
                    this.Levels.Add(new Level(LocalStrings.Encyclopedias, "ReferenceLibrary", ""));
                    break;
                case LocalStrings.History :
                    this.Levels.Add(new Level(LocalStrings.History, "ReferenceLibrary", ""));
                    break;
                case LocalStrings.Lexicons :
                    this.Levels.Add(new Level(LocalStrings.Lexicons, "ReferenceLibrary", ""));
                    break;
                case LocalStrings.OtherResources :
                    this.Levels.Add(new Level(LocalStrings.OtherResources, "ReferenceLibrary", ""));
                    break;
                case LocalStrings.Devotionals :
                    this.Levels.Add(new Level(LocalStrings.Devotionals, "Devotionals", ""));
                    this.Levels.Add(new Level(LocalStrings.DevotionalArticles, "Devotional-Articles", ""));
                    break;
                case LocalStrings.StudyResources :
                    this.Levels.Add(new Level(LocalStrings.StudyResources, "Bible-Study-Groups", ""));
                    this.Levels.Add(new Level(LocalStrings.BibleStudyTopics, "Bible-Study-Topics", ""));
                    this.Levels.Add(new Level(LocalStrings.BibleStudyArticles, "Bible-Study-Articles", ""));
                    break;
                default :
                    this.Levels.Add(new Level(LocalStrings.Libraries, "Libraries", ""));
            }
            this.FinderAddList(object);
        }
    }

    //the code from here on is basically executed in sequence for every dropdown list click
    //each time, it uses only one of the get... webservice calls

    this.FinderAddList = function(clicked)
    {
        /*
        Function: FinderAddList
        Add a new dropdown list (populate and make visible a hidden one)
        Reposition lists based on clicked list
        Check to see if the next level exists
        Calls FinderFillALevel, FinderEnableGo, or FinderCreateDisplay

        Parameters:
        clicked - the drop down list entry selected

        Returns:
        Fills the next level of drop down list

        */
        var id = clicked.id;
        var clickednumber = parseInt(id.charAt(id.length - 2, 1));
        this.CurrentFind = clickednumber + 1;

        // check for login-more item - prevent from processing
        if (clicked[clicked.selectedIndex].value == 'login-more')
        {
            clicked.selectedIndex = clicked.selectedIndex - 1;
            dashboardLoginExpand();
            return;
        }

        //check to see if the next level exists
        var levelcount = this.Levels.length - 1;
        if (levelcount < this.CurrentFind)
        {
            this.Levels.Add(new Level("", "ReferenceLibrary", ""));
        }

        //see if the usage of next list has changed by comparing currently selected with previous usage
        var selectedvalue;
        selectedvalue = clicked[clicked.selectedIndex].value;
        var selectedtext;
        selectedtext = clicked[clicked.selectedIndex].text;


        //this loop occurs only once
        for (var f = 0; f < 1; f++)
        {
            //reference library selectedvalues might begin with a vertical bar indicating a file instead of a directory
            //these must be a better way to determine this
            if (selectedvalue.charAt(0) == "|")
            {
                //remove the bar and display the reference document
                this.FinderEnableGo(2, clicked);
                break;
            }

            //otherwise, continue as usual
            //put new list in current list
            if (this.Levels[this.CurrentFind].Title == "")
            {
                this.Levels[this.CurrentFind].Title = LocalStrings.Choose;
            }
            assign("finderlisttitle" + eval(this.CurrentFind) + eval(this.Index), this.Levels[this.CurrentFind].Title);

            if (this.Levels[this.CurrentFind - 1].Usage == LocalStrings.Devotionals)
            {
                //this is a devotional article
                this.FinderFillALevel(selectedvalue);
                break;
            }

            if (this.Levels[this.CurrentFind - 1].Usage == LocalStrings.StudyResources)
            {
                //this is a Study Resources article
                this.FinderFillALevel(selectedvalue);
                break;
            }

            if (this.Levels[this.CurrentFind - 1].Usage == LocalStrings.BibleStudyTopics)
            {
                //this is a Study Resources article
                this.FinderFillALevel(selectedvalue);
                break;
            }

            if (selectedvalue != this.Levels[this.CurrentFind].Usage)
            {
                //get the data
                this.FinderFillALevel(selectedvalue);
                break;
            }

            finders[this.Index].FinderCreateDisplay();
            break;
        }
    }

    this.FinderAddListInt=function(ddlfindernumber)
    {
        /*
        Function: FinderAddListInt
            This function is a helper for using FinderAddList from the bread crumb.
            There were too many levels of single-quotes and double-quotes in the select to do this in the onclick().
            Calls FinderAddList

        Parameters:
            ddlfindernumber - the number of the drop down list entry to select

        Returns:
            nothing

        */
        //if the finder is not open, open it

        if ( ! isFinderOpen(this.Index)) openFinder('e', this.Index, '', '', '');

        var ddl = document.getElementById("ddlfinder" + eval(ddlfindernumber) + eval(this.Index));
				this.FinderAddList(ddl);
    }

    this.FinderFillALevel = function(usage)
    {
        /*
        Function: FinderFillALevel
            During initialization, FillALevel is called to populate the Library list -- FinderFillALevel("Libraries")
            Load the values into a dropdown list
            Determine the function to access the list information
            Calls one of these webservice getfinder... functions

        Parameters:
            usage - the drop down list entry selected

        Returns:
            nothing

        */

        // No action necessary if blank value has been used
        if (usage.length == 0)
					return;


        document.getElementById("btnFindOn" + eval(index)).style.visibility = "hidden";
        if (usage == "Libraries")
        {
            this.CurrentFind = 0;
            this.getfinderlibraries(usage);
        } 
        else 
        {

            switch (this.Levels[this.CurrentFind].DataAccess) 
            {
                case "Apocryphal-Translations" :
                    this.getfinderapocryphaltranslations(usage);
                    break;
                case "Bibles-Translations" :
                    this.getfindertranslations(usage);
                    break;
                case "Bibles-Books" :
                    this.getfinderbooks(usage);
                    break;
                case "Bibles-Chapters":
                    this.getfinderchapters(usage);
                    break;
                case "ReferenceLibrary" :
                    this.getfinderreferencelibrary(usage);
                    break;
                case "Devotionals" :
                    this.getfinderdevotionals(usage);
                    break;
                case "Devotional-Articles" :
                    this.getfinderdevotionalarticles(usage);
                    break;
                case "Bible-Study-Groups" :
                    this.getfinderbiblestudygroups(usage);
                    break;
                case "Bible-Study-Topics" :
                    this.getfinderbiblestudytopics(usage);
                    break;
                case "Bible-Study-Articles" :
                    this.getfinderbiblestudyarticles(usage);
                    break;
                default :
                    break;
            }
        }
    }

    this.getfinderlibraries=function(usage)
    {
        /*
        Function: getfinderlibraries
            Data retrieval to get the libraries list.
            Calls BibleStudyTools.Site.BLLService.GetLibraries

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"Libraries"+","+firstoption;
        BibleStudyTools.Site.BLLService.GetLibraries(referenceLibraryRootId, "~/SiteInfo/Components/FinderLibraries.ascx", FinderWS_callback, FinderWSFailed_callback, context, null);
    }

    this.getfindertranslations = function(usage)
    {
        /*
        Function: getfindertranslations
            Data retrieval to get the translations list.
            Calls BibleStudyTools.Site.BLLService.GetFinderTranslationsByRegion

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"Bibles"+","+firstoption;
        BibleStudyTools.Site.BLLService.GetFinderTranslationsByRegion("N","~/SiteInfo/Components/FinderTranslations.ascx",LocalStrings.culture,FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderapocryphaltranslations = function(usage)
    {
        /*
        Function: getfinderapocryphaltranslations
            Data retrieval to get the apocryphal translations list.
            Calls BibleStudyTools.Site.BLLService.GetFinderApocryphalTranslations

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"Apocryphals"+","+firstoption;
        BibleStudyTools.Site.BLLService.GetFinderApocryphalTranslations("N","~/SiteInfo/Components/FinderTranslations.ascx",FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderbooks = function(usage)
    {
        /*
        Function: getfinderbooks
            Data retrieval to get the books list.
            Calls BibleStudyTools.Site.BLLService.GetFinderBooks

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"Books"+","+firstoption;

        var apocryphalOnly = false;
        if ((this.CurrentFind == 2) && (this.Levels[1].DataAccess == "Apocryphal-Translations"))
					apocryphalOnly = true;

				if (apocryphalOnly)
					BibleStudyTools.Site.BLLService.GetFinderBooks(usage,"D","~/SiteInfo/Components/FinderBooks.ascx",FinderWS_callback,FinderWSFailed_callback,context,null);
				else
	        BibleStudyTools.Site.BLLService.GetFinderBooks(usage,"A","~/SiteInfo/Components/FinderBooks.ascx",FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderchapters=function(usage)
    {
        /*
        Function: getfinderchapters
            Generates the chapters list from a static array of chapter counts per book.
            Calls FinderSaveAndDisplay

        Parameters:
            usage - the drop down list entry selected

        Returns:
            sets the contents of the chapters drop down list

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var chaptercounts = ",1ch,29,1co,16,1es,9,1jo,5,1ki,22,1ma,16,1pe,5,1sa,31,1th,5,1ti,6,2ch,36,2co,13,2es,16,2jo,1,2ki,25,2ma,15,2pe,3,2sa,24,2th,3,2ti,4,3jo,1,3ma,7,4ma,18,ac,28,am,9,aza,1,bar,6,bel,1,col,4,da,12,de,34,ec,12,eph,6,es,10,esx,10,ex,40,eze,48,ezr,10,ga,6,ge,50,hab,3,hag,2,heb,13,ho,14,isa,66,jas,5,jdt,16,jer,52,job,42,joe,3,joh,21,jon,4,jos,24,jud,21,jude,1,la,5,le,27,ljr,1,lu,24,mal,4,mic,7,mns,1,mr,16,mt,28,na,3,ne,13,nu,36,ob,1,phm,1,php,4,pr,31,ps,150,psx,1,re,22,ro,16,ru,4,sir,52,so,8,sus,1,tit,3,tob,14,wis,19,zec,14,zep,3,";
        var countstr = chaptercounts.substr(chaptercounts.indexOf("," + usage.toLowerCase() + ",") + 2 + usage.length,6);
        var chaptercount = countstr.substring(0,countstr.indexOf(","));
        this.Levels[this.CurrentFind].Select = "<select class='finderlistddl' id='ddlfinder3" + eval(this.Index) + "' onchange='finders[" + eval(this.Index) + "].FinderEnableGo(1,this);' ><option value=''></option>";
        for (var i = 1; i <= chaptercount; i++)
        {
            this.Levels[this.CurrentFind].Select = this.Levels[this.CurrentFind].Select + "<option value='" + eval(i) + "'>" + eval(i) + "</option>";
        }
        this.Levels[this.CurrentFind].Select = this.Levels[this.CurrentFind].Select + "</select>";
        this.FinderSaveAndDisplay(this.Levels[this.CurrentFind].Select);
    }

    this.getfinderreferencelibrary=function(usage)
    {
        /*
        Function: getfinderreferencelibrary
            Data retrieval to get the reference library lists.
            Calls BibleStudyTools.Site.BLLService.GetLibraries

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"ReferenceLibrary"+","+firstoption;
        BibleStudyTools.Site.BLLService.GetLibraries(usage,"~/SiteInfo/Components/FinderLibraries.ascx",FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderdevotionals = function(usage)
    {
         /*
        Function: getfinderdevotionals
            Data retrieval to get the devotional list.
            Calls BibleStudyTools.Site.CMSService.GetFinderDevotionals

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"Devotionals"+","+firstoption;
        BibleStudyTools.Site.CMSService.GetFinderDevotionals(FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderdevotionalarticles = function(usage)
    {
        /*
        Function: getfinderdevotionalarticles
            Data retrieval to get the devotional articles list.
            Calls BibleStudyTools.Site.CMSService.GetFinderDevotionalArticles

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"Devotional Articles"+","+firstoption;
        BibleStudyTools.Site.CMSService.GetFinderDevotionalArticles(usage,FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderbiblestudygroups = function(usage)
    {
        /*
        Function: getfinderbiblestudygroups
            Data retrieval to get the groups list.
            Calls BibleStudyTools.Site.CMSService.GetFinderBibleStudyGroups

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"GetFinderBibleStudyGroups"+","+firstoption;
        BibleStudyTools.Site.CMSService.GetFinderBibleStudyGroups(FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderbiblestudytopics = function(usage)
    {
        /*
        Function: getfinderbiblestudytopics
            Data retrieval to get the bible study topics list.
            Calls BibleStudyTools.Site.CMSService.GetFinderBibleStudyTopics

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"GetFinderBibleStudyTopics"+","+firstoption;
        BibleStudyTools.Site.CMSService.GetFinderBibleStudyTopics(usage,FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getfinderbiblestudyarticles = function(usage)
    {
        /*
        Function: getfinderbiblestudyarticles
            Data retrieval to get the bible study articles list.
            Calls BibleStudyTools.Site.CMSService.GetFinderArticleList

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Levels[this.CurrentFind].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentFind+","+this.Index+","+"GetFinderArticleList"+","+firstoption;
        BibleStudyTools.Site.CMSService.GetFinderArticleList(usage,FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.FinderSaveAndDisplay = function(selectstatement)
    {
        /*
        Function: FinderSaveAndDisplay
            Usually called by FinderWS_callback
            Save retrieved values, assign to dropdown list, format the display
            Make the list visible
            Calls FinderCreateDisplay

        Parameters:
            selectstatement - the options for a drop down list

        Returns:
            assigns the options to the dropdown list

        */
        document.getElementById("finderlist" + eval(this.CurrentFind) + eval(this.Index)).style.display = "block";
        document.getElementById("finderlistddl" + eval(this.CurrentFind) + eval(this.Index)).style.display = "block";
        assign("finderlistddl" + eval(this.CurrentFind) + eval(this.Index),selectstatement);
        this.FinderCreateDisplay();
    }

    this.FinderCreateDisplay = function()
    {
        /*
        Function: FinderCreateDisplay
            Enables the display of the drop down lists.
            Set the visible/hidden properties of the dropdown list and set the breadcrumb
            Loops to display first to last
            Creates the bread crumb unless being called from the References Tab.

        Parameters:
            none

        Returns:
            nothing

        */
        var i;

        if (this.IsReferenceFinder == 0)
        { //do not do this for a web page finder
            //hide all the lists
            for (i = 1; i <= 5; i++)
            {
                document.getElementById("finderlist" + eval(i) + eval(this.Index)).style.display = "none";
            }

            //make only the valid lists visible
            for (i = 1; i <= this.CurrentFind; i++)
            {
                document.getElementById("finderlist" + eval(i) + eval(this.Index)).style.display = "inline";
            }

            //update the breadcrumbs
            var ddlname = "";
            var breadcrumb = "";
            var finderelement;
            var selectedtext;
            for (i = 0; i < this.CurrentFind; i++)
            {
                ddlname = "ddlfinder" + eval(i) + eval(this.Index);
                finderelement = document.getElementById(ddlname);
                selectedtext = finderelement[finderelement.selectedIndex].text;
                breadcrumb += "<a href='javascript:finders[" + eval(this.Index) + "].FinderAddListInt(" + i + ")'>" + selectedtext + "</a>";
                if(i < this.CurrentFind - 1) breadcrumb += " <IMG SRC='" + skinImagePath + "arrow_red.gif' alt='>>'/>  ";
                //manage the use of each drop down with its parent drop down value
                this.Levels[i].Usage = finderelement[finderelement.selectedIndex].value;
            }
            if(breadcrumb=="") breadcrumb = "Library";

            //display the usages
            var st = "";
            for (i = 0; i <= this.CurrentFind; i++)
            {
                st = st + this.Levels[i].Usage + ":";
            }

            assign("breadcrumb" + eval(this.Index),breadcrumb);
        }
        else 
        {
            //for a web page finder, just show 2 and up
            //hide all the lists
        for (i = 0; i <= 5; i++)
            {
                document.getElementById("finderlist" + eval(i) + eval(this.Index)).style.display = "none";
            }

            //make only the valid lists visible
            for (i = 2; i <= this.CurrentFind; i++)
            {
                document.getElementById("finderlist" + eval(i) + eval(this.Index)).style.display = "inline";
            }
        }
    }

    this.FinderEnableGo = function(destination, clicked) 
    {
        /*
        Function: FinderEnableGo
            Calls

        Parameters:
            destination - the number of the drop down list
            clicked - the option selected from that drop down list

        Returns:
            Turns on the Go button

        */
        this.DropdownChoice1=destination;
        if(destination==1)
        {		//verse text
            this.DropdownChoice2=this.Levels[2].Usage;
            this.DropdownChoice3=clicked[clicked.selectedIndex].value;
            this.DropdownChoice4=this.Levels[1].Usage;
        }
        else
        {
            if(destination==2)
            { //reference library
                var documentid = clicked[clicked.selectedIndex].value;
                this.DropdownChoice2=documentid.substr(1,documentid.length-1);
                this.DropdownChoice3=clicked[clicked.selectedIndex].text;
                this.DropdownChoice4="";
            }
            else
            {
                if(destination==3)
                { //devotional
                    this.DropdownChoice2=clicked[clicked.selectedIndex].value;
                    this.DropdownChoice3=clicked[clicked.selectedIndex].text;
                    this.DropdownChoice4="";
                }
            }
        }

        document.getElementById("btnFindOn" + eval(index)).style.visibility = "visible";
        return true;
    }

    this.SearchSelectLibrary = function(ddl)
    {
        /*
        Function: SearchSelectLibrary
            Create titles and decide data access for dropdown lists.
            Used only when the first dropdown list is clicked.
            Decide which library is being used.
            Calls SearchAddList

        Parameters:
            ddl - the drop down list

        Returns:
            Fills Search levels with drop down list information

        */
        var selectedvalue;
        selectedvalue = ddl[ddl.selectedIndex].text; //this would be Bibles if the first is clicked
        if (selectedvalue > "")
        {
            //delete all the lists except the first one
            var loopct = this.Search.length-1;
            for(var i=0;i<loopct;i++)
                this.Search.pop();
            this.CurrentSearch=0;
            //this switch is used to set the current library when the first drop down list is clicked
            switch (selectedvalue) 
            {
                case "Bibles" :
                    //this set of levels has a static structure
                    this.Search.Add(new Level(LocalStrings.Translations, "Search-Translations", ""));
                    this.Search.Add(new Level(LocalStrings.PartOfBible, "Search-Parts", ""));
                    break;
                case "Reference Library" :
                    //this set of levels has a static structure
                    this.Search.Add(new Level(LocalStrings.ReferenceLibrary, "Search-ReferenceLibrary", ""));
                    break;
                default :
                    this.Search.Add(new Level(LocalStrings.Translations, "Search-Translations", ""));
                    this.Search.Add(new Level(LocalStrings.PartOfBible, "Search-Parts", ""));
                    break;
           }
            this.SearchAddList(ddl);
        }
    }

    //the code from here on is basically executed in sequence for every dropdown list click
    //each time, it uses only one of the get... webservice calls

    this.SearchAddList = function(ddl) 
    {
        /*
        Function: SearchAddList
            Add a new dropdown list (populate and make visible a hidden one)
            Reposition lists based on clicked list
            Check to see if the next level exists
            Calls SearchFillALevel or SearchCreateDisplay

        Parameters:
            ddl - the drop down list entry selected

        Returns:
            Fills the next level of drop down list

        */
        var id = ddl.id;
        var clickednumber = parseInt(id.charAt( id.length - 2,1));
        this.CurrentSearch = clickednumber + 1;


        // check for login-more item - prevent from processing
        if (ddl[ddl.selectedIndex].value == 'login-more')
        {
            ddl.selectedIndex = ddl.selectedIndex - 1;
            dashboardLoginExpand();
            return;
        }
        

        var levelcount = this.Search.length-1;
        if (levelcount < this.CurrentSearch)
        {
            this.Search.Add(new Level("", "Search-ReferenceLibrary", ""));
        }

        //see if the usage of next list has changed by comparing currently selected with previous usage
        var selectedvalue;
        selectedvalue = ddl[ddl.selectedIndex].value;
        var selectedtext;
        selectedtext = ddl[ddl.selectedIndex].text;

        if (selectedvalue != this.Search[this.CurrentSearch].Usage)
        {
            //put new list in current list
            if (this.Search[this.CurrentSearch].Title=="")
            {
                this.Search[this.CurrentSearch].Title= LocalStrings.Choose;
            }
            assign("searchlisttitle" + eval(this.CurrentSearch) + eval(this.Index),this.Search[this.CurrentSearch].Title);
            //get the data
            this.SearchFillALevel(selectedvalue);
        } 
        else 
        {
            this.SearchCreateDisplay();
        }
    }

    this.SearchFillALevel = function(usage)
    {
        /*
        Function: SearchFillALevel
            During initialization, FillALevel is called to populate the Library list -- SearchFillALevel("Libraries")
            Load the values into a dropdown list
            Determine the function to access the list information
            Calls one of these webservice getsearch... functions

        Parameters:
            usage - the drop down list entry selected

        Returns:
            nothing

        */
        switch (this.Search[this.CurrentSearch].DataAccess) 
        {
            case "Search-Translations" :
                this.getsearchtranslations(usage);
                break;
            case "Search-Parts" :
                this.getsearchparts(usage);
                break;
            case "Search-ReferenceLibrary" :
                this.getsearchlibraries(usage);
                break;
            default :
                break;
        }
    }

    this.getsearchtranslations = function(usage)
    {
        /*
        Function: getsearchtranslations
            Data retrieval to get the translations list.
            Calls BibleStudyTools.Site.BLLService.GetFinderTranslations

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Search[this.CurrentSearch].Usage = usage;
        var firstoption = "all";
        var context = this.CurrentSearch+","+this.Index+","+"Search-Translations"+","+firstoption;
        BibleStudyTools.Site.BLLService.GetFinderTranslationsByRegion("N","~/SiteInfo/Components/FinderTranslations.ascx",LocalStrings.culture,FinderWS_callback,FinderWSFailed_callback,context,null);
    }

    this.getsearchparts = function(usage)
    {
        /*
        Function: getsearchparts
            Generate the Parts of the Bible list from a static array.
            Calls SearchSaveAndDisplay

        Parameters:
            usage - the drop down list entry selected

        Returns:
            nothing

        */
        this.Search[this.CurrentSearch].Usage = usage;
        this.Search[this.CurrentSearch].Select = "<select class='finderlistddl' id='ddlsearch2" + eval(this.Index) + "' onchange='finders["+this.Index+"].SearchCreateDisplay()'>"
        this.Search[this.CurrentSearch].Select += "<option value='All'>" + LocalStrings.All + "</option><option value='ot'>" + LocalStrings.OldTestament + "</option><option value='nt'>" + LocalStrings.NewTestament + "</option><option value='apa'>" + LocalStrings.ApochryphalWritings + "</option><option value='apo'>" + LocalStrings.ApocalypticBooks + "</option><option value='gep'>" + LocalStrings.GeneralEpistles + "</option><option value='gos'>" + LocalStrings.Gospels + "</option><option value='his'>" + LocalStrings.HistoryBooks + "</option><option value='law'>" + LocalStrings.BooksOfTheLaw + "</option><option value='map'>" + LocalStrings.MajorProphets + "</option><option value='mip'>" + LocalStrings.MinorProphets + "</option><option value='pep'>" + LocalStrings.PaulsEpistles + "</option><option value='wis'>" + LocalStrings.WisdomBooks + "</option>"
        this.Search[this.CurrentSearch].Select += "</select>";
        this.SearchSaveAndDisplay(this.Search[this.CurrentSearch].Select);
    }

    this.getsearchlibraries = function(usage)
    {
        /*
        Function: getsearchlibraries
            Data retrieval to get the search libraries list.
            Calls BibleStudyTools.Site.BLLService.GetLibraries

        Parameters:
            usage - the drop down list entry selected

        Returns:
            context and web service result to FinderWS_callback

        */
        this.Search[this.CurrentSearch].Usage = usage;
        var firstoption = "blank";
        var context = this.CurrentSearch+","+this.Index+","+"Search-Libraries"+","+firstoption;
        BibleStudyTools.Site.BLLService.GetLibraries(referenceLibraryRootId, "~/SiteInfo/Components/FinderLibraries.ascx", FinderWS_callback, FinderWSFailed_callback, context, null);
    }

    this.SearchSaveAndDisplay = function(selectstatement)
    {
        /*
        Function: SearchSaveAndDisplay
            Usually called by FinderWS_callback
            Save retrieved values, assign to dropdown list, format the display
            Make the list visible
            Calls SearchCreateDisplay

        Parameters:
            selectstatement - the options for a drop down list

        Returns:
            assigns the options to the dropdown list

        */
        document.getElementById("searchlist" + eval(this.CurrentSearch) + eval(this.Index)).style.display = "block";
        document.getElementById("searchlistddl" + eval(this.CurrentSearch) + eval(this.Index)).style.display = "block";
        assign("searchlistddl" + eval(this.CurrentSearch) + eval(this.Index),selectstatement);
        this.SearchCreateDisplay();
    }

    this.SearchCreateDisplay = function()
    {
        /*
        Function: SearchCreateDisplay
            Enables the display of the drop down lists.
            Set the visible/hidden properties of the dropdown list and set the breadcrumb
            Loops to display first to last
            Creates the bread crumb.

        Parameters:
            none

        Returns:
            nothing

        */
        var i;
        //hide all the lists
        for (i = 1; i <= 2; i++)
        {
            document.getElementById("searchlist" + eval(i) + eval(this.Index)).style.display = "none";
        }
        //make only the valid lists visible
        for (i = 1; i <= this.CurrentSearch; i++)
        {
            document.getElementById("searchlist" + eval(i) + eval(this.Index)).style.display = "block";
        }

        //update the breadcrumbs
        var ddlname = "";
        var breadcrumb = LocalStrings.YouSearchedFor + " '" + document.getElementById("searchText" + eval(this.Index)).value + "' in ";
        var finderelement;
        var selectedtext;
        for (i = 0; i <= this.CurrentSearch; i++)
        {
            ddlname = "ddlsearch" + eval(i) + eval(this.Index);
            searchelement = document.getElementById(ddlname);
            selectedtext = searchelement[searchelement.selectedIndex].text;
            breadcrumb += "<b>" + selectedtext + "</b>";
            if(i <= this.CurrentSearch - 1) breadcrumb += " " + LocalStrings.In + " ";
            //manage the use of each drop down with its parent drop down value
            this.Search[i].Usage = searchelement[searchelement.selectedIndex].value;
        }
        if(breadcrumb=="") breadcrumb = LocalStrings.Searching;

        //display the usages
        var st = "";
        for (i = 0; i <= this.CurrentSearch; i++)
        {
            st = st + this.Search[i].Usage + ":";
        }

        assign("breadcrumb" + eval(this.Index),breadcrumb);
    }

}

//
//------------------- End of Finder Class ----------------------------------
//


function assign(itemname, content)
{
    /*
    Function: assign
        General function to get/assign a value to a document element

    Parameters:
        itemname - the element to assign a value
        content - the value to assign to the element

    Returns:
        Set the element value

    */
    document.getElementById(itemname).innerHTML = content;
}

function theValueOf(itemname)
{
    /*
    Function: theValueOf
        General function to get the value of an element

    Parameters:
        itemname - the name of the element to get the value of

    Returns:
        the value

    */
    return document.getElementById(itemname).value;
}

function theSelectedValueOf(itemname)
{
    /*
    Function: theValueOf
        General function to get the selected value of an drop down list

    Parameters:
        itemname - the name of the drop down list to get the selected value of

    Returns:
        the selected value

    */
    var finderelement;
    finderelement = document.getElementById(itemname);
    return finderelement[finderelement.selectedIndex].value;
}

//
//---------------------- Webservice Callback Section -----------------------------
//

function FinderWS_callback(result, context, methodName)
{
    /*
    Function: FinderWS_callback
        In the Finder, this function handles all web service return events for both Explore (find) and Search.
        Called when a web service method call completes.
        Analyzes the context to determine next action.
        Calls FinderSaveAndDisplay or SearchSaveAndDisplay

    Parameters:
        result - the text string from the web service, usually an option list
        context - the context set by the function that called the web service method
        methodName - the web service method called, not used here (used by FinderWSFailed_callback)

    Returns:
        nothing

    */
    var splitString = context.split(",")
    var ddllevel = splitString[0];
    var finderindex = splitString[1];
    var ddllist = splitString[2];
    var firstoptiontype = splitString[3];

    var firstoption = "";
    if (firstoptiontype == "blank")
    {
        firstoption="<option value=''></option>";
    }
    else 
    {
    if (firstoptiontype == "all")
        {
            firstoption="<option value='all'>" + LocalStrings.All + "</option>";
        }
    }

    switch (ddllist) 
    {
        //finder cases
        case "Libraries" :
            //remove the Lexicons <option value='n'>Lexicons</option> from the result
            var options = new RegExp("(\<option value='[0-9]+'\>Lexicons\</option\>)");
            var result = result.replace(options, "");

            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderSelectLibrary(this)'>" + firstoption + "<option value='Bibles'>" + LocalStrings.Bibles + "</option><option value='Apocryphal Writings'>" + LocalStrings.ApocryphalWritings + " </option>" + result + "<option value='Devotionals'>" + LocalStrings.Devotionals + " </option><option value='Study Resources'>" + LocalStrings.StudyResources + " </option></select>";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "Bibles":

            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder" + ddllevel + "" + finderindex + "' onchange='finders[" + finderindex + "].FinderAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);


            // check if a translation was already selected. If so, fire off next select event
            var finderId = 'ddlfinder' + ddllevel + finderindex;
            var finderList = document.getElementById(finderId);
            if (finderList.selectedIndex > 0)
                finders[finderindex].FinderAddList(finderList);

            break;
        case "Apocryphals" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "Books" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "ReferenceLibrary" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "Devotionals" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "Devotional Articles" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderEnableGo(3,this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "GetFinderBibleStudyGroups" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "GetFinderBibleStudyTopics" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;
        case "GetFinderArticleList" :
            finders[finderindex].Levels[ddllevel].Select = "<select class='finderlistddl' id='ddlfinder"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].FinderEnableGo(3,this)'>" + firstoption + result + "</select";
            finders[finderindex].FinderSaveAndDisplay(finders[finderindex].Levels[ddllevel].Select);
            break;

        //search cases
        case "Search-Translations" :
            finders[finderindex].Search[ddllevel].Select = "<select class='finderlistddl' id='ddlsearch"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].SearchAddList(this)'>" + firstoption + result + "</select";
            finders[finderindex].SearchSaveAndDisplay(finders[finderindex].Search[ddllevel].Select);
            break;
        case "Search-Libraries" :
            finders[finderindex].Search[ddllevel].Select = "<select class='finderlistddl' id='ddlsearch"+ddllevel+""+finderindex+"' onchange='finders["+finderindex+"].SearchCreateDisplay()' >" + firstoption + "<option value='Apocryphal Writings'>" + LocalStrings.ApocryphalWritings + " </option>" + result + "<option value='Devotionals'>" + LocalStrings.Devotionals + " </option><option value='Study Resources'>" + LocalStrings.StudyResources + " </option></select>";
            finders[finderindex].SearchSaveAndDisplay(finders[finderindex].Search[ddllevel].Select);
            break;
        default :
    }
}

function FinderWSFailed_callback(result, context, methodName)
{
    /*
    Function: FinderWSFailed_callback
        General function to handle web service calling problems.
        Displays an alert box with the error message.

    Parameters:
        result - the text string from the web service, usually an option list
        context - the context set by the function that called the web service method
        methodName - the web service method called

    Returns:
        Alert box with error message

    */
    alert(LocalStrings.ErrorInWebMethod + " '"+methodName+"'.\n" + LocalStrings.Message + "='"+result._message+"'\n" + LocalStrings.StatusCode + "="+result._statusCode+"\n" + LocalStrings.StackTrace + "='"+result._stackTrace+"'\n" + LocalStrings.ExceptionType + "='"+result._exceptionType+"'\n" + LocalStrings.TimedOut + "="+result._timedOut);
}

//
//---------------------- Main Finder Code Section -----------------------------
//

//these Finder variables in the GlobalVariables.js
//  finders, levelCollection, debug, graphicurl

//these functions used for all the instances of the finder

function openFinder(choice,index,library,level1,level2)
{
    /*
    Function: openFinder
        Open the Explore or Search tab and make the Finder window visible.
        Called by internal Finder.js function FinderAddListInt
        Called by Finder.ascx and ReferenceFinder.ascx user controls
        Instantiate the finder collection and all Level and Search collections for the finders
        Calls FinderAddList then openExplorer, or openSearch

    Parameters:
        choice - e for Explorer, s for Search
        index - 1 or 2 for verse text panel number, left or right
        library - optional library name
        level1 - optional level
        level2 - optional level for search library choice

    Returns:
        nothing

    */    
  var levelcollection1e;
  var levelcollection2s;
  var levelcollection1e;
  var levelcollection2s;

  if (finders == null)
  {
		//instantiate the finder collection
		finders = new CreateCollection("FinderCollection");

		//instantiate the level collections for the finders
		levelCollection1e = new CreateCollection("LevelCollection");
		levelCollection2e = new CreateCollection("LevelCollection");
		levelCollection1s = new CreateCollection("LevelCollection");
		levelCollection2s = new CreateCollection("LevelCollection");
		levelCollection1e.Add(new Level(LocalStrings.Libraries, "", ""));
		levelCollection2e.Add(new Level(LocalStrings.Libraries, "", ""));
		levelCollection1s.Add(new Level(LocalStrings.Libraries, "", ""));
		levelCollection2s.Add(new Level(LocalStrings.Libraries, "", ""));

		if(level2>"")
		{
				//this is a finder for a web page 0
				finders.Add(new Finder(0)); //unused
				finders.Add(new Finder(1));
				finders[1].IsReferenceFinder = 1;

				if(choice=="e")
				{
						//and it is an explorer page
						//put the level collections into the finders
						finders[1].Levels = levelCollection1e;
						finders[1].Levels.Add(new Level(level2, "ReferenceLibrary", ""));
						finders[1].Finderopenedflag = 1;
						finders[1].CurrentFind = 1;

						//fake the selection level for the current library
						document.forms[0].ddlfinder11.options[0] = new Option("title", level2, true, true);
						document.forms[0].ddlfinder11.selectedIndex = 0;
						//go get the library titles
						finders[1].FinderAddList(document.forms[0].ddlfinder11);
				}
				else 
				{
						//and it is a search page
						finders[1].Searchopenedflag = 1;
				}
		} 
		else
		{
				//this is a finder for panels 1 & 2
				finders.Add(new Finder(0)); //unused
				finders.Add(new Finder(1));
				finders.Add(new Finder(2));

				//put the level collections into the finders
				finders[1].Levels = levelCollection1e;
				finders[2].Levels = levelCollection2e;
				finders[1].Search = levelCollection1s;
				finders[2].Search = levelCollection2s;
		}
  }

	if(choice=="e")
	{
		openExplorer(index);
	}
	else if (choice == 'f')
	{
		openMyFlags(index);
	}
	else 
	{
		openSearch(index);
	}
}

function isFinderOpen(index)
{
    /*
    Function: isFinderOpen
        Checks to see if the either of the Finder windows are open in the current panel.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        True or false

    */
	var searchBox = document.getElementById("searchbox" + eval(index));
	var exploreBox = document.getElementById("explorebox" + eval(index))

	if ((exploreBox.style.display == 'block') || (searchBox.style.display == 'block'))
		return true;

	return false;
}

//navigation functions
function openExplorer(index)
{
    /*
    Function: openExplorer
        Opens the Explorer window in the current panel.
        Seeds the bread crumb.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    var myFlagsBox = document.getElementById("myflagsbox" + eval(index));
    
    document.getElementById("searchbox" + eval(index)).style.display = "none";
    if (myFlagsBox != null)	myFlagsBox.style.display = "none";
    document.getElementById("explorebox" + eval(index)).style.display = "block";
    if (finders[index].IsReferenceFinder == 0) 
    {
        document.getElementById("finderlist0" + eval(index)).style.display = "block";
    }

    try 
    {
        document.getElementById("btnGotoExplorer" + eval(index)).src = skinImagePath + "button_ExploreLibrary_on.gif";
        document.getElementById("btnGotoSearch" + eval(index)).src = skinImagePath + "button_SearchLibrary_off.gif";
        document.getElementById("btnGotoMyFlags" + eval(index)).src = skinImagePath + "button_MyFlags_off.gif";
    }
    catch (err)
    { }

    document.getElementById("btnFindOn" + eval(index)).style.visibility = "hidden";
    if (finders[index].Finderopenedflag == 0)
    {
        //initialize the first levels with the libraries list
        finders[index].FinderFillALevel("Libraries");
        finders[index].Finderopenedflag = 1;
        assign("breadcrumb" + eval(index), LocalStrings.YouAreExploring + " <u>" + LocalStrings.Bibles + "</u>");
    }
}

function openSearch(index)
{
    /*
    Function: openSearch
        Opens the Search window in the current panel.
        Seeds the bread crumb.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    var myFlagsBox = document.getElementById("myflagsbox" + eval(index));
    document.getElementById("explorebox" + eval(index)).style.display = "none";
    if (myFlagsBox != null) myFlagsBox.style.display = "none";
    document.getElementById("searchbox" + eval(index)).style.display = "block";

    var searchTextBox = document.getElementById('searchText' + index)
    if (typeof(searchTextBox) != 'undefined')
    {
			searchTextBox.focus();
			searchTextBox.select();
		}


    var sl = document.getElementById("searchlist0" + eval(index))
    if (sl != null)
			sl.style.display = "block";

    try 
    {
        document.getElementById("btnGotoExplorer" + eval(index)).src = skinImagePath + "button_ExploreLibrary_off.gif";
        document.getElementById("btnGotoSearch" + eval(index)).src = skinImagePath + "button_SearchLibrary_on.gif";
        document.getElementById("btnGotoMyFlags" + eval(index)).src = skinImagePath + "button_MyFlags_off.gif";
    }
    catch (err)
    {    }
    if(finders[index].Searchopenedflag == 0)
    {
        //initialize the first levels with the libraries list
        finders[index].Searchopenedflag = 1;
        assign("breadcrumb" + eval(index), LocalStrings.YouAreSearchingIn + " <b>" + LocalStrings.Bibles + "</b>");
        this.DropdownChoice1=1;
        var ddl = document.getElementById("ddlsearch0" + eval(index));
        selectedvalue = ddl[ddl.selectedIndex].value;
    }
}

function openMyFlags(index)
{
    /*
    Function: openMyFlags
        Opens the User flags window in the current panel.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */	
    document.getElementById("explorebox" + eval(index)).style.display = "none";
    document.getElementById("searchbox" + eval(index)).style.display = "none";
    document.getElementById("myflagsbox" + eval(index)).style.display = "block";
    
    try 
    {
        document.getElementById("btnGotoExplorer" + eval(index)).src = skinImagePath + "button_ExploreLibrary_off.gif";
        document.getElementById("btnGotoSearch" + eval(index)).src = skinImagePath + "button_SearchLibrary_off.gif";
        document.getElementById("btnGotoMyFlags" + eval(index)).src = skinImagePath + "button_MyFlags_on.gif";
    }
    catch(err)
    {
    }
}

function closeExplorer(index)
{
    /*
    Function: closeExplorer
        Closes the Explorer window.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    try
    {
        document.getElementById("btnGotoExplorer" + eval(index)).src = skinImagePath + "button_ExploreLibrary_off.gif";
        document.getElementById("btnGotoSearch" + eval(index)).src = skinImagePath + "button_SearchLibrary_off.gif";
        document.getElementById("btnGotoMyFlags" + eval(index)).src = skinImagePath + "button_MyFlags_off.gif";
    }
    catch(err)
    {
    }

    document.getElementById("explorebox" + eval(index)).style.display = "none";
}

function closeSearch(index)
{
    /*
    Function: closeSearch
        Closes the Search window.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    try {
        document.getElementById("btnGotoExplorer" + eval(index)).src = skinImagePath + "button_ExploreLibrary_off.gif";
        document.getElementById("btnGotoSearch" + eval(index)).src = skinImagePath + "button_SearchLibrary_off.gif";
        document.getElementById("btnGotoMyFlags" + eval(index)).src = skinImagePath + "button_MyFlags_off.gif";
    }
    catch (err)
    {
    }
    document.getElementById("searchbox" + eval(index)).style.display = "none";
}

function closeMyFlags(index)
{
    /*
    Function: closeMyFlags
        Closes the My Flags window.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    try 
    {
        document.getElementById("btnGotoExplorer" + eval(index)).src = skinImagePath + "button_ExploreLibrary_off.gif";
        document.getElementById("btnGotoSearch" + eval(index)).src = skinImagePath + "button_SearchLibrary_off.gif";
        document.getElementById("btnGotoMyFlags" + eval(index)).src = skinImagePath + "button_MyFlags_off.gif";
    }
    catch (err)
    {  }
    
    document.getElementById("myflagsbox" + eval(index)).style.display = "none";
}

//
//---------------------- Selection Code Section -----------------------------
//

function GoFind(index)
{
    /*
    Function: GoFind
        Sends the current Finder selections to the functions that interface the Finder to MyBst.js,
        gotoVerseText, gotoReferenceDoc, or gotoDevotional, depending on document type.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    if (finders[index].DropdownChoice1 == 1)
    { //verse text
        gotoVerseText(index,finders[index].DropdownChoice2,finders[index].DropdownChoice3,finders[index].DropdownChoice4);
    }
    if (finders[index].DropdownChoice1 == 2)
    { //reference library
        gotoReferenceDoc("",index,finders[index].DropdownChoice2,finders[index].DropdownChoice3);
    }
    if (finders[index].DropdownChoice1 == 3)
    { //devotional
        gotoDevotional("",index,finders[index].DropdownChoice2,finders[index].DropdownChoice3);
    }
    closeExplorer(index);
    return true;
}

function GoReferencePageFind(index)
{
    /*
    Function: GoReferencePageFind
        Sends the current Finder selections to the default MyBst tab page to display a reference document.
        It will only be a reference document, but we are outside of MyBST so we have to navigate to the page.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    var contentid = finders[index].DropdownChoice2;
    location.href  = "../mybst/default.aspx?type=library&contentid="+contentid+"&category=REF";
    closeExplorer(index);
    return true;
}

function GoSearch(index)
{
    /*
    Function: GoSearch
        Sends the current Search selections to the internal function gotoKeywordSearch to interface to MyBST.js.
        Updates the bread crumb and closes the Search window.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    var words = document.getElementById("searchText" + eval(index)).value;
    var level0 = LocalStrings.Bibles;
    var level1 = LocalStrings.All;
    var level2 = LocalStrings.All;
    var contenttype = "bible";
    var category = '';
    var translation = '';
    var searchelement;

    if (words == "")
    {
	    alert(LocalStrings.PleaseEnterAKeyword);
        return false;
    }

    searchelement = document.getElementById("ddlsearch0" + eval(index));
    level0 = searchelement[searchelement.selectedIndex].value;
    if (level0 == LocalStrings.Bibles)
    {
        contenttype = "bible";

        //translation
        searchelement = document.getElementById("ddlsearch1" + eval(index));
        level1 = searchelement[searchelement.selectedIndex].value;
        translation = level1;

        //part
        searchelement = document.getElementById("ddlsearch2" + eval(index));
        level2 = searchelement[searchelement.selectedIndex].value;
    }
    else 
    {
        contenttype = "library";

        //library
        searchelement = document.getElementById("ddlsearch1" + eval(index));
        level1 = searchelement[searchelement.selectedIndex].text;
        category = level1;

        //title
        searchelement = document.getElementById("ddlsearch2" + eval(index));
        level2 = searchelement[searchelement.selectedIndex].text;
    }

    finders[index].SearchCreateDisplay(); //update the breadcrumbs
    gotoKeywordSearch(index,contenttype,category,words,translation,level2);

    closeSearch(index);
    return true;
}

function GoReferencePageSearch(index)
{
    /*
    Function: GoReferencePageSearch
        Sends the current Search text to the default Search tab page to perform a word search.
        It will only be a reference document, but we are outside of MyBST so we have to navigate to the page.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right

    Returns:
        nothing

    */
    var words = document.getElementById("searchText" + eval(index)).value;
    var librarytitle = document.getElementById("ctl00_maincontent_resourcetitle").innerHTML;

    var section = document.getElementById("section").value;

    if (words == "")
    {
	    alert(LocalStrings.PleaseEnterAKeyword);
        return false;
    }

    if (words == LocalStrings.PleaseEnterAWordToSearchFor)
    {
        return false;
    }
    location.href = "../search/default.aspx?keyword=" + words + "&type=library" + "&category=" + section + "&title=" + librarytitle + "&p=0";

    closeSearch(index);
    return true;
}

//
//---------------------- Calls to MyBST.js functions for display -----------------------------
//

function gotoVerseText(index, bookcode, chapterid, translationcode)
{
    /*
    Function: gotoVerseText
        Calls the MyBST.js function callRefSearchByKey to display verse text by Book, Chapter, and Verse.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right
        bookcode - Bible book code
        chapterid -  numeric chapter number
        translationcode - Bible translation code

    Returns:
        nothing

    */
    callRefSearchByKey(index, bookcode, "", chapterid, 1, translationcode, true);
    return;
}

function gotoReferenceDoc(categoryname, index, docid, title)
{
    /*
    Function: gotoReferenceDoc
        Calls the MyBST.js function contentResultsPage to display a reference library document.

    Parameters:
        categoryname - one of the names in the reference library
        index - 1 or 2 for verse text panel number, left or right
        docid -  numeric document id number
        title - title of the document

    Returns:
        nothing

    */
    contentResultsPage(categoryname, index, docid, "REF");
    return;
}

function gotoDevotional(categoryname, index, docid, title)
{
    /*
    Function: gotoDevotional
        Calls the MyBST.js function contentResultsPage to display a devotional article from CMS.

    Parameters:
        categoryname - one of the names of the article section
        index - 1 or 2 for verse text panel number, left or right
        docid -  numeric article id number
        title - title of the document

    Returns:
        nothing

    */
    contentResultsPage(categoryname,index, docid, "DEVO");
    return;
}

function gotoKeywordSearch(index, content, category, words, translation, bookgroup)
{
    /*
    Function: gotoKeywordSearch
        Calls the MyBST.js functions verse text or a document by a reference or string.
        For library searches, calls getFinderSearchResults.
        For Bible searches,
        with a search that contains a passage reference, calls callRefSearchByString,
        with a word search (no digits), calls getFinderSearchResults.

    Parameters:
        index - 1 or 2 for verse text panel number, left or right
        content - bible or library
        category - optional library section name
        words -  words or passage reference to search for
        translation - Bible translation code or All, optional
        bookgroup - Part of the Bible book group or All

    Returns:
        nothing

    */
    var iswords = true;
    var num = new RegExp("[0-9]+");
    var alpha = new RegExp("[a-zA-Z]+");
    //default the translationcode
    var translationcode = translation;
    if (translationcode == "")
    {
        translationcode = userTranslation;
    }
    if (content == "library")
    {
        getFinderSearchResults(index,content,category,words,translationcode,bookgroup,"bible",true);
    }
    else 
    {
        //for bibles
        if (words.match(num)) 
        {
            if (words.match(alpha))
                iswords = false;
        }
        if (iswords) 
        {
            getFinderSearchResults(index,content,category,words,translationcode,bookgroup,"bible",true);
        }
        else 
        {
            //for bibles, change all to preferred
            if (translationcode.toLowerCase() == "all")
            {
                translationcode = userTranslation;
            }
            callRefSearchByString(words,index,translationcode,BSTState);
        }
    }
    return;
}
