<?php

function Currency($num,$sign='before',$red=false,$comment=true)
{

	$original = $num;
	if($sign=='before' && $num<0)
	{
		$negative = true;
//		$num *= -1;
	}
	elseif($sign=='CR' && $num<0)
	{
		$cr = true;
//		$num *= -1;
		$red = true;
	}
	elseif($sign=='BL' && $num<0)
	{
		$bl = true;
//		$num *= -1;
		$red = true;
	}
	elseif($sign=='DTR' && $num<0)
	{
		$dtr = true;
//		$num *= -1;
	}
	
	if(SystemPreferences('DECIMAL')==',')
	{
		if(Preferences('CURRENCY_SL')=='Left')
			$num = SystemPreferences('CURRENCY').' '.number_format($num,2,',',' ');
		else
			$num = number_format($num,2,',',' ').' '.SystemPreferences('CURRENCY');
	}
	else
	{
		if(Preferences('CURRENCY_SL')<>'Left')
			$num = number_format($num,2,'.',' ').' '.SystemPreferences('CURRENCY');
		else
			$num = SystemPreferences('CURRENCY').' '.number_format($num,2,'.',' ');
	}


	if($negative)
		$num = '-'.$num;
	elseif($cr && $original > 0)
//		$num = $num.'CR';
		$num = $num.'';
	elseif($cr && $original < 0)
	{
		$num = '<font color=red>'.$num.'</font>';
		$red = false;
	}		
	elseif($bl && $original < 0)
	{
//	echo $num;
		$num ='<font color=red>-'.str_replace('-','',$num).'</font>';
//		$num = '<font color=red>'.$num.'</font>';
		$red = false;
	}		
	elseif($dtr && $original < 0)
	{
//		echo $num;
		$num ='-'.str_replace('-','',$num).'';
//		$num = '<font color=red>'.$num.'</font>';
		$red = false;
	}		
//	echo $num;

	if($red && $original<0)
		$num = '<font color=red>'.$num.'</font>';		
	elseif($original<0 && !$bl)
	{
//		echo $num;
		if(Preferences('CURRENCY_SL')=='Left')
		{
			$num = str_replace('#','',$num);
//			$num ='-'.SystemPreferences('CURRENCY').' '.str_replace('-','',$num).'';
//			$num ='-'.SystemPreferences('CURRENCY').' '.str_replace('-','',$num).'';
			$num ='<B>&ndash;</B>'.str_replace('-','',$num).'';
		}
		else
			$num ='-'.str_replace('-','',$num).'';
	}
	if($comment==true)
		//$comment = '<!-- '.$original.' -->';
		$comment = '';
	else
		$comment = '';

	return $comment.$num;
}
?>