var tableWidget_okToSort = true;
var tableWidget_arraySort = new Array();
tableWidget_tableCounter = 1;
var activeColumn = new Array();
var currentColumn = false;
//var prev_sort_index = 'sort1';
var sorted_by = 0;

function bare_string (str)
{
  str = str.replace(/<SCRIPT.*?SCRIPT>/i, '');
  str = str.replace(/<A.*?>/i, '');
  str = str.replace(/<\/A>/i, '');
  return str;
}

function sortNumeric(a,b){

  a = bare_string(a);
  a = a.replace(/,/,'.');
  a = a.replace(/[^\d\.\/]/g,'');
  b = bare_string(b);
  b = b.replace(/,/,'.');
  b = b.replace(/[^\d\.\/]/g,'');
  if(a=='')a=0;
  if(b=='')b=0;
  /*if(a.indexOf('/')>=0)a = eval(a);
  if(b.indexOf('/')>=0)b = eval(b);*/
  return a/1 - b/1;
}


function sortString(a, b) {

  a = bare_string(a);
  b = bare_string(b);
  if ( a.toUpperCase() < b.toUpperCase() ) return -1;
  if ( a.toUpperCase() > b.toUpperCase() ) return 1;
  return 0;
}

function sortTable()
{
  if (!tableWidget_okToSort) return;

  self.status = "Sorting...";

  tableWidget_okToSort = false;
  /* Getting index of current column */
  var obj = this;
  var indexThis = 0;
  while(obj.previousSibling){
    obj = obj.previousSibling;
    if(obj.tagName=='TH')indexThis++;
  }

  if(this.getAttribute('direction') || this.direction){
    direction = this.getAttribute('direction');
    if(navigator.userAgent.indexOf('Opera')>=0)direction = this.direction;
    if (indexThis == sorted_by){
      if(direction=='ascending'){
        direction = 'descending';
        this.setAttribute('direction','descending');
        this.direction = 'descending';
      }else{
        direction = 'ascending';
        this.setAttribute('direction','ascending');
        this.direction = 'ascending';
      }
    }
  }else{
    if (indexThis == 0 && sorted_by == 0) {
      direction = 'descending';
      this.setAttribute('direction','descending');
      this.direction = 'descending';
    }else{
      direction = 'ascending';
      this.setAttribute('direction','ascending');
      this.direction = 'ascending';
    }
  }
  sorted_by = indexThis;

  var tableObj = this.parentNode.parentNode.parentNode;
  var tBody = tableObj.getElementsByTagName('TBODY')[0];

  var widgetIndex = tableObj.getAttribute('tableIndex');
  if (!widgetIndex) widgetIndex = tableObj.tableIndex;
  var prevSortIndex = tableObj.getAttribute('prevSortIndex');
  if (!prevSortIndex) prevSortIndex = tableObj.prevSortIndex;

  /* Update image sort arrows */

  var sortimga = getbyid(prevSortIndex);
  if (sortimga) {
    sortimga.style.display = 'none';
    sortimga.src = '';
    sortimga.alt = '';
  }
  var sortimg = getbyid('sort' + indexThis);
  if (!sortimg)
    sortimg = getbyid(tableObj.id + 'sort' + indexThis);
  if (sortimg) {
    sortimg.style.display = '';
    if (direction == 'ascending') {
      sortimg.src = '/design/table/down.gif';
      //sortimg.alt = '&dArr;';
      sortimg.alt = '&or;';
    }
    else {
      sortimg.src = '/design/table/up.gif';
      //sortimg.alt = '&uArr;';
      sortimg.alt = '&and;';
    }
    tableObj.setAttribute('prevSortIndex', sortimg.id);
    tableObj.prevSortIndex = sortimg.id;
  }

  var sortMethod = tableWidget_arraySort[widgetIndex][indexThis]; // N = numeric, S = String
  if (activeColumn[widgetIndex] && activeColumn[widgetIndex] != this)
    if (activeColumn[widgetIndex])
      activeColumn[widgetIndex].removeAttribute('direction');

  activeColumn[widgetIndex] = this;

  var cellArray = new Array();
  var cellObjArray = new Array();
  for (var no=1; no<tableObj.rows.length; no++){
    var content= tableObj.rows[no].cells[indexThis].innerHTML+'';
    cellArray.push(content);
    cellObjArray.push(tableObj.rows[no].cells[indexThis]);
  }

  if(sortMethod=='N')
    cellArray = cellArray.sort(sortNumeric);
  else
    cellArray = cellArray.sort(sortString);

  if(direction=='descending'){
    for(var no=cellArray.length;no>=0;no--){
      for(var no2=0;no2<cellObjArray.length;no2++){
        if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
          cellObjArray[no2].setAttribute('allreadySorted','1');
          tBody.appendChild(cellObjArray[no2].parentNode);
        }
      }
    }
  }else{
    for(var no=0;no<cellArray.length;no++){
      for(var no2=0;no2<cellObjArray.length;no2++){
        if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
          cellObjArray[no2].setAttribute('allreadySorted','1');
          tBody.appendChild(cellObjArray[no2].parentNode);
        }
      }
    }
  }

  for(var no2=0;no2<cellObjArray.length;no2++){
    cellObjArray[no2].removeAttribute('allreadySorted');
  }

  for(var no=1;no<tableObj.rows.length;no++){
    tableObj.rows[no].setAttribute('class', no % 2 ? 'tr2' : 'tr1');
  }

  tableWidget_okToSort = true;

  self.status = "Sorted";

  return false;
}

function initSortTable (objId, sortArray, prevSortIndex)
{
  if (!prevSortIndex)
    prevSortIndex = 'sort1';
  var obj = document.getElementById(objId);
  obj.setAttribute('tableIndex', tableWidget_tableCounter);
  obj.tableIndex = tableWidget_tableCounter;
  obj.setAttribute('prevSortIndex', prevSortIndex);
  obj.prevSortIndex = prevSortIndex;
  tableWidget_arraySort[tableWidget_tableCounter] = sortArray;
  var tHead = obj.getElementsByTagName('THEAD')[0];
  var cells = tHead.getElementsByTagName('TH');
  for (var no=0; no<cells.length; no++)
    if (sortArray[no])
      cells[no].onclick = sortTable;
    else
      cells[no].style.cursor = 'default';
  /*for(var no2=0;no2<sortArray.length;no2++){  // Right align numeric cells
    if(sortArray[no2] && sortArray[no2]=='N')obj.rows[0].cells[no2].style.textAlign='right';
  }*/

  tableWidget_tableCounter++;
}