var KPMGBudgCalc = {
name:"KPMGBudgCalc"
};

KPMGBudgCalc.DebugOutput = function( ElementID, Caption, Value )
{
	this.ElementID = ElementID;
	this.Caption = Caption;
	this.Value = Value;
}

KPMGBudgCalc.Output = function( Name, CurrentValue, NextYearValue )
{
	this.Name = Name;
	this.CurrentValue = CurrentValue;
	this.NextYearValue = NextYearValue;
}

KPMGBudgCalc.IndirectTax = function( Units, Type )
{
	this.Levy = 0;
	this.Units = Units;
	this.Tax = 0;
	this.Type = Type;
}

KPMGBudgCalc.IndirectTax.prototype.CalculateTax = function(Weekly)
{
	this.Tax = this.Units * this.Levy;
	
	if (Weekly)
	{
		this.Tax = this.Tax * 52;
	}
}

KPMGBudgCalc.Levies = function( BeerUnits, WineUnits, CigPackets, SpiritsUnits, FuelLitre, FuelType, FlightsShort, FlightsShortPremium, FlightsLong, FlightsLongPremium)
{
	this.Beer = new KPMGBudgCalc.IndirectTax(BeerUnits, '');
	this.Wine = new KPMGBudgCalc.IndirectTax(WineUnits, '');
	this.Cig = new KPMGBudgCalc.IndirectTax(CigPackets, '');
	this.Spirits = new KPMGBudgCalc.IndirectTax(SpiritsUnits, '');
	this.Fuel = new KPMGBudgCalc.IndirectTax(FuelLitre, FuelType);
	this.FlightsShort = new KPMGBudgCalc.IndirectTax(FlightsShort, '');
	this.FlightsShortPremium = new KPMGBudgCalc.IndirectTax(FlightsShortPremium, '');
	this.FlightsLong = new KPMGBudgCalc.IndirectTax(FlightsLong, '');
	this.FlightsLongPremium = new KPMGBudgCalc.IndirectTax(FlightsLongPremium, '');
	this.Total = 0;
}


KPMGBudgCalc.Levies.prototype.Calculate = function()
{
	this.Beer.CalculateTax(true);
	this.Wine.CalculateTax(true);
	this.Cig.CalculateTax(true);
	this.Spirits.CalculateTax(true);
	this.Fuel.CalculateTax(true);
	this.FlightsShort.CalculateTax(false);
	this.FlightsShortPremium.CalculateTax(false);
	this.FlightsLong.CalculateTax(false);
	this.FlightsLongPremium.CalculateTax(false);
	this.Total = this.Beer.Tax + this.Wine.Tax + this.Cig.Tax + this.Spirits.Tax + this.Fuel.Tax; 
	this.Total += this.FlightsShort.Tax + this.FlightsShortPremium.Tax + this.FlightsLong.Tax + this.FlightsLongPremium.Tax;
}


KPMGBudgCalc.Income = function(savingsIncome, dividendIncome, employmentIncome, rentalIncome, selfEmplIncome, nonStatePension)
{
	this.savingsIncome = (savingsIncome / 0.8);
	this.dividendIncome = dividendIncome / 0.9;
	this.nonSavingsIncome = employmentIncome + selfEmplIncome + nonStatePension + rentalIncome;	
	this.earningsRelevantForNI = employmentIncome;
	this.earningsRelevantForNIClass4 = selfEmplIncome;
	this.OutputLines = new Array();
}

KPMGBudgCalc.Income.prototype.Total = function ()
{
	var retval = 0;
	retval = this.savingsIncome; 
	retval += this.dividendIncome;
	retval += this.nonSavingsIncome;
	return retval;
};

KPMGBudgCalc.Income.prototype.IncomeForTaxCredits = function ()
{
	var retval = 0;
	retval += this.earningsRelevantForNI + this.earningsRelevantForNIClass4;
	return retval;
};

KPMGBudgCalc.Income.prototype.IncomeForTaxCreditsToRestrict = function ()
{
	var retval = 0;
	retval += this.nonSavingsIncome - this.earningsRelevantForNI - this.earningsRelevantForNIClass4 + this.savingsIncome + this.dividendIncome;
	return retval;
};

KPMGBudgCalc.Income.prototype.TotalIncome = function ()
{
	var retval;
	retval = this.savingsIncome; 
	retval += this.dividendIncome * 0.9;
	retval += this.nonSavingsIncome;
	return retval;
};

KPMGBudgCalc.Band = function ( dAmount, dITRate, dDivRate, dSavRate )
{
	this.rates = new Array(3);
	this.amount = dAmount;
	this.rates[0] = dITRate;
	this.rates[1] = dDivRate;
	this.rates[2] = dSavRate;
	this.allUsed = false;
}

KPMGBudgCalc.RoadTaxBand = function ( BandName, Unleaded, Diesel, LPG )
{
	this.BandName = BandName;
	this.Unleaded = Unleaded;
	this.Diesel = Diesel;
	this.LPG = LPG;
}

KPMGBudgCalc.CarsForRoadTax = function ( Band, FuelType )
{
	this.Band = Band;
	this.FuelType = FuelType;
}

KPMGBudgCalc.CompanyCar = function ( ListPrice, CO2, Euro4, FuelProvided )
{
	this.ListPrice = ListPrice;
	this.CO2 = CO2;
	this.Euro4 = Euro4;
	this.FuelProvided = FuelProvided;
}

KPMGBudgCalc.CompanyVan = function ( FuelProvided )
{
	this.FuelProvided = FuelProvided;
}

KPMGBudgCalc.Person = function(includeSpouse, Sex,Age, NoChildrenUnder16, WeeklyHours, MaritalStatus, Income, spouseSex, spouseAge, spouseHours, spouseIncome, Levies, CarsForRoadTax, CompanyCars, SpouseCompanyCars, CompanyVans, SpouseCompanyVans)
{
	
	this.Age = Age;
	this.Sex = Sex;
	this.spouseSex = spouseSex; 
	this.NoChildrenUnder16 = NoChildrenUnder16;
	this.WeeklyHours = WeeklyHours;
	this.MaritalStatus = MaritalStatus;
	this.Income = Income;
	this.spouseIncome = spouseIncome;
	
	this.NextIncome = new KPMGBudgCalc.Income(0, 0, 0, 0, 0);
	this.NextIncome.savingsIncome = this.Income.savingsIncome;
	this.NextIncome.dividendIncome = this.Income.dividendIncome;
	this.NextIncome.nonSavingsIncome = this.Income.nonSavingsIncome;
	this.NextIncome.earningsRelevantForNI = this.Income.earningsRelevantForNI;
	this.NextIncome.earningsRelevantForNIClass4 = this.Income.earningsRelevantForNIClass4;
	
	this.NextspouseIncome = new KPMGBudgCalc.Income(0, 0, 0, 0, 0);
	this.NextspouseIncome.savingsIncome = this.spouseIncome.savingsIncome;
	this.NextspouseIncome.dividendIncome = this.spouseIncome.dividendIncome;
	this.NextspouseIncome.nonSavingsIncome = this.spouseIncome.nonSavingsIncome;
	this.NextspouseIncome.earningsRelevantForNI = this.spouseIncome.earningsRelevantForNI;
	this.NextspouseIncome.earningsRelevantForNIClass4 = this.spouseIncome.earningsRelevantForNIClass4;
	
	this.OutputLines = new Array();	
	this.NextOutputLines = new Array();	
	this.includeSpouse = includeSpouse;
	this.spouseHours = spouseHours;
	this.spouseWeeklyHours = spouseHours;
	this.spouseAge = spouseAge;
	this.Levies = Levies;
	this.CarsForRoadTax = CarsForRoadTax;
	this.CompanyCars = CompanyCars;
	this.SpouseCompanyCars = SpouseCompanyCars;
	
	this.CompanyVans = CompanyVans;
	this.SpouseCompanyVans = SpouseCompanyVans;
	
	this.Output = new Array();
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Alcohol', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Tobacco', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Fuel', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Air travel', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Income tax', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'National insurance ', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Child benefits ', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Tax credits ', 0, 0);	
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Total ', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'Vehicle excise duty ', 0, 0);
	this.Output[this.Output.length] = new KPMGBudgCalc.Output( 'State pension ', 0, 0);
}

KPMGBudgCalc.Person.prototype.InsertOutput = function (index, value, NextYear)
{
	if ( NextYear )
	{
		this.Output[index].NextYearValue = value;
	}
	else
	{
		this.Output[index].CurrentValue = value;		
	}
};


KPMGBudgCalc.Person.prototype.AddOutputLine = function (elementid, caption, value, NextYear)
{
	if (!NextYear)
	{
		this.OutputLines[this.OutputLines.length] = ( new KPMGBudgCalc.DebugOutput (elementid, caption, value) );
	}
	else
	{
		this.NextOutputLines[this.NextOutputLines.length] = ( new KPMGBudgCalc.DebugOutput (elementid, caption, value) );
	}
	
};

KPMGBudgCalc.Person.prototype.Round2dp = function(Num2Round)
{
	var retval = 0;
	
	retval = Math.round(Num2Round * 100) / 100;
	// the .toFixed function is not available in IE 5.01 or Safari 1.
	//retval = Num2Round.toFixed(2); 
	//retval = Math.round(Num2Round*Math.pow(10,2))/Math.pow(10,2); // rounding for 0dp
	return retval;	
};

KPMGBudgCalc.Person.prototype.GetKPMGOutputs = function ()
{
var difference = 0;
	var retval = '<div>';
	
	difference = this.Output[8].NextYearValue - this.Output[8].CurrentValue;
	difference = this.Round2dp(difference);
	if ( difference < 0 )
	{
		difference = -difference;
		retval += '<h2 class="result">If you lead a similar lifestyle next year, the indications are that you will be <span class="diffless">' + String.fromCharCode(0x0A3) + difference + '</span> worse off.</h2</div>';
	}
	else if (difference == 0 )
	{
		retval += '<h2 class="result">If you lead a similar lifestyle next year, the indications are that you will be no better or worse off.</h2</div>';
	}
	else
	{
		retval += '<h2 class="result">If you lead a similar lifestyle next year, the indications are that you will be <span class="diffmore">' + String.fromCharCode(0x0A3) + difference + '</span> better off.</h2</div>';
	}
	retval += '<h3>Breakdown of difference</h3>';
	
	retval += '<table border="0" class="resulttab" width="629">';
	retval += '<thead><tr>';
	retval += '<th width="179"></th><th width="150" id="curyear">Last Year ' + String.fromCharCode(0x0A3) + '</th><th width="150" id="nextyear">Current Year ' + String.fromCharCode(0x0A3) + '</th><th id="diff" width="150">Difference ' + String.fromCharCode(0x0A3) + '</th>';
	retval += '</tr></thead><tbody>';
	retval += this.GetKMPGOutputBreakdownLine(0, true);
	retval += this.GetKMPGOutputBreakdownLine(1, true);
	retval += this.GetKMPGOutputBreakdownLine(2, true);
	retval += this.GetKMPGOutputBreakdownLine(3, true);
	retval += this.GetKMPGOutputBreakdownLine(4, true);
	retval += this.GetKMPGOutputBreakdownLine(5, true);
	retval += this.GetKMPGOutputBreakdownLine(6, false);
	retval += this.GetKMPGOutputBreakdownLine(7, false);
	retval += this.GetKMPGOutputBreakdownLine(9, true);
	retval += this.GetKMPGOutputBreakdownLine(10, false);	
	retval += '</tbody></table>'
	
	return retval;
};

KPMGBudgCalc.Person.prototype.GetKMPGOutputBreakdownLine = function (index, tax)
{
	var difference = 0;
	var retval = 0;
	if ( tax )
	{
		difference = this.Output[index].CurrentValue - this.Output[index].NextYearValue;
	}
	else
	{
		difference = this.Output[index].NextYearValue - this.Output[index].CurrentValue;
	}
	
	difference = this.Round2dp(difference);
	
	if ( difference < 0 )
	{
		retval = '<tr class=FormTextField><td>' + this.Output[index].Name + '</td><td align=\'right\'>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td align=\'right\'>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td align=\'right\'><font color = red>'+ String.fromCharCode(0x0A3) + difference + '</font></td></tr>';
	}
	else if (difference == 0 )
	{
		retval = '<tr class=FormTextField><td>' + this.Output[index].Name + '</td><td align=\'right\'>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td align=\'right\'>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td align=\'right\'><font color = blue>' + difference + '</font></td></tr>';
		
	}
	else
	{
		retval = '<tr class=FormTextField><td>' + this.Output[index].Name + '</td><td align=\'right\'>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td align=\'right\'>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td align=\'right\'><font color = green>'+ String.fromCharCode(0x0A3) + difference + '</font></td></tr>';
	}
	return retval;
};

KPMGBudgCalc.Person.prototype.GetOutputs = function ()
{
	var difference = 0;
	var retval = '<h2 class="result">';
	difference = this.Output[8].NextYearValue - this.Output[8].CurrentValue;
	difference = this.Round2dp(difference);
	if ( difference < 0 )
	{
		difference = -difference;
		retval += 'Next year, the indications are that you will be <span class="diffless">' + String.fromCharCode(0x0A3) + difference + '</span> worse off.</h2>';
	}
	else if (difference == 0 )
	{
		retval += 'Next year, the indications are that you will be no better or worse off.</h2>';
	}
	else
	{
		retval += 'Next year, the indications are that you will be <span class="diffmore">' + String.fromCharCode(0x0A3) + difference + '</span> better off.</h2>';
	}
	
	retval += '<table border="0" class="resulttab" width="629">';
	retval += '<thead><tr>';
	retval += '<td width="179"></td><th width="150" id="curyear">Current Year</th><th width="150" id="nextyear">Next Year</th><th id="diff" width="150">Difference</th>';
	retval += '</tr></thead><tbody>';
	retval += this.GetOutputBreakdownLine(0, true);
	retval += this.GetOutputBreakdownLine(1, true);
	retval += this.GetOutputBreakdownLine(2, true);
	retval += this.GetOutputBreakdownLine(3, true);
	retval += this.GetOutputBreakdownLine(4, true);
	retval += this.GetOutputBreakdownLine(5, true);
	retval += this.GetOutputBreakdownLine(6, false);
	retval += this.GetOutputBreakdownLine(7, false);
	retval += this.GetOutputBreakdownLine(9, true);
	retval += this.GetOutputBreakdownLine(10, false);	
	retval += '</tbody></table>'
	return retval;
};

KPMGBudgCalc.Person.prototype.GetOutputBreakdownLine = function (index, tax)
{
	var difference = 0;
	var retval = 0;
	if ( tax )
	{
		difference = this.Output[index].CurrentValue - this.Output[index].NextYearValue;
	}
	else
	{
		difference = this.Output[index].NextYearValue - this.Output[index].CurrentValue;
	}
	
	difference = this.Round2dp(difference);
	
	if ( difference < 0 )
	{
		retval = '<tr><td>' + this.Output[index].Name + '</td><td>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td><font color = red>'+ String.fromCharCode(0x0A3) + difference + '</font></td></tr>';
	}
	else if (difference == 0 )
	{
		retval = '<tr class=FormTextField><td>' + this.Output[index].Name + '</td><td>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td><font color = blue>' + difference + '</font></td></tr>';
		
	}
	else
	{
		retval = '<tr><td>' + this.Output[index].Name + '</td><td>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td><font color = green>+'+ String.fromCharCode(0x0A3) + difference + '</font></td></tr>';
	}
	return retval;
};

KPMGBudgCalc.Person.prototype.GetScenarioOutputs = function ()
{
	var difference = 0;
	var retval = '<table>';
	
	difference = this.Output[8].NextYearValue - this.Output[8].CurrentValue;
	difference = this.Round2dp(difference);
	/*if ( difference < 0 )
	{
		retval += '<tr><td><b>Next year you will be worse off by ' + difference + '</b></td></tr></table>';
	}
	else
	{
		retval += '<tr><td><b>Next year you will be better off by ' + difference + '</b></td></tr></table>';
	}
	*/
	retval += '<table border=1>';
	retval += '<tr>';
	retval += '<td>Current Year</td><td>Next Year Year</td><td>Difference</td>';
	retval += '</tr>';
	retval += this.GetOpScenBreakdownLine(0, true);
	retval += this.GetOpScenBreakdownLine(1, true);
	retval += this.GetOpScenBreakdownLine(2, true);
	retval += this.GetOpScenBreakdownLine(3, true);
	retval += this.GetOpScenBreakdownLine(4, true);
	retval += this.GetOpScenBreakdownLine(5, true);
	retval += this.GetOpScenBreakdownLine(6, false);
	retval += this.GetOpScenBreakdownLine(7, false);
	retval += this.GetOpScenBreakdownLine(9, true);
    retval += this.GetOpScenBreakdownLine(10, false);	
	retval += '</table>'
	
	return retval;
};

KPMGBudgCalc.Person.prototype.GetOpScenBreakdownLine = function (index, tax)
{
	var difference = 0;
	var retval = 0;
	if ( tax )
	{
		difference = this.Output[index].CurrentValue - this.Output[index].NextYearValue;
	}
	else
	{
		difference = this.Output[index].NextYearValue - this.Output[index].CurrentValue;
	}
	
	difference = this.Round2dp(difference);
	
	if ( difference < 0 )
	{
		retval = '<tr><td>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td><font color = red>'+ String.fromCharCode(0x0A3) + difference + '</font></td></tr>';
	}
	
	else
	{
		retval = '<tr><td>' + this.Round2dp(this.Output[index].CurrentValue) + '</td><td>' + this.Round2dp(this.Output[index].NextYearValue) + '</td><td><font color = green>+'+ String.fromCharCode(0x0A3) + difference + '</font></td></tr>';
	}
	return retval;
};

KPMGBudgCalc.Person.prototype.GetTestSuiteOutputs = function ()
{
	var difference = 0;
	var retval = '';
	
	difference = this.Output[8].NextYearValue - this.Output[8].CurrentValue;
	difference = this.Round2dp(difference);
	
	retval += this.GetTSBreakdownLine(8, false);		    
	retval += this.GetTSBreakdownLine(0, true);
	retval += this.GetTSBreakdownLine(1, true);
	retval += this.GetTSBreakdownLine(2, true);
	retval += this.GetTSBreakdownLine(3, true);
	retval += this.GetTSBreakdownLine(4, true);
	retval += this.GetTSBreakdownLine(5, true);
	retval += this.GetTSBreakdownLine(6, false);
	retval += this.GetTSBreakdownLine(7, false);
	retval += this.GetTSBreakdownLine(9, true);
    retval += this.GetTSBreakdownLine(10, false);		    
	return retval;
};

KPMGBudgCalc.Person.prototype.GetTSBreakdownLine = function (index, tax)
{
	var difference = 0;
	var retval = '';
	if ( tax )
	{
		difference = this.Output[index].CurrentValue - this.Output[index].NextYearValue;
	}
	else
	{
		difference = this.Output[index].NextYearValue - this.Output[index].CurrentValue;
	}
	
	difference = this.Round2dp(difference);	
	if ( difference < 0 )
	{
		retval += '<td align=right><font color = red>' +  difference + '</font></td>';
	}
	
	else
	{
		retval += '<td align=right><font color = green>' + difference + '</font></td>';
	}	
	return retval;
};


KPMGBudgCalc.Person.prototype.GetDebugOutputs = function ()
{
	var retval = '<table border=1><tr><td><b>Current Year</b></td><td><b>Next Year</b></td></tr><tr><td><table>';
	
	for( var i = 0; i < this.OutputLines.length; i ++ )
	{
		retval += '<tr><td>' + this.OutputLines[i].ElementID + '</td><td>' +  this.OutputLines[i].Caption + '</td><td>' + this.OutputLines[i].Value + '</td></tr>';
	}
	retval += '</table>';
	retval += '</td><td>';
	retval += '<table>';
	for( var i = 0; i < this.NextOutputLines.length; i ++ )
	{

		retval += '<tr><td>' + this.NextOutputLines[i].ElementID + '</td><td>' +  this.NextOutputLines[i].Caption + '</td><td>' + this.NextOutputLines[i].Value + '</td></tr>';
	}
	retval += '</table></td></tr></table>';
	return "";
}

KPMGBudgCalc.Person.prototype.GetTax = function ()
{
	var ly;
	var ny;
	ny = new NextYearTaxConst.TaxConstants();
	ly  = new CurrentTaxConst.TaxConstants();
	
	this.CalculateTax(ly.Constants, false, this.Income, this.spouseIncome);
	this.Age += 1;
	this.spouseAge += 1;
	this.CalculateTax(ny.Constants, true, this.NextIncome, this.NextspouseIncome);
};

KPMGBudgCalc.Person.prototype.CalculateTax = function (ly, NextYear, Income, SpouseIncome)
{
	var myIT = 0;
	var myNI = 0;
	var myNIClass1 = 0;
	var myNIClass4 = 0;
	var myNIClass2 = 0;
	var myTotal = 0;
	
	var spouseIT = 0;
	var spouseNI = 0;
	var spouseNIClass1 = 0;
	var spouseNIClass4 = 0;
	var spouseNIClass2 = 0;
	var spouseTotal = 0;
	var RoadTax = 0;
	var Total = 0;
	
	var myPension = 0;
	var spousePension = 0;
	
	var ageRelatedBand = 0;
	var spouseAgeRelatedBand = 0;
	var basicAllowance = 0;
	var spousebasicAllowance = 0;
	
	var TaxCredits = 0;
	var ChildBenefit = 0;	
	var mcaTaxCredit = 0;
	
	var CompanyCarBen = 0;
	var SpouseCompanyCarBen = 0;
	
	var CompanyVanBen = 0;
	var SpouseCompanyVanBen = 0;
	
	var myMaxNIRate = new Array;
	myMaxNIRate[myMaxNIRate.length] = 0;
	var spouseMaxNIRate = new Array;
	spouseMaxNIRate[spouseMaxNIRate.length] = 0;
	
	ly.SetLevies(this.Levies);
	this.Levies.Calculate();
	
	this.AddOutputLine('Levies', '<i>Beer</i>', this.Levies.Beer.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Wine</i>', this.Levies.Wine.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Cigs</i>', this.Levies.Cig.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Spirits</i>', this.Levies.Spirits.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Fuel</i>', this.Levies.Fuel.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Flights - short haul standard</i>', this.Levies.FlightsShort.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Flights - short haul premium</i>', this.Levies.FlightsShortPremium.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Flights - long haul standard</i>', this.Levies.FlightsLong.Tax, NextYear);
	this.AddOutputLine('Levies', '<i>Flights - long haul preimium</i>', this.Levies.FlightsLongPremium.Tax, NextYear);
	this.AddOutputLine('Levies', '<b>Levies Total</b>', this.Levies.Total, NextYear);
	
	this.InsertOutput( 0, this.Levies.Wine.Tax + this.Levies.Beer.Tax + this.Levies.Spirits.Tax, NextYear);
	this.InsertOutput( 1, this.Levies.Cig.Tax, NextYear);
	this.InsertOutput( 2, this.Levies.Fuel.Tax, NextYear);
	this.InsertOutput( 3, this.Levies.FlightsShort.Tax + this.Levies.FlightsShortPremium.Tax + this.Levies.FlightsLong.Tax + this.Levies.FlightsLongPremium.Tax, NextYear);
	
	CompanyCarBen =  ly.GetCompanyCarBen(this.CompanyCars);				
	CompanyVanBen = ly.GetCompanyVanBen(this.CompanyVans);			
	if ( Income.earningsRelevantForNI + CompanyCarBen + CompanyVanBen < ly.BenefitsThreshold)
	{
		CompanyCarBen = 0;
		CompanyVanBen = 0;
		this.AddOutputLine('Benefits', '<b>Car and Van benefit set to 0 </b>', 0, NextYear);
		this.AddOutputLine('Benefits', '<b>Total employment income < </b>', ly.BenefitsThreshold, NextYear);
	}
			
	this.AddOutputLine('Co Cars', '<b>Co Car Ben</b>', CompanyCarBen, NextYear);
	this.AddOutputLine('Co Vans', '<b>Co Van Ben</b>', CompanyVanBen, NextYear);	
	
	//Get my pension
	myPension = ly.GetPension(this.Age, this.Sex);
	Income.nonSavingsIncome += myPension + CompanyCarBen + CompanyVanBen;
	
	RoadTax = this.CalculateRoadTax(ly.RoadTaxBands, NextYear);
	this.AddOutputLine('Road Tax', '<b>Total</b>', RoadTax, NextYear);
	this.AddOutputLine('Pension', '<b>Pension</b>', myPension, NextYear);
	this.AddOutputLine('Band Adjustment', '<b>Band before</b>', ly.bands[0].amount, NextYear);	
	basicAllowance = ly.bands[0].amount;
	
	ageRelatedBand = ly.MCABands(this.Age,ly.bands[0].amount);
	ly.bands[0].amount = ly.AdjustBands(this.Age, Income.Total(),ly.bands[0].amount );
	this.AddOutputLine('Band Adjustment', '<b>Band after</b>', ly.bands[0].amount, NextYear);	
	myIT = this.Calculate(ly.GetBands(), Income, NextYear);
	this.AddOutputLine('IT', '<b>My IT</b>', myIT, NextYear);
	
	if ( this.Age < ly.MaxNIAge(this.Sex) && this.Age > 15 )
	{	
		myNIClass1 = this.CalcNI( ly.GetNIBands(), Income.earningsRelevantForNI, 0, myMaxNIRate);
		this.AddOutputLine('NI Class 1', '<b>my NI Class 1</b>', myNIClass1, NextYear);
		
		if ( Income.earningsRelevantForNIClass4 > ly.Class2NIMin )
		{
			myNIClass2 = 52 * ly.Class2NIAmount;
			this.AddOutputLine('NI Class 2', '<b>my NI Class 2</b>', myNIClass2, NextYear);
		}
		
		myNI = myNIClass1 + myNIClass2;
		var tempNumber = myMaxNIRate[0] + myNIClass2;
		if ( (myMaxNIRate[0] + myNIClass2) > ly.Class2and4NIMax )
		{			
			myNI -= (myMaxNIRate[0] + myNIClass2 - ly.Class2and4NIMax);
			this.AddOutputLine('Class 1 and Class 2 restricted', '<b>my NI restricted</b>', myNI, NextYear);
		}
		
		myNIClass4 = this.CalcNI( ly.GetNIBands(), Income.earningsRelevantForNIClass4, 1, myMaxNIRate );
		this.AddOutputLine('NI Class 4', '<b>my NI Class 4</b>', myNIClass4, NextYear);
		
		myNI += myNIClass4;				
		this.AddOutputLine('NI', '<b>my NI</b>', myNI, NextYear);
	}
	else
	{	myNI = 0; }
	this.AddOutputLine('NI', '<b>My NI</b>', myNI, NextYear);
	
	
	if (this.includeSpouse)
	{
		spousePension = ly.GetSpousePension(this.spouseAge, this.spouseSex, this.Age, this.Sex);
		SpouseCompanyCarBen =  ly.GetCompanyCarBen(this.SpouseCompanyCars);
		SpouseCompanyVanBen = ly.GetCompanyVanBen(this.SpouseCompanyVans);
		
		this.AddOutputLine('Spouse Pension', '<b>Spouse Pension</b>', spousePension, NextYear);
		this.AddOutputLine('Spouse Co Cars', '<b>Spouse Co Car Ben</b>', SpouseCompanyCarBen, NextYear);
		this.AddOutputLine('Spouse Co Vans', '<b>Spouse Co Van Ben</b>', SpouseCompanyVanBen, NextYear);
		SpouseIncome.nonSavingsIncome += spousePension + SpouseCompanyCarBen + SpouseCompanyVanBen;
		
		this.AddOutputLine('Spouse Band Adjustment', '<b>Band before</b>', ly.spousebands[0].amount, NextYear);	
		ly.spousebands[0].amount = ly.AdjustBands(this.spouseAge, SpouseIncome.Total(),ly.spousebands[0].amount );
		spousebasicAllowance = ly.bands[0].amount;
		
	
		spouseAgeRelatedBand = ly.MCABands(this.spouseAge,ly.bands[0].amount);
		this.AddOutputLine('Spouse Band Adjustment', '<b>Band after</b>', ly.spousebands[0].amount, NextYear);	
		spouseIT = this.Calculate(ly.GetSpouseBands(), SpouseIncome, NextYear);
		
		this.AddOutputLine('Spouse IT Total', '<b>Spouse IT</b>', spouseIT, NextYear);
		if ( this.spouseAge < ly.MaxNIAge(this.spouseSex) && this.spouseAge > 15 )
		{	
			spouseNIClass1 = this.CalcNI( ly.SpouseNIBands, SpouseIncome.earningsRelevantForNI, 0, spouseMaxNIRate);			
			this.AddOutputLine('NI Class 1', '<b>spouse NI Class 1</b>', spouseNIClass1, NextYear);

			if ( SpouseIncome.earningsRelevantForNIClass4 > ly.Class2NIMin )
			{
				spouseNIClass2 = 52 * ly.Class2NIAmount;
				this.AddOutputLine('NI Class 2', '<b>spouse NI Class 2</b>', spouseNIClass2, NextYear);
			}
			
			spouseNI = spouseNIClass1 + spouseNIClass2;
			if ( spouseMaxNIRate[0] + spouseNIClass2 > ly.Class2and4NIMax )
			{
				spouseNI -= spouseMaxNIRate[0] + spouseNIClass2 - ly.Class2and4NIMax ;
				this.AddOutputLine('Class 1 and Class 2 restricted', '<b>spouse NI restricted</b>', spouseNI, NextYear);
			}			

			spouseNIClass4 = this.CalcNI( ly.SpouseNIBands, SpouseIncome.earningsRelevantForNIClass4, 1, spouseMaxNIRate );
			spouseNI += spouseNIClass4;			
			this.AddOutputLine('NI Class 4', '<b>spouse NI Class 4</b>', spouseNIClass4, NextYear);			
			this.AddOutputLine('NI', '<b>spouse NI</b>', spouseNI, NextYear);
		}
		else
		{	spouseNI = 0; }
		this.AddOutputLine('Spouse NI', '<b>Spouse NI</b>', spouseNI, NextYear);	
		
	}
	
	var combinedIncomeForTaxCredits = 0;
	combinedIncomeForTaxCredits += Income.IncomeForTaxCredits() + CompanyCarBen + CompanyVanBen;
	//if (this.includeSpouse)
	//{	
		combinedIncomeForTaxCredits += SpouseIncome.IncomeForTaxCredits() + SpouseCompanyCarBen + SpouseCompanyVanBen;
	//}	
	this.AddOutputLine('TaxCredits', '<i>Non restricted income</i>', combinedIncomeForTaxCredits, NextYear);
	var combinedIncomeForTaxCreditsToRestrict = 0;
	combinedIncomeForTaxCreditsToRestrict += Income.IncomeForTaxCreditsToRestrict() - CompanyCarBen - CompanyVanBen;
	//if (this.includeSpouse)
	//{		
		combinedIncomeForTaxCreditsToRestrict += SpouseIncome.IncomeForTaxCreditsToRestrict() - SpouseCompanyCarBen - SpouseCompanyVanBen;		
	//}
	this.AddOutputLine('TaxCredits', '<i>Restricted income (pre restriction)</i>', combinedIncomeForTaxCreditsToRestrict, NextYear);
	if ( combinedIncomeForTaxCreditsToRestrict > 300)
	{
		combinedIncomeForTaxCreditsToRestrict -= 300;
	}
	else
	{
		combinedIncomeForTaxCreditsToRestrict = 0;
	}
	this.AddOutputLine('TaxCredits', '<i>Restricted income (post restriction)</i>', combinedIncomeForTaxCreditsToRestrict, NextYear);
	this.AddOutputLine('TaxCredits', '<i>Total income for tax credit purposes</i>', combinedIncomeForTaxCredits + combinedIncomeForTaxCreditsToRestrict, NextYear);
	
	TaxCredits = ly.getTaxCredits(this.NoChildrenUnder16, this.MaritalStatus, this.WeeklyHours, this.spouseWeeklyHours, combinedIncomeForTaxCredits + combinedIncomeForTaxCreditsToRestrict, this.Age,  this.spouseAge);	
	
	if (this.Age > this.spouseAge) {
	

	MCA = ly.getMCATaxCredit(this.MaritalStatus, this.Age,  this.spouseAge, Math.max(Income.TotalIncome(), SpouseIncome.TotalIncome()),ageRelatedBand,basicAllowance);
	} else {
	MCA = ly.getMCATaxCredit(this.MaritalStatus, this.Age,  this.spouseAge, Math.max(Income.TotalIncome(), SpouseIncome.TotalIncome()),spouseAgeRelatedBand,spousebasicAllowance);
	}
	
	this.AddOutputLine('Married Couples Allowance', '<b>MCA for both</b>', MCA, NextYear);	
	
	if (MCA > myIT)
	{		
		spouseIT -= (MCA - myIT);
		spouseIT = Math.max(spouseIT, 0);
		myIT = 0;
	}
	else
	{
		myIT -= MCA;
	}
	
		
	Income.nonSavingsIncome = Income.nonSavingsIncome - CompanyCarBen - CompanyVanBen;
	SpouseIncome.nonSavingsIncome = SpouseIncome.nonSavingsIncome - SpouseCompanyCarBen - SpouseCompanyVanBen;

	
	
	myTotal = Income.TotalIncome() - myIT - myNI - this.Levies.Total - RoadTax;
	spouseTotal = SpouseIncome.TotalIncome() - spouseIT - spouseNI;
	
	this.AddOutputLine('Tax Credits', '<b>Tax Credits</b>', TaxCredits, NextYear);	
	ChildBenefit = ly.totalChildBenefit(this.NoChildrenUnder16, this.MaritalStatus);
	this.AddOutputLine('Child ben', '<b>Child ben</b>', ChildBenefit, NextYear);
	
	Total = spouseTotal + myTotal + TaxCredits + ChildBenefit;	
	this.AddOutputLine('', '<b>Total</b>', Total, NextYear);		
	
	this.InsertOutput( 4, Math.max(myIT + spouseIT, 0), NextYear); 
	this.InsertOutput( 5, myNI + spouseNI, NextYear); 
	this.InsertOutput( 6, ChildBenefit, NextYear); 
	this.InsertOutput( 7, TaxCredits, NextYear); 
	this.InsertOutput( 9, RoadTax, NextYear); 	
	this.InsertOutput( 10, myPension + spousePension, NextYear); 		
	this.InsertOutput( 8, Total, NextYear); 
	
};

KPMGBudgCalc.Person.prototype.CalcTaxCreditOnDiv = function(bands, income)
{
	var taxCreditOnDividends;
	
	taxCreditOnDividends = 0.1 * income.dividendIncome;
	if ( income.nonSavingsIncome + income.savingsIncome < bands[0].amount )
	{
		taxCreditOnDividends -= 0.1 * (bands[0].amount - income.nonSavingsIncome - income.savingsIncome);
		if ( taxCreditOnDividends < 0 )
		{
			taxCreditOnDividends = 0;
		}
	}
	return taxCreditOnDividends
};

KPMGBudgCalc.Person.prototype.CalculateRoadTax = function(RoadTaxBands, NextYear)
{
	var retval = 0;
	var TaxOnCar;
	var i = 0;
	
	for( i = 0; i < this.CarsForRoadTax.length; i ++ )
	{
		
		TaxOnCar = this.GetRoadTaxForCar(RoadTaxBands, this.CarsForRoadTax[i].Band, this.CarsForRoadTax[i].FuelType) ;
		this.AddOutputLine('Road Tax', '<i>Tax On Car</i>', TaxOnCar, NextYear);	
		retval += TaxOnCar;
	}
	return retval;
}

KPMGBudgCalc.Person.prototype.GetRoadTaxForCar = function(RoadTaxBands, Band, FuelType)
{
	var retval = 0;
	var i = 0;
	
	for( i = 0; i < RoadTaxBands.length; i ++ )
	{
		if (RoadTaxBands[i].BandName == Band )
		{
			if ( FuelType == "U" )
			{
				retval = RoadTaxBands[i].Unleaded;
			}
			else if ( FuelType == "D" )
			{
				retval = RoadTaxBands[i].Diesel;
			}
			else if ( FuelType == "L" )
			{
				retval = RoadTaxBands[i].LPG;
			}
			break;
		}
	}
	return retval;
};


KPMGBudgCalc.Person.prototype.Calculate = function(bands, income, NextYear)
{
	var retval = 0.0;
	var SchedE = 0.0;
	var SchedDiv = 0.0;
	var SchedSav = 0.0;
	var taxCreditOnDividends;
	var taxCredits;	
	
	taxCreditOnDividends = this.CalcTaxCreditOnDiv(bands, income);
	//Calculate income tax 
	this.AddOutputLine('IT', '<i>Start tax on employment</i>', income.nonSavingsIncome, NextYear);
	SchedE += this.CalcTax( bands, income.nonSavingsIncome, 0 );
	this.AddOutputLine('IT', '<i>tax on employment</i>', SchedE, NextYear);
	
	this.AddOutputLine('IT', '<i>Start tax on savings</i>', income.savingsIncome, NextYear);
	SchedSav += this.CalcTax( bands, income.savingsIncome, 1 );
	this.AddOutputLine('IT', '<i>tax on savings</i>', SchedSav, NextYear);
	
	this.AddOutputLine('IT', '<i>Start tax on dividends</i>', income.dividendIncome, NextYear);
	SchedDiv += this.CalcTax( bands, income.dividendIncome, 2 );
	this.AddOutputLine('IT', '<i>tax on dividends</i>', SchedDiv, NextYear);
	
	this.AddOutputLine('IT', '<i>tax credit on dividends</i>', taxCreditOnDividends, NextYear);
			
	retval = SchedSav + SchedE + SchedDiv;
	if (taxCreditOnDividends > retval )
	{
		retval = 0;
	}
	else
	{
		retval -= taxCreditOnDividends;
	}		
	return retval;
};

KPMGBudgCalc.Person.prototype.CalcNI = function( bands, income, iTaxRate, MaxRateAmount )	{
	var retTaxLiab = 0.0;
	var Temp = 0;
	var grossAmount;
	grossAmount = income;
	//If income is equal to zero or negative then return zero
	
	if ( income == 0 )
		return 0;

	if ( income < 0 )
		return 0;
	
	for( var i =0; i < bands.length; i ++ )
	{		
		if( grossAmount > 0)
		{
			//if value is -1 then band has no upper limit
			if ( bands[i].amount == -1 )
			{
				retTaxLiab += bands[i].rates[iTaxRate] * grossAmount;
				grossAmount = 0;
				break;
			}
			if ( grossAmount > bands[i].amount )
			{				
				grossAmount -= bands[i].amount;				
				Temp += bands[i].rates[iTaxRate] * bands[i].amount;
				if ( i == 1 )
				{
					MaxRateAmount[0] += Temp;
				}

				// Adjust the band to 0 - this uses up all of the band.
				bands[i].amount = 0;
				
				retTaxLiab += Temp;				
			}
			else
			{
				// Adjust the band by this amount of income
				bands[i].amount -= grossAmount;
				Temp += bands[i].rates[iTaxRate] * grossAmount;
				if ( i == 1 )
				{
					MaxRateAmount[0] += Temp;
				}
				retTaxLiab += Temp;
				grossAmount = 0;
				break;
			}			
		}
	}
	return retTaxLiab;
};

KPMGBudgCalc.Person.prototype.CalcTax = function( bands, income, iTaxRate )	{
	var retTaxLiab = 0.0;
	var grossAmount;
	
	grossAmount = income;
	//If income is equal to zero or negative then return zero
	if ( income == 0 )
		return 0;

	if ( income < 0 )
		return 0; 
	for( var i =0; i < bands.length; i ++ )
	{
		if( grossAmount > 0 && !bands[i].allUsed)
		{
			//if value is -1 then band has no upper limit
			if ( bands[i].amount == -1 )
			{
				retTaxLiab += bands[i].rates[iTaxRate] * grossAmount;
				grossAmount = 0;
				break;
			}
			if ( grossAmount > bands[i].amount )
			{
				grossAmount -= bands[i].amount;
				retTaxLiab += bands[i].rates[iTaxRate] * bands[i].amount;
				bands[i].allUsed = true;
			}
			else
			{
				bands[i].amount -= grossAmount;
				retTaxLiab += bands[i].rates[iTaxRate] * grossAmount;
				grossAmount = 0;
				break;
			}
		}
	}
	return retTaxLiab;
};

KPMGBudgCalc.Constants = function()
{
	
	
	//IT Bands
	this.bands = new Array();	
	//IT Bands for spouse
	this.spousebands = new Array();	
	//Road Tax Bands
	this.RoadTaxBands = new Array();
	//NI Bands
	this.NIBands = new Array();
	this.SpouseNIBands = new Array();
	//Max age for NI
	this.MaleMaxNIAge = 0;
	this.FemaleMaxNIAge = 0;
	this.Class2NIMin = 0;
	this.Class2and4NIMax = 0;
	this.Class2NIAmount = 0;
	//Pensions
	this.MaleMinPensionAge = 0;
	this.FemaleMinPensionAge = 0;
	this.StatePensionMarried = 0;
	this.StatePensionSingle = 0;
	//Abatement - adjust bands
	this.AgeForBandExtensionLower = 0;
	this.ElderlyBandExtensionLower = 0;
	this.AgeForBandExtensionUpper = 0;
	this.ElderlyBandExtensionUpper = 0;
	this.ElderlyBandExtensionLimit = 0;
	this.AbatementRate = 0;
	//Tax Credits
	this.wtcBasic = 0;
	this.wtcCouple = 0;
	this.wtc30hrsSupplement = 0;
	
	this.wtcIncomeThreshold = 0;
	this.ctcFamilyThreshold = 0;
	this.ctcChild = 0;
	this.wtcWorkingHrsBand1 = 0; 
	this.wtcWorkingHrsBand2 = 0;
	this.wtcAgeBand = 0;
	this.ctcIncomeThreshold = 0;
	this.ctcMax = 0;
	
	this.ChildBenefitSingle = 0;
	this.ChildBenefitCouple = 0;
	this.ChildBenefitAdditional = 0;
	
	//Indirect Tax
	this.VAT = 0;
	//Beer
	this.BeerTaxRate = 0;
	this.BeerAlcohol = 0;
	this.BeerUnitSize = 0;
	this.BeerUnitPrice = 0;
	//Wine
	this.WineTaxRate = 0;
	this.WineAlcohol = 0;
	this.WineUnitSize = 0;
	this.WineUnitPrice = 0;
	//Spirits
	this.SpiritsTaxRate = 0;
	this.SpiritsAlcohol = 0;
	this.SpiritsUnitSize = 0;
	this.SpiritsUnitPrice = 0;
	//Cigarettes
	this.CigarettesTaxRate = 0;
	this.CigarettesUnitSize = 0;
	this.CigarettesAdValorum = 0;
	this.CigarettesUnitPrice = 0;
	//Fuel
	this.PetrolUnitSize = 0;
	
	this.UnleadedTaxRate = 0;
	this.DieselTaxRate = 0;
	this.LPGTaxRate = 0;
	
	this.UnleadedUnitPrice = 0;
	this.DieselUnitPrice = 0;
	this.LPGUnitPrice = 0;
	
	this.UnleadedDensity = 0;
	this.DieselDensity = 0;
	this.LPGDensity = 0;
	
	this.FlightShortLevy = 0;
	this.FlightShortPremiumLevy = 0;
	this.FlightLongLevy = 0;
	this.FlightLongPremiumLevy = 0;
	
	this.MaxCompanyCarValue = 0;
	
	this.MinCompanyCarBenefitPercentDiesel = 0;
	this.MinCompanyCarBenefitPercent = 0;
	this.MinCompanyCarEmissions = 0;
	this.MaxCompanyCarBenefitPercent =0;
	
	this.FuelBenefitAmount = 0;
	
	this.VanTax = 0;
	this.VanFuelBenefitTax = 0;
	
}

KPMGBudgCalc.Constants.prototype.ShowConstants = function()	{	

	var retval = '';
	retval += '<b><u>Income Tax Constants</b></u><br>';
	retval += '<table border = 1><tr><td>Upper Limit</td><td>Employment Tax Rate</td><td>Dividend Tax Rate</td><td>UK Savings Tax Rate</td></tr>';
	for( var i =0; i < this.bands.length; i ++ )
	{		
		retval += '<tr><td>' + this.bands[i].amount + '</td><td>' + this.bands[i].rates[0] + '</td><td>' + this.bands[i].rates[1] + '</td><td>' + this.bands[i].rates[2] + '</td></tr>';
	}
	retval += '</table>'
	retval += '<p></p>'
	retval += '<b><u>National Insurance Constants</b></u><br>';
	retval += '<table border = 1><tr><td>Upper Limit</td><td>Rate</td></tr>';
	for( var i =0; i < this.NIBands.length; i ++ )
	{		
		retval += '<tr><td>' + this.NIBands[i].amount + '</td><td>' + this.NIBands[i].rates[0] + '</td></tr>';
	}
	retval += '</table>'
	retval += '<p></p>'
	retval += '<b><u>Road Tax Bands</b></u><br>';
	retval += '<table border = 1><tr><td>Band</td><td>Unleaded</td><td>Diesel</td><td>LPG</td></tr>';
	for( var i =0; i < this.RoadTaxBands.length; i ++ )
	{		
		retval += '<tr><td>' + this.RoadTaxBands[i].BandName + '</td><td>' + this.RoadTaxBands[i].Unleaded + '</td><td>' + this.RoadTaxBands[i].Diesel +'</td><td>' + this.RoadTaxBands[i].LPG +'</td></tr>';
	}
	retval += '</table>'
	retval += '<p></p>'
	retval += '<b><u>Tax Constants</b></u><br>';
	retval += '<table><tr><td>Constant</td><td>Value</td></tr>';
	retval += '<tr><td>Male Max NIAge</td><td>' + this.MaleMaxNIAge + '</td></tr>';
	retval += '<tr><td>Female Max NIAge</td><td>' + this.FemaleMaxNIAge + '</td></tr>';
	retval += '<tr><td>MaleMinPensionAge</td><td>' + this.MaleMinPensionAge + '</td></tr>';
	retval += '<tr><td>FemaleMinPensionAge</td><td>' + this.FemaleMinPensionAge + '</td></tr>';
	retval += '<tr><td>StatePensionSingle</td><td>' + this.StatePensionSingle + '</td></tr>';
	
	retval += '<tr><td>AgeForBandExtensionLower</td><td>' + this.AgeForBandExtensionLower + '</td></tr>';
	
	retval += '<tr><td>ElderlyBandExtensionLower</td><td>' + this.ElderlyBandExtensionLower + '</td></tr>';
	retval += '<tr><td>AgeForBandExtensionUpper</td><td>' + this.AgeForBandExtensionUpper + '</td></tr>';
	retval += '<tr><td>ElderlyBandExtensionUpper</td><td>' + this.ElderlyBandExtensionUpper + '</td></tr>';
	retval += '<tr><td>ElderlyBandExtensionLimit</td><td>' + this.ElderlyBandExtensionLimit + '</td></tr>';
	retval += '<tr><td>AbatementRate</td><td>' + this.AbatementRate + '</td></tr>';
	
	retval += '<tr><td>wtcBasic</td><td>' + this.wtcBasic + '</td></tr>';
	retval += '<tr><td>wtcCouple</td><td>' + this.wtcCouple + '</td></tr>';
	retval += '<tr><td>wtc30hrsSupplement</td><td>' + this.wtc30hrsSupplement + '</td></tr>';
	
	retval += '<tr><td>wtcIncomeThreshold</td><td>' + this.wtcIncomeThreshold + '</td></tr>';
	retval += '<tr><td>ctcFamilyThreshold</td><td>' + this.ctcFamilyThreshold + '</td></tr>';
	retval += '<tr><td>ctcChild</td><td>' + this.ctcChild + '</td></tr>';
	retval += '<tr><td>wtcWorkingHrsBand1</td><td>' + this.wtcWorkingHrsBand1 + '</td></tr>';
	retval += '<tr><td>wtcWorkingHrsBand2</td><td>' + this.wtcWorkingHrsBand2 + '</td></tr>';
	retval += '<tr><td>wtcAgeBand</td><td>' + this.wtcAgeBand + '</td></tr>';
	retval += '<tr><td>ctcIncomeThreshold</td><td>' + this.ctcIncomeThreshold + '</td></tr>';
	retval += '<tr><td>ctcMax</td><td>' + this.ctcMax + '</td></tr>';
	
	retval += '<tr><td>ChildBenefitSingle</td><td>' + this.ChildBenefitSingle + '</td></tr>';
	retval += '<tr><td>ChildBenefitCouple</td><td>' + this.ChildBenefitCouple + '</td></tr>';
	retval += '<tr><td>ChildBenefitAdditional</td><td>' + this.ChildBenefitAdditional + '</td></tr>';
	retval += '</table>'; 
	retval += '<p></p>'
	retval += '<b><u>Indirect Tax Constants</b></u><br>';
	retval += '<table><tr><td>Constant</td><td>Value</td></tr>';
	retval += '<tr><td>VAT</td><td>' + this.VAT + '</td></tr>';
	retval += '<tr><td>BeerTaxRate</td><td>' + this.BeerTaxRate + '</td></tr>';
	retval += '<tr><td>BeerAlcohol</td><td>' + this.BeerAlcohol + '</td></tr>';
	retval += '<tr><td>BeerUnitSize</td><td>' + this.BeerUnitSize + '</td></tr>';
	retval += '<tr><td>BeerUnitPrice</td><td>' + this.BeerUnitPrice + '</td></tr>';
	retval += '<tr><td>WineTaxRate</td><td>' + this.WineTaxRate + '</td></tr>';
	retval += '<tr><td>WineAlcohol</td><td>' + this.WineAlcohol + '</td></tr>';
	retval += '<tr><td>WineUnitSize</td><td>' + this.WineUnitSize + '</td></tr>';
	retval += '<tr><td>WineUnitPrice</td><td>' + this.WineUnitPrice + '</td></tr>';
	retval += '<tr><td>SpiritsTaxRate</td><td>' + this.SpiritsTaxRate + '</td></tr>';
	retval += '<tr><td>SpiritsAlcohol</td><td>' + this.SpiritsAlcohol + '</td></tr>';
	retval += '<tr><td>SpiritsUnitSize</td><td>' + this.SpiritsUnitSize + '</td></tr>';
	retval += '<tr><td>SpiritsUnitPrice</td><td>' + this.SpiritsUnitPrice + '</td></tr>';
	retval += '<tr><td>CigarettesTaxRate</td><td>' + this.CigarettesTaxRate + '</td></tr>';
	retval += '<tr><td>CigarettesUnitSize</td><td>' + this.CigarettesUnitSize + '</td></tr>';
	retval += '<tr><td>CigarettesAdValorum</td><td>' + this.CigarettesAdValorum + '</td></tr>';
	retval += '<tr><td>CigarettesUnitPrice</td><td>' + this.CigarettesUnitPrice + '</td></tr>';
	retval += '<tr><td>PetrolUnitSize</td><td>' + this.PetrolUnitSize + '</td></tr>';
	retval += '<tr><td>UnleadedTaxRate</td><td>' + this.UnleadedTaxRate + '</td></tr>';
	retval += '<tr><td>DieselTaxRate</td><td>' + this.DieselTaxRate + '</td></tr>';
	retval += '<tr><td>LPGTaxRate</td><td>' + this.LPGTaxRate + '</td></tr>';
	retval += '<tr><td>UnleadedUnitPrice</td><td>' + this.UnleadedUnitPrice + '</td></tr>';
	retval += '<tr><td>DieselUnitPrice</td><td>' + this.DieselUnitPrice + '</td></tr>';
	retval += '<tr><td>LPGUnitPrice</td><td>' + this.LPGUnitPrice + '</td></tr>';
	retval += '<tr><td>UnleadedDensity</td><td>' + this.UnleadedDensity + '</td></tr>';
	retval += '<tr><td>DieselDensity</td><td>' + this.DieselDensity + '</td></tr>';
	retval += '<tr><td>LPGDensity</td><td>' + this.LPGDensity + '</td></tr>';
	retval += '<tr><td>FlightShortLevy</td><td>' + this.FlightShortLevy + '</td></tr>';
	retval += '<tr><td>FlightShortPremiumLevy</td><td>' + this.FlightShortPremiumLevy + '</td></tr>';
	retval += '<tr><td>FlightLongLevy</td><td>' + this.FlightLongLevy + '</td></tr>';
	retval += '<tr><td>FlightLongPremiumLevy</td><td>' + this.FlightLongPremiumLevy + '</td></tr>';
	retval += '<tr><td>MaxCompanyCarValue</td><td>' + this.MaxCompanyCarValue + '</td></tr>';
	retval += '<tr><td>MinCompanyCarBenefitPercentDiesel</td><td>' + this.MinCompanyCarBenefitPercentDiesel + '</td></tr>';
	retval += '<tr><td>MinCompanyCarBenefitPercent</td><td>' + this.MinCompanyCarBenefitPercent + '</td></tr>';
	retval += '<tr><td>MinCompanyCarEmissions</td><td>' + this.MinCompanyCarEmissions + '</td></tr>';
	retval += '<tr><td>MaxCompanyCarBenefitPercent</td><td>' + this.MaxCompanyCarBenefitPercent + '</td></tr>';
	retval += '<tr><td>MaxCompanyCarBenefitPercent</td><td>' + this.FuelBenefitAmount + '</td></tr>';
	
	
	retval += '</table>';
	
	return retval;
};

KPMGBudgCalc.Constants.prototype.GetBands = function()	{	
	return this.bands;
};

KPMGBudgCalc.Constants.prototype.ResetNIBands = function()	{	
	
};

KPMGBudgCalc.Constants.prototype.GetSpouseBands = function()	{	
	return this.spousebands;
};

KPMGBudgCalc.Constants.prototype.MaxNIAge = function(Sex)	{	
	var retval = this.MaleMaxNIAge;
	if ( Sex == "F" )
	{
		retval = this.FemaleMaxNIAge;
	}
	return retval;
};

KPMGBudgCalc.Constants.prototype.GetNIBands = function()	{	
	return this.NIBands;
};

KPMGBudgCalc.Constants.prototype.GetPension = function(Age, Sex)	{
	var retval = 0;
	var MaxPensionAge = this.MaleMinPensionAge;
	
	if ( Sex == "F" )
	{
		MaxPensionAge = this.FemaleMinPensionAge
	}
	
	if ( Age >= MaxPensionAge )
	{
		retval = this.StatePensionSingle;
	}
		
	if ( Age >= this.StatePensionExtraAge)
	{
		retval += this.StatePensionExtra;		
	}	

	return retval;
};	

KPMGBudgCalc.Constants.prototype.GetSpousePension = function(SpouseAge, SpouseSex, Age, Sex)	{
	var retval = 0;
		
	// Original (self)
	var MaxPensionAge = this.MaleMinPensionAge;	
	if ( Sex == "F" )
	{
		MaxPensionAge = this.FemaleMinPensionAge
	}
	
	// Spouse
	var SpousePensionAge = this.MaleMinPensionAge;	
	if ( SpouseSex == "F" )
	{
		SpousePensionAge = this.FemaleMinPensionAge
	}		
	
	if ( Age >= MaxPensionAge  && SpouseAge >= SpousePensionAge)
	{	
		retval = this.StatePensionMarried;
	}
	else if (SpouseAge >= SpousePensionAge)
	{	
		retval = this.StatePensionSingle;
	}			
	
	if ( SpouseAge >= this.StatePensionExtraAge)
	{
		retval += this.StatePensionExtra;		
	}				
	return retval;
};	

KPMGBudgCalc.Constants.prototype.GetCompanyCarBen = function(CompanyCars)	{
	var retval = 0;
	var firstCar = true;
	var percent = 0;
		
	for ( var i = 0; i < CompanyCars.length; i++ )
	{
		if ( CompanyCars[i].ListPrice > 0 && CompanyCars[i].CO2 > 0 )
		{
			if ( CompanyCars[i].ListPrice > this.MaxCompanyCarValue )
			{
				CompanyCars[i].ListPrice = this.MaxCompanyCarValue;
			}
		
			percent = this.CarBenPercent(CompanyCars[i].CO2, CompanyCars[i].Euro4 );	
			retval += percent * CompanyCars[i].ListPrice;
			firstCar = false;
		
			if ( CompanyCars[i].FuelProvided)
			{
				retval += this.FuelBenefitAmount * percent; 
			}
		}
	}
	return retval;
};	


KPMGBudgCalc.Constants.prototype.GetCompanyVanBen = function(CompanyVans)	{
	var retval = 0;
		for ( var i = 0; i < CompanyVans.length; i++ )
		{
			retval += this.VanTax;
			if ( CompanyVans[i].FuelProvided)
			{
				retval += this.VanFuelBenefitTax;
			}
		}
	return retval;
};	



KPMGBudgCalc.Constants.prototype.CarBenPercent = function(emissions, diesel)	{
	var retval = 0;
	var firstCar = false;
	var percent = 0;
	
	if ( diesel )
	{
		percent = this.MinCompanyCarBenefitPercent;
		
	}
	else
	{
		percent = this.MinCompanyCarBenefitPercentDiesel;
	}
	retval = Math.min((Math.max(Math.floor((emissions - this.MinCompanyCarEmissions)/5.0),0)) + percent, this.MaxCompanyCarBenefitPercent);
	retval = retval / 100;
	
	return retval;
};	


KPMGBudgCalc.Constants.prototype.AdjustBands = function(Age, Income, band)	{

	var adjustment = 0;
	var abatement = 0;
	
	if ( Age < this.AgeForBandExtensionLower )
	{	return band;	}
	
	
	if ( Age >= this.AgeForBandExtensionUpper)
	{
		adjustment = this.ElderlyBandExtensionUpper;
	}
	else
	{
		adjustment = this.ElderlyBandExtensionLower;
	}
	
	abatement = this.getAbatement(Income);
	
	if ( adjustment > abatement )
	{
		band += adjustment;
		band -= abatement;
	}
	
	return band;
	
};

KPMGBudgCalc.Constants.prototype.MCABands = function(Age,  band)	{

	var adjustment = 0;
	var abatement = 0;
	
	if ( Age < this.AgeForBandExtensionLower )
	{	return band;	}
	
	
	if ( Age >= this.AgeForBandExtensionUpper)
	{
		adjustment = this.ElderlyBandExtensionUpper;
	}
	else
	{
		adjustment = this.ElderlyBandExtensionLower;
		
	}
	
	
	
	if ( adjustment > abatement )
	{
		band += adjustment;
		
	}
	
	return band;
	
};


KPMGBudgCalc.Constants.prototype.getAbatement = function(Income)	{
	var retval = 0;
	if (Income > this.ElderlyBandExtensionLimit)
	{
		retval = this.AbatementRate * (Income - this.ElderlyBandExtensionLimit);
	}
	return retval;
};

KPMGBudgCalc.Constants.prototype.GetPetrolTaxRate = function(FuelType)	{	
	var retval = this.UnleadedTaxRate;
	
	if ( FuelType == "L" )
	{
		retval = this.LPGTaxRate
	}
	else if ( FuelType == "D" )
	{
		retval = this.DieselTaxRate
	
	}
	return retval;
};
	
KPMGBudgCalc.Constants.prototype.GetPetrolUnitPrice = function(FuelType)	{	
	var retval = this.UnleadedUnitPrice;
	
	if ( FuelType == "L" )
	{
		retval = this.LPGUnitPrice
	}
	else if ( FuelType == "D" )
	{
		retval = this.DieselUnitPrice
	
	}
	return retval;
};
	
KPMGBudgCalc.Constants.prototype.GetPetrolDensity = function(FuelType)	{	
	var retval = this.UnleadedDensity;
	
	if ( FuelType == "L" )
	{
		retval = this.LPGDensity
	}
	else if ( FuelType == "D" )
	{
		retval = this.DieselDensity
	
	}
	return retval;
};
	
KPMGBudgCalc.Constants.prototype.SetLevies = function(Levies)	{
	Levies.Beer.Levy = (this.BeerTaxRate * this.BeerAlcohol * this.BeerUnitSize) + ((this.VAT/ (1 + this.VAT)) * this.BeerUnitPrice );
	Levies.Wine.Levy = (this.WineTaxRate * this.WineAlcohol * this.WineUnitSize) + ((this.VAT/ (1 + this.VAT)) * this.WineUnitSize * this.WineUnitPrice );
	Levies.Spirits.Levy = (this.SpiritsTaxRate * this.SpiritsAlcohol * this.SpiritsUnitSize) + ((this.VAT/ (1 + this.VAT)) * this.SpiritsUnitSize * this.SpiritsUnitPrice );
	Levies.Cig.Levy = ( (this.CigarettesTaxRate/1000) * this.CigarettesUnitSize )+ ( (this.CigarettesAdValorum/100) * this.CigarettesUnitPrice) + ((this.VAT/(1 + this.VAT))* this.CigarettesUnitPrice);
	Levies.Fuel.Levy = (this.GetPetrolTaxRate(Levies.Fuel.Type) * this.PetrolUnitSize * 1000 / this.GetPetrolDensity(Levies.Fuel.Type) )+ ((this.VAT/(1 + this.VAT))* this.GetPetrolUnitPrice(Levies.Fuel.Type));
	Levies.FlightsShort.Levy = this.FlightShortLevy;
	Levies.FlightsShortPremium.Levy = this.FlightShortPremiumLevy;
	Levies.FlightsLong.Levy = this.FlightLongLevy;
	Levies.FlightsLongPremium.Levy  = this.FlightLongPremiumLevy;
};


KPMGBudgCalc.Constants.prototype.totalChildBenefit = function(NoChildren, Status){
	var retval = 0;
	
	if (NoChildren>0)
	{
		//First child
		//If single, then ChildBenefitSingle, otherwise ChildBenefitCouple
		if (Status != "M")
		{
			retval = this.ChildBenefitSingle;
		}
		else
		{
			retval = this.ChildBenefitCouple;
		}
		//Additional children
		//If more than one child, add ChildBenefitAdditional for each additional child
		retval += (NoChildren - 1) * this.ChildBenefitAdditional;
	}
	return retval;
};

KPMGBudgCalc.Constants.prototype.getMCATaxCredit = function(Status, Age,  spouseAge, income)	{
	var retval = 0;	
	var allowance = 0;
	var minimum = 0;
	var abatement = 0;	
	

	
	if (Status == "M")
	{
		
		// Allowance is based on age of older of couple
		if ( Math.max(Age, spouseAge) >= this.mcaUpperAgeLimit) 
		{
			allowance = this.mcaUpperAmount;
			minimum = this.mcaUpperMinimumAmount;
		} 
		else if ( Math.max(Age, spouseAge) >= this.mcaLowerAgeLimit) 
		{
			allowance = this.mcaLowerAmount;
			minimum = this.mcaLowerMinimumAmount;	
		}
	
		// Apply the abatement
		if (allowance > 0)
		{			
			abatement = this.getMcaAbatement(income, this.mcaIncomeThreshold);
		}

		// Apply the tax rate to the allowance
		retval = Math.max((allowance - abatement) * this.mcaTaxRate, 0);
		retval = Math.max(retval, minimum * this.mcaTaxRate, 0);		
	}
	return retval;		
};

KPMGBudgCalc.Constants.prototype.getMcaAbatement = function(income, incomeThreshold)	{
	var retval = 0;
	if (income > incomeThreshold)
	{
		retval = 0.5 * (income - incomeThreshold);
	}
	
	return retval;
};





KPMGBudgCalc.Constants.prototype.getTaxCredits = function(NoChildren, Status, Hours, spouseHours, TotalIncome,  Age,  spouseAge)	{
	var retval = 0;
	var wtc = 0;
	var ctc = 0;
	var familyCTC = 0;
	var entitled = false;
	var maxWTC = 0;
	var maxChildCTC = 0;
	var ctcThreshold = 0;
	
	// Ignore spouse hours/details if Single or Divorces
	if ( Status == "S" || Status == "D" )
	{
		spouseHours = 0;		
		spouseAge = 0;
	}
	
	if (NoChildren > 0)
	{	entitled = ( (Hours >=this.wtcWorkingHrsBand1 && Age >= this.wtcQualifyingAge )|| (spouseHours >=this.wtcWorkingHrsBand1  && Age >= this.wtcQualifyingAge) );	}
	else
	{	entitled = ((Hours >=this.wtcWorkingHrsBand2 && Age > this.wtcAgeBand) || (spouseHours >=this.wtcWorkingHrsBand2 && spouseAge >this.wtcAgeBand));	}
	

	maxWTC = this.wtcBasic;
	if (entitled)
	{
		if  ((NoChildren > 0) || (Status == "M" || Status == "C"))
		{	maxWTC += this.wtcCouple;	}

		//if  ((NoChildren > 0) || (Status == "S" || Status == "D"))
		//{	maxWTC += this.wtcLoneParent;	}
		
		if ((Hours >=this.wtcWorkingHrsBand2) || (spouseHours >=this.wtcWorkingHrsBand2))
		{	maxWTC += this.wtc30hrsSupplement;	}
		else if((Hours + spouseHours >= this.wtcWorkingHrsBand2) && (NoChildren > 0) && ( Hours >= this.wtcWorkingHrsBand1 || spouseHours >= this.wtcWorkingHrsBand1))
		{	maxWTC += this.wtc30hrsSupplement;	}
		
		if (TotalIncome <= this.wtcIncomeThreshold)		
		{	wtc = maxWTC;	}
		else if (TotalIncome > this.ctcFamilyThreshold) { wtc = -1; } else
		{	wtc = maxWTC - this.wtcFirstWithdrawalRate  * (TotalIncome - this.wtcIncomeThreshold);	}  //YJ changed
	}

	if (wtc < 0) {wtc=0;}
	if (NoChildren > 0)
	{
		familyCTC  = this.ctcMax;

		if (TotalIncome > this.ctcFamilyThreshold)
		{
			//If combined salary > Threshold, then reduce by 1/15 of the excess
			familyCTC -= (TotalIncome - this.ctcFamilyThreshold)/15;
		}
		if (familyCTC < 0) {familyCTC=0;}
				
		//Child Element
		//Max entitlement for each child
		maxChildCTC = NoChildren * this.ctcChild;
		//Do you satisfy requirement for WTC, even if £0 given? ->Satifies requirement for Child element
		if (entitled == true)
		{
			//Threshold = WTC Income level + Max WTC/37
			ctcThreshold = this.wtcIncomeThreshold + (maxWTC / this.wtcFirstWithdrawalRate);
		}
		else
		{
			ctcThreshold = this.ctcIncomeThreshold;			
		}
		
		ctc = maxChildCTC;
		if (TotalIncome > ctcThreshold)		
		{
			//Reduce CTC by 37% of the excess combined salary over the threshold
			ctc -= this.wtcFirstWithdrawalRate * (TotalIncome - ctcThreshold);
			if (ctc < 0) {ctc=0;}
		}
		
	}
	retval = wtc + ctc + familyCTC;
	return retval;
};



/****************************************************************/




























