﻿function ConvertCurrency(formRef) {
	if ((formRef.Amount.value=="") || (isNaN(formRef.Amount.value))) {alert("Please enter a valid amount"); return false}
	if (formRef.FromCurrency.value==formRef.ToCurrency.value) {alert("Please select two different currencies"); return false}
	alert((formRef.ToCurrency.value=="GBP" ? "£" : "€") + formatCurrency(formRef.Amount.value * (formRef.ToCurrency.value=="GBP" ? EURtoGBP : GBPtoEUR)));
	}

function CalculateMortgage(formRef) {
	if (formRef.MortgagePrinciple.value!="") {var GotPrinciple=true} else {var GotPrinciple=false}
	if (formRef.MonthlyPayment.value!="") {var GotMonthly=true} else {var GotMonthly=false}
	if ((GotPrinciple==true && GotMonthly==true) || (GotPrinciple==false && GotMonthly==false)) {alert("Please enter EITHER a Mortgage Principle OR a Monthly Payment"); return false}
	if (GotPrinciple && (isNaN(formRef.MortgagePrinciple.value))) {alert("Please enter a valid Mortgage Principle"); return false} else {var decPrinciple=parseFloat(formRef.MortgagePrinciple.value)}
	if (GotMonthly && (isNaN(formRef.MonthlyPayment.value))) {alert("Please enter a valid Monthly Payment"); return false} else {var decMonthly=parseFloat(formRef.MonthlyPayment.value)}
	if ((formRef.Term.value=="") || (isNaN(formRef.Term.value))) {alert("Please enter a valid Term"); return false} else {var decTerm=parseFloat(formRef.Term.value)}
	if ((formRef.InterestRate.value=="") || (isNaN(formRef.InterestRate.value))) {alert("Please enter a valid Interest Rate"); return false} else {var decRate=formRef.InterestRate.value / 1200}	
	alert( (GotPrinciple ? "Monthly Payment = €" + formatCurrency((decRate * decPrinciple) / (1 - (Math.pow((1 + decRate), -(decTerm * 12))))) : "Mortgage Principle = €" + formatCurrency(decMonthly * (1 - (Math.pow((1 + decRate), -(decTerm * 12)))) / decRate) ))
	}

function formatCurrency(decAmount) {
	var strAmount=(Math.round(decAmount * 100) / 100).toString();
	if (strAmount.lastIndexOf(".")==-1) {strAmount+=".00"}
	else if ((strAmount.length - strAmount.lastIndexOf("."))==2) {strAmount+="0"}
	return strAmount;
	}

