// Updated 07-02-2010
var currency_types = ['ARS', 'AUD', 'BRL', 'CAD', 'CLP', 'CNY', 'COP', 'DKK', 'EGP', 'EUR', 'HKD', 'ILS', 'INR', 'JPY', 'KRW', 'MXN', 'NOK', 'NZD', 'PEN', 'PHP', 'PKR', 'RUB', 'SAR', 'SEK', 'SGD', 'THB', 'TWD', 'USD', 'ZAR', ,'GBP'];
var currency_rates = ['5.99030218', '1.80632091', '2.93180358', '1.67435721', '844.318182', '10.6529843', '3', '8.5197117', '8.53873432', '1.14441837', '12.1245794', '5.84331687', '72.7412587', '139.150985', '1', '20.5988356', '9.37584502', '2.27814279', '4.47494085', '72.5855973', '132.161613', '47.3722561', '5.85065695', '11.6746104', '2.22015111', '51.7855958', '50.0770268', '1.5603', '12.1371231', ,'1.0'];

// currency_convert(2, 'GBP', 'USD') => 3.6254
function currency_convert(fromvalue, fromtype, totype) {
  var tovalue = null;
  for (var x=0; x < currency_types.length; x++)
    if (currency_types[x] == fromtype) {
      tovalue = fromvalue / currency_rates[x];
      break;
    }
  if (tovalue == null)
    return null;
  for (var x=0; x < currency_types.length; x++)
    if (currency_types[x] == totype)
      return tovalue * currency_rates[x];
  return null;
}

// currency_string(2, 'GBP', 'USD') => "$3.62 (USD)"
function currency_string(fromvalue, fromtype, totype) {
  var tovalue = currency_convert(fromvalue, fromtype, totype);
  if (tovalue == null)
    return null;

  var decimals, sepchar, decchar, prechar, postchar;
  var data = [
['ARS', 2, ',', '.', '$', ''],
['AUD', 2, ',', '.', '$', ''],
['BBD', 2, ',', '.', '$', ''],
['BMD', 2, ',', '.', '$', ''],
['BND', 2, ',', '.', '$', ''],
['BRL', 2, ',', '.', 'R$', ''],
['BSD', 2, ',', '.', '$', ''],
['CAD', 2, ',', '.', '$', ''],
['CLP', 2, ',', '.', '$', ''],
['CNY', 2, ',', '.', '&#20803;', ''],
['COP', 2, ',', '.', '&#8369;', ''],
['CRC', 2, ',', '.', '&#8353;', ''],
['CYP', 2, ',', '.', '&pound;', ''],
['DKK', 2, '.', ',', '', ':-'],
['DOP', 2, ',', '.', '&#8369;', ''],
['EGP', 2, ',', '.', '&pound;', ''],
['EUR', 2, ',', '.', '&#8364;', ''],
['FJD', 2, ',', '.', '$', ''],
['GBP', 2, ',', '.', '&pound;', ''],
['HKD', 2, ',', '.', '$', ''],
['ILS', 2, ',', '.', '&#8362;', ''],
['INR', 2, ',', '.', '&#8360;', ''],
['IRR', 2, ',', '.', '&#65020;', ''],
['JMD', 2, ',', '.', '$', ''],
['JPY', 0, ',', '.', '&yen;', ''],
['KRW', 2, ',', '.', '&#8361;', ''],
['LBP', 2, ',', '.', '&pound;', ''],
['LKR', 2, ',', '.', '&#3065;', ''],
['MTL', 2, ',', '.', '&#8356;', ''],
['MUR', 2, ',', '.', '&#8360;', ''],
['MXN', 2, ',', '.', '$', ''],
['NOK', 2, '.', ',', '', ':-'],
['NZD', 2, ',', '.', '$', ''],
['OMR', 2, ',', '.', '&#65020;', ''],
['PEN', 2, ',', '.', 'S/.', ''],
['PHP', 2, ',', '.', '&#8369;', ''],
['PKR', 2, ',', '.', '&#8360;', ''],
['QAR', 2, ',', '.', '&#65020;', ''],
['RUB', 2, ',', '.', '&#1088;&#1091;&#1073;', ''],
['SAR', 2, ',', '.', '&#65020;', ''],
['SEK', 2, '.', ',', '', ':-'],
['SGD', 2, ',', '.', '$', ''],
['THB', 2, ',', '.', '&#3647;', ''],
['TRY', 2, ',', '.', '&#8356;', ''],
['TTD', 2, ',', '.', '$', ''],
['TWD', 2, ',', '.', '&#20803;', ''],
['USD', 2, ',', '.', '$', ''],
['VND', 2, ',', '.', '&#8363;', ''],
['ZAR', 2, ',', '.', 'R', '']
  ];
  var found = false;
  for (var x=0; x < data.length; x++)
    if (data[x][0] == totype) {
      decimals = data[x][1];
      sepchar = data[x][2];
      decchar = data[x][3];
      prechar = data[x][4];
      postchar = data[x][5];
      found = true;
      break;
    }
  if (!found)
    return null

  tovalue = Math.round(tovalue*Math.pow(10, decimals))/Math.pow(10, decimals);

  // Divide the string into number and decimal parts.
  var a = tovalue.toString().split(".");
  a.push("");
  var numstr = a[0];
  var decstr = a[1];

  // Number: Add thousands separators.
  for (var x=numstr.length-3; x>0; x-=3)
    numstr = numstr.substring(0, x)+sepchar+numstr.substring(x)

  // Decimal: Add trailing 0s, or strip surplus digits.
  while (decstr.length < decimals)
    decstr += '0';
  if (decstr.length > decimals)
    decstr = decstr.substring(0, decimals);

  // Assemble the return string.
  if (decimals > 0)
    tovalue = numstr+decchar+decstr;
  else
    tovalue = numstr;
  return prechar+tovalue+postchar+" ("+totype+")";
}