// Affordability Calculator
// HousingPoint 2.0
// Jaie Claudet, May 2011

// This script takes income data entered by the user and outputs recommendations on monthly expenditures for rent and utilities.

function calculator(){
	// get SysAdmin values
	var recPerc = document.getElementById('recPerc').value;
		recPerc = eval(recPerc); //convert to a number
		recPerc = recPerc/100; // convert to a percentage
	var utilities = document.getElementById('utilities').value;
		utilities = eval(utilities); //convert to a number

	// get income values
	var income1 = document.getElementById('income1').value;
		income1 = eval(income1); // convert to a number
	var income2 = document.getElementById('income2').value;
		income2 = eval(income2); // convert to a number
	var income3 = document.getElementById('income3').value;
		income3 = eval(income3); // convert to a number
	var income4 = document.getElementById('income4').value;
		income4 = eval(income4); // convert to a number
	var income5 = document.getElementById('income5').value;
		income5 = eval(income5); // convert to a number		
	var income6 = document.getElementById('income6').value;
		income6 = eval(income6); // convert to a number		
	var income7 = document.getElementById('income7').value;
		income7 = eval(income7); // convert to a number		
	var income8 = document.getElementById('income8').value;
		income8 = eval(income8); // convert to a number		
	var income9 = document.getElementById('income9').value;
		income9 = eval(income9); // convert to a number

	// validate that income entered matches number of earners
	var earners = document.getElementById('earners').value;
	earners=eval(earners); // convert to a number
	
		switch (earners){
			case 1:
				if (income1!=null) {
					break;
				}
			case 2:
				if (income1!=null && income2!=null) {
					break;
				}			
			case 3:
				if (income1!=null && income2!=null && income3!=null) {
					break;
				}
			case 4:
				if (income1!=null && income2!=null && income3!=null && income4!=null) {
					break;
				}
			case 5:
				if (income1!=null && income2!=null && income3!=null && income4!=null && income5!=null) {
					break;
				}
			case 6:
				if (income1!=null && income2!=null && income3!=null && income4!=null && income5!=null && income6!=null) {
					break;
				}			
			case 7:
				if (income1!=null && income2!=null && income3!=null && income4!=null && income5!=null && income6!=null && income7!=null) {
					break;
				}			
			case 8:
				if (income1!=null && income2!=null && income3!=null && income4!=null && income5!=null && income6!=null && income7!=null && income8!=null) {
					break;
				}			
			case 9:
				if (income1!=null && income2!=null && income3!=null && income4!=null && income5!=null && income6!=null && income7!=null && income8!=null && income9!=null) {
					break;
				}						
			default:
				document.getElementById('errorEmptyField').style.display="block";
				return false;			
		}

	// convert income entered to monthly value
		//get variables
		var freq1 = document.getElementById('freq1').value;
		var freq2 = document.getElementById('freq2').value;
		var freq3 = document.getElementById('freq3').value;
		var freq4 = document.getElementById('freq4').value;
		var freq5 = document.getElementById('freq5').value;
		var freq6 = document.getElementById('freq6').value;
		var freq7 = document.getElementById('freq7').value;
		var freq8 = document.getElementById('freq8').value;
		var freq9 = document.getElementById('freq9').value;

		// income1
		if (freq1=="year"){
			income1=income1/12; // if frequency is "per year" divide by twelve
		}
		if (freq1=="twoWeeks"){
			income1=income1*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq1=="week"){
			income1=income1*4; // if frequency is "per week" multiply by four
		}

		// income2
		if (freq2=="year"){
			income2=income2/12; // if frequency is "per year" divide by twelve
		}
		if (freq2=="twoWeeks"){
			income2=income2*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq2=="week"){
			income2=income2*4; // if frequency is "per week" multiply by four
		}

		// income3
		if (freq3=="year"){
			income3=income3/12; // if frequency is "per year" divide by twelve
		}
		if (freq3=="twoWeeks"){
			income3=income3*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq3=="week"){
			income3=income3*4; // if frequency is "per week" multiply by four
		}

		// income4
		if (freq4=="year"){
			income4=income4/12; // if frequency is "per year" divide by twelve
		}
		if (freq4=="twoWeeks"){
			income4=income4*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq4=="week"){
			income4=income4*4; // if frequency is "per week" multiply by four
		}

		// income5
		if (freq5=="year"){
			income5=income5/12; // if frequency is "per year" divide by twelve
		}
		if (freq5=="twoWeeks"){
			income5=income5*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq5=="week"){
			income5=income5*4; // if frequency is "per week" multiply by four
		}

		// income6
		if (freq6=="year"){
			income6=income6/12; // if frequency is "per year" divide by twelve
		}
		if (freq6=="twoWeeks"){
			income6=income6*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq6=="week"){
			income6=income6*4; // if frequency is "per week" multiply by four
		}

		// income7
		if (freq7=="year"){
			income7=income7/12; // if frequency is "per year" divide by twelve
		}
		if (freq7=="twoWeeks"){
			income7=income7*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq7=="week"){
			income7=income7*4; // if frequency is "per week" multiply by four
		}

		// income8
		if (freq8=="year"){
			income8=income8/12; // if frequency is "per year" divide by twelve
		}
		if (freq8=="twoWeeks"){
			income8=income8*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq8=="week"){
			income8=income8*4; // if frequency is "per week" multiply by four
		}

		// income9
		if (freq9=="year"){
			income9=income9/12; // if frequency is "per year" divide by twelve
		}
		if (freq9=="twoWeeks"){
			income9=income9*2; // if frequency is "every two weeks" multiply by two
		}
		if (freq9=="week"){
			income9=income9*4; // if frequency is "per week" multiply by four
		}

	// calculate total household income
		switch (earners){
			case 1:
				totalIncome = income1;
				break;
			case 2:
				totalIncome = income1+income2;
				break;			
			case 3:
				totalIncome = income1+income2+income3;
				break;
			case 4:
				totalIncome = income1+income2+income3+income4;
				break;			
			case 5:
				totalIncome = income1+income2+income3+income4+income5;
				break;			
			case 6:
				totalIncome = income1+income2+income3+income4+income5+income6;
				break;			
			case 7:
				totalIncome = income1+income2+income3+income4+income5+income6+income7;
				break;			
			case 8:
				totalIncome = income1+income2+income3+income4+income5+income6+income7+income8;
				break;			
			case 9:
				totalIncome = income1+income2+income3+income4+income5+income6+income7+income8+income9;
				break;
			default:
				break;
		}
	
	totalIncome=Math.round(totalIncome); // round to whole number
	
	// calculate recommended maximum monthly expenditure for rent and utilities
	withUtilities = totalIncome*recPerc;
	withUtilities=Math.round(withUtilities); // round to whole number	
	
	// calculate recommended maximum monthly expenses for rent alone
	maxRent = withUtilities-utilities;
	maxRent=Math.round(maxRent); // round to whole number
	
	//output results
	document.getElementById('results').style.display="block"; // reveal Results section
	document.getElementById("totalIncome").innerHTML="Total monthly income is: <strong>$"+totalIncome+"</strong>";
	document.getElementById("withUtilities").innerHTML="Recommended maximum monthly expense for <span>rent and utilities</span> is: <strong>$"+withUtilities+"</strong>";
	document.getElementById("maxRent").innerHTML="Recommended maximum monthly expense for rent only is: <strong>$"+maxRent+"</strong>";

} // close calculator


