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 getStampDuty()
	{
	var	housePrice = parseFloat(document.getElementById('HousePrice').value.replace('/[^0-9\.]/g', ''));
	var	stampDuty = 0;

	if(housePrice <= 175000)
		{
		stampDuty = 0;
		}
	else if(housePrice <= 250000)
		{
		stampDuty = .01 * housePrice;
		}
	else if(housePrice <= 500000)
		{
		stampDuty = .03 * housePrice;
		}
	else
		{
		stampDuty = .04 * housePrice;
		}

	var StampDutyText = 'Stamp duty on a house valued at \u00A3' + addCommas(housePrice.toFixed(2)) + ' would be: \u00A3' + addCommas(stampDuty.toFixed(2));

	if (document.getElementById('StampDuty'))
		{
		document.getElementById('StampDuty').innerHTML = StampDutyText;
		}
	else
		{
		var newP = document.createElement('p');
		newP.setAttribute('id', 'StampDuty');
		document.getElementById('StampDutyForm').appendChild(newP);
		newP.innerHTML = StampDutyText;
		}
	}



function getBuyToLet()
	{
	var	PropertyValue = parseFloat(document.getElementById("PropertyValue").value.replace('/[^0-9\.]/g', ''));
	var	MonthlyRent = parseFloat(document.getElementById("MonthlyRent").value.replace('/[^0-9\.]/g', ''));

	var MaxLoanLTV = 0
	var MaxLoanLTR = 0
	var MaxLoan = 0

	if (MonthlyRent > 0)
		{
		MaxLoanLTR = parseFloat((MonthlyRent * 12)/ 0.06875);
		MaxLoan = MaxLoanLTR;
		}

	if (PropertyValue > 0)
		{
		MaxLoanLTV = PropertyValue * 0.85;
		MaxLoan = MaxLoanLTV;
		}

	if (MonthlyRent > 0 && PropertyValue > 0)
		{
		(MaxLoanLTV > MaxLoanLTR) ? MaxLoan = MaxLoanLTR : MaxLoan = MaxLoanLTV;
		}

	MaxLoan = Math.round(MaxLoan * 100) / 100

	var MaxLoanText = 'A conservative estimate of what you could borrow, based on the information you have provided the mortgage calculator is \u00A3' + addCommas(MaxLoan.toFixed(2));

	if (document.getElementById('BuyToLet'))
		{
		document.getElementById('BuyToLet').innerHTML = MaxLoanText;
		}
	else
		{
		var newP = document.createElement('p');
		newP.setAttribute('id', 'BuyToLet');
		document.getElementById('BuyToLetForm').appendChild(newP);
		newP.innerHTML = MaxLoanText;
		}
	}



function getRentCharge()
	{
	//declare variables
	var	PropertyValue = parseFloat(document.getElementById('RPropertyValue').value.replace('/[^0-9\.]/g', ''));
	var	Deposit = (parseInt(document.getElementById('Deposit15').value) == 0 || isNaN(parseInt(document.getElementById('Deposit15').value))) ? setDeposit() : document.getElementById('Deposit15').value;

	var MinDeposit = PropertyValue * 0.15;
	if (Deposit < MinDeposit) { Deposit = MinDeposit; }

	var DepositString = Deposit.toString();
	(DepositString.indexOf('.') > 0) ? document.getElementById('Deposit15').value = parseFloat(Deposit).toFixed(2) : document.getElementById('Deposit15').value = Deposit;

	var Loan = PropertyValue - Deposit;
	var Rent = Math.round(parseFloat(Loan * 1.25 * 0.065 / 12) * 100) / 100;

	var RentChargeText = 'You need at least \u00A3' + addCommas(Rent.toFixed(2)) + ' per month in rent';

	if (document.getElementById('RentCharge'))
		{
		document.getElementById('RentCharge').innerHTML = RentChargeText;
		}
	else
		{
		var newP = document.createElement('p');
		newP.setAttribute('id', 'RentCharge');
		document.getElementById('RentChargeForm').appendChild(newP);
		newP.innerHTML = RentChargeText;
		}
	}

function setDeposit ()
	{
	var	PropertyValue = parseFloat(document.getElementById('RPropertyValue').value.replace('/[^0-9\.]/g', ''));
	var	Deposit = PropertyValue * 0.15
	document.getElementById('Deposit15').value = Deposit;
	return Deposit;
	}