function htmlToText(htmlContent)
{
	//var Tmp = htmlContent;
	//var tags = /<(.*)>.*<\/\1>/;
	//Tmp = htmlContent.replace(tags, "TAG");
	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 getValutaValue(waarde)
{
	var vTemp;
	vTemp = waarde.substring(1);
	vTemp = vTemp.replace(".","");
	vTemp = vTemp.replace(",",".");
	return parseFloat(vTemp);
}


function getNumericValue(waarde)
{
	var vTemp;
	vTemp = waarde.replace(",","");
	return parseFloat(vTemp);
}


function getDateTimeDutch(waarde)
{
	var vTemp;
	vTemp = waarde.substring(6, 10) + waarde.substring(3, 5) + waarde.substring(0, 2); // DD-MM-YYYY -> YYYYMMDD
	if (waarde.length > 10) // time
		vTemp += waarde.substring(10); // HH:MM:SS
	return vTemp;
}


function valuesSorted(vNew, vPast, asc, columnType)
{
	var numeriek = /numeric/i;
	if (numeriek.test(columnType))
	{
		// Numeriek
		if (asc)
		{
			if (getNumericValue(vNew) < getNumericValue(vPast))
				return false;
		}
		else
		{
			if (getNumericValue(vNew) > getNumericValue(vPast))
				return false;
		}
		return true;
	}
	var valuta = /valuta/i;
	if (valuta.test(columnType))
	{
		// Valuta
		if (asc)
		{
			if (getValutaValue(vNew) < getValutaValue(vPast))
				return false;
		}
		else
		{
			if (getValutaValue(vNew) > getValutaValue(vPast))
				return false;
		}
		return true;
	}
	var datetimedutch = /datetimedutch/i;
	if (datetimedutch.test(columnType))
	{
		// Valuta
		if (asc)
		{
			if (getDateTimeDutch(vNew) < getDateTimeDutch(vPast))
				return false;
		}
		else
		{
			if (getDateTimeDutch(vNew) > getDateTimeDutch(vPast))
				return false;
		}
		return true;
	}
	// Tekst
	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;
	
	if (tbl.rows.length > 1000)
	{
		tbl.rows[0].cells[col].style.backgroundPosition="100% -90px";
		return;
	}

	var tmpRows = new Array();
	var tmpSort = new Array();
	var i, s, tmp, prev;
	
	tmp = tbl.rows[0].cells[col].getAttribute('style');
	if (tmp != null)
	{
		if (typeof tmp == 'object')
			tmp = tmp.cssText;
		tmp = tmp.toLowerCase();
		if (tmp.indexOf("-30px") != -1)
			asc = false;
		if (tmp.indexOf("-60px") != -1)
			asc = true;
	}
	
	//if (tbl.rows[0].cells[col].style.backgroundPosition == "right -30px")
	//if (tmp == "backgroundposition: 100% -30px")
	for (i = 0; i < tbl.rows[0].cells.length; i++)
		if (i == col)
		{
			if (asc)
				tbl.rows[0].cells[i].style.backgroundPosition="100% -30px";
			else
				tbl.rows[0].cells[i].style.backgroundPosition="100% -60px";
		}
		else
			tbl.rows[0].cells[i].style.backgroundPosition="100% 0px";
	
	i = 1;
	while (i < tbl.rows.length)
	{
		tmpRows[i] = tbl.rows[tbl.rows.length-i];
				
		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))
			if (valuesSorted(tmpRows[i].cells[col].innerHTML, tmpRows[tmpSort[s-1]].cells[col].innerHTML, asc, columnType))
				break;
			else
				tmpSort[s] = prev;

		}
		tmpSort[s] = i;
		i++;
	}
	
	for (i = 1; i < tbl.length; i++)
		tbl.removeChild(tbl.lastChild);
	//var tbody = document.createElement('tbody'); 
	for (i = 0; i < tmpSort.length; i++)
		tbl.appendChild(tmpRows[tmpSort[i]]);
	//	tbody.appendChild(tmpRows[tmpSort[i]]);
	//tbl.appendChild(tbody); 
}

