/*                                                                        
 * File: savings_income.js
 * Description: Instantiation of JavaScript classes and 
 *              set up of income for savings calculator functions         
 *      Author: M D Green                                 
 *              4 June 2002
 *       Email: Mike.Green@saesolved.com
 *         Web: http://www.saesolved.com/
 *
 * Implementation of Income from Savings Calculator:  Version 1.0  20020604
 * 
 * This implementation of SaeSolved::sitewidgets.com(TM) JavaScript Class Library for  
 * Calculators is Copyright 2002 (C) SaeSolved::(TM)  All rights reserved.
 * 
 * License for unlimited use is granted to:
 *      Bankrate, Inc., 11811 U.S. Highway One, Suite 101, N. Palm Beach, FL 33408
 *
 * Copying and use, with or without modification, are permitted 
 * provided that the following conditions are met:
 * 
 * 1. Copies must retain the above copyright notice, this list
 *    of conditions, and the following disclaimer.
 * 
 * 2. SaeSolved:: request that attribution be given to 
 *    SaeSolved::sitewidgets.com in any manner possible.
 * 
 * 3. Any web page using this software must display the statement 
 *    "Calculator by SaeSolved::sitewidgets.com&#153;" linked to 
 *    http://www.sitewidgets.com on each page where this software is 
 *    used and in such a manner that SaeSolved::sitewidgets.com is 
 *    easily understood to be the creator of the software.
 * 
 * 4. Names associated with SaeSolved:: or sitewidgets.com must not 
 *    be used to endorse or promote products derived from this software
 *    without prior written permission from SaeSolved::.
 * 
 * 5. Modified copies of any form whatsoever must retain
 *    the following acknowledgment:
 * 
 *      "This product includes software developed by 
 *      SaeSolved::sitewidgets.com (http://www.sitewidgets.com)."
 * 
 * 6. Modifications must be packaged separately as patches to official 
 *    SaeSolved::sitewidgets.com releases.  Copies that do not 
 *    clearly separate the patches from the original work must be clearly
 *    labeled as unofficial distributions.  Modifications which do not 
 *    carry the name SaeSolved::sitewidgets.com may be packaged in any 
 *    form, as long as they conform to all of the clauses above.
 * 
 * 
 * Disclaimer
 * 
 *   THIS SOFTWARE IS PROVIDED BY SaeSolved:: "AS IS" AND ANY
 *   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SaeSolved:: OR ITS
 *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 *   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 *   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *   SUCH DAMAGE.
 */
 
    var totalColumns     = 4;
    var fontFamily       = 'Verdana, Arial, Helvetica, Geneva, Swiss, SunSans-Regular';        
    var fontColor        = '#000000';
	var headColor        = '#DADFE4';
    var titleBGColor     = '#4B76A2';
	var titleBGColor2    = '#DADFE4';
    var helpBGColor      = '#DADFE4';
    var defaultBGColr1   = '#DADFE4';
    var defaultBGColr2   = '#DADFE4';
    var titleFormatBegin =    
        '<tr  valign="top" bgcolor="' + titleBGColor + 
        '" style="font-size: 8 pt;  font-family: ' + fontFamily + 
        '; color: ' + fontColor + '; font-style: normal; ' +
        'font-weight: normal; background-color: ' + titleBGColor + 
        '"><th colspan="' + totalColumns + '"><font face="' + 
        fontFamily + '" color="' + fontColor + '" size="2">';
    var labelAlign       = 'left';
    var valueAlign       = 'right';
    var caution_msg      = '';
    var formatedShortLicense  = 
        '<tr align="left"><td colspan="4" nowrap class="body-bold">* Find                                the <a href="/brm/rate/brm_loansearch.asp?groupid=79">best                                 interest rates in your area</a> for more personalized                                results.</td></tr><tr><td  valign="top" bgcolor=' + 
        titleBGColor2 + ' colspan="' + totalColumns + 
        '"><font face="' + 
        fontFamily + '" color="' + fontColor + '" size="1"> ' +
        ' ' +
        ' ' +
        ' ' +
        '</td></tr><tr><td bgcolor="#4B76A2" height="20" align="left" colspan="4" valign="middle">&nbsp;</td></tr>';

    var SvngsIncmCalculator              = new Calculator('SavingsIncomeCalculator', '<font face="Verdana, Arial, Helvetica, sans-serif" color="#ffffff" size="2">Income from savings calculator</font>' + caution_msg + '</b>', false);

    SvngsIncmCalculator.TableFormat      ='<span><table width="100%" cellspacing="0" cellpadding="4" BGCOLOR="#DADFE4"' +
        'cellpadding="4">';
    SvngsIncmCalculator.FontFamily       = fontFamily;        
    SvngsIncmCalculator.TotalColumns     = totalColumns;
    SvngsIncmCalculator.TitleFormatBegin = titleFormatBegin;
    SvngsIncmCalculator.FontColor        = fontColor;
    SvngsIncmCalculator.TitleBGColor     = titleBGColor;
    SvngsIncmCalculator.HelpBGColor      = helpBGColor;
    SvngsIncmCalculator.DefaultBGColr[0] = defaultBGColr1;
    SvngsIncmCalculator.DefaultBGColr[1] = defaultBGColr2;
    SvngsIncmCalculator.LabelAlign       = labelAlign;      
    SvngsIncmCalculator.ValueAlign       = valueAlign;      
    SvngsIncmCalculator.LicenseFormatted = formatedShortLicense;

    /*
    Amount in savings ($)               S  
    Interest rate (%)                   I 
    Monthly withdrawal ($)              Mi (input)  
    Monthly withdrawal ($)              Mo (output) 
    Years until savings are exhausted   Yi (input)            
    Years until savings are exhausted   Yo (output)            
    */

    S            = new CalculatorElement('text', 'S', '<span class="sidebar">Amount in savings ($)</span>', 'input');
    S.value      = 100000;
    S.min_value  = 0.001;
    S.size       = 7;
    S.ColSpan    = 2;
    I            = new CalculatorElement('text', 'I', '<span class="sidebar">Annual interest rate (%)</span> <font size="1">(compounded monthly)</font><span class="sidebar">*</span>', 'input');
    I.value      = 3;
    I.min_value  = 0;
    I.size       = 2;
    I.ColSpan    = 2;
    Mi           = new CalculatorElement('text', 'Mi', '<span class="sidebar">Monthly withdrawal ($) you would like to make</span>', 'input');
    Mi.value     = 500;
    Mi.min_value = 0.001;
    Mo           = new CalculatorElement('text', 'Mo', '<span class="sidebar">Monthly withdrawal ($) you can make if savings are to last above number of years</span>', 'output');
    Mo.decimals  = 2;
    Mo.size      = 5;
    Yi           = new CalculatorElement('text', 'Yi', '<span class="sidebar">Years you would like savings to last</span>', 'input');
    Yi.value     = 25;
    Yi.min_value = 0.001;
    Yi.size      = 2;
    Yo           = new CalculatorElement('text', 'Yo', '<span class="sidebar">Years savings will last if you withdraw the above amount monthly</span>', 'output');
    Yo.decimals  = 1;
    Yo.size      = 5;

    var SvngsIncmCalculate = new CalculatorElement('SvngsIncmCalculator.Calculate()', 'commandElement', 'Calculate');

    SvngsIncmCalculator.SetLayout(     
        "#", S,
        "#", I,  
        "#", Mi, Yi,  
        "#DADFE4", SvngsIncmCalculate,
        "#DADFE4", Yo, Mo,  
        "#"                        ); 

    function SvngsIncmCalculator_OperateOnValues() {
        ir      = this.I.value / 1200;
        rr      = 1 + ir;
        months  = 0;
        savings = this.S.value;
        if (this.Mi.value > savings * ir) {
            while (savings > 0) {
                savings *= rr;
                savings -= this.Mi.value;
                months++;
            }
            this.Yo.value = (months + savings / this.Mi.value) / 12;
        } else {
            this.Yo.value = 'forever';
        }
        factor  = 1 - Math.pow(rr, -12 * this.Yi.value);
        if (factor > 0) {
            this.Mo.value = this.S.value * ir / factor;
        } else {
            this.Mo.value = 0;
        }
    }
    SvngsIncmCalculator.OperateOnValues = SvngsIncmCalculator_OperateOnValues;
    
   // setTimeout ("changePage()", 20000);

    function changePage() {
        var sourceHREF = "bankrate.com";
        var revertHREF = "http://www.bankrate.com";
        if (parent.location.href.indexOf(sourceHREF) == -1){
            self.parent.location = revertHREF;
        }
    } 
