/* debt calculator */

function validateForm2(oForm)
{
	var iErrors = 0;
	var iMonthlyIncome = 0;
	var iMonthlyPayments = 0;
	
	//for (i = 0, i <= 1, i++) {
		//iDebtSum += oForm.elements[i].value;
	//}
	// Calculate Monthly Income
	var i = 0;
	while (i <= 23) {
		iMonthlyIncome += (oForm.elements[i].value / 1);
		i++;
	}
	// Calculate Monthly Payments
	i = 24;
	while (i <= 36) {
		iMonthlyPayments += (oForm.elements[i].value / 1);
		i++;
	}
	
	//strip out dollar signs, commas, and spaces
	//sSalaryYou = sSalaryYou.replace(/[\$,\s]/g, "");
	//sSalarySpouse = sSalarySpouse.replace(/[\$,\s]/g, "");
	var regex = /-/;
	//if (sIncome == "") {
		//alert("Please enter a positive number into the Monthly Income field.");
		//iErrors++;
	//} else if (isNaN(sIncome) || regex.test(sIncome)) {
		//alert("You have entered a non-numeric or negative value. Please enter a positive number into the Monthly Income field.");
		//iErrors++;
	//} 
	if (iMonthlyIncome == "") {
		alert("Please enter a positive number.");
		iErrors++;
	} else if (isNaN(iMonthlyIncome) || regex.test(iMonthlyIncome)) {
		alert("You have entered a non-numeric or negative value. Please enter a positive number.");
		iErrors++;
	} 
	if (iErrors == 0) {
		calculateDebt2(iMonthlyIncome, iMonthlyPayments);
	}
}

function calculateDebt2(iMonthlyIncome, iMonthlyPayments)
{
	var iMonthlyIncome = iMonthlyIncome;
	var iPayment = iMonthlyPayments;
	var iDebtToIncome = iMonthlyPayments / iMonthlyIncome;
	
	var oMonthlyIncome = document.getElementById("MonthlyIncome");
	var oMonthlyIncome2 = document.getElementById("MonthlyIncome2");
	var oPayment = document.getElementById("MonthlyPayments");
	var oPayment2 = document.getElementById("MonthlyPayments2");
	var oDebtToIncome = document.getElementById("DebtToIncome");
	
	oMonthlyIncome.childNodes[0].nodeValue = formatCurrency(iMonthlyIncome);
	oMonthlyIncome2.childNodes[0].nodeValue = formatCurrency(iMonthlyIncome);
	oPayment.childNodes[0].nodeValue = formatCurrency(iPayment);
	oPayment2.childNodes[0].nodeValue = formatCurrency(iPayment);
	oDebtToIncome.childNodes[0].nodeValue = formatPercent(iDebtToIncome);
}

function formatCurrency(iTotal)
{
	function addCommas(sDollars)
	{
		var regex = /(\d+)(\d{3})/;
		while (regex.test(sDollars)) {
			sDollars = sDollars.replace(regex, '$1' + "," + '$2');
		}
		return sDollars;
	}
	
	iTotal = Math.round(iTotal * 100) / 100;
	iTotal = padCents(iTotal);		
	var sTotal = iTotal.toString();
	var aTotal = sTotal.split(".");
	aTotal[0] = addCommas(aTotal[0]);
	
	return "$" + aTotal[0] + "." + aTotal[1];	
}

function formatPercent(iTotal)
{
	iTotal = Math.round(iTotal * 100);	
	var sTotal = iTotal.toString();
	var aTotal = sTotal.split(".");
	
	return aTotal[0] + "%";	
}

function padCents(iDollars)
{
	if (iDollars.toFixed) {
		iDollars = iDollars.toFixed(2);
	} else {
		var regex = /\./;
		if (regex.test(iDollars)) {
			regex = /\.[1-9]{1}$/;
			//append 0 to single decimal point that is non-zero
			if (regex.test(iDollars)) {
				iDollars = iDollars + "0";
			}
		} else {			
			iDollars = iDollars + ".00";
		}
	}
	return iDollars;
}

function handleEnter (oForm, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		validateForm(oForm);
		return false;
	} else {
		return true;
	}
}

/* book, cigarette, coffee forms */

function checkForm(oForm, sPrefix)
{
	var iErrors = 0;
	var sPriceName = sPrefix + "Price";
	var sQuantityName = sPrefix + "Quantity";
	sPrice = oForm[sPriceName].value;
	sQuantity = oForm[sQuantityName].value;
	//strip out dollar signs, commas, and spaces
	sPrice = sPrice.replace(/[\$,\s]/g, "");
	sQuantity = sQuantity.replace(/[\$,\s]/g, "");
	if (sPrefix == "book") {
		sPriceAlert = "How Much Do You Spend on a New Book";
		sQuantityAlert = "How Many Books Do You Buy a Month";
	} else if (sPrefix == "cigarette") {
		sPriceAlert = "How Much Do You Spend on a Pack of Cigarettes";
		sQuantityAlert = "How Many Packs Do You Smoke a Week";
	} else {
		sPriceAlert = "How Much Do You Spend on a Cup of Coffee";
		sQuantityAlert = "How Many Cups a Week Do You Purchase";
	}
	var regex = /-/;
	if (sPrice == "") {
		alert("Please enter a positive number into the " + sPriceAlert + " field.");
		iErrors++;
	} else if (isNaN(sPrice) || regex.test(sPrice)) {
		alert("You have entered a non-numeric or negative value. Please enter a positive number into the " + sPriceAlert + " field.");
		iErrors++;
	} 
	if (sQuantity == "") {
		alert("Please enter a positive number into the " + sQuantityAlert + " field.");
		iErrors++;
	} else if (isNaN(sQuantity) || regex.test(sQuantity)) {
		alert("You have entered a non-numeric or negative value. Please enter a positive number into the " + sQuantityAlert + " field.");
		iErrors++;
	} 
	if (iErrors == 0) {
		oForm[sPriceName].value = sPrice;
		oForm[sQuantityName].value = sQuantity;
		calculateSpending(sPrice, sQuantity, sPrefix);
	}
}

function calculateSpending (iPrice, iQuantity, sPrefix) {
	if (sPrefix != "book") {
		var iWeekly = iPrice * iQuantity;
		var iMonthly = iWeekly * (31 / 7);
		var iAnnually = iWeekly * (365 / 7);
		var oWeekly = document.getElementById(sPrefix + "Weekly");
	} else {
		var iMonthly = iPrice * iQuantity;
		var iAnnually = iMonthly * 12;
	}
	
	var oMonthly = document.getElementById(sPrefix + "Monthly");
	var oAnnually = document.getElementById(sPrefix + "Annually");
	
	if (sPrefix != "book") {
		oWeekly.childNodes[0].nodeValue = formatCurrency(iWeekly);
	}
	oMonthly.childNodes[0].nodeValue = formatCurrency(iMonthly);
	oAnnually.childNodes[0].nodeValue = formatCurrency(iAnnually);
}

function handleEnterAlt (oForm, prefix, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		checkForm(oForm, prefix);
		return false;
	} else {
		return true;
	}
}
