function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x[0])) {
        x[0] = x[0].replace(rgx, '$1' + ',' + '$2');
    }
    return x[0] + x2;
}

function calculate(tma, companya, companyb, tma_percent, a_percent, b_percent, tma_fees, a_fees, b_fees, form) {
		var commission = form.commission.value.replace(/[^\d\.]/g,"");
		if (form.commission.value <= 0) {
				form.tma.value = 0
				form.companya.value = 0
				form.companyb.value = 0
		} else {
			form.tma.value = CurrencyFormatted(commission * (form.tma_percent.value / 100) - form.tma_fees.value * 12);
			form.tma.value = addCommas(form.tma.value);
			form.companyb.value = CurrencyFormatted(commission * (form.b_percent.value / 100) - form.b_fees.value * 12);
			form.companyb.value = addCommas(form.companyb.value);
			if (form.commission.value > 59999) { 
				form.companya.value = CurrencyFormatted(commission * (form.a_percent.value / 100));
				form.companya.value = addCommas(form.companya.value);
			} else { 
				form.companya.value = CurrencyFormatted(commission * (form.a_percent.value / 100) - form.a_fees.value * 12);
				form.companya.value = addCommas(form.companya.value);
			}
		}
}