//Rewrites a value to something resembling an amount with only a decimal separator
function rewriteToAmount(val)
{
	//1. Make sure we're working with a string through which we can loop
	val	= removeNonNumeric(val);
	//2. Instantiate result variable
	var nieuw	= new String();
	//3. Loop through the variable we're testing in order to skip all the lead zero's
	for (var a=0; a < val.length; a++)
	{
		//3a. As long as we're encountering zero's without encountering anything else, continue the loop
		if (val.charAt(a) == "0")
		{
			continue;
		}
		//3b. We've encountered a non-zero value. Put the remainder of val into nieuw and break the loop
		else
		{
			nieuw = val.substr(a);
			break;
		}
	}
	//4. Replace points with emptiness and commas with points
	nieuw	= nieuw.replace(".", "");
	nieuw	= nieuw.replace(",", ".");
	//5. Convert nieuw to a float value
	nieuw	= parseFloat(nieuw);
	//6. If nieuw is not a valid float value, return 0
	if (isNaN(nieuw)) { return 0; }
	//7. Return the resulting float value
	return nieuw;
}

//Removes all non-numeric characters from a string
function removeNonNumeric(val)
{
	//1. Make sure we're working with a string through which we can loop
	val	= String(val);
	//2. Instantiate result variable
	var nieuw	= new String();
	//3. Loop through the variable we're testing and skip all non-numeric characters
	for (var a=0; a < val.length; a++)
	{
		//3a. Only if the character is a number, thousand separator or decimal separator, add it to the result
		if ((! isNaN(parseInt(val.charAt(a)))) || (val.charAt(a) == ".") || (val.charAt(a) == ","))
		{
			nieuw	= nieuw + val.charAt(a);
		}
	}
	//4. Return the resulting string
	return nieuw;
}

//Removes all numeric characters from a string
function removeNumeric(val)
{
	//1. Make sure we're working with a string through which we can loop
	val	= String(val);
	//2. Instantiate result variable
	var nieuw	= new String();
	//3. Loop through the variable we're testing and skip all numeric characters
	for (var a=0; a < val.length; a++)
	{
		//3a. Only if the character is non-numeric, add it to the result string
		if (isNaN(parseInt(val.charAt(a))))
		{
			nieuw	= nieuw + val.charAt(a);
		}
	}
	//4. Return the resulting string
	return nieuw;
}

//Rewrites a certain value to a rounded integer of the original value
function rewriteToInteger(val)
{
	if (String(val).length == 0) { return ""; }
	val = rewriteToAmount(val);
	return Math.round(val);
}

//Rewrites a certain value to a float value rounded to a given precision
function rewriteToDecimal(val, precision)
{
	if (String(val).length == 0) { return ""; }
	val = rewriteToAmount(val);
	return Number(val).toFixed(precision);
}

//Rewrites a field value to a single word. All spaces are removed, as well as illegal and special characters
function rewriteToSingleWord(val, letterCase)
{
	var nieuw = "";
	for (var l=0; l < val.length; l++)
	{
		if ((val.charAt(l) != " ") && (val.charAt(l) != ",") && (val.charAt(l) != ".") && (val.charAt(l) != "'"))
		{
			nieuw = nieuw + val.charAt(l);
		}
	}
	val = nieuw;
	switch(letterCase)
	{
		case 1:
			return uc(val);
		case 2:
			return lc(val);
		case 3:
			return ucfirst(val);
		case 4:
			return ucwords(val);
		default:
			return val;
	}
}

//Rewrites a field value maintaining mulptiple words but removing special and illegal characters
function rewriteToMultipleWords(val, letterCase)
{
	var nieuw = "";
	for (var l=0; l < val.length; l++)
	{
		if ((val.charAt(l) != ",") && (val.charAt(l) != ".")) { nieuw = nieuw + val.charAt(l); }
	}
	val = nieuw;
	switch(letterCase)
	{
		case 1:
			return uc(val);
		case 2:
			return lc(val);
		case 3:
			return ucfirst(val);
		case 4:
			return ucwords(val);
		default:
			return val;
	}
}

//This is free text. All that happens here is rewriting of value casing
function rewriteToFreeText(val, letterCase)
{
	switch(letterCase)
	{
		case 1:
			return uc(val);
		case 2:
			return lc(val);
		case 3:
			return ucfirst(val);
		case 4:
			return ucwords(val);
		default:
			return val;
	}
}

//Rewrite a string to uppercase
function uc(val)
{
	return String(val).toUpperCase();
}

//Rewrite a string to lowercase
function lc(val)
{
	return String(val).toLowerCase();
}

//Rewrite a string to lower case and only the first character to uppercase
function ucfirst(val)
{
	val			= lc(val);
	var nieuw	= "";
	for (var l=0; l < val.length; l++)
	{
		if (l == 0) { nieuw += uc(val.charAt(l)); }
		else { nieuw+= val.charAt(l); }
	}
	return nieuw;
}

//Rewrite a string to lower case with all words starting with a capital
function ucwords(val)
{
	val 		= lc(val);
	var nieuw	= "";
	for (var l=0; l < val.length; l++)
	{
		var toUp	= false;
		if (l == 0) { toUp = true; }
		else if (val.charAt(parseInt(l-1)) == " ") { toUp = true; }
		if (toUp == true) { nieuw += uc(val.charAt(l)); }
		else { nieuw += val.charAt(l); }
	}
	return nieuw;
}


function DisplayAmount(val)
{
	if (String(val).length == 0)	{	return 	"0,-";	}
	if (isNaN(parseInt(val)))		{	return	"0,-";	}
	if (parseInt(val) == 0)			{	return	"0,-";	}
	var delen	= String(val).split(",");
	var voor	= String(delen[0]);
	if (voor.length > 3)
	{
		var nieuwVoor	= "";
		var count		= 0;
		for (var l=parseInt(voor.length-1); l > -1; l--)
		{
			count++;
			nieuwVoor = voor.charAt(l)+nieuwVoor;
			if ((l != 0) && (count == 3))	{	nieuwVoor = "."+nieuwVoor;	count	= 0;	}
		}
		voor = nieuwVoor;
	}
	var retour	= voor;
	if (delen.length > 1)	{	retour+= ","+delen[1];	}
	//else 					{	retour+= ",-";			}
	return retour;
}



function DisplayAmount2(val)
{
	if (String(val).length == 0)	{	return 	"0,-";	}
	if (isNaN(parseInt(val)))		{	return	"0,-";	}
	if (parseFloat(val) == 0)		{	return	"0,-";	}

	var nRounded	= Math.floor(val);
	var nDecimals	= Math.round((val - nRounded) * 100);
	var sRounded	= "" + nRounded;
	
	if (sRounded.length > 3)
	{
		var sDotted	= "";
		var count	= 0;

		for (var l = parseInt(sRounded.length - 1); l > -1; l--)
		{
			count++;
			sDotted = sRounded.charAt(l) + sDotted;
			if ((l != 0) && (count == 3))	{	sDotted = "." + sDotted;	count	= 0;	}
		}

		sRounded = sDotted;
	}

	var	retour = sRounded + "," + ( (nDecimals == 0) ? "-" : (((nDecimals < 10) ? "0" : "") + nDecimals));
	return retour;
}			



function formatNumericThreshold(iVal, sFldClass)
{
	for (var a=0; a < fieldDefinition.length; a++)
	{
		if (fieldDefinition[a][0] == sFldClass)
		{
			var iKeyPress	=	parseInt(fieldDefinition[a][1]);
			switch(iKeyPress)
			{
				case 2:
					return "€ "+DisplayAmount(iVal);
				default:
					return iVal;
			}
		}
	}
}
