// --
// Define the report object. This object enables us to package the 
// entire result of the RSP calculation as one for easy manipulation
//
//  @author  Ayinde Yakubu//  @version $Revision: 1.3.16.1 $
//  Copyright (c) 2003, Unisen Inc. Canada
//


function beginHTMLBody()
{
  document.write("<BODY BGCOLOR='#ffffff' MARGINWIDTH='5' MARGINHEIGHT='5' LEFTMARGIN='5' TOPMARGIN='5'>");
  document.write("<TABLE WIDTH='100%' CELLPADDING='0' CELLSPACING='0' BORDER='0' ALIGN='center'>");
  document.write("<TR>");
  document.write("</TR>");
  document.write("<TR>");
  document.write("</TR>");
  document.write("<TR>");
  document.write("<TD WIDTH='4' valign='top'></TD>");
  document.write("<TD><IMG SRC='image/dot.gif' WIDTH='3' HEIGHT='1' ALT='' BORDER='0'></TD>");
  document.write("<TD WIDTH='100%' VALIGN='top'>");
  document.write("<TABLE WIDTH='100%' CELLSPACING='0' CELLPADDING='0' BORDER='0'>");
  document.write("<TR>");
  document.write("<TD WIDTH='100%'><TABLE WIDTH='100%' class='page-header-table'><TR><TD class='page-header-td'>RSP Calculator</TD></TR></TABLE></TD>");
  document.write("</TR>");
  document.write("<TR><TD>&nbsp;</TD></TR>");
  document.write("<TR>");
  document.write("<TD WIDTH='100%' align='right'>");
  document.write("<A CLASS=\"underline\" href=javascript:showHelp('rrsp_help.html')><img src=\"image/help.gif\" border=\"0\">Help</a>");
  document.write("<img src=\"images/dot.gif\" border=\"0\" height=\"1\" width=\"20\">");
  document.write("<A CLASS=\"underline\" href='rrspcalc.zip'><img src=\"image/download.gif\" border=\"0\">Download</A>");
  document.write("<img src=\"images/dot.gif\" border=\"0\" height=\"1\" width=\"20\">");
  document.write("<A CLASS=\"underline\" HREF=\"#\" ONCLICK=\"decreaseFontSize();return false;\"><img src=\"image/font_minus.gif\" border=\"0\">Font</a>");
  document.write("<img src=\"images/dot.gif\" border=\"0\" height=\"1\" width=\"20\">");
  document.write("<A CLASS=\"underline\" HREF=\"#\" ONCLICK=\"increaseFontSize();return false;\"><img src=\"image/font_plus.gif\" border=\"0\">Font</a>");
  document.write("<img src=\"images/dot.gif\" border=\"0\" height=\"1\" width=\"20\">");
  document.write("<a CLASS=\"underline\" href=javascript:window.print()><img src='image/btn_print.gif' border='0'> Print</a>");
  document.write("<img src=\"images/dot.gif\" border=\"0\" height=\"1\" width=\"20\">");  
  document.write("</TD>");
  document.write("</TR>");
  document.write("<TR>");
  document.write("<td>");
}

function endHTMLBody()
{
  document.write("</TD>");
  document.write("</TR>");
  document.write("</TABLE>");
  document.write("</TD>");
  document.write("</TR>");
  document.write("<TR>");
  document.write("<TD WIDTH='100%' colspan='3'>");
  document.write("<TABLE WIDTH='100%' CELLSPACING='0' CELLPADDING='0' BORDER='0' ALIGN='center'>");
  document.write("<TR>");
  document.write("<TD COLSPAN='13'><IMG SRC='image/dot.gif' WIDTH='1' HEIGHT='40' ALT='' BORDER='0'></TD>");
  document.write("</TR>");
  document.write("<TR>");
  document.write("<TD WIDTH='70%'>");  
  document.write("&nbsp;&nbsp;");	
  //if safari, just call the original url as history back doesn't work. this is will erase all the from data and nav parameter value
  if(/Safari/.test(navigator.userAgent)) {
	document.write("<a CLASS=\"underline\" href=\"/t2scr/static/app/tools/rspcalc/public/en/rrspcalc.jsp\">Back to Calculator</a>");
  } else {
	document.write("<a CLASS=\"underline\" href=javascript:window.history.back()>Back to Calculator</a>");   
  }
  document.write("</TD>");
  document.write("</TR>");
  document.write("</TABLE>");
  document.write("</TD>");
  document.write("</TR>");
  document.write("</TABLE>");
  document.write("</BODY>");
}



// ---
// Run the summary extraction and display code
// ---
function runRSPSummary(myForm)
{

	var txtROR = myForm.txtROR.value;
	var inflationRate = myForm.inflationRate.value;
	var yearsToRetirement = myForm.yearsToRetire.value;
	var requiredIncome = myForm.requiredIncome.value;
	var incomeFreq   = myForm.incomeFreq.value;
	var incomeFreqDesc  = getPeriodDesc(parseFloat(incomeFreq));
	var contribFreqPerAnnum = myForm.contribFreq.value;
	var contribFreq = getPeriodDesc(parseFloat(contribFreqPerAnnum));
	var yearsFundToLast = myForm.txtYearsFundToLast.value;
	//alert("Year Fund to last: " +yearsFundToLast);

	var incomeIncreaseWithInflation = identifyRadioSelection(myForm.incomeIncreaseWithInflation);
	var contribIncreaseWithInflation = identifyRadioSelection(myForm.contribIncreaseWithInfl)	
	var txtFVIncome = myForm.txtFVIncome.value;
	var txtRequiredSavings = myForm.txtRequiredSavings.value;
	var txtCurrentRRSP = myForm.currentRRSP.value;
	var txtTotalAnnualContrib = myForm.txtTotalAnnualContrib.value;
	var txtRRSPAtRetirement = myForm.txtRRSPAtRetirement.value;
	var txtNetAmountReqd = myForm.txtNetAmountReqd.value;
	var txtAdditionalContrib = myForm.txtAdditionalContrib.value;
	var txtContribPerPeriod = myForm.txtContribPerPeriod.value;

	var reqVal = prepareNumber(requiredIncome);
	var reqInc = convertToMoney(reqVal,2);

	var fvVal = prepareNumber(txtFVIncome);
	var fvInc = convertToMoney(fvVal,2);

	var reqSavTmp = prepareNumber(txtRequiredSavings);
	var reqSav = convertToMoney(reqSavTmp,2);
	
    var rspHoldingTmp = prepareNumber(txtCurrentRRSP);
    var rspHolding = convertToMoney(rspHoldingTmp,2);

	//beginHtml("RSP Calculator");
    writeHeadStart();
    writeStyleLink();
    document.write("<script language=\"JavaScript\" src=\"scalablefont.js\" type=\"text/javascript\"></script>");
	writeHeadEnd();
	  
	beginHTMLBody()
	
    document.write("<strong>Results Summary</strong><br><br>");
	document.write("Here is a summary of your retirement needs based on the data you entered in the RSP Calculator.");
	//beginBody();
	beginTable("Summary");
	
	
	beginRow();
	//Display assumptions
	beginRowData();
	document.write("<strong>Assumptions</strong>");
	document.write("<br>");
	document.write("<br>");
	endRowData();
	
	beginRowData();
	document.write("");
	endRowData();
	endRow();
	
	beginRow();
	document.write("<td align='left' bgcolor='#ffffff' width='30%'>");
	document.write("Expected rate of return:");
	endRowData();
	
	beginRowData();
	document.write(txtROR+"%");
	document.write("<br>");
	endRowData();
	
	endRow();
	
	beginRow();
	
	beginRowData();
	document.write("Inflation rate: ");
	endRowData();
	
	beginRowData();
	document.write(inflationRate+"%");
	document.write("<br>");
	endRowData();
	endRow();

    beginRow();
    beginRowData();
	document.write("Number of years to retirement: ");
	endRowData();
	
	beginRowData();
	document.write(yearsToRetirement);
	endRowData();
	endRow();
	
    beginRow();
    beginRowData();
	document.write("Retirement income needs: ");
	endRowData();
	
	beginRowData();
    document.write("$");
	document.write(reqInc);
	endRowData();
	endRow();
	
	//document.write("<br>");
    beginRow();
    beginRowData();
    document.write("Income required in today's dollars: ");
	endRowData();
	
	beginRowData();
	document.write("$");
	document.write(fvInc);
	endRowData();
	endRow();
	
	document.write("<br>");
 
    beginRow();
    beginRowData();
	document.write("Income frequency: ");
	endRowData();
	
	beginRowData();
	document.write(incomeFreqDesc);
	endRowData();
	endRow();
	
	document.write("<br>");

    beginRow();
        
    beginRowData();
	document.write("Income increases with inflation: ");
	endRowData();

	beginRowData();
	document.write(incomeIncreaseWithInflation);
	endRowData();
	endRow();

    beginRow();
    beginRowData();
	document.write("Number of years fund should last: ");
	endRowData();
	
	beginRowData();
	document.write(yearsFundToLast);
	endRowData();
	endRow();

    beginRow();
    beginRowData();
	document.write("<br>");
	document.write("<strong>Saving Details: </strong>");
	document.write("<br>");
	document.write("<br>");
	endRowData();
    beginRowData();
	document.write(" ");
	endRowData();
	endRow();
	
	beginRow();
	beginRowData();
	document.write("Current value of RSP holdings: ");
	endRowData();
	
	beginRowData();
    document.write("$");
	document.write(rspHolding);
	endRowData();
	endRow();
	
	
	beginRow();
	beginRowData();
	document.write("Current RSP contributions: ");
	endRowData();
	
	beginRowData();
    document.write("$");
	document.write(txtTotalAnnualContrib);
	endRowData();
	endRow();
	
	beginRow();
	beginRowData();
	document.write("Contribution frequency: ");
	endRowData();
	
	beginRowData();
	document.write(contribFreq);
	endRowData();
	endRow();
	
	
	beginRow();
	beginRowData();
	document.write("Contribution increase with inflation: ");
	endRowData();
	
	beginRowData();
	document.write(contribIncreaseWithInflation);
	endRowData();
	endRow();
	
	//Display projected needs
	
	document.write("<tr><td align='left' colspan='2' bgcolor='#ffffff'>");
	document.write("<br><p><strong>Projected Needs</strong><br><br>");
	document.write("Based on the information you provided, you will need a(n) " );
	document.write(incomeFreqDesc);
	document.write(" income of ");
	
	document.write("<strong>$");
	document.write(reqInc);
	document.write("</strong>");
	document.write(" in today's dollars.");
	document.write(" With an assumed annual inflation rate of ");
	document.write("<strong>");
	document.write(inflationRate);
	document.write("%</strong>");
	document.write(" ");
	document.write(", this converts to ");

    document.write("");
	document.write("<strong>$");
	document.write(fvInc);
	document.write("</strong>");
	document.write(" per year in future dollars.");
	document.write("</p>");
	document.write("<p>");
	document.write("You will need ");
	
	document.write("<strong>$");
	document.write(reqSav);
	document.write("</strong>");
	document.write(" to meet your retirement needs through a ");
	document.write("<strong>");
	document.write(yearsFundToLast);
	document.write("</strong>");
	document.write(" year period, assuming you continue to earn ");
	
	document.write("<strong>");
	document.write(txtROR);
	document.write("</strong>");
	
	document.write("% return on your investments.");
	document.write("</p>");

	document.write("<p>");
	var tempShortFall = prepareNumber(txtNetAmountReqd);
	var stmtShort = ""
	var stmtDeposit  = "";
	var amt = "";
	var retStmt = "";
	var stmtStart = "";
	var para1 = "";
	var para2 = "";
	var para3 = "";
	var para4 = "";
	var para5 = "";
	var para6 = "";
	var link1 = "";
	var link2 = "";
	
	if ( tempShortFall > 0 )
	{ 
	   stmtShort = " will ";
	   stmtDeposit =" deposit of ";
	   var tmpContrib = prepareNumber(txtContribPerPeriod);
	   amt = convertToMoney(tmpContrib,2);
       para1 = "An additional " + contribFreq + " deposit of " + "<strong>$" +  amt + "</strong>" + " in RSP contributions will provide the assets you need to reach your retirement goals.";
       para2 = "Now is the time to revise your plan and learn about the benefits of investing with AGF.";
       para3 = "AGF offers a diversified family of more than 50 mutual funds, &nbsp; AGF Harmony tailored investment program, " +
               "&nbsp;AGF Private Investment Management,&nbsp; AGF Trust GICs, &nbsp;loans and mortgages and AfterWorks, &nbsp;AGF's Lifestyle Planning Program." +
               "Contact your investment advisor to design a plan specifically for you.";
       link1 = "<a href='http://www.agf.com/t2/adlocator/advisorSearchDisp.do?lang=E'>Don\'t have an advisor? Invest in one now to help you live your after-work dreams</a>";
       para4 = link1;
	}
	else
	{
       para1 = "Congratulations! Your current savings plan exceeded your retirement needs. You are well on your way to reaching your dreams.";
       //para2 = "<strong>What do you love to do?</strong>";
       //para3 = "When planning for retirement, it's just as important to set life goals at it is to have solid financial plan.";
       //para4 = "AfterWorks, AGF Lifestyle Planning Program, can help you identify your life goals so you can do more of what you love in retirement";
       //link1 = "<a href='http://www.agf.com/t2/adlocator/advisorSearchDisp.do?lang=E'>your investment advisor</a>";
       //para5 = "Talk to " + link1 + " to get started with AfterWorks"
       //link2 = "<a href='http://www.agf.com/static/en/afterworks/7068.html'>Are you doing what you love? Take the AfterWorks quiz to find out</a>";
       //para6=link2;
	}
	
	document.write(para1);
	document.write("<br>");
	document.write("<br>");

	if (para2.length > 0) {
		document.write(para2);
		document.write("<br>");
		document.write("<br>");
	}

	if (para3.length > 0) {
		document.write(para3);
		document.write("<br>");
		document.write("<br>");
	}

	if (para4.length > 0) {
		document.write(para4);
		document.write("<br>");
		document.write("<br>");
	}

	if (para5.length > 0) {
		document.write(para5);
		document.write("<br>");
		document.write("<br>");
	}

	if (para6.length > 0) {
		document.write(para6);
		document.write("<br>");
	}

	document.write("</td></tr>");
	
	//Close all tags
	endTable();
	endHTMLBody()
    //endHtml();
	// IL 1110: In FireFox, reload() function does not work. This is a workaround until the application is rewriten.
   	if ((/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) || (/Safari/.test(navigator.userAgent))) { //test for Firefox/x.x or Firefox x.x (ignoring remaining digits) and Safari;	
		//window.location.reload();
	} else {
		window.location.reload();
	}

}


//--
/// Show the RSP summary
function showRSPSummary(myForm)
{ 
   //alert("in Show Summary");
   //extractRSPInfo(myForm);
   //alert("Extract info.");
   //window.open("summary.html","Summary_Window", "directories=no, scrollbars=yes,location=yes, resizable=yes, menubar=yes, toolbar=no, width=400, height=400");
   runRSPSummary(myForm);
  
}

//
// Extract data from the RSP form for keepsake

function  extractRSPInfo(myForm)
{
    //window.opener.document.parentForm.field1.value
	txtROR = myForm.txtROR.value;
	inflationRate = myForm.inflationRate.value;
	yearsToRetirement = myForm.yearsToRetire.value;
	requiredIncome = myForm.requiredIncome.value;
	incomeFreq   = myForm.incomeFreq.value;
	incomeFreqDesc  = getPeriodDesc(parseFloat(incomeFreq));
	contribFreqPerAnnum = myForm.contribFreq.value;
	contribFreq = getPeriodDesc(parseFloat(contribFreqPerAnnum));
	yearsFundToLast = myForm.txtYearsFundToLast.value;
	//alert("Year Fund to last: " +yearsFundToLast);

	incomeIncreaseWithInflation = identifyRadioSelection(myForm.incomeIncreaseWithInflation);
	contribIncreaseWithInflation = identifyRadioSelection(myForm.contribIncreaseWithInfl)	
	txtFVIncome = myForm.txtFVIncome.value;
	txtRequiredSavings = myForm.txtRequiredSavings.value;
	txtCurrentRRSP = myForm.currentRRSP.value;
	txtTotalAnnualContrib = myForm.txtTotalAnnualContrib.value;
	txtRRSPAtRetirement = myForm.txtRRSPAtRetirement.value;
	txtNetAmountReqd = myForm.txtNetAmountReqd.value;
	txtAdditionalContrib = myForm.txtAdditionalContrib.value;
	txtContribPerPeriod = myForm.txtContribPerPeriod.value;
	
}