



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 calc(form) {
	fr=form['fixed_rate'].value.replace(/[^0-9.]/, '');
	vr=form['var_rate'].value.replace(/[^0-9.]/, '');
	b=form['balance'].value.replace(/[^0-9.]/, '');
	m=(form['months'].value)/12;
	f=form['figure'].value.replace(/[^0-9.]/, '');


	w=Math.abs((fr-vr)/100);
	n=(b-f)/b;
	break_cost=w*b*m*n;

	if(isNaN(break_cost)) {
		alert('Please complete all fields correctly');
		return;
	}

	break_cost=break_cost.toFixed(2);
	break_cost=addCommas(break_cost);
	document.getElementById('result').innerHTML='Your estimated break cost is: <B>$'+break_cost+'</B>';

}



function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


//addLoadEvent(addthis);