single = 0;
married = 1;
headOfHousehold = 2;

householdTypeString = new Array("Single","Married","Head of Household");

function fairtax_table(household_sizes,spending_levels,householdType,caption,cellfunc) {
	// Speficy the row labels and data.
	var rformatfunc = function(input) { return formatNumber(input,"$,##.##"); }
	var rd = new RowDef(spending_levels,rformatfunc,"Spending($)");
	
	// Specify the col labels and data.
	var cformatfunc = function(input) { return input; }
	var hht_formatfunc = function(input) { return householdTypeString[input]; }
	var cd_htt_array = new Array(1);
	cd_htt_array[0] = householdType;
	var cd_hht = new ColDef(cd_htt_array,hht_formatfunc);
	var cd_hhs = new ColDef(household_sizes,cformatfunc);
		
	// Specify the table info
 	var td = new TableDef(rd,Array(cd_hht,cd_hhs),cellfunc,caption);
 	var st = new SmartTable(td);
 	return st.getNode();
}

function fairtax_bp_cell() {
	var spending_level = arguments[0];
	var householdType = arguments[1];
	var household_size = arguments[2]; 
	var isMarried = true;
	
	if(householdType == single) isMarried = false;
	// Check to see if we have single married people.
	if (isMarried == true && household_size == 1) return detailedCell("N/A");
	
  // Create cell
  var eft = new EffectiveFairTaxRate(spending_level,household_size,isMarried);
  var bp = eft.buyingPower();
  var title = "Click for details";
  var onclick = function () { revealDetails(this,eft.buyingPowerDescription());}
	cell = detailedCell(bp,"%##.##",title,onclick);      
  return cell;
}

function fairtax_rate_cell() {
	var spending_level = arguments[0];
	var householdType = arguments[1];
	var household_size = arguments[2]; 
	var isMarried = true;
	
	if(householdType == single) isMarried = false;
	// Check to see if we have single married people.
	if (isMarried == true && household_size == 1) return detailedCell("N/A");
	
  // Create cell
  var eft = new EffectiveFairTaxRate(spending_level,household_size,isMarried);
  var rate = eft.rate();
  var title = "Click for details";
	var onclick = function () { revealDetails(this,eft.rateDescription()); };
	cell = detailedCell(rate,"%##.##",title,onclick);      
  return cell;
}

function EffectiveFairTaxRate(spending, householdSize,isMarried) {
  this.fairtaxRate = 0.23;
  this.singlePoverty = 9130;
  this.povertyIncr = 3180;

  this.spending = spending;
  this.householdSize = householdSize;
  this.isMarried = isMarried;

  this.fca = function() {
    var rv = this.singlePoverty + (this.householdSize - 1)*this.povertyIncr;
    if(this.isMarried) {
      rv = rv + (this.singlePoverty - this.povertyIncr);
    }	
    return rv;
  }
  
  this.tax = function() {
  	return this.fairtaxRate*(this.spending - this.fca());
  }

  this.rate = function() {
    var rv = this.tax()/this.spending;
    return rv;
  }
  
  this.buyingPower = function() { 
  	var rv = (this.spending - this.tax())/this.spending;
  	return rv;
  }
    
  this.fcaDescription = function(dsn) {
		// Add FCA
		dsn.addText("a = Annual Family Consumption Allowance (");
		dsn.addText(this.isMarried ? "Married" : "Single");
		dsn.addText(" Household of " + this.householdSize + " People) = ");
		dsn.addNode(formatNode(this.fca(),"$,##.##"));
		dsn.addLine("");
	}
	
	this.taxDescription = function(dsn) {
		// Add spending
		var text = "s = Annual Spending = " + formatNumber(this.spending,"$,##.##")
		dsn.addLine(text);
		
		this.fcaDescription(dsn);
		
		dsn.addText("t = FairTax Paid = ");
		dsn.addNode(formatNode(this.fairtaxRate,"%##.##"));
		dsn.addText("*(s - a) = ");
		dsn.addNode(formatNode(this.fairtaxRate,"%##.##"));
		dsn.addText("*(");
		dsn.addNode(formatNode(this.spending,"$,##.##"));
		dsn.addText(" - ");
		dsn.addNode(formatNode(this.fca(),"$,##.##"));
		dsn.addText(") = ");
		dsn.addNode(formatNode(this.tax(),"$,##.##"));
		dsn.addLine("")
	}
	
	this.rateDescription = function() { 
		// Create a DescriptionNode
		dn = new DescriptionNode();
	
		// Create a DescriptionSectionNode
		var dsn = dn.addSection();

		// Add Header
		var text = "FairTax Rate Computation";
		dsn.addBoldLine(text);
		
		// Add tax
		this.taxDescription(dsn);
		
		// Add first rate calculation line
	  text = "r = FairTax Effective Tax Rate";
	  dsn.addLine(text);
		
		// Add second rate calculation line
	  dsn.addText("r = t/s = ");
	  dsn.addNode(formatNode(this.tax(),"$,##.##"));
	  dsn.addText("/");
	  dsn.addNode(formatNode(this.spending,"$,##.##"));
	  dsn.addText(" = ");
	  dsn.addNode(formatNode(this.rate(),"%##.##"));
	  dsn.addLine("");
	  		
		return dn.getNode();
	}
	
	this.buyingPowerDescription = function(dsn) {
		// Create a DescriptionNode
		dn = new DescriptionNode();
	
		// Create a DescriptionSectionNode
		var dsn = dn.addSection();

		// Add Header
		var text = "FairTax Buying Power Computation";
		dsn.addBoldLine(text);
		
		// Add tax
		this.taxDescription(dsn);
		
		// First line for buying power
		dsn.addText("b = Buying Power = (s - t)/s = (");
		dsn.addNode(formatNode(this.spending,"$,##.##"));
		dsn.addText(" - ");
		if(this.tax() < 0) dsn.addText("(");
		dsn.addNode(formatNode(this.tax(),"$,##.##"));
		if(this.tax() < 0) dsn.addText(")");
		dsn.addText(") = ");
		dsn.addNode(formatNode(this.buyingPower(),"%##.##")); 
		dsn.addLine("");
		return dn.getNode();
	}	
		  
}
