// Net eligibility and allotment calculations
var FSCalc = function() {
	this.refresh();
};

// Maximum allotment table
FSCalc.allotTable = [176, 323, 463, 588, 698, 838, 926, 1058];
FSCalc.allotIncrement = 132; // for HH > 8 members

FSCalc.prototype.getMaxAllotment = function() {
	var limit = 0;
	if(this.hhSize <= FSCalc.allotTable.length) 
		limit = FSCalc.allotTable[this.hhSize - 1];
	else
		limit = FSCalc.allotTable[FSCalc.allotTable.length - 1] + ((this.hhSize - FSCalc.allotTable.length) * increment);
	return limit;
};

FSCalc.prototype.refresh = function() {
	// Get values from form
	// TODO make this intelligent
	this.hhSize = $('#hhSize').val() - 0;
	this.hasDisabled = ($("input:radio[@name='hasDisabled']:checked").val() == 'yes');
	this.monthlyEarned = $('#monthlyEarned').val() - 0;
	this.monthlyUnearned = $('#monthlyUnearned').val() - 0;
	this.numChildren = $('#numChildren').val() - 0;
	this.numDependents = $('#numDependents').val() - 0;
	this.monthlyDaycare = $('#monthlyDaycare').val() - 0;
	this.monthlyChildsupport = $('#monthlyChildsupport').val() - 0;
	this.monthlyDisabled = $('#monthlyDisabled').val() - 0;
	this.isHomeless = ($("input:radio[@name='isHomeless']:checked").val() == 'yes');
	this.housingCosts = $('#housingCosts').val() - 0;
	this.payHeat = ($("input:radio[@name='payHeat']:checked").val() == 'yes');
	this.payUtils = ($("input:radio[@name='payUtils']:checked").val() == 'yes');
	this.payPhone = ($("input:radio[@name='payPhone']:checked").val() == 'yes');

	// Get gross eligibility
	this.isGrossEligible = this.grossIncomeEligible();

	// Get standard deduction
	this.stdDeduction = this.calcStandardDeduction();

	// Get dependent care deduction
	this.daycareDeduction = this.calcDaycareDeduction();

	// child support deduction
	this.childsupportDeduction = this.monthlyChildsupport;

	// Excess (> $35) senior medical expenses
	this.medDeduction = (this.monthlyDisabled > 35)?this.monthlyDisabled - 35:this.monthlyDisabled;
	log("Medical expenses: "+ this.monthlyDisabled);
	
	// Unverified shelter expenses if homeless
	this.homelessDeduction = this.isHomeless?143:0;

  log("Monthly earned: "+ this.monthlyEarned);
	log("Monthly earned (after 20% deduction): "+ (this.monthlyEarned - (this.monthlyEarned * 0.20)));
 	log("Monthly unearned: "+ this.monthlyUnearned);
  log("Standard deduction: "+ this.stdDeduction);
  log("Daycare deduction: "+ this.daycareDeduction);
  log("Medical deduction: "+ this.medDeduction);
  log("Homeless deduction: "+ this.homelessDeduction);


	// Calculate adjusted income
	//    20% earned income deduction
	//   + unearned income
	//   - standard deduction
	//   - dependent daycare deduction
	//   - excess medical deduction
	//   - homeless shelter deduction
	this.adjustedIncome = ((this.monthlyEarned - (this.monthlyEarned * 0.20)) + this.monthlyUnearned - this.stdDeduction - this.daycareDeduction - this.medDeduction - this.homelessDeduction - this.childsupportDeduction) - 0; 
	log("Adjusted income: "+ this.adjustedIncome);
	
	// Total housing costs
	this.totalHousing = this.housingCosts + this.calcUtilityDeduction();
	log("Total housing costs are: "+ this.housingCosts +" + "+ this.calcUtilityDeduction() +" = "+ this.totalHousing);

	// Shelter difference is 1/2 of adjusted income subtracted from shelter costs
	this.shelterDifference = this.totalHousing - 0.5 * this.adjustedIncome;
	
	// Shelter deduction
	if(this.hasDisabled) {
		this.shelterDeduction = this.shelterDifference;
	}
	else {
		var max = 431;
		this.shelterDeduction = (this.shelterDifference < max)?
			this.shelterDifference:
			max;
	}
	log("Shelter deduction: "+ this.shelterDeduction);
	
	this.monthlyNet = this.adjustedIncome - this.shelterDeduction;
	log("Monthly net: "+ this.monthlyNet);
	
	this.netEligible = this.calcNetEligible();
	
	this.allotment = this.calcAllotment();
	log(this);
}



// Determine whether gross income is eligible TODO rename
FSCalc.prototype.grossIncomeEligible = function() {
	this.grossIncome = this.monthlyEarned + this.monthlyUnearned;
	var eligTable = [ 1107, 1484, 1861, 2238, 2615, 2992, 3369, 3746 ];
	var increment = 377;
	var maxIncome = 0;
	var tSize = eligTable.length;
	if(this.hhSize <= tSize)
		maxIncome = eligTable[ this.hhSize - 1];
	else 
		maxIncome = eligTable[ tSize - 1 ] + (this.hhSize - tSize) * increment;
	return (this.grossIncome <= maxIncome);
};


// std deduction by HH size
//   1,2,3: 134; 4: 157;  5: 162;  6+: 186
FSCalc.prototype.calcStandardDeduction = function() {
	// var deductionTable = [ 134, 134, 134, 157, 162, 186 ];
	var deductionTable = [ 144, 144, 144, 147, 172, 197 ];
	var maxSize = deductionTable.length;
	if(this.hhSize < maxSize) return deductionTable[this.hhSize - 1];
	if(this.hhSize >= maxSize) return deductionTable[maxSize - 1];
};

FSCalc.prototype.calcDaycareDeduction = function() {
	var deduction = 0;
	// var max = 0;
	// max of 200 per child under 2; max of 175 per other dependent; cap removed for 10/1/2008
	// if(this.numChildren > 0) max += this.numChildren * 200;
	// if(this.numDependents > 0) max += this.numDependents * 175;
	// return (this.monthlyDaycare < max)?
	// 	this.monthlyDaycare:
	// 	max;		
	return (this.numChildren > 0 || this.numDependents > 0)?this.monthlyDaycare:0;
};

FSCalc.prototype.calcUtilityDeduction = function() {
	// If they pay heat/cool: 271
	if(this.payHeat) return 287; //271;
	// If they pay for 2 or more utils (not counting heat/cool): 75
	if(this.payUtils) return 83; //75;
	// If they pay for phone
	if(this.payPhone) return 20;
	// None of the above
	return 0;
};

FSCalc.prototype.calcNetEligible = function() {
	var netEligTable = [ 817, 1100, 1384, 1667, 1950, 2234, 2517, 2800 ];
	var increment = 284;
	var limit = 0;
	if(this.hhSize <= netEligTable.length) {
		limit = netEligTable[this.hhSize - 1];		
	}
	else {
		limit = netEligTable[netEligTable.length] + ( (this.hhSize - netEligTable.length) * increment);
	}
	return (this.monthlyNet <= limit);	
};

FSCalc.prototype.calcAllotment = function() {
	if(!this.netEligible) return false;
	
	// var allotTable = FSCalc.allotTable; // var allotTable = [155, 284, 408, 518, 615, 738, 816, 932];
	// var increment = FSCalc.allotIncrement; // var increment = 117;
	var limit = this.getMaxAllotment();

	log("Max allotment: "+ limit);
	this.adjustedFSIncome = Math.ceil(this.monthlyNet * 0.3);
	log("Adjusted food stamp income: "+ this.adjustedFSIncome);

	var allotment = limit - this.adjustedFSIncome;
	log("Initial allotment: "+ allotment);

	// Do some weird adjustments (see spreadsheet W and X)
	if(this.hhSize >= 3 && (allotment == 1 || allotment == 3 || allotment == 5)) allotment += 1;
	if(this.hhSize <= 2 && (this.adjustedFSIncome > limit || allotment < 10)) allotment = 10;
	log("Adjusted allotment: "+ allotment);
	if(allotment > limit) allotment = limit;
	return allotment;
};

// Get formatted value (to 2 pts precision) if set
FSCalc.prototype.fval = function(field) {
	if(field === 'ME20') {
		return this.monthlyEarned?(this.monthlyEarned - (this.monthlyEarned * 0.20)).toFixed(2):'';
	}
	return this[field]?(this[field]).toFixed(2):'';
}

