﻿// JScript File

function getPagerString(firstRecord, totalRecords, recordsPerPage, pagesPerGroup, pagingFunction) {
    //variables for paging
    var pageInfo = new Array;
    var numbOfPages = Math.ceil(totalRecords / recordsPerPage);
    var thisPage = (firstRecord / recordsPerPage) + 1;
    var thisGroup = Math.floor((thisPage - 1) / pagesPerGroup) + 1;
    var totalGroups = Math.floor(Math.floor((totalRecords / recordsPerPage) / pagesPerGroup)) + 1;
    var startPage = (thisGroup * pagesPerGroup) - (pagesPerGroup - 1); //i.e. (2 * 5) - (5 -1) = 6
    var lastPage;
    if (((thisGroup * pagesPerGroup * recordsPerPage) - recordsPerPage) < totalRecords) {
        //have full set of pages in this group
        lastPage = startPage + pagesPerGroup - 1;
    } else {
        lastPage = startPage + Math.ceil(((totalRecords - (startPage * recordsPerPage)) / recordsPerPage));
    }
     
    var sPageOf = "<b>Page " + thisPage.toString() + " of " + numbOfPages.toString();
    pageInfo["pageOf"] = sPageOf;
    
    var sPager = "";
    for (i = startPage; i <= lastPage; i ++) {
        if (i == thisPage) {
            sPager += i.toString();
        } else {
            sPager += "<span class='linkText' onClick='" + pagingFunction + "(" + ((i * recordsPerPage) - recordsPerPage) + ")'>" + i.toString() + "</span>";
        }
        
        //add a buffer between each page number except the last
        if (i < lastPage) sPager += "&nbsp;&nbsp;"
    }

    if (thisGroup == 1) {
        //first group - no prev group needed
        if (totalGroups > 1) {
            sPager += "<span class='linkText' onClick='" + pagingFunction + "(" + (pagesPerGroup * recordsPerPage).toString() + ")'>...</span>";
        }
    } else if (thisGroup == totalGroups) {
        //last group - no next group needed
        if (totalGroups > 1) {
            sPager = "<span class='linkText' onClick='" + pagingFunction + "(" + (((thisGroup - 1) * pagesPerGroup * recordsPerPage) - recordsPerPage).toString()  + ")'>...</span>" + sPager;
        }
    } else {
        //we need both a prev and next button
        sPager = "<span class='linkText' onClick='" + pagingFunction + "(" + (((thisGroup - 1) * pagesPerGroup * recordsPerPage) - recordsPerPage).toString()  + ")'>...</span>&nbsp;&nbsp;" + sPager;
        sPager += "<span class='linkText' onClick='" + pagingFunction + "(" + (thisGroup * pagesPerGroup * recordsPerPage).toString() + ")'>...</span>";
    }
    
    pageInfo["pageString"] = sPager;
    
    return pageInfo;
}

function pageData(functionToGetData) {
    var pager = document.getElementById("selPage");    
    var page = pager.options[pager.selectedIndex].value;
    
    var evalString = functionToGetData + "(" + page + ");";
    
    eval(evalString); 
}

function getPagerString2(firstRecord, totalRecords, recordsPerPage, pagesPerGroup, pagingFunction) {
    //variables for paging
    var pageInfo = new Array;
    var numbOfPages = Math.ceil(totalRecords / recordsPerPage);
    var thisPage = (firstRecord / recordsPerPage) + 1;
    var thisGroup = Math.floor((thisPage - 1) / pagesPerGroup) + 1;
    var totalGroups = Math.floor(Math.floor((totalRecords / recordsPerPage) / pagesPerGroup)) + 1;
    var startPage = (thisGroup * pagesPerGroup) - (pagesPerGroup - 1); //i.e. (2 * 5) - (5 -1) = 6
    var lastPage = Math.ceil(totalRecords / recordsPerPage);

    var pageList = "<select class='searchResults' id='selPage' onchange='pageData(\"" + pagingFunction + "\");' >"
    for (i = 1; i <= lastPage; i++) {
        if (i == thisPage) {
            pageList += "<option selected='true' value='" + ((i - 1)*15).toString() + "'>" + i.toString() + " of " + numbOfPages.toString() + "</option>";
        } else {
            pageList += "<option value='" + i.toString() + "'>" + i.toString() + " of " + numbOfPages.toString() + "</option>";
        }
    }
    var sPageOf = "Page: " + pageList; // + " of " + numbOfPages.toString();
    pageInfo["pageOf"] = sPageOf;    
    
    pageInfo["pageString"] =  "";
    
    return pageInfo;
}

