var topCellsToSkip = "2";
var bottomCellsToSkip = "1";
var leftCellsToSkip = "0";
var rightCellsToSkip = "1";
var displayTooltips = "false";
var sortOnLoad = "false";
var imageServerAndPath;

var ts_PathToUpImage = "";
var ts_PathToDownImage = "";
var ts_check = new RegExp("[%&;'" + '"' + " ]");
var ts_WidthOfImage = 11;
var ts_HeightOfImage = 7;

/*removed as it no longer should be a global variable
var ts_tbody = null;*/
var ts_theadrow = null;
var ts_reverse = false;
var ts_lastClickedObject = null;

function ts_init(imageURL,tableName)
{
  imageServerAndPath = imageURL;
  if (imageServerAndPath.indexOf (ts_check) == -1) {
    ts_PathToUpImage = imageServerAndPath + "column_sort_up.gif";
    ts_PathToDownImage = imageServerAndPath + "column_sort_down.gif";
  }

  if (imageServerAndPath.indexOf ('etf') != -1) {
    ts_WidthOfImage = 7;
    ts_HeightOfImage = 9;
  }

  /* removed code as tbody is required only in the sort function
  ts_tbody = document.getElementById(tableName).tBodies[0];
  if (!ts_tbody) return;*/
  var thead = document.getElementById(tableName).tHead;
  if (!thead) return;
  if (thead.rows.length == 0) return;

  ts_theadrow = thead.rows[topCellsToSkip];

  var clickCell;
  var firstCell = '';
  var secondRowHeaderCount = 0;

  // Added  '- 0' below to force 'i' to numeric, Bug12929
  for (var i=leftCellsToSkip-0; i<(ts_theadrow.cells.length - rightCellsToSkip); i++) {

    clickCell = ts_theadrow.cells[i];

    // Check for column headers split over 2 rows
    if ( (clickCell.colSpan > 1) && (thead.rows.length > parseInt(topCellsToSkip)+1) ) {

      if (firstCell == '') { firstCell = clickCell; }

      for (var j=0; j<clickCell.colSpan; j++) {
        var clickCell2 = thead.rows[parseInt(topCellsToSkip)+1].cells[j+secondRowHeaderCount];
        clickCell2.selectIndex = i + j + secondRowHeaderCount;
		clickCell2.addEventListener ("click", ts_sortForm, false);
        if (displayTooltips == "true") clickCell2.title = "Click to sort by "+clickCell2.innerText;
      }
      secondRowHeaderCount+=clickCell.colSpan-1;
      continue;
    }

    if (firstCell == '') { firstCell = clickCell; }

    clickCell.selectIndex = i + secondRowHeaderCount;
	if (navigator.appName != 'Netscape') {
		clickCell.attachEvent ("onclick", ts_sortForm);
	}
	else {
		clickCell.addEventListener ("click", ts_sortForm, false);
	}
    clickCell.style.cursor = "hand";
    clickCell.style.textDecoration = "underline";
    if (displayTooltips == "true") clickCell.title = "Click to sort by "+clickCell.innerText;
  }

  if (sortOnLoad == "true") {
    ts_sortForm (firstCell);
  }
}

function ts_addArrowToTD (theTD, theImagePath)
{
  if (ts_PathToUpImage == "") return;
  var l = document.createElement ("IMG");
  l.src = theImagePath;
  l.id = "ts_SortArrow";
  l.width = ts_WidthOfImage;
  l.height = ts_HeightOfImage;
  theTD.appendChild (l);
}

function ts_sortForm (e)
{
  var clickObject;
  if (e.type == 'click') {
    if (navigator.appName != 'Netscape') {
		clickObject = e.srcElement;
	}
	else {
		clickObject = e.target;
	}
  }
  else {
    clickObject = e;
  }

   while (clickObject.tagName != "TD") clickObject = clickObject.parentElement;

  // Allow selectIndex to be zero - so first table-column is sortable
  if (clickObject.selectIndex == null) return;

  // If images are being used, erase the arrow image from the previously sorted column
  if (ts_PathToUpImage != "") if (ts_lastClickedObject!=null) {
	  var img = ts_lastClickedObject.getElementsByTagName("IMG")[0];
      ts_lastClickedObject.removeChild(img);
  }

  if (ts_lastClickedObject == clickObject) {

    if(ts_reverse == false) {
      ts_addArrowToTD (clickObject, ts_PathToDownImage);
      ts_reverse = true;
    }
    else {
      ts_addArrowToTD (clickObject, ts_PathToUpImage);
      ts_reverse = false;
    }
  }
  else {
    ts_addArrowToTD(clickObject, ts_PathToUpImage);
    ts_reverse = false;
    ts_lastClickedObject = clickObject;
  }

   // to fetch the table (from the clickobject td) within which the sort to be performed
   var tempTableRow = clickObject.parentNode;
   if (!tempTableRow) return;
   var tempTableHead = tempTableRow.parentNode;
   if (!tempTableHead) return;
   var tempTable = tempTableHead.parentNode;
   if (!tempTable) return;
   var tempTableId = tempTable.getAttribute('id');
   if (!tempTableId) return;

   //fetching table body on which sort is to be performed
   var ts_tbody = document.getElementById(tempTableId).tBodies[0];
   if (!ts_tbody) return;

  ts_insertionSort (ts_tbody, ts_tbody.rows.length - bottomCellsToSkip, ts_reverse, clickObject.selectIndex);
}

function ts_insertionSort (theTBody, numRowsToDo, fReverse, iColumn)
{
  var textRowCurrent, textRowInsert;
  var regexp_ignoreChars = /[,%$]/g
  for (var iRowInsert = 0 ; iRowInsert < numRowsToDo ; iRowInsert++ ) {

    if (theTBody.rows[iRowInsert].cells[iColumn] != null)
      textRowInsert = ts_getTextFromTD (theTBody.rows[iRowInsert].cells[iColumn]);
    else
      textRowInsert = "";

    for (var iRowWalk = 0; iRowWalk < iRowInsert ; iRowWalk++ ) {

      if (theTBody.rows[iRowWalk].cells[iColumn] != null)
        textRowCurrent = ts_getTextFromTD (theTBody.rows[iRowWalk].cells[iColumn]);
      else
        textRowCurrent = "";

      textRowCurrent = textRowCurrent.toString().replace(regexp_ignoreChars,"");
      textRowInsert = textRowInsert.toString().replace(regexp_ignoreChars,"");

      if ( !isNaN (textRowCurrent) && !isNaN (textRowInsert) && !isNaN (parseInt (textRowCurrent)) && !isNaN (parseInt(textRowInsert)) ) {
        if (textRowCurrent != "") textRowCurrent = eval (textRowCurrent);
        if (textRowInsert != "") textRowInsert = eval (textRowInsert);
      }
      else {
        textRowCurrent = textRowCurrent.toString().toLowerCase();
        textRowInsert = textRowInsert.toString().toLowerCase();
      }

      if ( (!fReverse && textRowInsert < textRowCurrent) || (fReverse && textRowInsert > textRowCurrent) ) {
		var eRowInsert = theTBody.rows[iRowInsert];
        var eRowWalk = theTBody.rows[iRowWalk];
		//theTBody.insertBefore(eRowInsert, eRowWalk);
		eRowWalk.parentNode.insertBefore(eRowInsert, eRowWalk);
        iRowWalk = iRowInsert;
      }
    }
  }
}

function ts_getTextFromTD (theTD)
{
  if (navigator.appName != 'Netscape') {
	var theText = theTD.innerText;
  }
  else {
	var theText = theTD.textContent ;
  }
  if (theText == "") theText = theTD.innerHTML;
  return theText;
}