

function TableUtility()
{

   var tableRef = Object;
   var rowArray = Array;
   var headCellArray = Array;
   var currSortColumn = Object;
   var prevSortColumn = '3';
   var dataType = Object;
   var paginateFlag=false;
   var sortFlag=false;
   var columnList;
   var filterFieldList;
   var defaultRowsPerPage = 10;
   var selectedIndustry='All';
   var selectedCellIndex=0;
   var tableId;
   var tableBodyRef;
   var header
   var headerRows
   var tableBodyRef
   var currPageNo

   function get()
   {
   }

   // method for getting the cell reference
   function getCellRef(index)
   {
      return rowArray[index].cells[currSortColumn];
   }

   this.hideRows = function() { hideRows(); };
   // init method    
   this.init = function(tId, pFlag, rpp, sFlag, cList, fList, sortField)
               {

                  tableId = tId;
                  paginateFlag = pFlag;
                  tableRef = document.getElementById(tableId);
                  header = tableRef.getElementsByTagName("thead");
                  headerRows = header[0].getElementsByTagName("tr");

                  if (headerRows.length > 1)
                  {
                     headCellArray = headerRows[1].getElementsByTagName("th");
                  }
                  else
                  {
                     headCellArray = headerRows[0].getElementsByTagName("th");
                  }

                  tableBodyRef = tableRef.getElementsByTagName("tbody");
                  sortFlag = sFlag;
                  paginateFlag = pFlag;
                  filterFieldList = fList;
                  columnList = cList;
                  defaultRowsPerPage = rpp;
                                    
                  if (sortFlag)
                  {
                     for(var i = 0; i < headCellArray.length ; i++)
                     {
                        headCellArray[i].onclick = function()
                                                   {
                                                      rowSort(this);
                                                   }
                     }
                  }
                        
                  if (fList)
                  {
                     for(var i = 0; i < fList.length ; i++)
                     {
                        fList[i].onchange= function() { hideRows(); };
                     }
                  }
                  if (pFlag)
                  {
                     paginate(sortField);
                  }
                                    
                  return true;
              };


   function paginate(sortField)
   {
      var totalRows = tableRef.rows.length;    
      var pageRowFooter = tableRef.insertRow(totalRows);
      var pageColFooter = pageRowFooter.insertCell(0);
                
      pageColFooter.setAttribute("colSpan", ''+headCellArray.length);
      pageColFooter.setAttribute("class", "th-content-left");

      var totalPages = Math.ceil((totalRows-1)/defaultRowsPerPage);     

      if (totalPages < 2) return false;
        
      /* for (var t=0; t < totalPages; t++)
      {
         var pagelink = document.createElement("a");
         pagelink.setAttribute("href", " ");
         pagelink.setAttribute("id", t);
         pagelink.onclick = function() { return repaginate(this.id)};
         var textNode = document.createTextNode(''+(t+1));
         pagelink.appendChild(textNode);
         pageColFooter.appendChild(pagelink);
         textNode = document.createTextNode(' ');
         pageColFooter.appendChild(textNode);
      }
      */
                
      var ftable = document.createElement("table");
      ftable.setAttribute("cellpadding","0");
      ftable.setAttribute("cellspacing","0");
      ftable.setAttribute("cellborder","0");
      ftable.setAttribute("width", "100%");
                
      var frow = document.createElement("tr");
      var fcol1 = document.createElement("td");
      var fcol2 = document.createElement("td");
      fcol2.setAttribute("align", "right");
      var fform = document.createElement("form");
      var frowList = document.createElement("select");
      frowList.onchange = function () { changeDefaultRowsPerPage(this); }
                
      var t1= parseInt(tableRef.rows.length/5)+1;
                
      for (var t2=1; t2 <= t1; t2++)
      {
         var fopt = document.createElement("option");
         fopt.setAttribute("value", ''+(t2*5));
                        
         if (defaultRowsPerPage == (t2*5))
         {
            fopt.setAttribute("selected", "true");
         }
                        
         var ftext = document.createTextNode(''+(t2*5));
         fopt.appendChild(ftext);
         frowList.appendChild(fopt);        
      }
                
      fcol1.appendChild(frowList);
      frow.appendChild(fcol1);
      
      frow.appendChild(fcol2);                
      
      ftable.appendChild(frow);                
      fform.appendChild(ftable);
      pageColFooter.appendChild(fform);
                
      if (sortFlag) 
      {
          rowSort(headCellArray[0]);
          rowSort(headCellArray[sortField]);
      }

      repaginate(0);

      return false;

    }

    function changeDefaultRowsPerPage(frList)
    {
       defaultRowsPerPage = frList.options[frList.selectedIndex].value;
       repaginate(0);
    }
        
    function repaginate(currPageIndex)
    {
       currPageNo = currPageIndex;
       //alert(currPageIndex);
       var totalRows = tableRef.rows.length;
       var tableRows = tableRef.rows;
       var ridx=(headerRows.length == 2 ? 2 : 1);
       // here you have to loop through the columns
      
       var matchingRowCount=0;

       tableRef.deleteRow(tableRef.rows.length-1);    
            
       showRows(currPageIndex);
        
       while (currRow = tableRows.item(ridx))
       {
          var currCol;
          var colMatchCount=0;
          var cidx=0;
          while (currCol = headCellArray[columnList[cidx]])
          {
             var datatype = currCol.abbr;
             var currCell = currRow.cells[columnList[cidx]];
             var cellValue;
                
             if (datatype == "hyperlink")
             {
                cellValue = currCell.firstChild.firstChild.nodeValue;
             }        
             else if (currCell.firstChild)
             {
                cellValue = currCell.firstChild.nodeValue;
             }
             else
             {
                cellValue ='';
             }

             var fcol = filterFieldList[cidx];
             var fcolVal = fcol.options[fcol.selectedIndex].value;
                             
             if (cellValue == fcolVal || fcolVal == 'All')
             {
                colMatchCount++;
             }
             cidx++;
          }
                        
          if (colMatchCount == columnList.length)
          {
             matchingRowCount++;
          }
                        
          ridx++;
       }
                
       totalRows = matchingRowCount;
        
       var totalPages = Math.ceil((totalRows-1)/    defaultRowsPerPage);

       /*if (totalPages < 2)
       {
          var pageRowFooter = tableRef.insertRow(tableRef.rows.length);
          var pageColFooter = pageRowFooter.insertCell(0);
          var textNode = document.createTextNode('');
          pageColFooter.appendChild(textNode);
          pageRowFooter.style.display='none';
          return false;
       }*/
             
       var pageRowFooter = tableRef.insertRow(tableRef.rows.length);
       pageRowFooter.setAttribute('style','background-color: #CCCCCC;');    
        
       var halfspan = (headCellArray.length % 2);
                
       var pageColFooter1 = pageRowFooter.insertCell(0);
       pageColFooter1.setAttribute("colSpan", ''+ (headCellArray.length/2));
       pageColFooter1.style.backgroundColor='#cccccc';
                
                
       var pageColFooter2 = pageRowFooter.insertCell(1);
       pageColFooter2.setAttribute("colSpan", ''+(halfspan == 1 ? (parseInt(headCellArray.length/2)+1) : headCellArray.length/2));                
       pageColFooter2.setAttribute("align", "right");        
       pageColFooter2.style.backgroundColor='#cccccc';

       var div0 = document.createElement("div");
       var div1 = document.createElement("div");
       div1.setAttribute('style','text-align: left');
       var div2 = document.createElement("div");
       div2.setAttribute('style','text-align: right');
                
                                
       var fform1 = document.createElement("form");
       var fform2 = document.createElement("form");
       var frowList = document.createElement("select");
       frowList.setAttribute("name", "selectrows");
       frowList.onchange = function () { changeDefaultRowsPerPage(this); }
                
       var t1= parseInt(tableRef.rows.length/5)+1;
                
       for (var t2=1; t2 <= t1; t2++)
       {
          var fopt = document.createElement("option");
          fopt.setAttribute("value", ''+(t2*5));
                        
          if (defaultRowsPerPage == (t2*5))
          {
             fopt.setAttribute("selected", "true");
          }
                        
          var ftext = document.createTextNode(''+(t2*5));
          fopt.appendChild(ftext);
          frowList.appendChild(fopt);        
       }
                
       fform1.appendChild(document.createTextNode("Items per Page : "));
       fform1.appendChild(frowList);
       pageColFooter1.appendChild(fform1);
       var c1 = ((parseInt(currPageIndex)+1) * defaultRowsPerPage);
  
       var currText = 'Showing ' + ((parseInt(currPageIndex) * defaultRowsPerPage)+1) +' - ' 
                 + (c1 > totalRows ? totalRows : c1) + ' of ' + totalRows+' | ';    
                 
       var fplink
                
       if (currPageIndex == 0)
       {
          fplink = document.createTextNode(currText+'< Previous');
          fform2.appendChild(fplink);
       }
       else
       {
          fplink = document.createElement("a");
          fplink.setAttribute("id", ''+(parseInt(currPageIndex)-1));
          fplink.onclick = function () { return repaginate(this.id)};
          fplink.appendChild(document.createTextNode('< Previous'));
          fform2.appendChild(document.createTextNode(currText+' '));
          fform2.appendChild(fplink);
       }
                        
       frowList = document.createElement("select");
       frowList.setAttribute("name", "pageselect");
       frowList.onchange = function () { return repaginate(this.options[this.selectedIndex].value); }
                
       for (var t2=0; t2 < totalPages; t2++)
       {
          var fopt = document.createElement("option");
          fopt.setAttribute("value", ''+t2);
                        
          if (currPageIndex == t2)
          {
             fopt.setAttribute("selected", "true");
          }
                        
          var ftext = document.createTextNode(''+(t2+1));
          fopt.appendChild(ftext);
          frowList.appendChild(fopt);        
       }
                
       var fnlink
                
       if (currPageIndex == (totalPages-1))
       {
          fnlink = document.createTextNode('Next >');
       }
       else
       {
          fnlink = document.createElement("a");
          fnlink.setAttribute("id", ''+(parseInt(currPageIndex)+1));
          fnlink.onclick = function () { return repaginate(this.id)};
          fnlink.appendChild( document.createTextNode('Next >'));
       }
                

       fform2.appendChild( document.createTextNode(' '));                
       fform2.appendChild(frowList);
       fform2.appendChild( document.createTextNode(' '));
       fform2.appendChild(fnlink);;
                
       pageColFooter2.appendChild(fform2);                                
                
       return false;
    }

    function showRows(currPageIndex)
    {

       var tableRows = tableRef.rows;
       var currRow;
       var ridx=(headerRows.length == 2 ? 2 : 1);


       var matchingRowCount=0;

       while (currRow = tableRows.item(ridx))
       {
          var cidx=0;
          var cellValue;
          var colMatchCount=0;
                        
          while (currCol = headCellArray[columnList[cidx]])
          {
             var datatype = currCol.abbr;
             var currCell = currRow.cells[columnList[cidx]];
             var cellValue;
                
             if (datatype == "hyperlink")
             {
                cellValue = currCell.firstChild.firstChild.nodeValue;
             }        
             else if (currCell.firstChild)
             {
                cellValue = currCell.firstChild.nodeValue;
             }
             else
             {
                cellValue ='';
             }

             var fcol = filterFieldList[cidx];
             var fcolVal = fcol.options[fcol.selectedIndex].value;
                             
             if (cellValue == fcolVal || fcolVal == 'All')
             {
                colMatchCount++;
             }
             cidx++;
          }

          if (colMatchCount == columnList.length)
          {
             var currPageRowIndex = matchingRowCount + (currPageIndex * defaultRowsPerPage);
             var currPageStartIndex = (currPageIndex * defaultRowsPerPage);
             var currPageEndIndex = ((parseInt(currPageIndex)+1)*defaultRowsPerPage)-1;
                        
             //alert(matchingRowCount+','+currPageStartIndex+','+currPageEndIndex+','+defaultRowsPerPage+','+currPageIndex);

             if ((matchingRowCount < currPageStartIndex || matchingRowCount > currPageEndIndex) && paginateFlag)
             {
                currRow.style.display='none';
             }
             else
             {
                currRow.style.display='';
             }

             matchingRowCount++;
          }
          else
          {
             currRow.style.display='none';
          }
                        
          ridx++;
       }

       return false;
    }        

    function hideRows()
    {
       var tableRows = tableRef.rows;
       //alert('in hide rows');

       var currRow;
       var ridx=(headerRows.length == 2 ? 2 : 1);
       var matchingRowCount=0;
       var currPageIndex=0;

       while (currRow = tableRows.item(ridx))
       {
          if (ridx++ == tableRows.length -1 && paginateFlag) continue;                
                    
          var cidx=0;
          var cellValue;
          var colMatchCount=0;
                        
          while (currCol = headCellArray[columnList[cidx]])
          {

             var datatype = currCol.abbr;

             var currCell = currRow.cells[columnList[cidx]];
             var cellValue;
                
             if (datatype == "hyperlink")
             {
                 cellValue = currCell.firstChild.firstChild.nodeValue;
             }        
             else if (currCell.firstChild)
             {
                 cellValue = currCell.firstChild.nodeValue;
             }
             else
             {
                 cellValue ='';
             }

             var fcol = filterFieldList[cidx];
             var fcolVal = fcol.options[fcol.selectedIndex].value;
                             
             if (cellValue == fcolVal || fcolVal == 'All')
             {
                 colMatchCount++;
             }
             cidx++;
          }
                        

          if (colMatchCount == columnList.length)
          {
             var currPageRowIndex = matchingRowCount + (currPageIndex * defaultRowsPerPage);
             var currPageStartIndex = (currPageIndex * defaultRowsPerPage);
             var currPageEndIndex = ((currPageIndex+1) * defaultRowsPerPage) -1;
                        
             if ((matchingRowCount < currPageStartIndex || matchingRowCount > currPageEndIndex) && paginateFlag)
             {
                currRow.style.display='none';
             }
             else
             {
                currRow.style.display='';
             }
             matchingRowCount++;
          }
          else
          {
             currRow.style.display='none';
          }                                        
       }

       if (paginateFlag)
       {
          repaginate(0);
       }
    }

        
    function rowSort(headCellRef)
    {
       currSortColumn = headCellRef.cellIndex;
       dataType = headCellRef.abbr;
       rowArray = tableRef.tBodies[0].getElementsByTagName("tr");


       //define the get function based on the data type
       defineGet(dataType)

       var img = document.getElementById('simg'+headCellRef.cellIndex);


       // if the table is sorted just reverse else sort the table
       if (prevSortColumn == currSortColumn)
       {
          //headCellRef.className = (headCellRef.className != 'ascend' ? 'descend' : 'ascend' );
          var iflds = img.src.split('/');
          if (iflds[iflds.length-1] == 'http://www.cisco.com/web/about/ac79/how/mod_grey_arrow_dwn.gif')
          {
             img.src = 'http://www.cisco.com/web/about/ac79/how/mod_grey_arrow_up.gif';
          }
          else
          {
             img.src = 'http://www.cisco.com/web/about/ac79/how/mod_grey_arrow_dwn.gif';                        
          }
          reverseHtmlTable();
       }
       else
       {
          for (x1=0; x1 < headCellArray.length; x1++)
          {
             var hcell = headCellArray[x1];
             var imgtmp = document.getElementById('simg'+hcell.cellIndex);
             imgtmp.src='http://www.cisco.com/swa/i/s.gif';
          }                
          //headCellRef.className = 'ascend';
          img.src='http://www.cisco.com/web/about/ac79/how/mod_grey_arrow_dwn.gif';
          if (headCellArray[prevSortColumn].className != 'th-content-left')
          {
             headCellArray[prevSortColumn].className = 'th-content-left';
          }

          if (paginateFlag)
             q_sort(0, rowArray.length-1);
          else
             q_sort(0, rowArray.length);
        }
                    
        prevSortColumn = currSortColumn;
                
        if (paginateFlag) repaginate(currPageNo);
    }


    function defineGet(dataType)
    {
       switch(dataType)
       {   
           case "hyperlink":
              get = function(index)
                    {
                       return  getCellRef(index).firstChild.firstChild.nodeValue;
                    };
              break;
                
           case "text":
              get = function(index)
                    {    
                       var cell = getCellRef(index);
                                                    
                       if (cell.firstChild)
                       {
                          return cell.firstChild.value;
                       }
                       else
                       {
                          return '';
                       }
                    };
              break;        
           case "date_pub":
              get = function(index)
                    {    
                       var months = new Array();
                       months['Jan']='01';
                       months['Feb']='02';
                       months['Mar']='03';
                       months['Apr']='04';
                       months['May']='05';
                       months['Jun']='06';
                       months['Jul']='07';
                       months['Aug']='08';
                       months['Sep']='09';
                       months['Oct']='10';
                       months['Nov']='11';
                       months['Dec']='12';
                       months['Year']='13';
                                                    
                       var cell = getCellRef(index);
                       var pubDate=' ';

                       if (cell.firstChild)
                       {
                          pubDate = cell.firstChild.nodeValue;
                       }
                                                    
                       //alert(pubDate);
                       var str = pubDate.split('-');
                                                    
                       var retValue='';
                       if (str.length > 1)
                       {
                          retValue = '9'+str[1]+months[str[0]];
                       }
                       else
                       {
                          retValue = '00000';
                       }
 
                       return retValue;
                    };
              break;                                    
           default:
              get = function(index)
                    {
                       //alert('default ' + getCellRef(index).firstChild.nodeValue);
                       return getCellRef(index).firstChild.nodeValue;
                    };
              break;
       };    
    }
        

        
    function exchangeCells(i, j)
    {
       if (i == j+1)
       {
          tableRef.tBodies[0].insertBefore(rowArray[i], rowArray[j]);
       }
       else if(j == i+1)
       {
          tableRef.tBodies[0].insertBefore(rowArray[j], rowArray[i]);
       }
       else
       {

          var tmpNode = tableRef.tBodies[0].replaceChild(rowArray[i], rowArray[j]);

          if (typeof(rowArray[i]) == "undefined")
          {
             tableRef.appendChild(tmpNode);
          }
          else
          {
             tableRef.tBodies[0].insertBefore(tmpNode, rowArray[i]);
          }
       }
    }

    function reverseHtmlTable()
    {
       var trows = rowArray.length;

       if (paginateFlag) trows--;

       for (var i = 1; i<trows; i++)
       {
          tableRef.tBodies[0].insertBefore(rowArray[i], rowArray[0]);
       }
    }
        

    function q_sort(lo, hi)
    {
       if (hi <= lo+1)
       {
          return;
       }

       if ((hi - lo) == 2)
       {
          if (get(hi-1) > get(lo)) 
          {
             exchangeCells(hi-1, lo);
          }
          return;
       }
    
       var i = lo + 1;
       var j = hi - 1;

       if (get(lo) > get(i))
       {
          exchangeCells(i, lo);
       }
                
       if (get(j) > get(lo))
       {
          exchangeCells(lo, j);
       }
                
       if (get(lo) > get(i))
       {
          exchangeCells(i, lo);
       }
    
       var temp = get(lo);

       while(true)
       {
          j--;
          while (temp > get(j)) 
          {
             j--;
          }
                        
          i++;

          while(get(i) > temp)
          {
             i++;
          }
                        
          if (j <= i) 
          {
             break;
          }
          exchangeCells(i, j);
       }

       exchangeCells(lo, j);

       if ((j-lo) < (hi-j))
       {
          q_sort(lo, j);
          q_sort(j+1, hi);
       }
       else
       {
          q_sort(j+1, hi);
          q_sort(lo, j);
       }
    }
}
