function mortgagecalculation() {
	theform = document.mortgageform;
	r = removecommas(theform.interest.value) / 100 / 12;
	n = removecommas(theform.term.value) * 12;
	p = removecommas(theform.mortgage.value);
	c = r / (1 - (1 / Math.pow(1 + r, n))) * p;
	document.getElementById("payment").innerHTML = valuefix(c);
	document.getElementById("totalpayments").innerHTML = valuefix(c * n);
	document.getElementById("totalinterest").innerHTML = valuefix(c * n - p);
}

function loancalculation() {
	theform = document.loanform;
	r = removecommas(theform.interest.value) / 100 / 12;
	n = removecommas(theform.term.value) * 12;
	p = removecommas(theform.loan.value);
	c = r / (1 - (1 / Math.pow(1 + r, n))) * p;
	document.getElementById("payment").innerHTML = valuefix(c);
	document.getElementById("totalpayments").innerHTML = valuefix(c * n);
	document.getElementById("totalinterest").innerHTML = valuefix(c * n - p);
}

function valuefix(value) {
	if (!isFinite(value)) {
		return "";
	} else {
		return(Math.round(value * 100) / 100);
	}
}

function interestcalculation() {
	theform = document.interestform;
	r = removecommas(theform.interest.value) / 100;
	n = removecommas(theform.years.value);
	prin = removecommas(theform.principal.value);
	p = Math.pow(1 + r, n);
	document.getElementById("compoundname").innerHTML = "Balance after " + n + " Years:";
	document.getElementById("compoundtotal").innerHTML = valuefix(p * prin);
	document.getElementById("compoundearning").innerHTML = valuefix((p - 1) * prin);
	document.getElementById("simplename").innerHTML = "Principal after " + n + " Years:";
	document.getElementById("simpletotal").innerHTML = valuefix((1 + r * n) * prin);
	document.getElementById("simpleearning").innerHTML = valuefix(r * n * prin);
}

function removecommas(value) {
	valuestring = value.toString();
	valuestring = valuestring.replace(/,/g, "");
	return(parseFloat(valuestring));
}

function checkdata() {
	theform = document.savingsform;
	p = removecommas(theform.principal.value);
	m = removecommas(theform.monthlydeposit.value);
	y = removecommas(theform.years.value);
	r = removecommas(theform.interest.value);
	if (!isFinite(p) || p < 0) {
		alert("The Initial Principal you entered is invalid, please check and try again.");
		return false;
	}
	if (!isFinite(m) || m < 0) {
		alert("The Monthly Deposit you entered is invalid, please check and try again.");
		return false;
	}
	if (y - Math.floor(y) != 0) {
		alert("The Number of Years you entered is not an integer, please check and try again.");
		return false;
	}
	if (!isFinite(r) || r < 0) {
		alert("The Annual Interest Rate you entered is invalid, please check and try again.");
		return false;
	}
	return true;
}

function checkcurrencydata() {
	theform = document.currencyform;
	a = removecommas(theform.amount.value);
	if (!isFinite(a) || a < 0) {
		alert("The amount you entered is invalid, please check and try again.");
		return false;
	}
}

function checkannuitydata() {
	count = 0;
	theform = document.annuityform;
	p = theform.principal.value;
	g = theform.growth.value;
	y = theform.years.value;
	w = theform.withdrawal.value;
	p2 = removecommas(p);
	g2 = removecommas(g);
	y2 = removecommas(y);
	w2 = removecommas(w);
	if ((g == "") || (g == null)) {
		alert("The Annual Growth Rate can not be left blank, please check and try again.");
		return false;
	}
	if ((p == "") || (p == null)) {
		count = count + 1;
	}
	if ((y == "") || (y == null)) {
		count = count + 1;
	}
	if ((w == "") || (w == null)) {
		count = count + 1;
	}
	if (count != 1) {
		alert("Only one value (other than Annual Growth Rate) can be left blank, please check and try again.");
		return false;
	}
	if (!isFinite(p2) || p2 < 0) {
		if ((p != "") && (p != null)) {
			alert("The Starting Principal you entered is invalid, please check and try again.");
			return false;
		}
	}
	if (!isFinite(g2) || g2 < 0) {
		alert("The Annual Growth Rate you entered is invalid, please check and try again.");
		return false;
	}
	if (y2 - Math.floor(y2) != 0) {
		if ((y != "") && (y != null)) {
			alert("The Length of Annuity you entered is not an integer, please check and try again.");
			return false;
		}
	}
	if (!isFinite(w2) || w2 < 0) {
		if ((w != "") && (w != null)) {
			alert("The Withdrawal Amount you entered is invalid, please check and try again.");
			return false;
		}
	}
	return true;
}

function checkminimumdata() {
	theform = document.minimumform;
	b = removecommas(theform.balance.value);
	a = removecommas(theform.apr.value);
	if (!isFinite(b) || b < 0) {
		alert("The Current Balance you entered is invalid, please check and try again.");
		return false;
	}
	if (!isFinite(a) || a < 0) {
		alert("The Interest Rate (APR) you entered is invalid, please check and try again.");
		return false;
	}
	return true;
}

function checkpayoffdata() {
	countm = 0;
	countp = 0;
	theform = document.payoffform;
	b = removecommas(theform.balance.value);
	a = removecommas(theform.apr.value);
	m = theform.months.value;
	p = theform.monthlypayment.value;
	m2 = removecommas(m);
	p2 = removecommas(p);
	if (!isFinite(b) || b <= 0) {
		alert("The Current Balance you entered is invalid, please check and try again.");
		return false;
	}
	if (!isFinite(a) || a < 0) {
		alert("The Interest Rate (APR) you entered is invalid, please check and try again.");
		return false;
	}
	if ((m == "") || (m == null)) {
		countm = 1;
	}
	if ((p == "") || (p == null)) {
		countp = 1;
	}
	if ((countm == 1) && (countp == 1)) {
		alert("Either Desired Months to Pay Off or Desired Monthly Payment needs to be entered a value, please check and try again.");
		return false;
	}
	if (countm == 1) {
		if (!isFinite(p2) || (p2 < 0)) {
			alert("The Desired Monthly Payment you entered is invalid, please check and try again.");
			return false;
		}
	} else {
		if ((m2 - Math.floor(m2) != 0) || (m2 < 0)) {
			alert("The Desired Months to Pay Off you entered is not a positive integer, please check and try again.");
			return false;
		}
	}
	return true;
}

function emptymonthlypayment() {
	document.payoffform.monthlypayment.value = "";
}

function emptymonths() {
	document.payoffform.months.value = "";
}
function currencyfrom(value1,value2) {
	theform = document.currencyform;
	unitvalue1 = theform.from.options[theform.from.selectedIndex].value;
	if (unitvalue1 == value1)	{
		theform.to.value = value2;
	} else {
		theform.to.value = value1;
	}
}
function currencyto(value1,value2) {
	theform = document.currencyform;
	unitvalue2 = theform.to.options[theform.from.selectedIndex].value;
	if (unitvalue2 == value1)	{
		theform.from.value = value2;
	} else {
		theform.from.value = value1;
	}
}

