lib.getCurrencyDecimalPlaces = function(nCountryCode)
{
	if (lib.currency[nCountryCode])
		return lib.currency[nCountryCode][0];
	return 2;
}

lib.getCurrencyDecimalDelimiter = function(nCountryCode)
{
	if (lib.currency[nCountryCode])
		return lib.currency[nCountryCode][1];
	return '.';
}

lib.getCurrencyThousandsDelimiter = function(nCountryCode)
{
	if (lib.currency[nCountryCode])
		return lib.currency[nCountryCode][2];
	return ',';
}

lib.getCurrencyPrefix = function(nCountryCode)
{
	if (lib.currency[nCountryCode])
		return lib.currency[nCountryCode][3];
	return '';
}

lib.getCurrencyPostfix = function(nCountryCode)
{
	if (lib.currency[nCountryCode])
		return lib.currency[nCountryCode][4];
	return ' ('+nCountryCode+')';
}

lib.getCurrencyGBPRate = function(nCountryCode)
{
	if (lib.currency[nCountryCode])
		return lib.currency[nCountryCode][5];
	return 0;
}

lib.formatCurrency = function(num, nCountryCode)
{
	if (num == null) return 'N/A';      
    if (isNaN(num))
    {
		return '&nbsp;';
    }
	var v = getCurrencyGBPRate(nCountryCode);
	if (v == 0)
	{
		return 'Not a Currency: ' + nCountryCode;
	}
    var minus = '';
    var DecimalSep = getCurrencyDecimalDelimiter(nCountryCode);
    var DecimalPlaces = getCurrencyDecimalPlaces(nCountryCode);
    var ThousandsSep = getCurrencyThousandsDelimiter(nCountryCode);
    num = String(num);
   	if (num.lastIndexOf("-") == 0) minus='-';
//   	num = getNumFromText(num.substring(0,num.lastIndexOf(DecimalSep)))+'.'+getNumFromText(num.substring(num.lastIndexOf(DecimalSep)+1));
	var f = Math.abs(parseFloat(num));
	f = Math.round(f * Math.pow(10,DecimalPlaces))/Math.pow(10,DecimalPlaces);
	var main = String(Math.floor(f));
	var dec = String(Math.round((f-main)*Math.pow(10,DecimalPlaces)));
	// watch out for values < 0.1 case
	if (dec.length == DecimalPlaces - 1) dec = '0' + dec;
	// leave in previous check. if my thinking is correct
	// this won't get called and is :. a bug. (how could a string
	// end up being < the desired length other than by being < 0.1?) if my thinking
	// is buggy then :. this is correct :)
	while(dec.length < DecimalPlaces) dec += '0';
	
	var sVar;
	if (main.length > 3)
	{
		var start;
	    var sVar = main.substr(main.length-3, 3);
		main = main.substr(0, main.length - 3);
		while(main.length)
		{
			start = main.length-3;
	    	if (start < 0) start = 0;
			sVar = main.substr(start)+ThousandsSep+sVar;
			main = main.substr(0, main.length - Math.min(3,main.length));
		}
	}
	else
		sVar = main;

	if (DecimalPlaces == 0){DecimalSep = ''; dec = '';}
    return getCurrencyPrefix(nCountryCode) + minus + sVar + DecimalSep + dec + getCurrencyPostfix(nCountryCode);
}

lib.getRateIn = function(num,nSourceCountryCode,nDestCountryCode)
{
	var fSource = lib.getCurrencyGBPRate(nSourceCountryCode);
	var fDest = lib.getCurrencyGBPRate(nDestCountryCode);
	if (fSource == 0 || fDest == 0)
	{
		return 0;
	}
	var gbpRate = num / fSource;
	return gbpRate * fDest;
}