/*
AJAX Tabs for Jones Day Site Redesign (JDAY27A / sitev2)

Ajax calls will not fire while in any Edit Mode

Helper Information:
    This page's AjaxTab information is in: ajaxpage
    The tabs are stored in the array: ajaxtabs
        Indexed by tab name.  (e.g. ajaxtabs["Professionals"] 
    
    To change to a tab with something other than the tab nav use: switchtab(tabname)
        Example: switchtab("Professionals");
          
Example for display on FCWSite/Features/_Locations/Office.Aspx

Setup:
    Execute the following 
        $(document).ready(function(){
             AjaxPageInit(navClass, tabIds, startingTab, firstPage, detailPageType, defaultPageSize, loadAtStart, loadingImageClass, breadcrumbJQuerySelector); 
        });
        
        AjaxPageInit inputs:
            navClass = ClassName that contains the Tab Navigation (JQuery style, e.g. ".tabSubNav")
            tabIds = Array of strings as JQuery Selectors (e.g. "#Professionals")
            startingTab = Title value of the Starting Tab (e.g. "Overview")
            firstPage = On Ajax Call, default first page that is returned
            detailPageType = Type of detail page we are on (e.g. "offices")
            defaultPageSize = On Ajax Call, default page size of the returned list
            loadAtStart = When page is done loading, run an Ajax call to load the starting tab.
            loadingImageClass = JQuery ClassName of the loading image (the Ajax "spinner", e.g. ".loadingImage")
            breadcrumbJQuerySelector = JQuery Selector for the Breadcrumb that is updated on tab change. (e.g. "li[ajaxbreadcrumb='true']")
            
    Anything that needs to be loaded with Ajax data needs a target in it.  Here is the template:
        <table id="ProfessionalsData" isloaded="false"></table>
        or
        <ul id="ExperienceData" isloaded="false"></ul>
        or
        <span id="AttorneyData" isloaded="false"></span> 
    
Dependant on what kind of data you are pulling from the .aspx pages

Add the following after the "Data" tags
   <!-- These are the controls to perform paging (Next, Previous, # (specific page number) or view all) -->
    <span class="ajaxpagecontrol">
    <div class="pageIndex"><a href="#" page="previous">&laquo; Previous</a> <span class="bdsresultspagelist"></span> <a href="#" page="next">Next &raquo;</a> </div>
    <div class="viewall"><a href="#" page="all" class="viewAll" onclick="return gopage(this);">View All</a><span class="loadingText">Loading...</span></div>
    </span>

If you want the results from a search to be returned, set the following variable:
    searchUrl = "the url and qstring of the search"
    ex: searchUrl = AjaxRoot + "/AjaxData.aspx?CT=CONTROLNAME&lastname=A"; // all qstring variable names will be in lowercase letters

Results will return like this
<tr ajax="true">
    <td> DATA </td>
</tr>

or

<li ajax="true">DATA</li>

Each html entity with the attribute ajax="true" will be loaded into the target Data id
Data excluded from ajax call but still returned can be set with ajax="false"

Results can display by category

Set up data block like this:
<table id="EventsData" isloaded="false">
    <tr category="CAT1"><td>Category One</td></tr>
    <tr category="CAT1End"><td><div class="dottedline"></div></td></tr>
</table> 

The ajax data will return thus:

<tr class="ajax" subcategory="CAT1"><td>DATA</td></tr>

it will be put inbetween the CAT1 and CAT1End table rows\

For best results, set the Data blocks with display:none, and the code will unhide those fields on initial load.

Use JonesDay/FCWSite/Features/_Locations/Office.aspx as a template

*/
function pageObject(tab_nav_class, tab_array, starting_tab, starting_page, detail_page_type, default_page_size, load_at_start, loading_image_selector, ajax_breadcrumb_selector)
{
    this.ajaxReady = true;
    
    // Page Info
    if (tab_nav_class != null) {
        this.navClass = tab_nav_class;
    } else {
        this.navClass = ".tabSubNav";
    }     
    if (tab_array != null) { 
        this.tabArray = tab_array;
    } else {
        this.tabArray = new Array();
        this.ajaxReady = false;
    }
    if (starting_tab != null) {    
        this.startingTab = starting_tab;
    } else {
        if (this.ajaxReady) {
            this.startingTab = this.tabArray[0].replace("#", ""); 
        } else {  
            this.startingTab = "";
            this.ajaxReady = false;
        }
    }
    if (detail_page_type != null) {    
        this.detailPageType = detail_page_type;
    } else {
        this.detailPageType = "";
        this.ajaxReady = false;
    }    
    
    if (default_page_size != null) { 
        this.defaultPageSize = default_page_size;
    } else {
        this.defaultPageSize = 10;
    }
    if (load_at_start != null) {
        this.load_at_start = load_at_start; 
    } else {
        this.load_at_start = true;
    }  
    if (loading_image_selector != null) {
        this.loadingImageSelector = loading_image_selector;
    } else {
        this.loadingImageSelector = ".loadingImage";
    }     
    if (starting_page != null) {
        this.startingPage = starting_page; 
    } else {
        this.startingPage = 0;
    }
    if (ajax_breadcrumb_selector != null) {
        this.ajaxBreadcrumbSelector = ajax_breadcrumb_selector;
    } else {
        this.ajaxBreadcrumbSelector = "li[ajaxbreadcrumb='true']";
    }
    if (this.ajaxReady) {
        if ($(this.ajaxBreadcrumbSelector).length > 0) {
            this.useAjaxBreadcrumbs = true; 
        } else {
            this.useAjaxBreadcrumbs = false;
        }  
    } else {
         this.useAjaxBreadcrumbs = false;
     }

    this.showingString = "(Showing %X% of %Y% results)";
     
    this.isEditMode = ""; 
    if (CultureRoot.indexOf("/97") == 0) { this.isEditMode = "Content"; }
    if (CultureRoot.indexOf("/98") == 0) { this.isEditMode = "Template"; }
    if (CultureRoot.indexOf("/100") == 0) { this.isEditMode = "Site"; } 
   
    this.getActiveTabName = getActiveTabName;
}

function getActiveTabName()
{
    var currentlyActiveTab = "";
    for (t in ajaxtabs) {
        if (ajaxtabs[t].isActiveTab) { currentlyActiveTab = ajaxtabs[t].tabName; break;}
    }
    return currentlyActiveTab; 
}

function tabObject(tab_name, tab_id, isStartUpTab, useAjax, defaultAjaxUrl, searchStub_Url)
{
    // General Tab info
    this.tabName = tab_name;
    this.tabID = tab_id;
    this.tabText = tab_name; 
    
    // Starting or Active? 
    if (isStartUpTab != null) { 
        this.isStartingTab = isStartUpTab;
        if (isStartUpTab) { 
            this.isActiveTab = true; 
        } else {
            this.isActiveTab = false;
        }
    } else {
        this.isStartingTab =  false;
        this.isActiveTab = false;
    }  
    
    // Has Ajax? 
    this.isAjaxTab = useAjax;
    
    // If Ajax Tab, these are the counters for the data 
    this.bdspagecount = 0;
    this.bdsentitycount = 0;
    this.bdscurrentpage = 0;
    this.bdscurrentpagesize = 10;
    
    // url for the default ajax call.  If empty, there is a blank area displayed
    if (defaultAjaxUrl != null) { 
        this.ajaxUrl = defaultAjaxUrl;
    } else {
        this.ajaxUrl = "";
    }
    // Example: AjaxRoot + "/" + tabname + "data.aspx?" + detailPageType + "=" + $("#" + detailPageType + "guid").text();
    
    // url for the current search performed on the tab, this will override the ajaxUrl above
    this.ajaxSearchUrl = "";
    // Example: AjaxRoot + "/" + tabname + "data.aspx?LastName=A";
   
    this.SortType = "";
    // current sort type, blank: use default   
    
    // The initial piece of the ajaxSearchUrl
    if (searchStub_Url != null) { 
        this.ajaxSearchUrlStub = searchStub_Url;
    } else {
        this.ajaxSearchUrlStub = "";
    } 
    // Example: AjaxRoot + "/" + tabname + "data.aspx?offices=" + GUID + "&"; <-- generate the rest of the link...
     
    // Is the tab loaded.  Set this to false and then "click" the tab to rerun the ajax. 
    this.isLoaded = false; 
   
    // JQuery Selector for other controls that are shown (run with $(OtherActiveControls).show()) after this tab is activated
    this.OtherActiveControls = "";   
    // JQuery Selector for other controls that are hidden ((run with $(OtherInActiveControls).hide()) after this tab is activated
    this.OtherInActiveControls = "";   
   
    // One Function to run on completion of an AjaxLoad 
    this.ExecuteAfterAjaxLoad = "";  
    // Example: Used to bind the rollover for Attorney Hovers
    //      ajaxtabs["Professionals"].ExecuteAfterAjaxLoad = "BindHover('#Professionals')";

    this.tabOffStyle = "";
    this.tabOnStyle = "on";
    
    if ($(ajaxpage.navClass + " li a[title='" + this.tabName + "']").parent().length > 0) {
        if ($(ajaxpage.navClass + " li a[title='" + this.tabName + "']").parent().hasClass("small")) {
            this.tabOffStyle = "small";
            this.tabOnStyle = "smallon";
        }
    }
    
    // methods
    // Get the Ajax Tab Information loaded into a string, used for debugging   
    this.toString=toString;
    // Set the search url to the following additional query string 
    this.SetSearchUrl=SetSearchUrl; 
}

function SetSearchUrl(queryString)
{
    var urlString = this.ajaxSearchUrlStub + queryString;
    if (this.ajaxSearchUrl != urlString)
    {
        this.ajaxSearchUrl = urlString;
        this.bdscurrentpagesize = 10;
        this.bdscurrentpage = 0;
        this.isLoaded = false;
    }
}

function toString()
{
    var retString = "";
    retString += "tabName = '";
    if (this.tabName != null) { retString += this.tabName; } else { retString += "null"; }
    retString += "'<br/> ";
    retString += "tabID = '";
    if (this.tabID != null) { retString += this.tabID; } else { retString += "null"; }
    retString += "'<br/> "; 
    retString += "tabText = '";
    if (this.tabText != null) { retString += this.tabText; } else { retString += "null"; }
    retString += "'<br/> ";  
    retString += "isStartingTab = '";
    if (this.isStartingTab) { retString += "true";  } else { retString += "false"; }
    retString += "'<br/> ";
    retString += "isActiveTab = '";
    if (this.isActiveTab) { retString += "true";  } else { retString += "false"; }
    retString += "'<br/> "; 
    retString += "isAjaxTab = '";
    if (this.isAjaxTab) { retString += "true";  } else { retString += "false"; }
    retString += "'<br/> "; 
    retString += "bdspagecount = '" + this.bdspagecount + "'<br/> ";
    retString += "bdsentitycount = '" + this.bdsentitycount + "'<br/> ";
    retString += "bdscurrentpage = '" + this.bdscurrentpage + "'<br/> ";
    retString += "bdscurrentpagesize = '" + this.bdscurrentpagesize + "'<br/> ";
    retString += "ajaxUrl = '";
    if (this.ajaxUrl != null) { retString += this.ajaxUrl; } else { retString += "null"; }
    retString += "'<br/> ";  
    retString += "ajaxSearchUrl = '";
    if (this.ajaxSearchUrl != null) { retString += this.ajaxSearchUrl; } else { retString += "null"; }
    retString += "'<br/> ";  
    retString += "ajaxSearchUrlStub = '";
    if (this.ajaxSearchUrlStub != null) { retString += this.ajaxSearchUrlStub; } else { retString += "null"; }
    retString += "'<br/> ";  
    retString += "isLoaded = '";
    if (this.isLoaded) { retString += "true";  } else { retString += "false"; }
    retString += "'<br/> ";
   return retString; 
}

var loadingLock = false;
var ajaxtabs = new Array();
var ajaxpage = new pageObject();

function AjaxPageInit(tab_nav_class, tab_array, starting_tab, starting_page, detail_page_type, default_page_size, load_at_start, loading_image_selector, ajax_breadcrumb_selector)
{
    ajaxpage = new pageObject(tab_nav_class, tab_array, starting_tab, starting_page, detail_page_type, default_page_size, load_at_start, loading_image_selector, ajax_breadcrumb_selector);
    if (ajaxpage.ajaxReady) {
        buildTabs();
        $(ajaxpage.navClass + " li a[title='" + ajaxpage.startingTab + "']").parent().addClass(ajaxtabs[ajaxpage.startingTab].tabOnStyle);
        $(ajaxpage.navClass + " li a").bind("click", function() {
            return clicktab($(this));
        });
    }
}
function buildTabs()
{  // Initialize tabs, display the first one (or the starting tab) from the tabIds Array and hide the rest.
    for(var a = 0; a < ajaxpage.tabArray.length; a++)
    {
        var tabtitle =  ajaxpage.tabArray[a].replace("#", "");
        var tabid =  ajaxpage.tabArray[a];
        var startup =  (ajaxpage.tabArray[a] == ("#"+ajaxpage.startingTab));
        var useajax =  ($(ajaxpage.tabArray[a] + "Data").length > 0);
        var ajaxpreloaded = false;
        var ajaxurl = "";
        var searchstub = "";
        if (useajax) {
            if (detailPageType != "search" ) 
            { 
                ajaxurl = AjaxRoot + "/ajaxdata.aspx?CT=" + tabtitle.toLowerCase() + "&" + ajaxpage.detailPageType + "=" + $("#"+ajaxpage.detailPageType+"guid").text();
                searchstub = AjaxRoot + "/ajaxdata.aspx?CT=" + tabtitle.toLowerCase() + "&" + ajaxpage.detailPageType + "=" + $("#"+ajaxpage.detailPageType+"guid").text() + "&";
            }
            else
            {
                searchstub = AjaxRoot + "/ajaxdata.aspx?CT=" + tabtitle.toLowerCase() + "&";
            }
            if (($(ajaxpage.tabArray[a] + "Data").attr("isloaded") == "true") || ($(ajaxpage.tabArray[a] + "Data").children("[ajax='true']").length > 0))
            {
                ajaxpreloaded = true;
                if ($(ajaxpage.tabArray[a] + "Data").attr("isloaded") != "true") { $(ajaxpage.tabArray[a] + "Data").attr("isloaded", "true");} 
            }
        }
        ajaxtabs[tabtitle] = new tabObject(tabtitle, tabid, startup, useajax, ajaxurl, searchstub);
        
        if (ajaxpreloaded) {  ajaxtabs[tabtitle].isLoaded = true; }

        
        if ($(tabid).length > 0) {
            var tab_text = $(ajaxpage.navClass + " li a[title='" + tabtitle + "']").text();
            if (tab_text != null && tab_text != "") {
                ajaxtabs[tabtitle].tabText = tab_text;
            }
        }
    }
    if (ajaxpage.isEditMode != "") { 
        $(ajaxpage.loadingImageSelector).css("display", "none");  
    }  else {
        var onTab = ""; 
        for (t in ajaxtabs) {
            if (ajaxtabs[t].tabName != ajaxpage.startingTab) { $(ajaxtabs[t].tabID).hide();}
            else { $(ajaxtabs[t].tabID).show();}
        }
        if (ajaxpage.loadAtStart) {
            ajaxLoadData(ajaxpage.startingTab);
            if (ajaxpage.useAjaxBreadcrumbs){
                $(ajaxpage.ajaxBreadcrumbSelector).html(ajaxtabs[ajaxpage.startingTab].tabText);
            }  
        } else {
            $(ajaxtabs[ajaxpage.startingTab].tabID + " " + ajaxpage.loadingImageSelector).hide();
        } 
    }  
} 

function switchtab(tabname)
{
    clicktab($(ajaxpage.navClass + " li a[title='" + tabname + "']"));
}
function clicktab(clickitem, isHiddenTab)
{  
    // activate the clicked tab, hide the currently active one, run the Ajax Load.
    // is hidden tab is a tab without a top sub nav tab link.  Opened from another location
    var tabname = "";
    if (isHiddenTab == null) { isHiddenTab = false; }
    
    if (isHiddenTab) { tabname = clickitem;}
    else { tabname = $(clickitem).attr("title"); }
    
    if (!loadingLock && ajaxpage.isEditMode == "") { 
        var oldtabname = ajaxpage.getActiveTabName();
        if (oldtabname != "") { 
            if (oldtabname != tabname) { 
                ajaxtabs[oldtabname].isActiveTab = false;
                $(ajaxpage.navClass + " li a[title='" + oldtabname + "']").parent().removeClass(ajaxtabs[oldtabname].tabOnStyle);
                if (ajaxtabs[oldtabname].tabOffStyle != "") { 
                    $(ajaxpage.navClass + " li a[title='" + oldtabname + "']").parent().addClass(ajaxtabs[oldtabname].tabOffStyle);
                }
                if (!isHiddenTab) { 
                    $(ajaxpage.navClass + " li a[title='" + tabname + "']").parent().addClass(ajaxtabs[tabname].tabOnStyle);
                    if (ajaxtabs[tabname].tabOffStyle != "") { 
                        $(ajaxpage.navClass + " li a[title='" + tabname + "']").parent().removeClass(ajaxtabs[tabname].tabOffStyle);
                    } 
                } 
                $("#"+oldtabname).hide();
            }
        } 
        $("#"+tabname).show();
        ajaxtabs[tabname].isActiveTab = true;
        ajaxLoadData(tabname); 
        if (ajaxtabs[tabname].OtherActiveControls != "") {
            $(ajaxtabs[tabname].OtherActiveControls).show();
        } 
        if (ajaxtabs[tabname].OtherInActiveControls != "") {
            $(ajaxtabs[tabname].OtherInActiveControls).hide();
        }
        if (ajaxpage.useAjaxBreadcrumbs){
            $(ajaxpage.ajaxBreadcrumbSelector).html(ajaxtabs[tabname].tabText);
        }  
    } else {
        // if in an edit mode, display the tab and skip the ajax load 
        if (ajaxpage.isEditMode != "") {
            var tabname = $(clickitem).attr("title");
            var oldtabname = ajaxpage.getActiveTabName();
            $(ajaxpage.navClass + " li a[title='" + oldtabname + "']").parent().removeClass(ajaxtabs[oldtabname].tabOnStyle);
            if (ajaxtabs[oldtabname].tabOffStyle != "") { 
                $(ajaxpage.navClass + " li a[title='" + oldtabname + "']").parent().addClass(ajaxtabs[oldtabname].tabOffStyle);
            } 
            $(ajaxpage.navClass + " li a[title='" + tabname + "']").parent().addClass(ajaxtabs[tabname].tabOnStyle);
            if (ajaxtabs[tabname].tabOffStyle != "") { 
                $(ajaxpage.navClass + " li a[title='" + tabname + "']").parent().removeClass(ajaxtabs[tabname].tabOffStyle);
            }
            $("#"+oldtabname).hide();
            $("#"+tabname).show();
            ajaxtabs[oldtabname].isActiveTab = false;
            ajaxtabs[tabname].isActiveTab = true;
            if (ajaxtabs[tabname].OtherActiveControls != "") {
                $(ajaxtabs[tabname].OtherActiveControls).show();
            }
            if (ajaxtabs[tabname].OtherInActiveControls != "") {
                $(ajaxtabs[tabname].OtherInActiveControls).hide();
            }
            if (ajaxpage.useAjaxBreadcrumbs) {
                $(ajaxpage.ajaxBreadcrumbSelector).html(ajaxtabs[tabname].tabText);
            }
        } 
    } 
    return false;
}
function changeSort(sortvalue, tabname)
{
    var currentSort = ajaxtabs[tabname].SortType;
    if (sortvalue != null && sortvalue != "") {
        if (currentSort != sortvalue) { 
            ajaxtabs[tabname].SortType = sortvalue; //SetSearchUrl("&sort=" + 
            ajaxtabs[tabname].bdscurrentpagesize = 10;
            ajaxtabs[tabname].bdscurrentpage = 0;
            ajaxtabs[tabname].isLoaded = false; 
        }
    } else {
        ajaxtabs[tabname].SortType = "";
        if (currentSort != "") { 
            ajaxtabs[tabname].bdscurrentpagesize = 10;
            ajaxtabs[tabname].bdscurrentpage = 0;
            ajaxtabs[tabname].isLoaded = false; 
        }
    }  
    ajaxLoadData(tabname);
}
function ajaxLoadData(tabname)
{
    // Function to perform the Ajax Load
    // Check if there is tab uses ajax, if so, check if it is previously loaded, if not, perform Ajax call
    if (ajaxtabs[tabname].isAjaxTab) {
        if (!ajaxtabs[tabname].isLoaded) {
            var tabtop =  $(ajaxtabs[tabname].tabID).offset().top;  // Scroll to the top of the tab if we cannot already see it.
            if ($(body).scrollTop() > tabtop){
                $(body).scrollTop(tabtop);
            } 
            var currentPage = ajaxtabs[tabname].bdscurrentpage; // Get the current page number (default to page 0)
            var currentPageSize = ajaxtabs[tabname].bdscurrentpagesize;  // Get the current page size (default to page 10)
            $(ajaxtabs[tabname].tabID + " " + ajaxpage.loadingImageSelector).show(); // display the "loading image" for this tab.
            loadingLock = true; // lock the tab navigation until Ajax call completed
            
            // Ajax Url:  Site / {TabName}Data.aspx?detailType=GUID&pagesize=#&page=#
            // TabName - Professionals, Experience, News, Events returns the appropriate data, formatted for the list (tr or li)
            var ajaxUrl = ajaxtabs[tabname].ajaxUrl;
            if (ajaxtabs[tabname].ajaxSearchUrl != "") {
                // set variable searchUrl to override simple data return with a search.
                ajaxUrl = ajaxtabs[tabname].ajaxSearchUrl;
            }
            if (currentPageSize != "all") { ajaxUrl += "&pagesize=" + currentPageSize + "&page=" + currentPage; }
            else { ajaxUrl += "&pagesize=all"; }
            
            // Include sort to query string, if necessary
            if (ajaxtabs[tabname].SortType != "")
            {
                ajaxUrl += "&sort=" + ajaxtabs[tabname].SortType;
            }
            var unloadCurrentData = false;
            // If this tab currently has data, clear the tab after new data has come back.
            if ($(ajaxtabs[tabname].tabID + "Data [ajax]").length > 0) {
                unloadCurrentData = true;
            }
            $.get(ajaxUrl, function(data) {
                var $returnData = $("<div/>").append(data.replace(/<script(.|\s)*?\/script>/g, "")); // load the data into a fake div tag and remove any javascript from the return 
                if ($returnData.find("[ajax='true']").length == 0) {
                    // No data was returned, clear the tab and display the "No Results Message" 
                    $(ajaxtabs[tabname].tabID + "Data [ajax]").remove();
                    unloadCurrentData = false;
                    clearPageIndex(tabname);
                    $(ajaxtabs[tabname].tabID + "Data").hide();
                    $(ajaxtabs[tabname].tabID + " .noresultsmessage").show();
                    ajaxtabs[tabname].isLoaded = false;
                } else {
                    // Data was returned, load it 
                    $(ajaxtabs[tabname].tabID + "Data").show();
                    $(ajaxtabs[tabname].tabID + " .noresultsmessage").hide();
                    $returnData.find("[ajax='true']").each(function(index, datarow) {
                        if (unloadCurrentData) { $(ajaxtabs[tabname].tabID + "Data [ajax]").remove(); unloadCurrentData = false; clearPageIndex(tabname) }
                        // Load data into the Tab's Data block
                        // Data Block is a TABLE, load the rows 
                        if ($(ajaxtabs[tabname].tabID + "Data > tbody").length > 0) {
                            if ($(datarow).attr("subcategory")) {
                                // Data Block is expecting categories, split them up 
                                $(ajaxtabs[tabname].tabID + "Data > tbody > tr[category='" + $(datarow).attr("subcategory") + "']").css("display", "block");
                                $(ajaxtabs[tabname].tabID + "Data > tbody > tr[category='" + $(datarow).attr("subcategory") + "End']").css("display", "block");
                                $(datarow).insertBefore(ajaxtabs[tabname].tabID + "Data > tbody > tr[category='" + $(datarow).attr("subcategory") + "End']");
                            } else {
                                // Data Block is expecting straight table rows 
                                $(ajaxtabs[tabname].tabID + "Data > tbody").append(datarow);
                            }
                            // Data Block is a UL, load the LI's
                        } else if ($("ul#" + ajaxtabs[tabname].tabName + "Data").length > 0) {
                            $("ul#" + ajaxtabs[tabname].tabName + "Data").append(datarow);
                            // Data Block is a SPAN, load the text (Used by Attorney Detail Page) 
                        } else if ($("span#" + ajaxtabs[tabname].tabName + "Data").length > 0) {
                            $("span#" + ajaxtabs[tabname].tabName + "Data").append(datarow);
                        }
                    });
                }
                // Setup page count
                $returnData.find("#pagecount").each(function(index, datarow) {
                    ajaxtabs[tabname].bdspagecount = new Number(datarow.innerHTML);
                });
                // Setup results count
                $returnData.find("#resultcount").each(function(index, datarow) {
                    ajaxtabs[tabname].bdsentitycount = new Number(datarow.innerHTML);
                }); 
                // build "Showing" data string 
                var totalResults = ajaxtabs[tabname].bdsentitycount;
                var maxListResults = ((currentPage * currentPageSize) + currentPageSize);
                if (totalResults < maxListResults || currentPageSize == 0) { maxListResults = totalResults; }

                var displayString = "";
                if (currentPageSize == "all") { displayString = "1 - " + totalResults; }
                else { displayString = ((currentPage * currentPageSize) + 1) + " - " + maxListResults; }
                var showingString = ajaxpage.showingString;
                showingString = showingString.replace("%X%", displayString);
                showingString = showingString.replace("%Y%", totalResults);
                //var showingString = '(Showing ' + displayString + ' of ' + totalResults + ' results)';
                $(ajaxtabs[tabname].tabID + " .showingNum, " + ajaxtabs[tabname].tabID + " .loadingText").html(showingString);
                $(ajaxtabs[tabname].tabID + " .loadingText").show()

                setPageIndex(tabname);
                // Show/Hide the "other controls" for the tab after load 
                runOtherTabControls(tabname);
                // Check Ajax ExecuteAfterAjaxLoad property for post load executions
                if (ajaxtabs[tabname].ExecuteAfterAjaxLoad != "") {
                    var fnCall = ajaxtabs[tabname].ExecuteAfterAjaxLoad;
                    try { eval(fnCall); } catch (er) { }
                }
                // Mark this tab as "loaded" 
                ajaxtabs[tabname].isLoaded = true;
                // Hide the loading image 
                $(ajaxtabs[tabname].tabID + " " + ajaxpage.loadingImageSelector).hide();
            });
            // Unlock the tab navigation
            loadingLock = false;
        }
    } 
}
function runOtherTabControls(tabname)
{
    // This function runs after data has been loaded from Ajax to show other controls within the tab
    if (ajaxtabs[tabname].OtherActiveControls != "") {
        $(ajaxtabs[tabname].OtherActiveControls).show();
    }
    if (ajaxtabs[tabname].OtherInActiveControls != "") {
        $(ajaxtabs[tabname].OtherInActiveControls).hide(); 
    } 
}
function clearPageIndex(tabname)
{
    $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist").html("");
}
function setPageIndex(tabname)
{
    if ($(ajaxtabs[tabname].tabID + " .ajaxpagecontrol").length > 0) {
        // This function builds out the page index and sets the click events for the pages and next, previous and view all links.
        var currentPage = ajaxtabs[tabname].bdscurrentpage;
        var currentPageSize = ajaxtabs[tabname].bdscurrentpagesize;
        var pageCount = ajaxtabs[tabname].bdspagecount;
        var entityCount = ajaxtabs[tabname].bdsentitycount;
        if (currentPageSize == 0 || pageCount == 1) {
            $(ajaxtabs[tabname].tabID + " .pageIndex").hide();
            $(ajaxtabs[tabname].tabID + " .viewall a").hide();
        } else {
            $(ajaxtabs[tabname].tabID + " .pageIndex").show();
            $(ajaxtabs[tabname].tabID + " .viewall a").show();
        } 
        var startPagePrint = 0;
        var endPagePrint = pageCount;
        var showMorePrevPages = false;
        var showMoreNextPages = false;
        if (pageCount > 10) {
            if (currentPage > 5) 
            {
                startPagePrint = currentPage - 5;
            }
            if (currentPage > (pageCount-5))
            {
                startPagePrint = pageCount - 10; 
            }
            endPagePrint = startPagePrint + 10;
            if (startPagePrint > 0) { showMorePrevPages = true;} 
            if (endPagePrint < pageCount) { showMoreNextPages = true;} 
        } 
        if ($(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist").text().length == 0) {
            $(ajaxtabs[tabname].tabID + " .pageIndex a").unbind("click");
            if (pageCount > 10) { $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist").append("<span class=\"showmoreprev\">... | </span>"); }
            for(var a = 0; a < pageCount; a++)
            {
                var pageString = "";
                //if (a == startPagePrint && showMorePrevPages) { pageString += ' ... | '; }
                pageString += '<a href="#" page="' + a + '">' + (a+1);
                if ((a+1) < pageCount) {
                    pageString += ' | ';
                } 
                pageString += "</a>"; 
                 //if (a == (endPagePrint-1) && showMoreNextPages) { pageString += ' | ...'; }
                 $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist").append(pageString);
            }
            if (pageCount > 10) { $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist").append("<span class=\"showmorenext\"> ... </span>"); } 
            $(ajaxtabs[tabname].tabID + " .pageIndex a").bind("click", function(e) { return gopage(this); });
        }
        if (showMorePrevPages) {  $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist .showmoreprev").show(); } else { $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist .showmoreprev").hide();}
        if (showMoreNextPages) {  $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist .showmorenext").show(); } else { $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist .showmorenext").hide();} 
        for(var h = 0; h < pageCount; h++)
        {
            if (h >= startPagePrint && h < endPagePrint) { 
                $(ajaxtabs[tabname].tabID + " .pageIndex a[page='" + h + "']").show();
            } else {
                $(ajaxtabs[tabname].tabID + " .pageIndex a[page='" + h + "']").hide();
            } 
        } 
        $(ajaxtabs[tabname].tabID + " .pageIndex a, " + ajaxtabs[tabname].tabID + " .viewall a").attr("tab", tabname);
        $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist a.currentpagelink:not(a:eq(" + currentPage + "))").toggleClass("currentpagelink");
        $(ajaxtabs[tabname].tabID + " .pageIndex .bdsresultspagelist a:eq(" + currentPage + ")").toggleClass("currentpagelink");
        if (currentPage == 0) { $(ajaxtabs[tabname].tabID + " .pageIndex > a[page='previous']").addClass("currentpagelink");}
        else { $(ajaxtabs[tabname].tabID + " .pageIndex > a[page='previous']").removeClass("currentpagelink"); }
        if (currentPage == (pageCount-1)) { $(ajaxtabs[tabname].tabID + " .pageIndex > a[page='next']").addClass("currentpagelink");}
        else { $(ajaxtabs[tabname].tabID + " .pageIndex > a[page='next']").removeClass("currentpagelink"); }
        if (entityCount == 0) {
            $(ajaxtabs[tabname].tabID + " .ajaxpagecontrol").hide();
        } else {
            $(ajaxtabs[tabname].tabID + " .ajaxpagecontrol").show(); 
        }  
    } 
}
function gopage(clickitem)
{
    // Change Page link click event (for specific page, next, previous or view all)
    var tabname = $(clickitem).attr("tab");
    if (!loadingLock) {
        var currentPage = ajaxtabs[tabname].bdscurrentpage;
        var currentPageSize = ajaxtabs[tabname].bdscurrentpagesize;
        var action = $(clickitem).attr("page");
        var changePage = false;
        if (action == "previous")
        {
            if (currentPage > 0) { currentPage--; changePage = true;}
        } 
        else if (action == "next") {
            var pageCount = ajaxtabs[tabname].bdspagecount;
            pageCount--; 
            if (currentPage < pageCount) { currentPage++;  changePage = true;} 
        }
        else if (action == "all") {
            currentPage = 0;
            currentPageSize = "all";
            changePage = true;  
        }
        else { currentPage = $(clickitem).attr("page"); changePage = true;}
       // If the click requires a reload or update, set the "isloaded" attribute and run the ajaxLoadData function above. 
        if (changePage) {
            ajaxtabs[tabname].bdscurrentpage = currentPage;
            ajaxtabs[tabname].bdscurrentpagesize = currentPageSize;
            ajaxtabs[tabname].isLoaded = false;
            ajaxLoadData(tabname);
        }
    } 
    return false; 
}   

