 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 abs (number) {
return ((n<0) ? n*-1 : n);
}


function  comparisoncalc(currentmor, currentmorrate, newmorrate, estarrangefee, estsurveyfee, estlegalfee, timenewrate, form )
{
currentmor=currentmor.replace(/\,/g,'');
estarrangefee=estarrangefee.replace(/\,/g,'');
estsurveyfee=estsurveyfee.replace(/\,/g,'');
estlegalfee=estlegalfee.replace(/\,/g,'');
var currentmor = parseFloat(currentmor);
var currentmorrate = parseFloat(currentmorrate);
var newmorrate = parseFloat(newmorrate);
var estarrangefee = parseFloat(estarrangefee);
var estsurveyfee = parseFloat(estsurveyfee);
var estlegalfee = parseFloat(estlegalfee);
var timenewrate = parseFloat(timenewrate);
var CMP1 = currentmor * (currentmorrate/100) / 12;
var NMP1 = currentmor * (newmorrate/100) /12;
var ISP1 = timenewrate * (CMP1 - NMP1);
var CS1 = estarrangefee + estsurveyfee + estlegalfee;
var NCS1 = ISP1 - CS1;

var CMP=Math.round(CMP1*100)/100
var NMP=Math.round(NMP1*100)/100 
var ISP=Math.round(ISP1*100)/100 
var CS=Math.round(CS1*100)/100 
var NCS=Math.round(NCS1*100)/100 
var NCSFinal=Math.abs(NCS)

	if (NCS > 0){
	document.getElementById('boldStuff2').innerHTML = ' saving ';}
	else{
		document.getElementById('boldStuff2').innerHTML = ' cost ';}
		

	
comparisoncalculator.currentmonthpay.value = addCommas(CMP);
comparisoncalculator.newmonthpay.value = addCommas(NMP);
comparisoncalculator.interestsaveperiod.value = addCommas(ISP);
comparisoncalculator.costswitch.value = addCommas(CS);
comparisoncalculator.netcostsave.value = addCommas(NCSFinal);

}



function ClearForm(form)
{
form.currentmor.value = "";
form.currentmorrate.value = "";
form.newmorrate.value = "";
form.estarrangefee.value = "";
form.estsurveyfee.value = "";
form.estlegalfee.value = "";
form.timenewrate.value = "";
form.currentmonthpay.value = "";
form.newmonthpay.value = "";
form.interestsaveperiod.value = "";
form.costswitch.value = "";
form.netcostsave.value = "";
}



