
<public:attach 	event=oncontentready onevent="init();"/>

<script>
//
// global variables
//
var tbody=null;					
var theadrow=null;
var colCount = null;

var reverse = false;
var lastclick = -1;					// stores the object of our last used object

var oTR = null;
var oStatus = null;
var none = 0;
var gridName = null;

function init() 
{
	// get TBODY - take the first TBODY for the table to sort			
	tbody = element.tBodies(0);
	gridName = element.id;	
	
	if (!tbody) return;

	//Get THEAD  
	//Commented By Vaibhav Poonekar
	//var thead = element.tHead;
	var thead = element.rows[0];	
	
	if (!thead)  return;	
	
	//Commented By Vaibhav Poonekar
	//theadrow = thead.children[0]; //Assume just one Head row
	theadrow = thead;
	
	if (theadrow.tagName != "TR") return;

	theadrow.runtimeStyle.cursor = "hand";

	colCount = theadrow.children.length;
  
	var l, clickCell;
	
	//for (var i=0; i<colCount; i++) 
	for (var i=0; i<1; i++) 
	{
		// Create our blank gif
		//Comented by AK
		//l=document.createElement("IMG");
		//l.src="blank.bmp";
		//l.id="srtImg";
		//l.width=25;
		//l.height=11;
		clickCell = theadrow.children[i];		
		
		clickCell.selectIndex = i;
		
		//clickCell.insertAdjacentElement("beforeEnd", l)		
		clickCell.attachEvent("onclick", doClick);
	}
}

//
// doClick handler
// 
//
function doClick(e) 
{
    //debugger;
	var clickObject = e.srcElement;			
	//alert(clickObject.parentElement.parentElement.parentElement.parentElement.parentElement.document);
	//var l_objDoc = clickObject.parentElement.parentElement.parentElement.parentElement.parentElement.document;
	
	var l_objDoc = clickObject.parentElement.parentElement.parentElement.parentElement.parentElement.document;
	
	var l_TableData = l_objDoc.getElementById(gridName);			
	
	tbody = l_TableData.tBodies(0);	
	
	while (clickObject.tagName != "TH") 
	{
		clickObject = clickObject.parentElement;
	}
	
	// clear the sort images in the head
	//var imgcol= theadrow.all('srtimg');

	//for(var x = 0; x < imgcol.length; x++) 
	//	imgcol[x].src = "blank.bmp";

	if(lastclick == clickObject.selectIndex)
	{
		if(reverse == false)
		{
		  //clickObject.children[0].src = "down.bmp";
		      reverse = true;
		}
		else 
		{
		  //clickObject.children[0].src = "up.bmp";
			reverse = false;
		}
	}
	else
	{
		reverse = false;		
		lastclick = clickObject.selectIndex;
		//clickObject.children[0].src = "up.bmp";
	}
	
	//alert(tbody.rows.length-1);
	
	insertionSort(tbody, tbody.rows.length-1,  reverse, clickObject.selectIndex);
	
    
}

function insertionSort(t, iRowEnd, fReverse, iColumn)
{
     var iRowInsertRow, iRowWalkRow, current, insert;
        
     var iEnd;  
         
     var l_bNumColumnType = false;
      
    
//     for (iEnd = 0+1 ; iEnd <= iRowEnd ; iEnd++ )
//     {   
//         if(null == t.children[iEnd].id  ||  t.children[iEnd].id == "")
//         {
//            break; 
//         } 
//     }    
    
    iEnd = iRowEnd-1; 
    
   // alert(iEnd);        
    
    //alert(iEnd);
   
    for ( iRowInsert = 0+1 ; iRowInsert <= iEnd ; iRowInsert++ )
    {         
        if (iColumn+1)
         {	
		    if( typeof(t.children[iRowInsert].children[iColumn]) != "undefined")
		    {
     		      textRowInsert = t.children[iRowInsert].children[iColumn].innerText;
     		}
		    else
		     {	    
			     textRowInsert = ""; 
		     } 
		  
		    if(null == textRowInsert  ||textRowInsert == ''|| textRowInsert == ' ') 
		     {			         
		         textRowInsert  = 0;
		         
	           // alert(textRowInsert + '     null outer for');		     
//		         
//		         eRowLast = t.children[iEnd];
//				 
//				 eRowInsertLast = t.children[iRowInsert];
//			       
//			     t.insertAfter[eRowLast, eRowInsertLast];
//			     
//			     t.deleteRow[iRowInsert];			      
//			   
//			     iRowInsert--;	
//			      
//			     iEnd--;
//			     
//			     continue;	  
		    }		   
        } 
        else 
        {
           //textRowInsert = t.children[iRowInsert].innerText;           
            continue;
        }      
	    
	    for (iRowWalk = 1; iRowWalk <= iRowInsert ; iRowWalk++ )
        {
            if (iColumn+1)
             {
			    if(typeof(t.children[iRowWalk].children[iColumn]) != "undefined")
			    {
				    textRowCurrent = t.children[iRowWalk].children[iColumn].innerText;
		        }
			    else
			    {				 
				  textRowCurrent = "";					  
			    }
			    
			    if(null == textRowCurrent  ||textRowCurrent == ''|| textRowCurrent == ' ') 
		     {			         
		         textRowCurrent  = 0;
		         
	            //alert(textRowCurrent + '     null  inner for');		 
	          }
            } 
            else 
            {
			    //textRowCurrent = t.children[iRowWalk].innerText;
			    continue;
            }
		//
		// We save our values so we can manipulate the numbers for
		// comparison
		//
		current = textRowCurrent;
		
		insert  = textRowInsert;		
		
		//  If the value is not a number, we sort normally, else we evaluate	
		//  the value to get a numeric representation
		//
		if ( !isNaN(current) &&  !isNaN(insert)) 
		{
			current= eval(current);
			
			insert= eval(insert);			
			
		}
		else if (isNaN(current) &&  !isNaN(insert)  )
		{
	       if( false == fReverse)  //Ascengding
	       {
	         insert = 'aaaaa'; 
	       }
	       else
	       {
	         insert = 'zzzzz'; 
	       }
	         current	= current.toLowerCase();			
		
		    insert	= insert.toLowerCase();		
		}
	    else if(( !isNaN(current) &&  isNaN(insert)) )
		{
	       if( false == fReverse)  //Ascengding
	       {
	         current = 'aaaaa'; 
	       }
	       else
	       {
	         current = 'zzzzz'; 
	       }		      
	         insert	= insert.toLowerCase();				
		      
		     current	= current.toLowerCase();
		}		
		else
		{
			current	= current.toLowerCase();			
			
			insert	= insert.toLowerCase();
		}
		
		//alert(current + ' ' + insert);
		
		if ( (   (!fReverse && insert < current)
				|| ( fReverse && insert > current) )
				&& (iRowInsert != iRowWalk) )
			   {
			    eRowInsert = t.children[iRowInsert];
				
				eRowWalk = t.children[iRowWalk];							
			    
			    t.insertBefore(eRowInsert, eRowWalk);								
				
				iRowWalk = iRowInsert; // done					
			}         
        }
     }
}



</script>
