/* debt calculator */

function validateForm(oForm)
{
	var iErrors = 0;
	sTotalDebt = oForm.debt.value;
	sIncome = oForm.income.value;
	//strip out dollar signs, commas, and spaces
	sTotalDebt = sTotalDebt.replace(/[\$,\s]/g, "");
	sIncome = sIncome.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 (sTotalDebt == "") {
		alert("Please enter a positive number into the Total Debt field.");
		iErrors++;
	} else if (isNaN(sTotalDebt) || regex.test(sTotalDebt)) {
		alert("You have entered a non-numeric or negative value. Please enter a positive number into the Total Debt field.");
		iErrors++;
	} 
	if (iErrors == 0) {
		oForm.debt.value = sTotalDebt;
		oForm.income.value = sIncome;
		calculateDebt(sIncome, sTotalDebt);
	}
}

function calculateDebt(iIncome, iDebt)
{
	var iOwed = iDebt * .65;
	var iFee = (iDebt - iOwed) * .25;
	var iYouPay = iOwed + iFee;
	var iPayment = (iOwed / 34) + (iFee / 34) + 29.95;
	//var iRatio = Math.round(100 * (iDebt / (iIncome * 12) * 100)) / 100;
	var iWithout = iDebt * 3;
	var iCCCS = iDebt * 1.4;
	
	//var oIncome = document.getElementById("monthlyIncome");
	//var oTotalDebt = document.getElementById("totalDebt");
	var oOwed = document.getElementById("owedDebt");
	//var oFee = document.getElementById("fee");
	//var oYouPay = document.getElementById("youPay");
	var oPayment = document.getElementById("payment");
	//var oRatio = document.getElementById("ratio");
	var oWithout = document.getElementById("withoutHelp");
	var oCCCS = document.getElementById("cccs");
	
	//oIncome.childNodes[0].nodeValue = formatCurrency(iIncome);
	//oTotalDebt.childNodes[0].nodeValue = formatCurrency(iDebt);
	oOwed.childNodes[0].nodeValue = formatCurrency(iOwed);
	//oFee.childNodes[0].nodeValue = formatCurrency(iFee);
	//oYouPay.childNodes[0].nodeValue = formatCurrency(iYouPay);
	oPayment.childNodes[0].nodeValue = formatCurrency(iPayment);
	//oRatio.childNodes[0].nodeValue = padCents(iRatio) + "%";
	oWithout.childNodes[0].nodeValue = formatCurrency(iWithout);
	oCCCS.childNodes[0].nodeValue = formatCurrency(iCCCS);
}

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 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;
	}
}