/**
 * Format a number in the user currency using it's culture specifications
 * 
 * Require a varaibale called 'FORMATED_NUMBER' that is the formated version of the number '-333222111.99'
 * 
 * @author Christophe Dolivet
 * @since 1.0 - 18 nov. 08 - Christophe Dolivet
 * @version 1.0 - 18 nov. 08 - Christophe Dolivet
 * @param Float amount The amount to format
 * @param String decimalMethod [OPTIONNAL] ('round' (default), 'ceil' or 'floor') The option to apply to use no more than 2 decimal digits
 * @param Integer forceXDecimalDigits [OPTIONNAL] (2 (default), 1 or 0 ) Force to dispaly at least X decimal digits event for decimal value of 0
 */
function format_currency( amount, decimalMethod, forceXDecimalDigits )
{
	if( !decimalMethod )
		decimalMethod	= 'round';
	if( typeof( forceXDecimalDigits ) == "undefined" )
		forceXDecimalDigits	= 0;
	var matches				= FORMATED_NUMBER.match( /^([^3]*)?333([^2]*)?222([^1]*)?111(?:([^9]*)?99)?(.*)?$/ );
	var start				= typeof( matches[1] ) == 'undefined' ? '' : matches[1];
	var milionSeparator		= typeof( matches[2] ) == 'undefined' ? '' : matches[2];
	var thousandSeparator	= typeof( matches[3] ) == 'undefined' ? '' : matches[3];
	var decimalSeparator	= typeof( matches[4] ) == 'undefined' ? '' : matches[4];
	var end					= typeof( matches[5] ) == 'undefined' ? '' : matches[5];
	
	var absAmount			= Math.abs( amount );
	var intAbsAmountVal		= parseInt( absAmount );
	var floatAbsAmountVal	= parseFloat( absAmount - intAbsAmountVal );

	// apply decimal transformation
	switch( decimalMethod )
	{
		case 'ceil':
			floatAbsAmountVal	= Math.ceil( floatAbsAmountVal * 100 );
			if( floatAbsAmountVal > 0 && forceXDecimalDigits == 0 )
				intAbsAmountVal++;
			break;
		case 'floor':
			floatAbsAmountVal	= Math.floor( floatAbsAmountVal * 100 );
			break;
		default:
			floatAbsAmountVal	= Math.round( floatAbsAmountVal * 100 );
			if( floatAbsAmountVal >= 50 && forceXDecimalDigits == 0 )
				intAbsAmountVal++;
			break;
	}
	var floatString	= ( floatAbsAmountVal == 0 ) ? '' : floatAbsAmountVal.toString();
	floatString		= floatString.substr( 0, forceXDecimalDigits );
	while( floatString.length < forceXDecimalDigits )
	{
		floatString	= floatString + '0';
	}

	// build result string
	var result	= start;
	if( intAbsAmountVal >= 1000000000 )
		result	+= intAbsAmountVal.toString().substr( 0, intAbsAmountVal.toString().length-9 ) + milionSeparator;
	if( intAbsAmountVal >= 1000000 )
		result	+= intAbsAmountVal.toString().substring( Math.max( 0, intAbsAmountVal.toString().length - 9 ), intAbsAmountVal.toString().length - Math.min( intAbsAmountVal.toString().length, 6 ) ) + milionSeparator;
	if( intAbsAmountVal >= 1000 )
		result	+= intAbsAmountVal.toString().substr( -6, Math.min( intAbsAmountVal.toString().length - 3, 3 ) ) + thousandSeparator;
	result	+= intAbsAmountVal.toString().substr( Math.max( 0, intAbsAmountVal.toString().length - 3 ) );

	if( floatString.length > 0 )
		result	+= decimalSeparator + floatString;
	result	+= end;
	// remove negative sign
	if( amount >= 0 )
		result	= result.replace( '-', '' );
	return result;
}
