// JavaScript Document
var currencyMap = new Object();
function createMap() {
	currencyMap['NA-USD'] = "$";
	currencyMap['NA-CAD'] = "$";
	currencyMap['NA-BRL'] = "R$";
	currencyMap['EMEA-USD'] = "$";
	currencyMap['EMEA-EUR'] = "€";
	currencyMap['EMEA-CHF'] = "CHF";
	currencyMap['EMEA-CZK'] = "Kč";
	currencyMap['EMEA-DKK'] = "kr";
	currencyMap['EMEA-GBP'] = "£";
	currencyMap['EMEA-HUF'] = 'Ft';
	currencyMap['EMEA-PLN'] = "zł";
	currencyMap['EMEA-NOK'] = "kr";
	currencyMap['EMEA-SEK'] = "kr";
	currencyMap['EMEA-ZAR'] = "R";
	currencyMap['APAC-AUS'] = "$";
	currencyMap['APAC-HKD'] = "$";
	currencyMap['APAC-USD'] = "$";
	currencyMap['APAC-KRW'] = "₩";
	currencyMap['APAC-NZD'] = "$";
	currencyMap['APAC-SGD'] = "$";
	currencyMap['APAC-THB'] = "฿";
	currencyMap['APAC-TWD'] = "$";
	currencyMap['APAC-INR'] = "Rs.";
	currencyMap['APAC-CNY'] = "¥";
	currencyMap['APAC-JPY'] = "¥";
	currencyMap['APAC-HKPR'] = "$";
	currencyMap['APAC-TWPR'] = "$";
	currencyMap['APAC-CNDVE'] = "$";
	currencyMap['APAC-ABTWD'] = "$";
	currencyMap['APAC-ABHKD'] = "$";
	currencyMap['APAC-ABC1USD'] = "$";
	currencyMap['NA-DSTUSD'] = "$";
	currencyMap['EMEA-DSTEUR'] = "$";
	currencyMap['EMEA-MEUSD'] = "$";
	currencyMap['EMEA-AFRUSD'] = "$";
}

function convertCurrency(value, region) {
	createMap();
	var splitter = region.split("-");
	var currencyValue = null;

	/*if (splitter[0] == "NA") {
		currencyValue = intoNA(value, currencyMap[region]);
	} else if (splitter[0] == "EMEA") {
		currencyValue = intoEMEA(value, currencyMap[region]);
	} else if (splitter[0] == "APAC") {
		currencyValue = intoAPAC(value, currencyMap[region]);
	}*/
	var precision = 2;
	var thousand = ",";
	if(currencyMap[region] == 'Ft' || currencyMap[region] == '₩' || currencyMap[region] == '¥'){
		 precision = 0;
		 thousand = ",";
	} else if(currencyMap[region] == 'R' || currencyMap[region] == 'zł'){
		precision = 2;
		thousand = " ";
	} else {
		precision = 2;
		thousand = ",";
	}
	
	var currencySymbol = currencyMap[region]+" ";
	
	var currencyValue = accounting.formatMoney(value,[symbol = currencySymbol],[precision = precision],[thousand = thousand],[decimal = "."],[format = "%s%v"])
	
	return currencyValue;
}

function intoNA(num, symbol) {
	
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	
	num = Math.floor(num * 100 + 0.50000000001);
	
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	
	if (cents < 10)
		cents = "0" + cents;
	for ( var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ','
				+ num.substring(num.length - (4 * i + 3));
	
			if((num == 00 ) && (cents == 00 ))
			{
				cents = '01';
			}
	return symbol + " " + (((sign)?'':'-') + num + '.' + cents );
}

function intoEMEA(num, symbol) {
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	for ( var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + '.'
				+ num.substring(num.length - (4 * i + 3));
	if((num == 00 ) && (cents == 00 ))
	{
		cents = '01';
	}
	return symbol + " " + (((sign)?'':'-') + num + ',' + cents );
}

function intoAPAC(nStr, symbol) {
	
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];	
	
	x2 = x.length > 1 ? '.' + x[1].substring(0,2) : '';
	if((x1==0) && ((x2 == 00) || (x2 == '')))
	{
		x2 = '.01';
		
	}
	if(x2.charAt(1) == 0)
	{

	}else
	{
		x2 = '.' + Math.round(x2 * 100);
	}	
	
		
	if((x2 == '.0') || (x2 == ''))
	{
		x2 = '.00'
	}
	
	var rgx = /(\d+)(\d{3})/;
	var z = 0;
	var len = String(x1).length;
	var num = parseInt((len / 2) - 1);

	while (rgx.test(x1)) {
		if (z > 0) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		} else {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
			rgx = /(\d+)(\d{2})/;
		}
		z++;
		num--;
		if (num == 0) {
			break;
		}
	}
	
	return symbol + " " + x1 + x2;
}
