// sorttable.js
// Matt Kruse
// used with heavy modification by FEBvar use_css=false;
var use_layers=false;
if (document.all) { use_css = true; };
if (document.layers) { use_layers = true; };
var sort_object;
var sort_column;
var reverse = 0;

function doSort(table, col, tableid, controlid) {
   //alert(table+"\n"+col+"\n"+tableid+"\n"+controlid);

   // call the actual sort function
   reverse = SortRows(table, col, tableid);
   sobj = document.getElementById("span_" + controlid + "_" + col);
   // figure out what image to use
   if ( reverse == 0 ) arrowsrc = '/commonspot/images/up-arrow.gif';
   else arrowsrc = '/commonspot/images/arrow.gif';

   // check for existence of arrow
   
   if ( sobj.innerHTML.indexOf('dsArrow') < 0 ) {
      othercols = table.sortable_cols.split(",");
      for (i = 0; i < othercols.length; i++) {
         id = othercols[i];
         testStr = '';
         if (id != col) {
			// alert(sobj+"  "+"span_" + controlid + "_" + col + '-- span_' + controlid + '_' + id);			 
            document.getElementById('span_' + controlid + '_' + id).innerHTML = '';
         }
      }
      sobj.innerHTML = '<img id="dsArrow_' + controlid + '" src="' + arrowsrc + '" alt="Sort" border="0" />';

   } else {
      // switch image
      imgel = document.getElementById('dsArrow_' + controlid);
      imgel.src = arrowsrc;
   }
}

// Constructor for SortTable object
function SortTable(name) {
   // Properties
   this.name = name;
   this.sortcolumn="";
   this.dosort=true;
   this.tablecontainsforms=false;
   this.sortable_cols = '';
   // Methods
   this.AddLine = AddLine;
   this.AddColumn = AddColumn;
   this.WriteRows = WriteRows;
   this.SortRows = SortRows;
   this.AddLineProperties = AddLineProperties;
   this.AddLineSortData = AddLineSortData;
   // Structure
   this.Columns = new Array();
   this.Lines = new Array();
   this.LineProperties = new Array();
   this.top = '';
   this.addTop = addTop;
   this.rows = new Array();
}

// Add a line to the grid
function AddLine() {
   var index = this.Lines.length;
   this.Lines[index] = new Array();
   rowText = '';

   for (var i=0; i<arguments.length; i++) {
      this.Lines[index][i] = new Object();
      //this.Lines[index][i].text = arguments[i];
      this.Lines[index][i].data = arguments[i];
      rowText = rowText + arguments[i];
   }

   this.Lines[index].offset = index;
   // save row text
   this.rows[index] = rowText;
}

function addTop(str) {
   this.top = str;
}

// Define properties for the <tr> of the last line added
function AddLineProperties(prop) {
   var index = this.Lines.length-1;
   if ( !this.LineProperties[index] ) this.LineProperties[index] = '';
   this.LineProperties[index] = this.LineProperties[index] + ' ' +prop;
}

// Define sorting data for the last line added
function AddLineSortData() {
   var index = this.Lines.length-1;

   for (var i=0; i<arguments.length; i++) {
      if (arguments[i] != '') {
         this.Lines[index][i].data = arguments[i].toLowerCase();
      }
   }
}

// Add a column definition to the table
// Arguments:
//   name = name of the column
//   td   = any arguments to go into the <td> tag for this column (ex: BGCOLOR="red")
//   align= Alignment of data in cells
//   type = type of data in this column (numeric, money, etc) - default alphanumeric
function AddColumn(name,type,id) {
   var index = this.Columns.length;
   this.Columns[index] = new Object;
   this.Columns[index].name = name;
   this.Columns[index].type = type;
   this.Columns[index].id = id;
}

// Print out the original set of rows in the grid
function WriteRows() {
   for (var i=0; i<this.Lines.length; i++) {
      document.write("<tr "+this.LineProperties[i]+" >" + this.rows[this.Lines[i].offset] + "</tr>");
   }
}

function FilterBy(table, controlid, url) {
   if (table.sort_column != null) {
      str = "&orderby_" + controlid + "=" + table.Columns[table.sort_column].id + escape("|");
      if (table.sort_reverse != 0) str += "desc";
      else str += "asc";
      url += str;
   }
   window.location.href = url;
}

// Sort the table and re-write the results to the existing table
function SortRows(table,column,sid) {
   sort_object = table;
   if (!sort_object.dosort) return;
   if (sort_column == column) reverse=1-reverse;
   else reverse=0;
   sort_column = column;table.sort_column = column;
   table.sort_reverse = reverse;
   if (table.Columns[column].type == "numeric") {
      // Sort by Float
      table.Lines.sort(	function by_name(a,b) {
         if (parseFloat(a[column].data) < parseFloat(b[column].data) ) { return -1; }
         if (parseFloat(a[column].data) > parseFloat(b[column].data) ) { return 1; }
         return 0;
      } );

   } else if (table.Columns[column].type == "money") {
      // Sort by Money
      table.Lines.sort(	function by_name(a,b) {
         if (parseFloat(a[column].data.substring(1)) < parseFloat(b[column].data.substring(1)) ) { return -1; }
         if (parseFloat(a[column].data.substring(1)) > parseFloat(b[column].data.substring(1)) ) { return 1; }
         return 0;
      });

   } else if (table.Columns[column].type == "date") {
      // Sort by Date
      table.Lines.sort(	function by_name(a,b) {
         if (Date.parse(a[column].data) < Date.parse(b[column].data) ) { return -1; }
         if (Date.parse(a[column].data) > Date.parse(b[column].data) ) { return 1; }
         return 0;
      });

   }else {
      // Sort by alphanumeric
      table.Lines.sort( function by_name(a,b) {
         if ( a[column].data+"" < b[column].data+"") {
            return -1;
         }
         if (a[column].data+"" > b[column].data+"") {
            return 1;
         }
         return 0;
      });
   }

   if (reverse) { table.Lines.reverse(); }
   var newContents = table.top;

   for (var i=0; i<table.Lines.length; i++) {
      //alert("index=" + i + "offset=" + table.Lines[i].offset);
      newContents = newContents + "<tr><td colspan='5'><div class='divDottedLine'></div></td></tr>";	  
      newContents = newContents + "<tr " + replaceOnClick(table.LineProperties[i],table.LineProperties[table.Lines[i].offset]) + ' summary="">' + table.rows[table.Lines[i].offset] + "</tr>";
      if (i == table.Lines.length-1) newContents = newContents + "<tr><td colspan='5'><div class='divDottedLine'></div></td></tr>";
   }
   newContents = newContents + "</table>";
   document.getElementById(sid).innerHTML = newContents;
   return reverse;
}

function replaceOnClick(strProps,strNewProps) {
   var reOnClick=/onclick=\"[^\"]*\"/gi;
   return strProps.replace(reOnClick, reOnClick.exec(strNewProps));
}
