var currPage = 1;
var rowChunks = 10;

function donumppChange(i) {
    var orig = document.getElementById('numpp-'+i);
    if (i==0) {
        j = 1;
    } else {
        j = 0;
    }
    var targ = document.getElementById('numpp-'+j);
    targ.selectedIndex = orig.selectedIndex;
    rowChunks = orig.options[orig.selectedIndex].value;
    currPage = 1;
    updatePaginationDisplay();
    gotoPage(1);
}

function updatePaginationDisplay() {
    var d0 = document.getElementById('paginationDisplay-0');
    var d1 = document.getElementById('paginationDisplay-1');
    var t = document.getElementById('planttable');
    var numrows = t.rows.length - 1;
    var starting = (currPage-1) * rowChunks + 1;
    var ending = currPage * rowChunks;
    if (ending > numrows) {
        ending = numrows;
    }
    var txt = 'Displaying <strong  >' + starting + ' to ';
    txt     = txt + ending+ '</strong> of ' + numrows;
    txt     = txt + ' available plants.';
    d0.innerHTML = txt;
    d1.innerHTML = txt;
}

function nextPage() {
    var t       = document.getElementById('planttable');
    var numrows = t.rows.length - 1;
    var maxPage = parseInt(numrows/rowChunks);
    if (numrows % 10 > 0) {
        maxPage++;
    }
    
    if (currPage == maxPage) {
        return;
    }
    currPage = currPage + 1;
    gotoPage(currPage);
}

function prevPage() {
    currPage = currPage - 1;
    if (currPage<1) {
        currPage = 1;
    }
    gotoPage(currPage);
}

function gotoPage(pnum) {
    var page = 1;
    
    var t = document.getElementById('planttable');
    var numrows = t.rows.length;
    var count = 1;
    for (var i=1; i<numrows; i++) {
        var row = t.rows[i];
        if (pnum == page) {
            row.style.display = 'table-row';
        } else {
            row.style.display = 'none';
        }
        count++;
        if (count > rowChunks) {
             count = 1;
             page = page + 1;
        }
    }

    updatePaginationDisplay();
}
