function SortableTable(id,rows,cols,border,width,align) {
 this.id = id
 this.rows = rows
 this.cols = cols
 this.width = width
 this.align = align
 this.buttons = new Array(cols)
 this.numeric = new Array(cols)
 for(var i=0;i<cols;++i)
  this.buttons[i]="Ý"
 for(var i=0;i<cols;++i)
  this.numeric[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.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 BORDER=0 WIDTH="')
 document.writeln(this.width+'" ALIGN="'+this.align+'">')
 document.writeln('<TR>')

 // Display buttons
 for(var i=0;i<this.cols;++i) {
  document.write('<TD>')
  document.write('<INPUT class=subs 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>')
  for(var j=0;j<this.cols;++j) {
   document.write('<TD class=beft>')
    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_sort(n) {

 // Sort rows
 var changes=true
 for(;changes;) {
  changes = false
  for(var i=0;i<this.rows-1;++i) {
   if(this.numeric[n]) {
    var v1 = parseFloat(this.data[i][n])
    if(isNaN(v1)) v1 = 0;
    var v2 = parseFloat(this.data[i+1][n])
    if(isNaN(v2)) v2 = 0;
    if(v1 > v2) {
     changes = true
     var temp = this.data[i]
     this.data[i] = this.data[i+1]
     this.data[i+1] = temp
    }
   }else{
    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()
}

