function Estimate() {
	var SellingPrice = StripNonNumeric(document.getElementById("sellingPrice").value);
	var DownPayment = StripNonNumeric(document.getElementById("downPayment").value);
	var LoanAmount = SellingPrice - DownPayment;
	var Exemption;
	var PercentFinanced;
	var PMI;
	
	document.getElementById("propertyTax").value = CurrencyFormat(SellingPrice * document.getElementById("taxrate").value);
	var Exemption1 = (SellingPrice * document.getElementById("taxrate").value) - ((SellingPrice - document.getElementById("homeowners").value) * document.getElementById("taxrate").value);
	var Exemption2 = (SellingPrice * document.getElementById("taxrate").value) - ((SellingPrice / 2) * document.getElementById("taxrate").value);
	Exemption = CurrencyFormat(0 - Math.min(Exemption1, Exemption2));
	
	document.getElementById("exemption").value = Exemption;
	document.getElementById("insurance").value = CurrencyFormat(SellingPrice * .005);
	PercentFinanced = (LoanAmount * 100) / SellingPrice;
	if (PercentFinanced > 95) {
		PMI = LoanAmount * 0.0090;
	} else if (PercentFinanced > 90) {
		PMI = LoanAmount * 0.0078;
	} else if (PercentFinanced > 85) {
		PMI = LoanAmount * 0.0052;
	} else if (PercentFinanced > 80) {
		PMI = LoanAmount * 0.0032;
	} else {
		PMI = 0;
	}	
	document.getElementById("pmi").value = CurrencyFormat(PMI);
	
	Calculate();
}

function Calculate() {
	document.getElementById('interestRate').value = StripNonNumeric(document.getElementById('interestRate').value);
	if (document.getElementById('interestRate').value <= 0) { document.getElementById('interestRate').value = 0.25; }
	document.getElementById('mortgageTerm').value = StripNonNumeric(document.getElementById('mortgageTerm').value);
	if (document.getElementById('mortgageTerm').value <= 0) { document.getElementById('mortgageTerm').value = 1; }
	document.getElementById('sellingPrice').value = CurrencyFormat(StripNonNumeric(document.getElementById('sellingPrice').value));
	document.getElementById('downPayment').value = CurrencyFormat(StripNonNumeric(document.getElementById('downPayment').value));
	
	if (document.getElementById('propertyTax')) {
		document.getElementById('propertyTax').value = CurrencyFormat(StripNonNumeric(document.getElementById('propertyTax').value));
		document.getElementById('exemption').value = CurrencyFormat(StripNonNumeric(document.getElementById('exemption').value));
		document.getElementById('insurance').value = CurrencyFormat(StripNonNumeric(document.getElementById('insurance').value));
		document.getElementById('pmi').value = CurrencyFormat(StripNonNumeric(document.getElementById('pmi').value));
	}

	var SellingPrice = StripNonNumeric(document.getElementById('sellingPrice').value);
	var DownPayment = StripNonNumeric(document.getElementById('downPayment').value);
	var PresentValue = SellingPrice - DownPayment;
	var InterestRate = StripNonNumeric(document.getElementById('interestRate').value) / 100 / 12;
	var PaymentPeriods = StripNonNumeric(document.getElementById('mortgageTerm').value * 12);
	var LoanPayment = (PresentValue * Math.pow(1 + InterestRate, PaymentPeriods) * InterestRate) / (Math.pow(1 + InterestRate, PaymentPeriods) - 1);
	if (document.getElementById('propertyTax')) {
		var TaxPayment = (StripNonNumeric(document.getElementById('propertyTax').value) - StripNonNumericAll(document.getElementById('exemption').value)) / 12;
		var InsurancePayment = StripNonNumeric(document.getElementById('insurance').value) / 12;
		var PMIPayment = StripNonNumeric(document.getElementById('pmi').value) / 12;
		
		document.getElementById('monthlyMortgagePayment').innerHTML = CurrencyFormat(LoanPayment);
		document.getElementById('monthlyTaxPayment').innerHTML = CurrencyFormat(TaxPayment);
		document.getElementById('monthlyInsurancePayment').innerHTML = CurrencyFormat(InsurancePayment);
		document.getElementById('monthlyPMIPayment').innerHTML = CurrencyFormat(PMIPayment);
	} else {
		var TaxPayment = 0;
		var InsurancePayment = 0;
		var PMIPayment = StripNonNumeric(document.getElementById('pmi').value) / 12;
	}
	var MonthlyPayment = LoanPayment + TaxPayment + InsurancePayment + PMIPayment;
	
	if (document.getElementById('propertyTax')) {
		document.getElementById('totalMonthlyPayment').innerHTML = CurrencyFormat(MonthlyPayment);
	} else {
		document.getElementById('totalMonthlyPayment').innerHTML = '$' + CurrencyFormat(MonthlyPayment) + '*';
	}
}

function Toggle(id) {	
	if (eval('document.getElementById("' + id + 'PopUp").style.display == "block"')) {
		eval('document.getElementById("' + id + 'PopUp").style.display = "none";');
		eval('document.getElementById("' + id + 'Button").style.color = "Black";');
		eval('document.getElementById("' + id + 'Button").style.fontWeight = "normal";');
		eval('document.getElementById("' + id + 'Button").style.backgroundColor = "";');
		eval('document.getElementById("' + id + 'Button").value = "?";');
	} else {
		eval('document.getElementById("' + id + 'PopUp").style.display = "block";');
		eval('document.getElementById("' + id + 'Button").style.color = "White";');
		eval('document.getElementById("' + id + 'Button").style.fontWeight = "bold";');
		eval('document.getElementById("' + id + 'Button").style.backgroundColor = "#C33";');
		eval('document.getElementById("' + id + 'Button").value = "X";');
	}
}

function CleanUp() {
	document.getElementById('price').value = StripNonNumericAll(document.getElementById('price').value);
}

function StripNonNumeric(strSource) {
	strReturn = new String(strSource);
	strReturn = strReturn.replace(/[^0-9\.-]/g, '');
	return strReturn;
}

function StripNonNumericAll(strSource) {
	strReturn = new String(strSource);
	strReturn = strReturn.replace(/[^0-9\.]/g, '');
	return strReturn;
}

function CurrencyFormat(dblValue) {	
	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = '0' + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + '.' + strCents);
}

function GoToAdvanced() {
	var sellingPrice = StripNonNumeric(document.getElementById('sellingPrice').value);
	var downPayment = StripNonNumeric(document.getElementById('downPayment').value);
	var interestRate = StripNonNumeric(document.getElementById('interestRate').value);
	var mortgageTerm = StripNonNumeric(document.getElementById('mortgageTerm').value);
	window.location.href = path + 'property/calculator/?price=' + sellingPrice + '&downPayment=' + downPayment + '&interestRate=' + interestRate + '&mortgageTerm=' + mortgageTerm;	
}
