<!-- Hide the scripts from non-javascript browsers --

/** [MSORT.JS]
 *
 *  Boedeker Plastics, Inc. : Javascript Source Code File - Table Sort Functions
 *
 *  History : Created by Scott Baer 2000
 *            01 - 13Oct00 - extracted from MTABLE.HTM
 *            02 - 17Oct01 - added reverse sort order function & tests
 *                           changed default numeric value (for alpha) from 0 to 99999
 *            03 - 25Jul07 - added CELLPADDING="2" to TABLE display
 */
 

function SortableTable(id,rows,cols,border,width,align) {
 this.id = id
 this.rows = rows
 this.cols = cols
 this.border = border
 this.width = width
 this.align = align
 this.buttons = new Array(cols)
 this.numeric = new Array(cols)
 this.reverse = new Array(cols)
 for(var i=0;i<cols;++i)
  this.buttons[i]=" "
 for(var i=0;i<cols;++i)
  this.numeric[i]=false
 for(var i=0;i<cols;++i)
  this.reverse[i]=false
 this.data = new Array(rows)
 for(var i=0;i<rows;++i) {
  this.data[i] = new Array(cols)
  for(var j=0;j<cols;++j)
   this.data[i][j] = " "
 }

 this.display = SortableTable_display
 this.setButtons = SortableTable_setButtons
 this.setData = SortableTable_setData
 this.setNumeric = SortableTable_setNumeric
 this.setReverse = SortableTable_setReverse
 this.sort = SortableTable_sort
}

function SortableTable_setData(data) {
 if(data == null) return
 if(data.length > 0) {
  for(var i=0;i<this.rows;++i) {
   if(i>=data.length) break
   if(data[i] != null) {
    var n = data[i].length
    if(n > this.cols) n = this.cols
    for(var j=0;j<n;++j)
     if(data[i][j]!=null) this.data[i][j] = data[i][j]
   }
  }
 }
}

function SortableTable_setButtons(buttons) {
 if(buttons == null) return
 var n = buttons.length
 if(n > this.cols) n = this.cols
 for(var i=0;i<n;++i)
  if(buttons[i]!=null) this.buttons[i] = buttons[i]
}

function SortableTable_display() {
 // Check for tableSortBy cookie
 if(document.cookie.length > 0) {
  var search = this.id + "="
  var offset = document.cookie.indexOf(this.id+"=")
  if(offset != -1) {
   offset += search.length
   var end = document.cookie.indexOf(";", offset)
   if(end == -1) end = document.cookie.length
   this.sort(document.cookie.substring(offset, end))
  }
 }

 document.writeln('<FORM NAME="tableForm">')

 document.write('<TABLE CELLPADDING="2" BORDER="'+this.border+'" WIDTH="')
 document.writeln(this.width+'" ALIGN="'+this.align+'" BGCOLOR="FFFFFF">')
 document.writeln('<TR>')
 // Display buttons
 for(var i=0;i<this.cols;++i) {
  document.write('<TD ALIGN="'+this.align+'">')
  document.write('<INPUT TYPE="BUTTON" VALUE="')
  document.write(this.buttons[i])
  document.write('" ONCLICK="SortableTable_handleColumnButton(\'')
  document.write(this.id+'\','+i+')">')
  document.writeln('</TD>')
 }
 document.writeln('</TR>')
 
 // Display sorted/unsorted table
 for(var i=0;i<this.rows;++i) {
  document.writeln('<TR>')
 // Display first cell (0) aligned to left
  document.write('<TD ALIGN="LEFT"><SMALL>')
     document.write(this.data[i][0])
     document.writeln('</TD>')
 // Display other cells aligned to variablename
  for(var j=1;j<this.cols;++j) {
   document.write('<TD ALIGN="'+this.align+'"><SMALL>')
    document.write(this.data[i][j])
    document.writeln('</TD>')
  }
  document.writeln('</TR>')
 }
 document.writeln('</TABLE>')
 document.writeln('</FORM>')
}

function SortableTable_setNumeric(n) {
 this.numeric[n] = true
}

function SortableTable_setReverse(n) {
 this.reverse[n] = true
}

function SortableTable_sort(n) {
// Sort rows 
// (numeric = descending order / numeric2 = ascending order / alpha = ascending)
 var changes=true
 for(;changes;) {
  changes = false
  for(var i=0;i<this.rows-1;++i) {

// test for Numeric Sort
   if(this.numeric[n]) {

// Sort rows in numerical order
//  convert array values to numeric values
    var v1 = parseFloat(this.data[i][n])
    if(isNaN(v1)) v1 = 99999;
    var v2 = parseFloat(this.data[i+1][n])
    if(isNaN(v2)) v2 = 99999;
//  test for Reverse Sort
    if(this.reverse[n]) {
//   sort in reverse numeric (1-9) order
     if(v1 > v2) {
      changes = true
      var temp = this.data[i+1]
      this.data[i+1] = this.data[i]
      this.data[i] = temp
     }
    }else{
//   sort in normal numeric (9-1) order
     if(v1 < v2) {
      changes = true
      var temp = this.data[i]
      this.data[i] = this.data[i+1]
      this.data[i+1] = temp
     }
    }
   
   }else{
// Sort rows (in alphabetical order)
//  test for Reverse Sort
     if(this.reverse[n]) {
//   sort in reverse alphabetical (Z-A) order
       if(this.data[i][n] < this.data[i+1][n]) {
         changes = true
         var temp = this.data[i+1]
         this.data[i+1] = this.data[i]
         this.data[i] = temp
       }
     }else{
//   sort in normal alphabetical (A-Z) order
       if(this.data[i][n] > this.data[i+1][n]) {
         changes = true
         var temp = this.data[i]
         this.data[i] = this.data[i+1]
         this.data[i+1] = temp
       }
     }
   }
  }
 }
}

function SortableTable_handleColumnButton(id,n) {
 // Set cookie and reload
 document.cookie = id + "="+n
 window.location.reload()
}


//--End JavaScript Hiding Here -->



