
function buildResultsTable( fam, res1, res2, base, indexed )
{
        var resultText = "";

        var gain = -(res2.totalTax - res1.totalTax);
        // The amount the family is better off -
        // overall result. If res2.totalTax is more than
        // res1.totalTax, then the family is worse off,
        // hence the negation.

        resultText += "<strong>";
        if ( gain > 0 ) {
          resultText += "You are " + mformat(gain) + " per week better off";
        }
        else if ( gain < 0 ) {
          resultText += "You are " + mformat(-gain) + " per week worse off";
        }
        else {
          resultText += "There is no change in your circumstances";
        }      
        resultText += "</strong><br><br>";

       	resultText += "<strong>Taxes On Income</strong><br><img src=\"/shared/img/o.gif\" width=\"1\" height=\"4\"><br>";
	resultText += buildDirectTable( fam, res1, res2 );
	
	resultText += "<p><strong>Taxes On Spending</strong><br><img src=\"/shared/img/o.gif\" width=\"1\" height=\"4\"><br>";
	resultText += buildSpendingTable( fam, res2 );

        resultText += "<p>";
        resultText += buildBaseAndIndexationFootnote( base, indexed );
        resultText += "</p>";

        return resultText;
}


/* Argument base:    string, indicates the type of
                     base system.
   Argument indexed: 1 or 0, indicates whether or not
                     to index.
   Result: a string, which is a footnote about the
   indexation and base. This string is a sentence or
   two, not including enclosing paragraph markers and
   other HTML.
*/
function buildBaseAndIndexationFootnote( base, indexed )
{
  var text = "";

  text += "<p/><p><font size='-1'><em>These calculations take into account ";
  if( "BASE1" == base ) { // include preannounced gains/losses
    if( indexed == 1 ) { // ignore indexation
      text += " all changes coming into effect this April, including those announced last year.";
      text += " However, some of the things announced merely adjust the tax system for inflation."; 
      text += " Our 'before' calculation assumes the inflation adjustment to the tax system has already happened, and so they do not count these as real gains.";
    } 
    else {  // allow indexation
      text += " all changes coming into effect this April, including those announced last year.";
      text += " Some of the things announced merely adjust the tax system for inflation."; 
      text += " Our calculations count these as real gains.";
    }
  } 
  else {  // ignore preannounced gains/losses
    if( indexed == 1 ) {
      text += " just the new tax measures announced this month (and not the National Insurance rise, for example)."; 
      text += " Some of the things announced merely adjust the tax system for inflation."; 
      text += " Our 'before' calculation assumes the inflation adjustment to the tax system and the pre-announced changes have already happened, and so they do not show up as real gains.";
    } 
    else {
      text += " just the new tax measures announced today (and not the National Insurance rise, for example)."; 
      text += " Some of the things announced merely adjust the tax system for inflation."; 
      text += " Our calculations count these as real gains.";
    }
 }
 text += "<p>If you like, you can use the two bottom options on the first page to control exactly what's counted in here.</p></em></font>"; 
 
  return text;
}

/* Argument fam:  a family structure.
   Argument res1: a 'results' structure returned by
                  the tax calculator for the pre-Budget system.
   Argument res2: a 'results' structure returned by
                  the tax calculator for the post-Budget system.
   Result:        a string containing the direct taxation
                  (income tax and national insurance) table.
*/
function buildDirectTable( fam, res1, res2 )
{       var tabstr = "";
	if( ( res1.ni[0] != 0 ) || ( res2.ni[0] != 0 ) || ( res1.incomeTax[0] != 0 ) || ( res2.incomeTax[0] != 0 ) ){
                tabstr += "<p><table cellpadding=\"1\" cellspacing=\"1\" class=\"bordercol\" width=\"629\">\n\n";
	        tabstr += '<tr class=\"bgcola\"><td colspan="4" class="alt-row">Your Results &#163; p.w.</td></tr>\n';
	        tabstr += '<tr class=\"bgcola\" class="alt-row"><td class=\"bgcolb\" width=\"300\">&nbsp;</td><td class=\"bgcolb\" width=\"100\"><strong>Before</strong></td><td class=\"bgcolb\" width=\"100\"><strong>After</strong></td><td class=\"bgcolb\" width=\"129\"><strong>Change</strong></td></tr>';
	        var i = 0;
	        if( ( res1.ni[0] != 0 ) || ( res2.ni[0] != 0 )){
		        tabstr += build3DiffRow( "National Insurance",res1.ni[0], res2.ni[0],i );
		        i++;
	        }
	
	        if( ( res1.incomeTax[0] != 0 ) || ( res2.incomeTax[0] != 0 )){
		        tabstr += build3DiffRow( "Income Tax",res1.incomeTax[0], res2.incomeTax[0],i );
		        i++;
	        }
                tabstr += "</table>\n<p/>";
	}
	if( ( fam.numAdults == 2 ) && ( ( res1.ni[1] != 0 ) || ( res2.ni[1] != 0 ) || ( res1.incomeTax[1] != 0 ) || ( res2.incomeTax[1] != 0 ) )){
		tabstr += "<table cellpadding=\"1\" cellspacing=\"1\" class=\"bordercol\" width=\"629\"><tr class=\"bgcola\"><td colspan='4' class='alt-row'>Your Spouse's Results &#163; p.w.</td></tr>";
		tabstr += '<tr class=\"bgcola\" class="alt-row"><td class=\"bgcolb\" width=\"300\">&nbsp;</td><td class=\"bgcolb\" width=\"100\"><strong>Before</strong></td><td class=\"bgcolb\" width=\"100\"><strong>After</strong></td><td class=\"bgcolb\" width=\"129\"><strong>Change</strong></td></tr>';
		i = 0;
		if( ( res1.ni[1] != 0 ) || ( res2.ni[1] != 0 )){
			tabstr += build3DiffRow( "National Insurance",res1.ni[1], res2.ni[1],i );
			i++;
		}
	
		if( ( res1.incomeTax[1] != 0 ) || ( res2.incomeTax[1] != 0 )){
			tabstr += build3DiffRow( "Income Tax",res1.incomeTax[1], res2.incomeTax[1],i );
			i++;
		}
                tabstr += '</table>\<p/>\n';
	}
        
	i++;
        
	tabstr += "<table cellpadding=\"1\" cellspacing=\"1\" class=\"bordercol\" width=\"629\"><tr class=\"bgcola\"><td colspan='4' class='alt-row'>Total Direct Taxes &#163; p.w.</td></tr>";
	tabstr += '<tr class=\"bgcola\" class="alt-row"><td class=\"bgcolb\" width=\"300\">&nbsp;</td><td class=\"bgcolb\" width=\"100\"><strong>Before</strong></td><td class=\"bgcolb\" width=\"100\"><strong>After</strong></td><td class=\"bgcolb\" width=\"129\"><strong>Change</strong></td></tr>';
        tabstr += build3DiffRow( "Total Direct Taxes",res1.direct, res2.direct, 999 );
	tabstr += '</table>\n';
	
        return tabstr;
}


/* Argument fam:  a family structure.
   Argument res : a 'results' structure returned by
                  the tax calculator for the post-Budget system.
   Result:        a string containing the expenditure table.
*/
function buildSpendingTable( fam, res )
{
	var i = 0;
	tabstr = "\n<table cellpadding=\"1\" cellspacing=\"1\" class=\"bordercol\" width=\"629\">\n";
	
	if( ( fam.fags != 0 ) || ( res.fags != 0 )){
		tabstr += buildDiffRow( "Cigarettes",res.fags,i );
		i++;		
	}
	
	if( ( fam.beer != 0 ) || ( res.beer != 0 )){
		tabstr += buildDiffRow( "Beer",res.beer,i );
		i++;		
	}
	
	if( ( fam.wine != 0 ) || ( res.wine != 0 )){
		tabstr += buildDiffRow( "Wine",res.wine, i );
		i++;		
	}
	
	if( ( fam.spirits != 0 ) || ( res.spirits != 0 )){
		tabstr += buildDiffRow( "Spirits",res.spirits, i );
		i++;		
	}
	
	if( ( fam.petrol != 0 ) || ( res.petrol != 0 )){
		tabstr += buildDiffRow( "Petrol",res.petrol, i );
		i++;		
	}
	
	tabstr += buildDiffRow( "Total Indirect Taxes",res.indirect, 999 );
	
	tabstr += "</table>";
	return tabstr;
}


function buildTableClass( i )
{
	if((i == 999 ))return "summation-row";
	if((i % 2 ) == 0) return "row";
	return "alt-row";	
}


/* Argument: real, the amount some quantity has changed
             after the Budget.
   Result:   string, a phrase saying this.
*/
function buildDiffStr( x )
{
	if( x == 0.0 ) return "no change";
	if( x < 0.0 ) return mformat( -1.0*x )+ " less";	
	if( x > 0.0 ) return mformat( x )+ " more";	
}


function buildDiffRow( name, x, i )
{
	tabstr = "<tr class=\"bgcolb\" class='"+buildTableClass(i)+"'>\n"
	tabstr += "<td class=\"bgcolb\" width=\"317\">"+name+"</td>";
	tabstr += "  <td class=\"bgcolb\" class='money-cell'>"+buildDiffStr( x )+"</td>\n</tr>\n";
	return tabstr;
}


function build3DiffRow( name, x1, x2, i )
{
	tabstr = "<tr class='"+buildTableClass(i)+"'>\n"
	tabstr += "<td class=\"bgcolb\"><strong>"+name+"</strong></td>";
	tabstr += "<td class=\"bgcolb\" class='money-cell'>"+mformat(x1)+"</td>\n";
	tabstr += "<td class=\"bgcolb\" class='money-cell'>"+mformat(x2)+"</td>\n";
	tabstr += "<td class=\"bgcolb\" class='money-cell'>"+buildDiffStr( x2-x1 )+"</td>\n";
	return tabstr;
}


/* Argument diff: real, the amount the user
                  is better off after the 
                  Budget.
   Result:        string, an <h1> heading
                  about this.
*/
function buildHeadline( diff )
{
	var result = "<h1 class='headerwords'>You Are ";
        /*alert( 'buildHeadline; diff='+diff );*/
	if((diff > -0.005 ) && ( diff < 0.005 )){
		result += 'unaffected';	
	} else {
		/* alert( 'difference = '+diff ); */
		if( diff < 0.0 ){
			result += mformat( -1.0*diff );
			result += " better off";
		} else {
			result += mformat( diff );
			result += " worse off";
		}
	} 
	result += "</h1>\n";	
	return result;
}


/* Argument: a real.
   Result:   a string.
   Returns the argument formatted to two
   decimal places, with a pounds sign in front.
*/
function mformat( number )
{
  POUND = unescape('%A3');
  return formatCurrency( number, 2, POUND );
}


