// JavaScript Document

//Function checks all values and alerts your message. 

function debtCalc () {

	// VARIABLES FOR DEBT CALCULATION
	
	var mortgagePymnt,equityPymnt,creditMedBillPymnt,carPmyt,personalLoanPymt,studentLoanPymt,alimonySupportPymnt,otherPymt,pageValues,payments,error,paymentTotal;
	
	//make our root arrays, we talked about multidimensional which I forgot you can't do in one sweep with js so its 2 steps
	mortgagePymnt = new Array(
		document.getElementById('mortgagePymt').value,
		"Please enter a positive numerical value in the Monthly Mortgage Payment field."
	);
	equityPymt = new Array(
		document.getElementById('equityPymt').value,
		"Please enter a positive numerical value in the Equity Loan Payment field."
	);
	creditMedBillPymt = new Array(
		document.getElementById('creditMedBillPymt').value,
		"Please enter a positive numerical value in the Credit Card/Medical Bill Payment field."
	);
	carPymt = new Array(
		document.getElementById('carPymt').value,
		"Please enter a positive numerical value in the Car Loan/Lease Obligation field."
	);
	personalLoanPymt = new Array(
		document.getElementById('personalLoanPymt').value,
		"Please enter a positive numerical value in the Personal Loan Payment field."
	);
	studentLoanPymt = new Array(
		document.getElementById('studentLoanPymt').value,
		"Please enter a positive numerical value in the Student Loan Payment field."
	);
	alimonySupportPymnt = new Array(
		document.getElementById('alimonySupportPymnt').value,
		"Please enter a positive numerical value in Alimony/Child Support Payment field."
	);
	otherPymt = new Array(
		document.getElementById('otherPymt').value,
		"Please enter a positive numerical value in the Other Montly Payments field."
	);
	
	//make an array of the arrays - step 2
	pageValues = new Array(mortgagePymnt, equityPymt, creditMedBillPymt, carPymt, personalLoanPymt, studentLoanPymt, alimonySupportPymnt, otherPymt);
	
	//Create an empty error container
	error = "";
	//this is why the math wasn't working for you, we had to tell JS that paymentTotal was a number
	paymentTotal = 0;
	
	//Loop through the array and do all your math
	for(payments=0; payments < pageValues.length; payments++){
		if(isNaN(pageValues[payments][0])){
			// \n is new line. Javascript alerts accept this to put each entry on its own line. :) 
			error = error + pageValues[payments][1] +  "\n";
		}else{
			//get rid of those nasty NaN's by making everything blank a number 0
			if(pageValues[payments][0] == ''){
				pageValues[payments][0] = 0;
			}else{
				//make sure we keep the number floating for decimals
				pageValues[payments][0] = parseFloat(pageValues[payments][0]);
			}
			//Do your math. 
			paymentTotal = paymentTotal + pageValues[payments][0];
			
			var paymentTotalResult=Math.round(paymentTotal*100)/100 
		}
	}
	
	//we set the error as an compilation of errors, so lets display them all at once. :)
	if(error != ""){
		//alert the erros if there are any
		alert(error);
	}else{
		//output to the div. 
		
		var outputPymt = document.getElementById('outputPymt');

		outputPymt.innerHTML = "Monthly payment amount is $" + paymentTotalResult;
		document.getElementById('js_debt_total').value = paymentTotal;	
	}
	return false;

}





function incomeCalc () {

	var income,bonus,otherIncome,alimonySupportIncome,incomeValues,incomeItems,incomeError,incomeTotal;
	
	income = new Array(
		document.getElementById('income').value,
		"Please enter a positive numerical value in the Monthly Income field."
	);
	bonus = new Array(
		document.getElementById('bonus').value,
		"Please enter a positive numerical value in the Monthly Bonus field."
	);
	otherIncome = new Array(
		document.getElementById('otherIncome').value,
		"Please enter a positive numerical value in the Other Monthly Income field."
	);
	alimonySupportIncome = new Array(
		document.getElementById('alimonySupportIncome').value,
		"Please enter a positive numerical value in the Monthly Alimony and Child Support field."
	);
	
	
	incomeValues = new Array(income, bonus, otherIncome, alimonySupportIncome);
	
	incomeError = "";
	incomeTotal = 0;
	
	
	for(incomeItems=0; incomeItems < incomeValues.length; incomeItems++){
		if(isNaN(incomeValues[incomeItems][0])){
			incomeError = incomeError + incomeValues[incomeItems][1] +  "\n";
		}else{
			if(incomeValues[incomeItems][0] == ''){
				incomeValues[incomeItems][0] = 0;
			}else{
				incomeValues[incomeItems][0] = parseFloat(incomeValues[incomeItems][0]);
			}
			incomeTotal = incomeTotal + incomeValues[incomeItems][0];
			
			var incomeTotalResult=Math.round(incomeTotal*100)/100 
		}
	}
	
	if(incomeError != ""){
		//alert the erros if there are any
		alert(incomeError);
	}else{
		//output to the div. 
		var outputIncome = document.getElementById('outputIncome');
		outputIncome.innerHTML = "Monthly payment amount is $" + incomeTotalResult;
		document.getElementById('js_income_total').value = incomeTotal;	
	}
	return false;

}


function debtIncomeRatio () {
	
	//var paymentTotal = 1;
	//var incomeTotal = 1;
	var paymentTotal,incomeTotal;
	var ratioFinal = 0;
	
	paymentTotal = document.getElementById('js_debt_total').value;
	incomeTotal = document.getElementById('js_income_total').value;
	
	if(paymentTotal == "0"){
		paymentTotal = 0;
	}else{
		paymentTotal = parseInt(paymentTotal);
	}
	
	if(incomeTotal == "0"){
		incomeTotal = 0;
	}else{
		incomeTotal = parseInt(incomeTotal);
	}
	
	ratioFinal = paymentTotal/incomeTotal*100;
	var ratioFinalResult=Math.round(ratioFinal*100)/100 
		
	var outputRatio = document.getElementById('outputRatio');
	outputRatio.innerHTML = "Debt-to-Income Ratio is " + ratioFinalResult + "%";
	
	
	return false;

}

