window.onload=init;


function init() {
	
	first_time=0;
	chart_on="no";
	numb = '0123456789';
/*	var input_tags = document.getElementById("calc_form").getElementsByTagName("input");
	for(i in input_tags){
		input_tags[i].onblur=calculate;	
	}*/

	var input_tags = document.getElementById("calc_form").getElementsByTagName("input");
	for(i in input_tags){
		input_tags[i].value="";	
	}
	
	document.getElementById("run_calc").onclick=validate_input;
	document.getElementById("reset_calc").onclick=reset_calc;
	for (i=1; i<=6; i++){
		document.getElementById("tt_area_"+i).onclick=calc_pop;
	}

//Pre-loadng popup header images
popim1 = new Image(); 
popim1.src = "../images_fr/pop_head_sign_question.gif";
popim2 = new Image(); 
popim2.src = "../images_fr/pop_head_text_help.gif";
popim3 = new Image(); 
popim3.src = "../images_fr/pop_head_sign_exclamation.gif";
popim4 = new Image(); 
popim4.src = "../images_fr/pop_head_text_error.gif";


}
function isValid(parm,val) {
  if (parm == "") return false;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}

function isNum(parm) {return isValid(parm,numb);}


function validate_input(){
	
	captureLinkAlt(document.location.href,'','','action=EO3410_lp1f_calculate'); // used for omniture metrics		
	
	var an_val=document.getElementById("investment_annual").value;
	var mo_val=document.getElementById("investment_monthly").value;
	var lu_val=document.getElementById("lump_sum").value;	
	
	if ((an_val!="" && !isNum(an_val)) || (mo_val!="" && !isNum(mo_val)) || (lu_val!="" && !isNum(lu_val))){
	calc_pop("not_an_integer");
	} else {
		
		if (an_val!="" && mo_val!=""){
		calc_pop("annual_and_monthly_entered");
		} else {
			
			if (an_val!=""){
				if (parseInt(an_val) > 5000){
					calc_pop("over_annual_limit");
				} else if (lu_val !="") {
					if (parseInt(lu_val) > 25000){
						calc_pop("over_lump_limit");
					} else {
					calculate();
					}
				} else {
					calculate();	
				}
			}


			if (mo_val!=""){
				if (parseInt(mo_val) > 416){
					calc_pop("over_monthly_limit");
				} else if (lu_val !="") {
					if (parseInt(lu_val) > 25000){
						calc_pop("over_lump_limit");
					} else {
					calculate();
					}
				} else {
					calculate();	
				}
			}


			
			if (lu_val !=""){
				if (parseInt(lu_val) > 25000){
					calc_pop("over_lump_limit");
				} else {
					calculate();
				}
			}
			
		}
	}
	

}




function blur_actions(whichone) {
	
/*	if(whichone==1){
	
		if (document.all){
			document.getElementById("toggle_wrapper").style.opacity="0.4";
		}else{
			document.getElementById("wrapper").style.opacity="0.4";
		}	
	}
	if(whichone==2){
	
		if (document.all){
			document.getElementById("toggle_wrapper").style.opacity="1";
		}else{
			document.getElementById("wrapper").style.opacity="1";
		}	
	}*/
}


function pop_actions(words, type){
		if (type=="help"){
			document.getElementById("calc_head_sign").src="../images_fr/pop_head_sign_question.gif";
			document.getElementById("calc_head_text").src="../images_fr/pop_head_text_help.gif";
		}
		if (type=="error"){
			document.getElementById("calc_head_sign").src="../images_fr/pop_head_sign_exclamation.gif";
			document.getElementById("calc_head_text").src="../images_fr/pop_head_text_error.gif";			
		}		
		
		document.getElementById("calc_pop_content").innerHTML=words;
		blur_actions(1);
		this_pop=document.getElementById("calc_pops");
		this_pop.style.visibility="visible";
		document.getElementById("close_popup").onclick=function (){this_pop.style.visibility="hidden"; blur_actions(2)};	
}

function calc_pop (tt) {
	if (this.id=="tt_area_1"){
		var pop_msg="S&#233;lectionnez une tranche de revenu dans la liste. Cette tranche de revenu servira &#224; d&#233;terminer le taux auquel seront impos&#233;s les revenus de vos placements dans des comptes non enregistr&#233;s ou des comptes enregistr&#233;s autres qu'un CELI."
		pop_actions(pop_msg, "help");
	}

	if (this.id=="tt_area_2"){
		var pop_msg="Aux fins d'utilisation du calculateur, vous pouvez choisir de verser des cotisations mensuelles ou annuelles &#224; votre CELI. Si vous optez pour des cotisations mensuelles, le calculateur ne permet pas de cotiser plus de 416 $ par mois (416 $ X 12 mois = 5 000 $, soit le plafond des cotisations annuelles)."
		pop_actions(pop_msg, "help");	
	}
	if (this.id=="tt_area_3"){
		var pop_msg="Vous pouvez verser des cotisations mensuelles ou annuelles &#224; votre CELI. Selon le gouvernement du Canada, le plafond de vos cotisations annuelles est de 5 000 $. Si vous ne versez pas le maximum annuel, vous pouvez reporter vos droits de cotisation inutilis&#233;s et vous rattraper une autre ann&#233;e."
		pop_actions(pop_msg, "help");		
	}
	if (this.id=="tt_area_4"){
		var pop_msg="Le total de vos cotisations en 2009 ne peut d&#233;passer 5 000 $. Si vous ne versez pas le maximum annuel, vous pouvez reporter vos droits de cotisation inutilis&#233;s et vous rattraper une autre ann&#233;e. Dans le cadre de la pr&#233;sente simulation, si vous avez accumul&#233; des droits de cotisation inutilis&#233;s, vous pouvez effectuer un versement forfaitaire pouvant atteindre 25 000 $. Si vous entrez un montant forfaitaire ainsi qu'une cotisation mensuelle ou annuelle, le revenu de la premi&#232;re ann&#233;e sera calcul&#233; seulement sur le montant forfaitaire. Par contre, le revenu gagn&#233; sur la cotisation mensuelle ou annuelle est calcul&#233; &#224; compter de la seconde ann&#233;e."
		pop_actions(pop_msg, "help");	
	}
	if (this.id=="tt_area_5"){
		var pop_msg="Il s'agit du rendement que vous pr&#233;voyez toucher sur vos placements. Le calculateur permet d'opter pour un taux de rendement annuel variant de 0 % &#224; 20 %. Toutefois, le rendement r&#233;el que vous obtiendrez pourrait &#234;tre diff&#233;rent."
		pop_actions(pop_msg, "help");
	}
	if (this.id=="tt_area_6"){
		var pop_msg="Il s'agit du nombre d'ann&#233;es pendant lesquelles vous pr&#233;voyez cotiser &#224; votre CELI."
		pop_actions(pop_msg, "help");	
	}	

	if (tt=="not_an_integer"){
		var pop_msg="S'il vous plait, n'inscrivez que des dollars. Les sous ne sont pas permis"
		pop_actions(pop_msg, "error");			
	}

	if (tt=="annual_and_monthly_entered"){
		var pop_msg="Veuillez entrer soit une cotisation annuelle soit une cotisation mensuelle"
		pop_actions(pop_msg, "error");			
	}	
	
	if (tt=="over_annual_limit"){
		var pop_msg="Votre cotisation annuelle ne peut d&#233;passer 5 000 $"
		pop_actions(pop_msg, "error");			
	}	
	
	if (tt=="over_monthly_limit"){
		var pop_msg="Votre cotisation mensuelle ne peut d&#233;passer 416 $"
		pop_actions(pop_msg, "error");			
	}	
	
	if (tt=="over_lump_limit"){
		var pop_msg="Le montant forfaitaire ne peut d&#233;passer 25 000 $"
		pop_actions(pop_msg, "error");			
	}	
	
	
}






function reset_calc(){
	
	captureLinkAlt(document.location.href,'','','action=EO3410_lp1f_reset'); // used for omniture metrics	

	first_time=0;
	chart_on="no";

	//document.getElementById("text_results_text_with_tfsa").innerHTML="";
	//document.getElementById("text_results_text_without_tfsa").innerHTML="";
	document.getElementById("text_results_tax_savings").innerHTML="";

	document.getElementById("with_tfsa_bar_amount").innerHTML="";
	document.getElementById("without_tfsa_bar_amount").innerHTML="";

	document.getElementById("income").value="";
	document.getElementById("investment_annual").value="";
	document.getElementById("investment_monthly").value="";
	document.getElementById("lump_sum").value="";
	document.getElementById("rate_of_return").value="";
	document.getElementById("term_of_investment").value="";
	
	document.getElementById("income").options[0].selected = true;
	document.getElementById("rate_of_return").options[0].selected = true;
	document.getElementById("term_of_investment").options[0].selected =true;

	document.getElementById("b_75").innerHTML="0 $";
	document.getElementById("b_50").innerHTML="0 $";
	document.getElementById("b_25").innerHTML="0 $";
	
	document.getElementById("table_results_wrapper").style.visibility="hidden"; 
	document.getElementById("table_back").style.visibility="hidden";
	document.getElementById("table_forward").style.visibility="hidden";				
	document.getElementById("chart_hide").style.visibility="hidden";
	document.getElementById("chart_view").style.visibility="hidden"; 

	bars_down();
}


function bars_down(){
//---------------------Start Bar Animation
    var with_tfsa_attributes = {
        height: { to: 130 }		
    };

    var without_tfsa_attributes = {
        height: { to: 130 }		
    };

	var with_tfsa_bar_anim = new YAHOO.util.Anim('with_tfsa_bar_mover', with_tfsa_attributes, .2, YAHOO.util.Easing.easeOut);
    var without_tfsa_bar_anim = new YAHOO.util.Anim('without_tfsa_bar_mover', without_tfsa_attributes, .2, YAHOO.util.Easing.easeOut);
	with_tfsa_bar_anim.onComplete.subscribe(function(){clear_bars();});

	with_tfsa_bar_anim.animate();
	without_tfsa_bar_anim.animate();
//----------------------End Bar Animation
}

function clear_bars(){
	//document.getElementById("with_tfsa_bar").style.display="none";
	//document.getElementById("without_tfsa_bar").style.display="none";
	//document.getElementById("with_tfsa_bar_interest").style.display="none";
	//document.getElementById("without_tfsa_bar_interest").style.display="none";	
	//document.getElementById("with_tfsa_bar_mover").style.display="none";
	//document.getElementById("without_tfsa_bar_mover").style.display="none";
}

function check_field(field){
	if (field == "" || field == null){
		field = 0;
		return field;
	} else {
		return parseFloat(field);	
	}	
}

function calculate(){
	principal=0;
	first_time = first_time + 1
	
	var income_tax_rate = parseFloat(document.getElementById("income").value);

	var an_invest = check_field(document.getElementById("investment_annual").value);
	var mo_invest = check_field(document.getElementById("investment_monthly").value);
	var lump_sum = check_field(document.getElementById("lump_sum").value);
	var return_rate = parseFloat(document.getElementById("rate_of_return").value);
	var investment_term = parseFloat(document.getElementById("term_of_investment").value);

	if ((an_invest != 0 && mo_invest==0)  || (lump_sum > 0)){

		if (lump_sum !=0){
			var amount = lump_sum;
			var t_amount = lump_sum;
			takeaway=an_invest;
		} else {
			var amount = an_invest;
			var t_amount = an_invest;
			takeaway=0;
		}
		
		var interest_earned = 0;
		var t_interest_earned = 0;
//declare year-end value storrage arrays		
		annual_array = new Array();
		t_annual_array = new Array();
		
		for (i=1;i <= investment_term; i++){

			if (i !== 1){
				amount = amount + an_invest;	
				t_amount = t_amount + an_invest;			
			}
			
			amount_before_interest = amount;
			amount = amount * (1 + return_rate);
			interest = amount - amount_before_interest;
			interest_earned = interest_earned + interest;
		
			t_amount_before_interest = t_amount;
			t_interest = t_amount * return_rate;
			t_interest = t_interest * (1-income_tax_rate);
			t_amount = t_amount + t_interest;
			t_interest_earned = t_interest_earned + t_interest;

/////// STORE YEAR-END Value of INVESTMENT for Table use
			annual_array[i] = amount //not taxed
			t_annual_array[i] = t_amount //taxed
		
		
		}
		principal=((an_invest) * (investment_term)) - takeaway
		
		TFSA_result = Math.round((principal) + (lump_sum) + (interest_earned));
		NOT_TFSA_result = Math.round((principal) + (lump_sum) + (t_interest_earned));
		tax_savings_result = TFSA_result - NOT_TFSA_result;		
	}
	
	if (mo_invest != 0 && an_invest==0){

		var lump_check=false;
		
		if (lump_sum !=0){
			lump_check=true;
			var amount = lump_sum;
			var t_amount = lump_sum;
			takeaway=mo_invest*12;
		} else {
			var amount = mo_invest;
			var t_amount = mo_invest;
			takeaway=0;
		}


		var interest_earned = 0;
		var t_interest_earned = 0;
		annual_return_rate=return_rate;
		return_rate=return_rate/12;

//declare year-end value storrage arrays
		annual_array = new Array();
		t_annual_array = new Array();
		
		for (a=1;a <= investment_term; a++){

			if (lump_check==true){
				amount_before_interest = amount;
				amount = amount * (1 + annual_return_rate);
				interest = amount - amount_before_interest;
				interest_earned = interest_earned + interest;

				t_amount_before_interest = t_amount;
				t_amount = t_amount * (1 + annual_return_rate);
				t_interest = t_amount - t_amount_before_interest;
				t_interest_earned = t_interest_earned + t_interest;	
				
				lump_check=false;
				
			} else {
				for (c=1; c <=12; c++){
					if ((a == 1) && (c == 1)){

					}else{
						amount = amount + mo_invest;	
						t_amount = t_amount + mo_invest;			
					}				
					amount_before_interest = amount;
					amount = amount * (1 + return_rate);
					interest = amount - amount_before_interest;
					interest_earned = interest_earned + interest;
					
					t_amount_before_interest = t_amount;
					t_amount = t_amount * (1 + return_rate);
					t_interest = t_amount - t_amount_before_interest;
					t_interest_earned = t_interest_earned + t_interest;					
				}


			}
			
			t_interest_earned= t_interest_earned * income_tax_rate;
			t_amount= (t_amount - t_interest_earned);			
			
			
/////// STORE YEAR-END Value of INVESTMENT for Table Use
			annual_array[a] = amount //not taxed
			t_annual_array[a] = t_amount //taxed			
			
		}
		
	TFSA_result = Math.round(amount);
	NOT_TFSA_result = Math.round(t_amount);
	tax_savings_result = TFSA_result - NOT_TFSA_result;		
	}


	if (chart_on=="yes") {		
		start=1;
		page=8;
		num_years=annual_array.length-1;
		create_table();
		
		document.getElementById("table_forward").style.visibility="visible";
		document.getElementById("table_back").style.visibility="visible";		
		
	}

	document.getElementById("chart_view").style.visibility="visible";
	
	document.getElementById("chart_view").onclick=function (){
		chart_on="yes";
		start=1;
		page=8;
		num_years=annual_array.length-1;


		document.getElementById("table_results_wrapper").style.visibility="visible";
		document.getElementById("chart_view").style.visibility="hidden";
		document.getElementById("chart_hide").style.visibility="visible"; 
		
		document.getElementById("table_forward").style.visibility="visible";
		document.getElementById("table_back").style.visibility="visible";		

		create_table();		
		
		};
		
	document.getElementById("chart_hide").onclick=function (){
		chart_on="no";
		start=1;
		page=8;
		num_years=annual_array.length-1;
		create_table();

		document.getElementById("table_results_wrapper").style.visibility="hidden";
		document.getElementById("chart_hide").style.visibility="hidden";
		document.getElementById("chart_view").style.visibility="visible"; 

		document.getElementById("table_forward").style.visibility="hidden";
		document.getElementById("table_back").style.visibility="hidden";		


		};




	context_numbers(TFSA_result);

	//document.getElementById("text_results_text_with_tfsa").innerHTML=TFSA_result;
	//document.getElementById("text_results_text_without_tfsa").innerHTML=NOT_TFSA_result;
	document.getElementById("text_results_tax_savings").innerHTML=fmtMoney(tax_savings_result);

	document.getElementById("with_tfsa_bar_amount").innerHTML=fmtMoney(TFSA_result);
	document.getElementById("without_tfsa_bar_amount").innerHTML=fmtMoney(NOT_TFSA_result);
	
	document.getElementById("with_tfsa_bar").style.display="block";
	document.getElementById("without_tfsa_bar").style.display="block";
	document.getElementById("with_tfsa_bar_interest").style.display="block";
	document.getElementById("without_tfsa_bar_interest").style.display="block";	
	document.getElementById("with_tfsa_bar_mover").style.display="block";
	document.getElementById("without_tfsa_bar_mover").style.display="block";	
	
//---------------------Start Bar Animation






	TFSA_mover_height = 140;	
	NOT_TFSA_mover_height = (NOT_TFSA_result * 100) / (TFSA_result)
	NOT_TFSA_mover_height = (NOT_TFSA_mover_height * 140) / (100);

	TFSA_mover_height = 140 - TFSA_mover_height;
	NOT_TFSA_mover_height = 140 - NOT_TFSA_mover_height;

	interest_bar_height = ((((interest_earned * 100) / TFSA_result) * 140) / 100);

		//if decision is made to go back to the hatches representing interest earned, change "NOT_TFSA_mover_height" in statment below to
		//interest_bar_height
    var interest_attributes = {
        height: { to: NOT_TFSA_mover_height }		
    };


    var with_tfsa_attributes = {
        height: { to: TFSA_mover_height }		
    };

    var without_tfsa_attributes = {
        height: { to: NOT_TFSA_mover_height }		
    };


    var with_tfsa_interest_bar_anim = new YAHOO.util.Anim('with_tfsa_bar_interest', interest_attributes, 0.5, YAHOO.util.Easing.easeOutStrong);
    var without_tfsa_interest_bar_anim = new YAHOO.util.Anim('without_tfsa_bar_interest', interest_attributes, 0.5, YAHOO.util.Easing.easeOutStrong);	
	var with_tfsa_bar_anim = new YAHOO.util.Anim('with_tfsa_bar_mover', with_tfsa_attributes, 0.6, YAHOO.util.Easing.easeOutStrong);
    var without_tfsa_bar_anim = new YAHOO.util.Anim('without_tfsa_bar_mover', without_tfsa_attributes, 0.5, YAHOO.util.Easing.easeOutStrong);
	
	
	if (first_time == 1){
		//if decision is made to go back to the hatches representing interest earned, change "NOT_TFSA_mover_height" in statment below to
		//interest_bar_height
		YAHOO.util.Dom.setStyle('with_tfsa_bar_interest', 'height', NOT_TFSA_mover_height+"px")
		YAHOO.util.Dom.setStyle('without_tfsa_bar_interest', 'height', interest_bar_height+"px")		
	} else {
	with_tfsa_interest_bar_anim.animate();
	without_tfsa_interest_bar_anim.animate();
	}

	with_tfsa_bar_anim.animate();
	without_tfsa_bar_anim.animate();
	
	
	document.getElementById("with_tfsa_bar_amount").style.top="-15px";
	document.getElementById("without_tfsa_bar_amount").style.top="-15px";	
	

//----------------------End Bar Animation

}


function context_numbers(val){
	
//calculated value of the TFSA principal + interest
TFSA = val;

//calculate dividers based on TFSA value
divider1 = TFSA*0.75; // 75% mark
divider2 = TFSA*0.5; // 50% mark
divider3 = TFSA*.25; // 25% mark

//
//now determine the scale group
//

if (TFSA < 10000 && TFSA >=1000) {

//for thousands, round to nearest ten

    //divide by ten
    divider1 = divider1 / 10;
    divider2 = divider2 / 10;
    divider3 = divider3 / 10;
    //round all numbers
    divider1 = Math.round(divider1);
    divider2 = Math.round(divider2);
    divider3 = Math.round(divider3);

    //multiply by ten
    divider1 = divider1 * 10;
    divider2 = divider2 * 10;
    divider3 = divider3 * 10;
}

if (TFSA < 100000 && TFSA >=10000) {

//for 10k's, round to nearest hundred

    //divide by hundred
    divider1 = divider1 / 100;
    divider2 = divider2 / 100;
    divider3 = divider3 / 100;
    //round all numbers
    divider1 = Math.round(divider1);
    divider2 = Math.round(divider2);
    divider3 = Math.round(divider3);

    //multiply by hundred
    divider1 = divider1 * 100;
    divider2 = divider2 * 100;
    divider3 = divider3 * 100;
}

if (TFSA < 1000000 && TFSA >= 10000) {

//for 100k's, round to nearest 1k

    //divide by thousand
    divider1 = divider1 / 1000;
    divider2 = divider2 / 1000;
    divider3 = divider3 / 1000;

    //round all numbers
    divider1 = Math.round(divider1);
    divider2 = Math.round(divider2);
    divider3 = Math.round(divider3);

    //multiply by thousand
    divider1 = divider1 * 1000;
    divider2 = divider2 * 1000;
    divider3 = divider3 * 1000;
}

if (TFSA <10000000 && TFSA >=1000000) {

//for millions, round to nearest 10k

    //divide by ten thousand
    divider1 = divider1 / 10000;
    divider2 = divider2 / 10000;
    divider3 = divider3 / 10000;
    //round all numbers
    divider1 = Math.round(divider1);
    divider2 = Math.round(divider2);
    divider3 = Math.round(divider3);

    //multiply by ten thousand
    divider1 = divider1 * 10000;
    divider2 = divider2 * 10000;
    divider3 = divider3 * 10000;
}

if (TFSA < 100000000 && TFSA >=10000000) {

//for the tens of millions, round to nearest 100,000

    //divide by one hundred thousand
    divider1 = divider1 / 100000;
    divider2 = divider2 / 100000;
    divider3 = divider3 / 100000;
    //round all numbers
    divider1 = Math.round(divider1);
    divider2 = Math.round(divider2);
    divider3 = Math.round(divider3);

    //multiply by one hundred thousand
    divider1 = divider1 * 100000;
    divider2 = divider2 * 100000;
    divider3 = divider3 * 100000;
}

if (TFSA < 1000000000 && TFSA >=100000000) {
//for the hundred millions, round to nearest million

    //divide by one million
    divider1 = divider1 / 1000000;
    divider2 = divider2 / 1000000;
    divider3 = divider3 / 1000000;
    //round all numbers
    divider1 = Math.round(divider1);
    divider2 = Math.round(divider2);
    divider3 = Math.round(divider3);

    //multiply by one million
    divider1 = divider1 * 1000000;
    divider2 = divider2 * 1000000;
    divider3 = divider3 * 1000000;
} 	
	
	
	
	
	
	
	
	
	
	
	document.getElementById("b_75").innerHTML=fmtMoney(divider1);
	document.getElementById("b_50").innerHTML=fmtMoney(divider2);
	document.getElementById("b_25").innerHTML=fmtMoney(divider3);	
	
}



function create_table(){
	tfsa_yes=annual_array;
	tfsa_no=t_annual_array;


	if (page > num_years){
		page=num_years;
		
	}
	

	
	if (page < num_years){
		document.getElementById("table_forward").onclick= function (){start=start+8;    page=(page+8) ;   create_table();};
		YAHOO.util.Dom.setStyle('table_forward', 'opacity', 1);
	} else {		
		document.getElementById("table_forward").onclick=function(){return false;};
		YAHOO.util.Dom.setStyle('table_forward', 'opacity', 0.3);		
		
	}
	if (start >= 9){	
		document.getElementById("table_back").onclick= function () {start=start-8;   page=(page-rows);  create_table();};	
		YAHOO.util.Dom.setStyle('table_back', 'opacity', 1);
	}
	
	if (start == 1) {
		document.getElementById("table_back").onclick=function(){return false;};			
		YAHOO.util.Dom.setStyle('table_back', 'opacity', 0.3);		
	}	

	if (num_years <=8) {
		document.getElementById("table_forward").style.display="none";
		document.getElementById("table_back").style.display="none";
	} else {
		document.getElementById("table_forward").style.display="block";
		document.getElementById("table_back").style.display="block";		
	}



	rows=(page+1) - start;
	
	table="<div id='chart_top_line'></div>";
	table+="<table style='float:left;'>";
	//table+="<tr class='table_heading_row'><td class='year_heading'>Year</td><td class='tfsa_yes_heading'>TFSA</td><td class='tfsa_no_heading'>Not TFSA</td></tr>";

	for (i=start; i <= page; i++){
		table+="<tr class='table_row";

		if (i == page){
			table+=" last_row";
		}
		
		if (i % 2 != 0){
			table+=" odd_row";
		} else {
			table+=" even_row";	
		}
		
		table+="'><td width='25' class='table_numbers'>&nbsp;&nbsp;&nbsp;&nbsp;" + i + "</td><td width='100' class='tfsa_yes_cell'>" + fmtMoney(Math.round(tfsa_yes[i])) + "</td><td width='125' class='tfsa_no_cell'>" + fmtMoney(Math.round(tfsa_no[i])) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>";
	}

	
	table+="</table>";
	
	document.getElementById("table_results").innerHTML=table;
	//document.getElementById("table_results_wrapper").style.visibility="visible";
	

}





















function fmtMoney(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+' '+
		dblValue.substring(dblValue.length-(4*i+3));
	//return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
	return (((blnSign)?'':'-') + dblValue + ' $');	
}


function fmtMoney1(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	//return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
	return (((blnSign)?'':'-') + dblValue);	
}