cur = $('.currency:first');
from = cur.attr('from');
to = cur.attr('to');

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function removeCommas(nString)
{
	var rgx = /,/;
	nString = nString.replace(rgx, '');
	return nString
}


if(to == from) {
	to = 'USD';
}
if (to != from){
	$.ajax({
		type: "POST",
		url: "/ajax/convert-currency",
		data: {from: from, to: to},
		success: function(data){
			$('.currency').each(function() {
				fromprice = removeCommas($(this).children('span.price').text());
				toprice = Number(data.buffer * fromprice).toFixed(2);
				toprice = addCommas(toprice);
				tag = ' (<span class="code">'+data.tosymbol+'</span><span class="price">'+toprice+'</span>)';
				$(this).append(tag);			
			});
		},
		dataType: "json"
	});
}