function htmlToText(htmlContent)
{
	while(htmlContent.indexOf("<") > -1 && htmlContent.indexOf(">") > -1) {
		s = htmlContent.indexOf("<");
		e = htmlContent.indexOf(">", s);
		htmlContent = htmlContent.substring(0, s) + htmlContent.substring(e+1);
	}
	
	return htmlContent;
}


function getNumericValue(v)
{
	var vTmp = '';
	while (v.length > 0)
	{
		switch (v[0])
		{
			case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': case '.':
				vTmp += v[0];
				break;
			case ',':
				vTmp += '.';
				break;
		}
		v = v.substring(1);
	}
	
	return parseFloat(vTmp);
}


function valuesSorted(vNew, vPast, asc, columnType)
{
	if (columnType != null)
		switch (columnType.toLowerCase())
		{
			case "numeric":
				vNew = getNumericValue(vNew);
				vPast = getNumericValue(vPast);
				break;
		}
	
	if (asc && (vNew < vPast))
		return false;
	
	if (!asc && (vNew > vPast))
		return false;
	
	return true;
}


function tableSort(tableId, col, asc, columnType)
{
	var tbl = document.getElementById(tableId);

	if (tbl.rows.length < 2)
		return;

	var oldstyle = tbl.style.display;
	tbl.style.display = "none";
	
	var tmpRows = new Array();
	var tmpSort = new Array();
	
	var i = 0, s, tmp, prev;

	while (tbl.rows.length > 0)
	{
		tmpRows[i] = tbl.rows[0].cloneNode(true);
		tbl.removeChild(tbl.rows[0]);
		
		for (s = tmpSort.length; s > 0; s--)
		{
			prev = tmpSort[s-1];
			if (valuesSorted(htmlToText(tmpRows[i].cells[col].innerHTML), htmlToText(tmpRows[prev].cells[col].innerHTML), asc, columnType))
				break;
			else
				tmpSort[s] = prev;

		}
		tmpSort[s] = i;
		i++;
	}
	
	for (i = 0; i < tmpSort.length; i++)
	{
		s = tmpSort[i];
		//tmp = tbl.insertRow(i);
		tmp.rows[i].appendChild(tmpRows[s]);
	}
	
	tbl.style.display = oldstyle;
}

