﻿// Copyright 2007 by Developer Utility
// Please see http://www.developerutility.com/Terms.htm for terms of use.

/*
=======================================
Page X of Y Event Quick list definition
=======================================
- OnGridPageXofY           : extract the page select in the navigation paging;
- RefreshPageXofY          : refresh/change the page X of Y content.
*/

/////////////////////////////////////////////////////////////////////////////////
//              THE FUNCTIONS BELOW ARE FOR RETRIEVING PAGE X OF Y             //
/////////////////////////////////////////////////////////////////////////////////
function OnGridPageXofY(evt, oEle) {
  try {
    // This is where you refresh/change the grid content with the page data selected
    // by the user. Before changing the grid data display, you should always call the 
    // SaveDataEntry function to save any user change currently on display on screen.
    // This function is call every time the user select a page X of Y or the previous/next icon.
    // The event (evt) is pass only as reference in case you want to tailor some special coding.
    
    // Call the save data entry function
    var bReturn = SaveDataEntry(evt);
    
    // Find and format the selected page
    var pageSelect = (browser.isIE) ? oEle.innerText : oEle.textContent;
    var oTR = (browser.isIE) ? oEle.parentElement : oEle.parentNode;
    var oTD;
    var page;
    var i;
    var refreshDirection = 0;
    
    if(Number(pageSelect)) {
      // Specific page selected (skip first and last oTD)
      i = 1;
      while(i < oTR.childNodes.length - 1) {
        oTD = oTR.childNodes[i];
        page = (browser.isIE) ? oTD.innerText : oTD.textContent;
        
        // Find the current select page
        if(pageSelect == page) {
          oTD.className = "pageselect";
          pageSelect = parseInt(page);
        } else { oTD.className = "pageof"; }
        i++;
      }
    } else {
        // Next or Previous page selected
        var currentPosition;
        i = 0;
        while(i < oTR.childNodes.length) {
          oTD = oTR.childNodes[i];
          
          // Find the current select page
          if(oTD.className == "pageselect") {
            currentPosition = i;
            i = oTR.childNodes.length;
          }
          i++;
        }
        
        var currentPage = (browser.isIE) ? oTR.childNodes[currentPosition].innerText : oTR.childNodes[currentPosition].textContent;
        
        switch(pageSelect) {
          case "<<":
            // Hightlight next page except if already on last page
            var previousPage = (browser.isIE) ? oTR.childNodes[currentPosition - 1].innerText : oTR.childNodes[currentPosition - 1].textContent;
            
            if(Number(previousPage)) {
              oTR.childNodes[currentPosition - 1].className = "pageselect";
              oTR.childNodes[currentPosition].className = "pageof";
              pageSelect = parseInt(previousPage);
            } else {
                // User is on first page display on screen and want to see
                // the previous page selection if applicable
                pageSelect = parseInt(currentPage);
                
                if(pageSelect != 1) {
                  refreshDirection = 1; // previous jumper
                  pageSelect--;
                }
              }
            break;
          
          case ">>":
            // Hightlight next page except if already on last page
            var nextPage = (browser.isIE) ? oTR.childNodes[currentPosition + 1].innerText : oTR.childNodes[currentPosition + 1].textContent;
            
            if(Number(nextPage)) {
              oTR.childNodes[currentPosition + 1].className = "pageselect";
              oTR.childNodes[currentPosition].className = "pageof";
              pageSelect = parseInt(nextPage);
            } else {
                // User is on last page display on screen and want to see
                // the next page selection if applicable
                pageSelect = parseInt(currentPage);
                var numberOfPage = parseInt(oTR.getAttribute("numberofpage"));
                
                if(pageSelect < numberOfPage) {
                  refreshDirection = 2; // next jumper
                  pageSelect++;
                }
              }
            break;
        }
      }
    
    // Get the current grid object
    var currentGridID = oTR.getAttribute("gridID");
    var currentGrid = arrGridMarshall[currentGridID];
    
    // Refresh the grid data (and paging if necessary)
    RefreshGrid(currentGrid, pageSelect, refreshDirection);
    
  } catch(e) {
      StatusBarWrite("Error (fnc : " + "OnGridPageXofY" + "). " + e.message, 3000);
    }
}

function RefreshPageXofY(gridID, pageSelect, tableLength, refreshDirection) {
  try {
    // This is where you refresh/change the page X of Y content.
    // This function is call every time the user select the previous/next icon 
    // and the page currently select is on the first/last page X of Y.
    // The event (evt) is pass only as reference in case you want to tailor some special coding.
    
    // Reajust listLength by the page select pointer and row per page
    var pageStart = (pageSelect - 1) * rowPerPage;
    var listLength = tableLength - pageStart;
    
    // Calculate the number of "page X of Y" to display
    var numberOfPage = Math.ceil(tableLength/rowPerPage);
    var numberOfPageDisplay = 0;
    if(listLength > 0) { numberOfPageDisplay = Math.ceil(listLength/rowPerPage); }
    
    if(numberOfPageDisplay > -1) {
      var pageNumber;
      var oPages = document.getElementById("gridShowPageOf");  // TR table marker but you can use
                                                               // your favorite style technique
      if(browser.isIE) {
        // Read only property for TR marker in Ie
        var oTR = document.createElement("tr");
        oTR.setAttribute("id", oPages.id);
        
        var oParent = oPages.parentNode;
        oParent.replaceChild(oTR, oPages);
        oPages = oTR;
      } else {
          oPages.innerHTML = "";
        }
      
      oPages.setAttribute("gridID", gridID);                   // Set the grid id
      oPages.setAttribute("tablelength", tableLength);         // Set the number of record
      oPages.setAttribute("numberofpage", numberOfPage);       // Set the number of page
                                                               // You can also set the row per page 
                                                               // for tailor rendering per table
      var oPage = document.createElement("td");
      oPage.innerHTML = "Page:";
      oPage.className = "page";
      oPages.appendChild(oPage);
      
      oPage = document.createElement("td");
      oPage.className = "pageof";
      oPage.title = "Previous Page";
      oPage.innerHTML = "&lt;&lt;"; // Previous symbol
      oPage.onclick = function(event){ OnGridPageXofY(event, this); };
      
      oPages.appendChild(oPage);
      
      var i = 0;
      while(i < maxPage && i < numberOfPageDisplay) {
        pageNumber = pageSelect + i;
        oPage = document.createElement("td");
        
        if(pageNumber == pageSelect && refreshDirection != 1) {
          oPage.className = "pageselect";
        } else { oPage.className = "pageof"; }
        
        oPage.title = "Page " + pageNumber;
        oPage.innerHTML = pageNumber;
        oPage.onclick = function(event){ OnGridPageXofY(event, this); };
        
        oPages.appendChild(oPage);
        i++;
      }
      
      // Previous page select at the begining of the page display
      // Highlight last page X of Y instead of first one
      if(refreshDirection == 1) { oPage.className = "pageselect"; }
      
      // If number of record display is lest than the rowPerPage, still display page 1
      if(numberOfPageDisplay == 0) {
        pageNumber = 1;
        oPage = document.createElement("td");
        oPage.className = "pageselect";
        oPage.title = "Page " + pageNumber;
        oPage.innerHTML = pageNumber;
        oPage.onclick = function(event){ OnGridPageXofY(event, this); };
        
        oPages.appendChild(oPage);
      }
      
      // Show next page
      oPage = document.createElement("td");
      oPage.className = "pageof";
      oPage.title = "Next Page";
      oPage.innerHTML = "&gt;&gt;"; // Next symbol
      oPage.onclick = function(event){ OnGridPageXofY(event, this); };
      
      oPages.appendChild(oPage);
      
      // Show the number of record found in data base table
      oPage = document.createElement("td");
      oPage.className = "page";
      if(tableLength > 1) { // Grammatical congugation
        oPage.title = tableLength + " records found";
        oPage.innerHTML = "(" + tableLength + " records)";
      } else {
          oPage.title = tableLength + "record found in DB";
          oPage.innerHTML = "(" + tableLength + " record)";
        }
      
      oPages.appendChild(oPage);
    }
    
  } catch(e) {
      StatusBarWrite("Error (fnc : " + "RefreshPageXofY" + "). " + e.message, 3000);
    }
}
