﻿function get_element(theirArray, theirElement,theirColumn)
{
	var return_this;
	
	if(theirArray[theirElement].childNodes[theirColumn].getAttribute("Sort") != null)
	return_this=theirArray[theirElement].childNodes[theirColumn].getAttribute("Sort");
	else
	return_this = theirArray[theirElement].childNodes[theirColumn].innerText;
	return return_this.toUpperCase();
}

function Quicksort(vec, loBound, hiBound, theirColumn)
{
	
	var pivot, pivot_value, loSwap, hiSwap, temp;
	
	// Two items to sort
	if (hiBound - loBound == 1)
	{
		//if (vec[loBound] > vec[hiBound])
		if (get_element(vec,loBound,theirColumn) > get_element(vec,hiBound,theirColumn))
		{
			temp = vec[loBound];
			vec[loBound] = vec[hiBound];
			vec[hiBound] = temp;
		}
		return;
	}
	
	// Three or more items to sort
	pivot = vec[parseInt((loBound + hiBound) / 2)];
	pivot_value = get_element(vec,parseInt((loBound + hiBound) / 2),theirColumn);
	
	vec[parseInt((loBound + hiBound) / 2)] = vec[loBound];
	vec[loBound] = pivot;
	loSwap = loBound + 1;
	hiSwap = hiBound;
	
	do {
		// Find the right loSwap
		//while (loSwap <= hiSwap && vec[loSwap] <= pivot)
		while (loSwap <= hiSwap && get_element(vec,loSwap,theirColumn) <= pivot_value)
		loSwap++;
		
		// Find the right hiSwap
		while (get_element(vec,hiSwap,theirColumn) > pivot_value)
		hiSwap--;
		
		// Swap values if loSwap is less than hiSwap
		if (loSwap < hiSwap)
		{
			temp = vec[loSwap];
			vec[loSwap] = vec[hiSwap];
			vec[hiSwap] = temp;
		}
	} while (loSwap < hiSwap);
	
	vec[loBound] = vec[hiSwap];
	vec[hiSwap] = pivot;
	
	// Recursively call function...  the beauty of quicksort
	
	// 2 or more items in first section
	if (loBound < hiSwap - 1)
	Quicksort(vec, loBound, hiSwap - 1,theirColumn);
	
	// 2 or more items in second section
	if (hiSwap + 1 < hiBound)
	Quicksort(vec, hiSwap + 1, hiBound,theirColumn);
}

function sortTable(Prefix, TableId, Column)
{
    var sval= Prefix + 'sortImg' + Column;
    var bDescending=false;
    
    if (document.getElementById(sval) == null)
        sval='sortImg' + Column;
        
    sval=document.getElementById(sval).src.toUpperCase();
    
    if(sval.match("SMALLUP.GIF")!= null){                                       // DETERMINE ORDER
        sval="../master/images/smalldown.gif";
    }
    else {
        bDescending=true;
        sval="../master/images/smallup.gif";
    }
    
    var oTable=document.getElementById(Prefix+TableId);
    var aList=new Array();
    var iLp=0;
    
    for(var i=0;i < oTable.firstChild.childNodes[0].childNodes.length;i++)      // SET COLUMN IMAGE (if exist)
    {
        var simage= Prefix + 'sortImg' + i;
        
        if(document.getElementById(simage) == null)
            simage='sortImg' + i;
        
        if(document.getElementById(simage) != null)
        {
            if(i==Column) {
                document.getElementById(simage).src=sval;
            }
            else {
                document.getElementById(simage).src="../master/images/blank.gif";
            }
        }
    }

    for(var i=oTable.firstChild.childNodes.length-1;i > 0;i--)		            // REMOVE ITEMS
    {
        var oNode=oTable.firstChild.childNodes[i];
        oTable.firstChild.removeChild(oNode);
        aList[iLp]=oNode;
        iLp++;
    }
    
    //Fastest sorting algorithm known to mankind
    //*******************************************
    document.body.style.cursor = "wait";
    window.status = "Sorting ...";
    
    Quicksort(aList,0, aList.length-1, Column);
    
    document.body.style.cursor = "default";
    window.status = " ";
    
    if(bDescending)							                                    // READD ITEMS
    {
        for(var i=0;i < aList.length;i++)
        {
            oTable.firstChild.appendChild(aList[i]);
        }
    }
    else
    {
        for(var i=aList.length;i > 0;i--)
        {
            oTable.firstChild.appendChild(aList[i-1]);
        }
    }
}