/*****************************************************************************
TableSort.js
Warren Blatt, June 2006
******************************************************************************/
	
var tableWidget_okToSort = true;


function sortNumeric(ObjA, ObjB)
  {
    a = ObjA.getAttribute('rContent')
    b = ObjB.getAttribute('rContent')

    return ( a - b );
  }


function sortString(ObjA, ObjB) 
  {
    a = ObjA.getAttribute('rContent')
    b = ObjB.getAttribute('rContent')

    if ( a < b ) { return -1 };
    if ( a > b ) { return 1 };
    return 0;
  }


function stripHTML(a) 
  {
    return a.replace (/<.*?>/g, "");
  }


function getRawContent(Obj)
  {
    content = Obj.innerHTML + '';
    content = unaccent(content);
    content = stripHTML(content);
    content = content.toUpperCase();
    return (content);
  }


function getNumericContent(input_str)
  {
    content = input_str.replace (/\,/g, "");
    content = parseFloat(content);
    if ( isNaN(content) )
      { content = 0; }
    return (content);
  }


function getNewDirection(Obj)
  {
    var direction = Obj.getAttribute('direction');
    if ( direction )
      {
        if (direction == 'asc')
          { newdirection = 'desc'; }
        else
          { newdirection = 'asc'; }
      }
    else
      { newdirection = 'asc'; }
    return (newdirection);
  }
	  
	  
function sortTable(evt)
  {
	if ( ! tableWidget_okToSort ) return;
	tableWidget_okToSort = false;
    
 	// Determine the object that was clicked on
 	//   (should be a <TH> element, or one of its sub-elements):
 	evt = (evt) ? evt : ((window.event) ? event : null);
 	if ( ! evt ) 
 	  { tableWidget_okToSort = true;  return; }
 	var clicked_obj = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    
 	// If user clicked on a INPUT subelement contained within the <TH>, then ignore the click.
 	if ( (clicked_obj.tagName == 'INPUT') || 
 	     (clicked_obj.tagName == 'SELECT') || 
 	     (clicked_obj.tagName == 'OPTION') )
 	  { tableWidget_okToSort = true;  return; }
 	  
 	//if (clicked_obj.tagname != 'TH')
	//  { alert ("sortTable(): tagname is '" + clicked_obj.tagName + "'."); }

    
        // In case the <TH> contains sub-elements:
        while (clicked_obj.tagName != 'TH') 
             { clicked_obj = clicked_obj.parentNode; }
 
	/* Get index of current column, indexThis: */
	var th_obj = clicked_obj;  // <TH> element
	if (th_obj.tagName != 'TH')
	  { alert ("sortTable(): 'this' is a '" + th_obj.tagName + "' element."); }
	var indexThis = 0;
	while (clicked_obj.previousSibling)
	  {
	    clicked_obj = clicked_obj.previousSibling;
	    if (clicked_obj.tagName=='TH') 
	      indexThis++;		
	  }
	
	var direction = getNewDirection(th_obj);
        th_obj.setAttribute('direction', direction);
        
	
	var tableObj = th_obj.parentNode.parentNode.parentNode;
	if (tableObj.tagName != 'TABLE')
	  { alert ("sortTable(): 'tableObj' is a '" + tableObj.tagName + "' element."); }
	var tBody = tableObj.getElementsByTagName('TBODY')[0];

	ToolTipNewMsg("Sorting...");
	tableObj.style.cursor = 'wait';
	
	
	// Set this table's "current column" indicator.
	var activeColumn = tableObj.getAttribute('activeColumn');
	
	if (activeColumn && activeColumn != th_obj)
	    activeColumn.removeAttribute('direction');			

	tableObj.setAttribute('activeColumn', th_obj);
	
	
	// Get Sort method: 'N' = numeric, 'S' = String.
	var sortMethod = th_obj.getAttribute('SortMethod');


	// Create array of rows to be sorted.
	var cellObjArray = new Array();
	var cellObj, content;
	for (var no=0; no<tBody.rows.length; no++)
	  {
	    cellObj = tBody.rows[no].cells[indexThis];
	    content = getRawContent(cellObj);
            if (sortMethod == 'N')
              content = getNumericContent(content);
	    cellObj.setAttribute('rContent', content);
	    cellObjArray.push(cellObj);
	  }
	

	// Sort the array of rows.
	if (sortMethod == 'N')
	  { cellObjArray = cellObjArray.sort(sortNumeric); }
	else if (sortMethod == 'S')
	  { cellObjArray = cellObjArray.sort(sortString); }
	else
	  { alert ("Invalid SortMethod: " + sortMethod); }


	// Update the HTML table.
	if (direction=='desc')
	  {
	    for (var no=cellObjArray.length-1; no>=0; no--)
	      { 
	        cellObj = cellObjArray[no];
                tBody.appendChild(cellObj.parentNode);	
	      }
	  }
	else
	  {
	    for (var no=0; no<cellObjArray.length; no++)
	      { 
	        cellObj = cellObjArray[no];
                tBody.appendChild(cellObj.parentNode);	
	      }
	  }
	
	// All done.
	ToolTipNewMsg("Sorted");
	ToolTipOut();
	tableObj.style.cursor = 'auto';
	tableWidget_okToSort = true;
  }


function initSortTable (objId, sortArray)
  {
    var obj = document.getElementById(objId);
    if ( ! obj )
      { 
        // alert ("Null object: '" + objId + "'."); 
        return;
      }

    var tHead = obj.getElementsByTagName('THEAD')[0];
    var cells = tHead.getElementsByTagName('TH');
    for (var no=0; no<cells.length; no++)
      {
	if ( sortArray[no] )
	  {
	    cells[no].style.cursor = 'pointer';
	    cells[no].onclick = sortTable;
	    cells[no].onmouseover = ToolTipUp;
	    cells[no].onmouseout  = ToolTipOut;
	    cells[no].setAttribute('SortMethod', sortArray[no]);
	  }
      }
  }
  

/*****************************************************************************
ToolTip code:
******************************************************************************/
  
// Initialize ToolTip element:
var Xoffset = 0;
var Yoffset = -25;
  
var yyy = -1000;
var dek, skn;

var ns6 = document.getElementById&&!document.all;

var ToolTipInited = false;

function ToolTipInit(Obj)
  {
    dek = document.createElement("div");
    document.body.appendChild(dek);
  
    skn = dek.style;
    skn.visibility="visible";
    skn.position="absolute";
    skn.display="none";
    skn.zindex="200";

    var tHeadObj = Obj.parentNode.parentNode;
    tHeadObj.onmousemove = get_mouse;

    ToolTipInited = true;
  }

  
function get_mouse(e)
  {
    var x = ns6 ? e.pageX : event.x + document.body.scrollLeft;
    skn.left = x + Xoffset;
  
    var y = ns6 ? e.pageY : event.y + document.body.scrollTop;
    skn.top = y + yyy;
  }
  
  
function ToolTipUp(evt)
  {
    // Determine the object that was clicked on
    //   (should be a <TH> element):
    evt = (evt) ? evt : ((window.event) ? event : null);
    if ( ! evt ) return;
    var obj = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    
    // In case the <TH> contains sub-elements:
    while (obj.tagName != 'TH') { obj = obj.parentNode; }
    
    if ( ! ToolTipInited ) 
      { ToolTipInit(obj); }


    // Create message text:
    var direction = getNewDirection(obj);
    var sortMethod = obj.getAttribute('SortMethod');
    
    if      ((direction == 'asc') &&  (sortMethod == 'S')) {label = "A-Z"; }
    else if ((direction == 'desc') && (sortMethod == 'S')) {label = "Z-A"; }
    else if ((direction == 'asc') &&  (sortMethod == 'N')) {label = "0-9"; }
    else if ((direction == 'desc') && (sortMethod == 'N')) {label = "9-0"; }
    else { label = "direction: '" + direction + "', sortMethod: '" + sortMethod + "'."; }
  
    var msg = "Sort " + label;
    
    // Display ToolTip:
    var content = "<TABLE BORDER=1 BORDERCOLOR=black CELLPADDING=2 CELLSPACING=0 "+
    "BGCOLOR='#ece1fb'><TD ALIGN=center><FONT COLOR=black SIZE=2>"+msg+"</FONT></TD></TABLE>";
    dek.innerHTML = content;

    yyy = Yoffset;
    skn.display = '';
  }

function ToolTipNewMsg(msg)
  {
    // Display ToolTip:
    dek.innerHTML = "<TABLE BORDER=1 BORDERCOLOR=black CELLPADDING=2 CELLSPACING=0 "+
    "BGCOLOR='#ece1fb'><TD ALIGN=center><FONT COLOR=black SIZE=2>"+msg+"</FONT></TD></TABLE>";
  }  
  
function ToolTipOut()
  {
    yyy = -1000;
    skn.display = "none";
  }
  
/****************************************************************************/
