<!--

/**
*
* This script contains javascript-functions which enable sortin of html-tables. Html-table shown
* on some web-page can be sorted by calling the sortTable()-function with correct parameters.
*
* @author Juha V?in?l?
* @version Apr 29, 2008
*
*/


/**
* int. Used only within sortTable-function and sort-helper-functions.
*/
var SORT_COLUMN_INDEX;
/**
* Array(boolean). Contains current sort-direction for every column of the html-table. True means 
* descending order and false means ascending order.
*/
var DOWN=new Array();
/**
* String. Defines file-name for an arrow-image which is pointing down.
*/
var UPIMAGE="arrowup.jpg";
/**
* String. Defines file-name for an arrow-image which is pointing up.
*/
var DOWNIMAGE="arrowdown.jpg";


/**
* A sort-function which is used to sort a certain html-table according to numeric-values.
* @param a A table-row
* @param b Another table-row
*/
function mySortNumeric(a,b) { 
   
	var aa = parseFloat(getCellData(a.cells[SORT_COLUMN_INDEX]));
    if (isNaN(aa)) aa = 0;
    var bb = parseFloat(getCellData(b.cells[SORT_COLUMN_INDEX])); 
    if (isNaN(bb)) bb = 0;
    return aa-bb;
}

/**
* A sort-function which is used to sort a certain html-table according to date-values.
* @param a A table-row
* @param b Another table-row
*/
function mySortDate(a,b) {
    

    var aa = getCellData(a.cells[SORT_COLUMN_INDEX]);
    var bb = getCellData(b.cells[SORT_COLUMN_INDEX]);

    aa=aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
    bb=bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);    
    

    if(aa.length==0 && bb.length>0) return 1;
    if(bb.length==0 && aa.length>0) return -1;
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}


function mySortShortDate(a,b){
 

    aa = a.cells[SORT_COLUMN_INDEX].innerHTML;
    bb = b.cells[SORT_COLUMN_INDEX].innerHTML;

	pieces=aa.split(" ");

	datePieces=pieces[0].split(".");
	hour=pieces[2];
	
	aa=datePieces[1]+""+datePieces[0]+""+hour;
	
//	alert(aa);
	
	pieces=bb.split(" ");
	datePieces=pieces[0].split(".");
	hour=pieces[2];
	bb=datePieces[1]+""+datePieces[0]+""+hour;


    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;


}

/**
* A sort-function which is used to sort a certain html-table according to string-values.
* @param a A table-row
* @param b Another table-row
*/
function mySortString(a,b) {
    

    var aa = getCellData(a.cells[SORT_COLUMN_INDEX]);
    var bb = getCellData(b.cells[SORT_COLUMN_INDEX]);
    
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}

/**
* Function extracts data (which is used when sorting html-table) within table-cell's inner-html. 
* @param cell Cell whithin whose inner-html the data is extracted from.
* @return String Exctarcted data piece.
*/
function getCellData(cell){

	var value=cell;
	

	
	if(value.childNodes.length>0 && value.childNodes[0].tagName!=null){
		
		//Loop cell's child-nodes if cell contains input-elements...
		while(value.childNodes!=null){
	
			value=value.childNodes[0];	
			
			//Extract data from plain text-object or from input-object...
			if(value.value!=null)
				value=value.value;
			else if(value.text!=null)
				value=value.text;
			else
				value=value.toString();
		
		}	
	}else
		value=value.innerHTML;
		

	return value;
}

/**
* Sorts a html-table in ascending order according to column specified with index-parameter.
* @param id Id-attribute of the sorted table
* @param index Index-number of the column according to whose data the table is sorted.
* @param ascending True if html-table should be sorted in ascending order and false otherwise. null
* means that sorting order if determined by the previous sorting order of the same column (first sort is 
* made ascending and second descending etc...)
* @param beginRowIndex Defines index-number for a table-row from where on the rows are sorted.
*
* @return Array(HTMLTableRowElement) Sorted table-rows.
*/
function sortTable(id, index, ascending, beginRowIndex){
	
	var table=document.getElementById(id);

	if(beginRowIndex==null)
		beginRowIndex=1;	
		
	if(table.rows.length<=beginRowIndex)
		return null;
	
	if(ascending==null && DOWN[index]==null)
		ascending=true;
	else if(ascending==null)
		ascending=DOWN[index];
	
		

	DOWN[index]=!ascending;
	
	
	SORT_COLUMN_INDEX=index;
	
	//alert(table.rows[6].className);
	
	var newRows = new Array();
	for (j=beginRowIndex;j<table.rows.length;j++) { newRows[newRows.length] = table.rows[j];}	

	//Get cell-data according to which the table is sorted...
	var value="";
	for (var i=0;i<newRows.length;i++){
		value=getCellData(newRows[i].cells[index]);
		if(value.length>0)
			break;
	}
	
	//Find out the type of the column...
	if (value.match(/^\d\d[\/.]\d\d[\/.]\d\d\d\d$/))
		newRows.sort(mySortDate);
	else if(value.match(/^\d\d[\/.]\d\d /))
		newRows.sort(mySortShortDate);
	else if(!isNaN(parseFloat(value)))
		newRows.sort(mySortNumeric);
	else{
		newRows.sort(mySortString);
	}

	//Descending order?
	if(!ascending)
		newRows.reverse();

	//Re-construct the html-table...	
	for (i=0;i<newRows.length;i++) { table.tBodies[0].appendChild(newRows[i]);}	
	
	return newRows;

}

/**
* Sorts a html-table in ascending order according to column specified with index-parameter. Function 
* also maintains an arrow-image in the header-section of the table which shows the direction of the
* sort.
* @param id Id-attribute of the sorted table
* @param index Index-number of the column according to whose data the table is sorted.
* @param begin Defines index-number for a table-row from where on the rows are sorted.
* @param folder Defines a path to a directory where the arrow-images are located.
* @param forceUp If this parameter is set to true, then table is always sorted ascending.
*/
function sortTableWithArrows(id, index,begin,folder,forceUp){

	var table=document.getElementById(id);
	if(table==null)
		table=window.frames[0].document.getElementById(id);
	
	var down=DOWN[index]!=null?DOWN[index]:false;
	var image;

	if(forceUp!=null && forceUp.length>0)
		down=false;
	
	if(down){
		image=folder+DOWNIMAGE;
		DOWN[index]=false;
	}else{
		image=folder+UPIMAGE;
		DOWN[index]=true;
	}
	SORT_COLUMN_INDEX=index;
	
	
	//alert(table.images[0].src);
	//Update sorting image...
	document.getElementById('sort'+index).src=image;
		
		
	//alert(table.rows[6].className);
	
	var newRows = new Array();
	for (j=begin;j<table.rows.length;j++) { newRows[j-begin] = table.rows[j];}
	
	var value=newRows[0].cells[SORT_COLUMN_INDEX].innerHTML;

//	alert(getCellData(newRows[0].cells[SORT_COLUMN_INDEX]);

	
	//Find out the type of the column...
	if (value.match(/^\d\d[\/.]\d\d[\/.]\d\d\d\d.*$/)){
		
		newRows.sort(mySortDate);
	}else if(value.match(/^\d\d[\/.]\d\d /))
		newRows.sort(mySortShortDate);
	else if(!isNaN(parseFloat(value)))
		newRows.sort(mySortNumeric);
	else
		newRows.sort(mySortString);
	
	if(!down)
		newRows.reverse();
		
	var i=0;

	for (a=0;a<newRows.length;a++){
		
		if(i%2==0)
			newRows[a].className="";
		else
			newRows[a].className="harmaatausta";
			
		i++;
	}	
	
	for (i=0;i<newRows.length;i++) { table.tBodies[0].appendChild(newRows[i]);}	
	
	
}

-->