
var SortAfterRequest = {

	aSortRules : {},

 	sortTables : function(){
		for(var sTableId in this.aSortRules){
			var oLink = document.getElementById(this.aSortRules[sTableId]['sTdId']).getElementsByTagName('a')[0];
			ts_resortTableMts(oLink, this.aSortRules[sTableId]['iColumn'], true, this.aSortRules[sTableId].sSortDirection, this.aSortRules[sTableId].sSortType);			
		}
	},	
	addSortRule : function(sTableId, oLink, iColumn, sSortDirection, sSortType){
	
		this.aSortRules[sTableId] = {'sTdId' : oLink.parentNode.id, 'iColumn' : iColumn, 'sSortDirection' : sSortDirection, 'sSortType' : sSortType };
		
	}

};
//SortAfterRequest.addSortRule(id_table, lnk, clid, sSortDirection);


var JSlEvent = {

       addEvent : function(oTargetElement, sEventType, fnHandler)
       {
               if(oTargetElement.addEventListener)
               { oTargetElement.addEventListener(sEventType, fnHandler, false); }
               else if(oTargetElement.attachEvent)
               { oTargetElement.attachEvent('on' + sEventType, fnHandler); }
               else
               { oTargetElement['on' + sEventType] = fnHandler; }
       },

       removeEvent : function(oTargetElement, sEventType, fnHandler)
       {
               if(oTargetElement.removeEventListener)
               { oTargetElement.removeEventListener(sEventType, fnHandler, false); }
               else if(oTargetElement.detachEvent)
               { oTargetElement.detachEvent('on' + sEventType, fnHandler); }
               else
               { oTargetElement['on' + sEventType] = null; }
       }
};

JSlEvent.addEvent(window, "load", sortables_init);

//addEvent(window, "load", sortables_init);
var SORT_COLUMN_INDEX;
function sortables_init() {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
            //initTable(thisTbl.id);
            ts_makeSortable(thisTbl);
        }
    }
}

function sortables_init2(html) {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = html.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
            //initTable(thisTbl.id);
            ts_makeSortable(thisTbl);
        }
    }
}

function ts_makeSortable(table) {
    if (table.rows && table.rows.length > 0) {
        var firstRow = table.rows[0];
    }
    if (!firstRow) return;
    
    // We have a first row: assume it's the header, and make its contents clickable links
    for (var i=0;i<firstRow.cells.length;i++) {
        	var cell = firstRow.cells[i];
		/*
    	if (((' '+cell.className+' ').indexOf("sortable") != -1)){
        	var txt = ts_getInnerText(cell);
        	cell.innerHTML = '<a href="#" class="sortheader" '+ 
        	'onclick="ts_resortTable(this, '+i+');return false;">' + 
        	txt+'<span class="sortarrow"></span></a>';
        }
	*/
    }
}


function ts_resortTableMts(lnk, clid, request, dir, type) {

    // get the span
    var span;
    for (var ci=0;ci<lnk.childNodes.length;ci++) {
        if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
    }
    var spantext = ts_getInnerText(span);
    var td = lnk.parentNode;
    var column = clid || td.cellIndex;
    var table = getParent(td,'TABLE');
    var id_table = table.id;
    // Work out a type for the column
    if (table.rows.length <= 1) return;
    var itm = ts_getInnerText(table.rows[1].cells[column]);
	if (itm.match(/^[1-9]/)) {
		itm = itm.replace(/[^0-9]+/g,"");
	}
    itm = itm.replace(/^[\s\n]+/g,'');
    itm = itm.replace(/[\s\n]+$/g,'');
    
    sortfn = ts_sort_caseinsensitive;	
	if (type=="int") sortfn = ts_sort_numeric;
	else if (type=="date") sortfn = ts_sort_date;
	else if (type=="currency") sortfn = ts_sort_currency;
    
    SORT_COLUMN_INDEX = column;
    var firstRow = new Array();
    var newRows = new Array();
    for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
	var tableEnd = table.rows.length;
	while (table.rows[tableEnd-1].parentNode.tagName == 'TFOOT') {
		tableEnd--;
	}
	var tableBegin = 0;
	while (table.rows[tableBegin].parentNode.tagName == 'THEAD') {
		tableBegin++;
	}
    
	for (j=tableBegin;j<tableEnd;j++) { newRows[j-1] = table.rows[j]; }
    newRows.sort(sortfn);
	
for(var i=0;i<td.parentNode.getElementsByTagName('td').length;i++)
	td.parentNode.getElementsByTagName('td')[i].style.background = '';
  	var tableHeader = table.getElementsByTagName('THEAD')[0];
	var tableTR = tableHeader.getElementsByTagName('TR')[0];
	var tableTD = tableTR.getElementsByTagName('TD');
  	 if (dir == 'down') {
		td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();
		
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}	
		
    } else{
		td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
		var sSortDirection = 'down';
				
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}			
    }
  	

	span.setAttribute('title', sSortDirection);
	if (request != true)
		SortAfterRequest.addSortRule(id_table, lnk, clid, sSortDirection);

    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows
    for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
    // do sortbottom rows only
    for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
    
    // Delete any other arrows there may be showing
    var allspans = document.getElementsByTagName("span");
    for (var ci=0;ci<allspans.length;ci++) {
        if (allspans[ci].className == 'sortarrow') {
            if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
                allspans[ci].innerHTML = '';
            }
        }
    }
        
	for (j=tableBegin;j<tableEnd;j++) { table.rows[j].className = (j%2)? 'od':'ev'; }

    span.innerHTML = '&nbsp;';
}













function ts_getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ' '
				str += ts_getInnerText(cs[i]);
				str;
				break;
			case 3:	//TEXT_NODE
				str += ' '
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}
function ts_resortTable(lnk,clid,request) {
    // get the span
    var span;
    for (var ci=0;ci<lnk.childNodes.length;ci++) {
        if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
    }
    var spantext = ts_getInnerText(span);
    var td = lnk.parentNode;
    var column = clid || td.cellIndex;
    var table = getParent(td,'TABLE');
    var id_table = table.id;
    // Work out a type for the column
    if (table.rows.length <= 1) return;
    var itm = ts_getInnerText(table.rows[1].cells[column]);
	if (itm.match(/^[1-9]/)) {
		itm = itm.replace(/[^0-9]+/g,"");
	}
    itm = itm.replace(/^[\s\n]+/g,'');
    itm = itm.replace(/[\s\n]+$/g,'');
   	sortfn = ts_sort_caseinsensitive;
   
    if (itm.match(/^([+-]?\d+([,.\s]?\d+)?)\s*(pkt|[$%]?)/)) sortfn = ts_sort_numeric;
    if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
    if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
    if (itm.match(/^[�?$]/)) sortfn = ts_sort_currency;	
    SORT_COLUMN_INDEX = column;
    var firstRow = new Array();
    var newRows = new Array();
    for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
	var tableEnd = table.rows.length;
	while (table.rows[tableEnd-1].parentNode.tagName == 'TFOOT') {
		tableEnd--;
	}
	var tableBegin = 0;
	while (table.rows[tableBegin].parentNode.tagName == 'THEAD') {
		tableBegin++;
	}
    
	for (j=tableBegin;j<tableEnd;j++) { newRows[j-1] = table.rows[j]; }
    newRows.sort(sortfn);
	
for(var i=0;i<td.parentNode.getElementsByTagName('td').length;i++)
	td.parentNode.getElementsByTagName('td')[i].style.background = '';
  	var tableHeader = table.getElementsByTagName('THEAD')[0];
	var tableTR = tableHeader.getElementsByTagName('TR')[0];
	var tableTD = tableTR.getElementsByTagName('TD');
  	 if (span.getAttribute("title") == 'down') {
		td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();
		
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}	
		
    } else{
		td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
		var sSortDirection = 'down';
				
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}			
    }
  	

	span.setAttribute('title', sSortDirection);
	if (request != true)
		SortAfterRequest.addSortRule(id_table, lnk, clid, sSortDirection);

    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows
    for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
    // do sortbottom rows only
    for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
    
    // Delete any other arrows there may be showing
    var allspans = document.getElementsByTagName("span");
    for (var ci=0;ci<allspans.length;ci++) {
        if (allspans[ci].className == 'sortarrow') {
            if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
                allspans[ci].innerHTML = '';
            }
        }
    }
        
	for (j=tableBegin;j<tableEnd;j++) { table.rows[j].className = (j%2)? 'od':'ev'; }

    span.innerHTML = '&nbsp;';
}

var as = new Array();

function ts_resortTableSort(lnk,clid,request,type) {
// get the span
    var span;
    
    for (var ci=0;ci<lnk.childNodes.length;ci++) {
        if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
    }
    var spantext = ts_getInnerText(span);
    var td = lnk.parentNode;
    var column = clid || td.cellIndex;
    var table = getParent(td,'TABLE');
    var id_table = table.id;
    // Work out a type for the column
    var xspans1 = table.getElementsByTagName('thead')
        var xspans = xspans1[0].getElementsByTagName('span');
    var dir = span.getAttribute('title');
    if(dir == ''){
    	for(var q=0;q<xspans.length;q++){
    		xspans[q].setAttribute("title", '');
    	}
    }
    var xsp = getParent(span, 'TD');
 
    if (table.rows.length <= 1) return;

    


	var de = new Array();
	var sprawdztab = 'false';
de.push(lnk);
de.push(clid);
de.push(request);
de.push(type);
de.push(td.id);
de.push(dir);
for (i=0;i<as.length;i++){
	if(as[i][4].split('_')[0] == de[4].split('_')[0]){
		sprawdztab = 'true';
		as[i] = de;
 	}
}
if(sprawdztab == 'false'){
as.push(de);}
	
	var tableRow = table.rows[1].cells[column];
	if(table.rows[1].parentNode.tagName == "TFOOT"){		
		tableRow = table.rows[2].cells[column];
	}
	var itm = ts_getInnerText(tableRow);

	if (itm && itm.match(/^[1-9]/)) {
			itm = itm.replace(/[^0-9]+/g,"");
		}
	sortfn = ts_sort_caseinsensitive;	
	if (type=="int") sortfn = ts_sort_numeric;
	else if (type=="date") sortfn = ts_sort_caseinsensitive3;
	else if (type=="currency") sortfn = ts_sort_currency;
	else if (type=="float") sortfn = ts_sort_numericFloat
	else if (new String(lnk.getAttribute("href").indexOf('nazwa')) > -1) sortfn = ts_sort_caseinsensitive2;	
	else if (new String(lnk.getAttribute("href").indexOf('seria')) > -1) sortfn = ts_sort_caseinsensitive2;	
    SORT_COLUMN_INDEX = column;
	
	var firstRow = new Array();
    var newRows = new Array();
    for (i=0;i<table.rows[0].length;i++){
		firstRow[i] = table.rows[0][i];
	}	
	var tableEnd = table.rows.length;

	while (table.rows[tableEnd-1].parentNode.tagName == 'TFOOT') {
		tableEnd--;
	}
	var tableBegin = 0;
	while (table.rows[tableBegin].parentNode.tagName == 'THEAD' || table.rows[tableBegin].parentNode.tagName == 'TFOOT') {
		tableBegin++;
	}

	//alert(tableBegin);
	//alert(tableEnd);


	for (j=tableBegin;j<tableEnd;j++){
		newRows[j-1] = table.rows[j];
	}
	
	newRows.sort(sortfn);

	
	for(var i=0;i<td.parentNode.getElementsByTagName('td').length;i++)
	td.parentNode.getElementsByTagName('td')[i].style.background = '';
	/*	
    if((!span.getAttribute("title") && SORT_COLUMN_INDEX == 1) || (!span.getAttribute("title") && SORT_COLUMN_INDEX == 0)){
    	span.setAttribute("title","down");
    } 
    */   
    var tableHeader = table.getElementsByTagName('THEAD')[0];
	var tableTR = tableHeader.getElementsByTagName('TR')[0];
	var tableTD = tableTR.getElementsByTagName('TD');
    if (dir == 'down') {
    	if (type == ''){
		td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();    	
    	}
    	else {
		td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();
		}
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}	
		
    } else { if (dir == 'up'){
    	if (type == ''){
    	td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
		var sSortDirection = 'down';
    	}
    	else{
		td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
		var sSortDirection = 'down';
		}		
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}			
    } else {
    if (type == ''){
    	td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();
    }
    else{
		td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();
		}
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}	
		
     }
	}
	span.setAttribute('title', sSortDirection);

	if (request && request !== true)
		SortAfterRequest.addSortRule(id_table, lnk, clid, dir, type);

    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows	

 	for (i=0;i<newRows.length;i++){				
		if (newRows[i] && (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))))
		table.tBodies[0].appendChild(newRows[i]);
	}
    // do sortbottom rows only
    for (i=0;i<newRows.length;i++){			
		if(newRows[i] && (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)))
			table.tBodies[0].appendChild(newRows[i]);
	}
	// Delete any other arrows there may be showing
	var allspans = document.getElementsByTagName("span");
    
	for (var ci=0;ci<allspans.length;ci++) {
        if (allspans[ci].className == 'sortarrow') {
            if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
                allspans[ci].innerHTML = '';
            }
        }
    }
        
	for (j=tableBegin;j<tableEnd;j++) { table.rows[j].className = (j%2)? 'od':'ev'; }
    span.innerHTML = '&nbsp;';
 
 
}


function ts_resortTableSort2(zak) {


for (var li=0;li<as.length;li++){
	var lnk = "";
	var clid = "";
	var request = "";
	var type = "";
	var rodzicTD = "";
	var children = "";
	var parg = "";
	var p44 = "";
	var p4 = "";
	var p5 = "";
	var p4Td = "";
	p44 = as[li][4];
	p4 = p44.split("_");
	p5 = p4[0];
	if(zak == 'customerRating'){
		p5 = p44;
	}

if(document.getElementById(p5)){

	ciastka = as[li];
	lnk = ciastka[0];
	clid = ciastka[1];
	request = ciastka[2];
	type = ciastka[3];
	rodzicTD = ciastka[4];
	parg = document.getElementById(rodzicTD);
	if (parg.hasChildNodes())
	// sprawdzamy czy obiekt nie jest pusty - czy ma dzieci
 	{ 
   	children = parg.childNodes;
   	for (var x = 0; x < children.length; x++) 
   		{
   			if(children[x] != null && children[x].className == 'sortheader'){
   			lnk = children[x];;}
   		};
 	};
// get the span
    var span;
    for (var ci=0;ci<lnk.childNodes.length;ci++) {
        if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
    }
    var spantext = ts_getInnerText(span);
    var td = lnk.parentNode;
    var column = clid || td.cellIndex;
    var table = getParent(td,'TABLE');
    var id_table = table.id;
    // Work out a type for the column
   var xspans1 = table.getElementsByTagName('thead')
        var xspans = xspans1[0].getElementsByTagName('span');
    span.setAttribute('title',ciastka[5]);
    var dir = span.getAttribute('title');
        if(dir == ''){
    	for(var q=0;q<xspans.length;q++){
    		xspans[q].setAttribute("title", '');
    	}
    }
    var xsp = getParent(span, 'TD');
    if(!(new String(xsp.style.background).match(/up\.gif|down\.gif/g) || new String(xsp.className).indexOf('sortdown') > -1 || ciastka[5])){
        var xspans = table.getElementsByTagName('span');
        for(var q=0;q<xspans.length;q++){
            var scn = new String(xspans[q].className);
            if(scn.indexOf('sortarrow') > -1){
                var xtd = getParent(xspans[q], 'TD');
                if(new String(xtd.style.background).match(/up\.gif|down\.gif/g) || new String(xtd.className).indexOf('sortdown') > -1)
                    dir = (xspans[q].getAttribute('title') == 'down' ? 'up' : 'down')
            } 
        }
    }

    if (table.rows.length <= 1) return;

	var tableRow = table.rows[1].cells[column];
	if(table.rows[1].parentNode.tagName == "TFOOT"){		
		tableRow = table.rows[2].cells[column];
	}
	var itm = ts_getInnerText(tableRow);

	if (itm && itm.match(/^[1-9]/)) {
			itm = itm.replace(/[^0-9]+/g,"");
		}
	sortfn = ts_sort_caseinsensitive;	
	if (type=="int") sortfn = ts_sort_numeric;
	else if (type=="date") sortfn = ts_sort_caseinsensitive3;
	else if (type=="currency") sortfn = ts_sort_currency;
	else if (new String(lnk.getAttribute("href").indexOf('nazwa')) > -1) sortfn = ts_sort_caseinsensitive2;	
    SORT_COLUMN_INDEX = column;
	
	var firstRow = new Array();
    var newRows = new Array();
    for (i=0;i<table.rows[0].length;i++){
		firstRow[i] = table.rows[0][i];
	}	
	var tableEnd = table.rows.length;

	while (table.rows[tableEnd-1].parentNode.tagName == 'TFOOT') {
		tableEnd--;
	}
	var tableBegin = 0;
	while (table.rows[tableBegin].parentNode.tagName == 'THEAD' || table.rows[tableBegin].parentNode.tagName == 'TFOOT') {
		tableBegin++;
	}

	//alert(tableBegin);
	//alert(tableEnd);


	for (j=tableBegin;j<tableEnd;j++){
		newRows[j-1] = table.rows[j];
	}
	
	newRows.sort(sortfn);

	
	for(var i=0;i<td.parentNode.getElementsByTagName('td').length;i++)
	td.parentNode.getElementsByTagName('td')[i].style.background = '';
	/*	
    if((!span.getAttribute("title") && SORT_COLUMN_INDEX == 1) || (!span.getAttribute("title") && SORT_COLUMN_INDEX == 0)){
    	span.setAttribute("title","down");
    } 
    */   
    var tableHeader = table.getElementsByTagName('THEAD')[0];
	var tableTR = tableHeader.getElementsByTagName('TR')[0];
	var tableTD = tableTR.getElementsByTagName('TD');
     if (dir == 'down') {
     	if (type == ''){
		td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();     	
     	}
     	else{
		td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();
		}
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}	
		
    } else { if (dir == 'up'){
    	if (type == ''){
		td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
		var sSortDirection = 'down';    	
    	}
    	else{
		td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
		var sSortDirection = 'down';
		}		
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}			
    } else {
    	if (type == ''){
		td.style.background = 'transparent url(/_iMg/site/down.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();    	
    	}
    	else{
		td.style.background = 'transparent url(/_iMg/site/up.gif) no-repeat 95% 95%';
       	var sSortDirection = 'up';
		newRows.reverse();
		}
		for(var i=0; i<tableTD.length; i++){
			if( tableTD[i].getAttribute("class") && (tableTD[i].getAttribute("class").indexOf("sortup") != -1 || tableTD[i].getAttribute("class").indexOf("sortdown") != -1 ) ){
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortup/,"") );
				tableTD[i].setAttribute("class", tableTD[i].getAttribute("class").replace(/sortdown/,"") );
			}			
		}	
		
     }
	}
 
	span.setAttribute('title', sSortDirection);

	if (request && request !== true)
		SortAfterRequest.addSortRule(id_table, lnk, clid, dir, type);

    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows	

 	for (i=0;i<newRows.length;i++){				
		if (newRows[i] && (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))))
		table.tBodies[0].appendChild(newRows[i]);
	}
    // do sortbottom rows only
    for (i=0;i<newRows.length;i++){			
		if(newRows[i] && (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)))
			table.tBodies[0].appendChild(newRows[i]);
	}
	// Delete any other arrows there may be showing
	var allspans = document.getElementsByTagName("span");
    
	for (var ci=0;ci<allspans.length;ci++) {
        if (allspans[ci].className == 'sortarrow') {
            if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
                allspans[ci].innerHTML = '';
            }
        }
    }
        
	for (j=tableBegin;j<tableEnd;j++) { table.rows[j].className = (j%2)? 'od':'ev'; }
    span.innerHTML = '&nbsp;';
}}}
	




function getParent(el, pTagName) {
if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()){	// Gecko bug, supposed to be uppercase
return el;

}	else{
		return getParent(el.parentNode, pTagName);
}
}
function ts_sort_date(a,b) {
    // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
    if (aa.length == 10) {
        dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
    } else {
        yr = aa.substr(6,2);
        if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
        dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
    }
    if (bb.length == 10) {
        dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
    } else {
        yr = bb.substr(6,2);
        if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
        dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
    }
    if (dt1==dt2) return 0;
    if (dt1<dt2) return -1;
    return 1;
}
function ts_sort_currency(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
    return parseFloat(aa) - parseFloat(bb);
}
function ts_sort_numeric(a,b) { 
	aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
      
    if(aa=="-0,0" || aa=="-0,00") aa="-0.001";
    if(aa=="+0,0" || aa=="+0,00") aa="0.001";

    aa = aa.replace(",","");
    aa = aa.replace(":","");
    aa = aa.replace(/\s*/g,"");
    aa = aa.replace("+","");
        aa = parseFloat(aa);

    if (isNaN(aa)) {
        aa = -9999999999999999999;
    }
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
    if(bb=="-0,00" || bb=="-0,0") bb="-0.001";
    if(bb=="+0,00" || bb=="+0,0") bb="0.001";
    
    bb = bb.replace("+","");
    bb = bb.replace(",","");
    bb = bb.replace(":","");
    bb = bb.replace(/\s*/g,"");
    
  	bb = parseFloat(bb);
    if (isNaN(bb)) bb = -9999999999999999999;
    return aa-bb;
}
function ts_sort_numericFloat(a,b) { 

	aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
      
    if(aa=="-0,0" || aa=="-0,00") aa="-0.001";
    if(aa=="+0,0" || aa=="+0,00") aa="0.001";

    aa = aa.replace(",",".");
    aa = aa.replace(":","");
    aa = aa.replace(/\s*/g,"");
    aa = aa.replace("+","");
        aa = parseFloat(aa);
    if (isNaN(aa)) {
        aa = -9999999999999999999;
    }
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
    if(bb=="-0,00" || bb=="-0,0") bb="-0.001";
    if(bb=="+0,00" || bb=="+0,0") bb="0.001";
    
    bb = bb.replace("+","");
    bb = bb.replace(",",".");
    bb = bb.replace(":","");
    bb = bb.replace(/\s*/g,"");
    
  	bb = parseFloat(bb);
    if (isNaN(bb)) bb = -9999999999999999999;
        
    return aa-bb;
}
function ts_sort_caseinsensitive(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
	aa = revertToPlain(aa);
	bb = revertToPlain(bb);
	
	
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}
function ts_sort_caseinsensitive2(a,b) {
    aa = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
    bb = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
	aa = revertToPlain(aa);
	bb = revertToPlain(bb);
	
	
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}
function ts_sort_caseinsensitive3(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
	if(aa.length == 4 || aa.length == 3 || aa.length == 2)aa = '  00:00 ';
	if(bb.length == 4 || bb.length == 3 || bb.length == 2)bb = '  00:00 ';
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}
function revertToPlain(value) {
	value = value.replace(/[\n\t\r]+/g, " ");
	value = value.replace(/^[ ]+/g, "");
	value = value.replace(/[ ]+$/g, "");
    
	value = value.replace(/ę/g, "ezz");
	value = value.replace(/ó/g, "ozz");
	value = value.replace(/ą/g, "azz");
	value = value.replace(/ś/g, "szz");
	value = value.replace(/ł/g, "lzz");
	value = value.replace(/ż/g, "zzz");
	value = value.replace(/ź/g, "xzz");
	value = value.replace(/ć/g, "czz");
	value = value.replace(/ń/g, "nzz");
	value = value.replace(/Ę?/g, "EZZ");
	value = value.replace(/Ó/g, "OZZ");
	value = value.replace(/Ą/g, "AZZ");
	value = value.replace(/Ś/g, "SZZ");
	value = value.replace(/Ł?/g, "LZZ");
	value = value.replace(/Ż/g, "ZZZ");
	value = value.replace(/Ź/g, "XZZ");
	value = value.replace(/Ć/g, "CZZ");
	value = value.replace(/Ń?/g, "NZZ");
	if (value.match(/^okre.* [0-9]$/)) {
		value = value.replace(/ /, ' 0');
	}
	
    if (value.match(/^[0-9\,]+\%/)) {
        value = value.replace(/[\r\n\t ]*/g, "");
        value = value.replace(/%.*$/g, "");
        value = value.replace(/,/g, ".");
    }
	return value;
}
function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 

