﻿var mortgage_anchorColor='0000ff';
var mortgage_bgColor='efefef';
var mortgage_interestColor='B21F24';
var mortgage_principalColor='033348';
var mortgage_balanceColor='23f3a3';

var mortgage_sInterest="";
var mortgage_sPrincipal="";
var mortgage_sBalance="";
var mortgage_sCategory="";
var mortgage_YMax=0;

var buysell_VAT=1.175;
var buysell_withoutHipColor='B21F24';
var buysell_withHipColor='033348';
var buysell_bgColor='efefef';

function CalculateMortgage_Small()
{
    CreateMortgageChart('5', 'mort_charter1', 'mort_chartArea', 200, 200, false);
    $('#divMortgageResult').show();
}

function CalculateBuySell_Small()
{
    CreateBuySellChart('buysell_charter1', 'buysell_chartArea', 500, 300, true);
    $('#divBuysellResult').show();
    if(document.all)
    {
        /*UpdateCurvyBoxes('divBuySellSave');
        alert('hgj');*/
    }
}

function DoMortgageCalc(period)
{
    var a=0; //amount
    var p=parseInt($('#txtMortCalc_Loan').val().replace(/[£,%]/g,'')); //loan amount
    mortgage_YMax=p;
    var r=parseFloat($("#txtMortCalc_IR").val().replace(/[£,%]/g,''));//interest rate
    r=(r/100)/12;//rate in fraction
    var n=parseInt($("#txtMortCalc_Term").val().replace(/[£,%]/g,'')); //period
    n=n*12;//number of months
    a=p * ((r * Math.pow(1+r,n))/(Math.pow(1+r,n)-1));
    a = a.toFixed(2);
    $("#txtMortCalc_Repay").val(a.toString().formatCurrency(2,'£'));
    $("#txtMortCalc_Io").val((p*r).toString().formatCurrency(2,'£'));
    
    var interval = 12;
    switch(period)
    {
        case 'm': interval=1; break;
        case 'hy': interval=6; break;
        case 'q': interval=3; break;
        case 'y': interval=12; break;
        default:
        {
            interval = parseInt(period)*12;
        }
    }

    mortgage_sInterest="";
    mortgage_sPrincipal="";
    mortgage_sBalance="";
    mortgage_sCategory="";
    
    var dtVal=new Date();
    for(var i=0; i<n+1; i++)
    {
        if(i%interval==0)
        {
            mortgage_sCategory += "<category name='" + (dtVal.getMonth()+1) + "/" + dtVal.getFullYear() + 
                            "' />\n";
            mortgage_sInterest += "<set value='" + (p*r).toFixed(2) + "' />\n";
            mortgage_sPrincipal += "<set value='" + (a - (p*r)).toFixed(2) + "' />\n";
            mortgage_sBalance += "<set value='" + p.toFixed(2) + "' />\n";
        }
        p -= (a - (p*r));
        dtVal.setMonth(dtVal.getMonth()+1);
    }
}

function CreateMortgageChart(period, chartId, divId, width, height, showlabel)
{
    var chart1 = new FusionCharts("Charts/FCF_MSColumn3DLineDY.swf", chartId, width, height);
    //generating data
    DoMortgageCalc(period);
    //getting xml
    var strXML=generateMortgageXML(showlabel);
    chart1.setDataXML(strXML);
    //finally plotting
    chart1.render(divId);
}

function CreateBuySellChart(chartId, divId, width, height, showlabel)
{
    var chart1 = new FusionCharts("Charts/FCF_MSBar2D.swf", chartId, width, height);
    //getting xml
    var strXML=generateBuySellXML(showlabel);
    chart1.setDataXML(strXML);
    //finally plotting
    chart1.render(divId);
}

//function UpdateChart(period, chartId, showlabel){
//    DoMortgageCalc(period);
//    updateChartXML(chartId, generateMortgageXML(showlabel));
//}

function generateMortgageXML(showlabel){
    //Variable to store XML
    var strXML;
    strXML = "<graph numberPrefix='£' decimalPrecision='0' animation='1'" + 
            "lineThickness='6' numDivLines='10' anchorSides='10' " + 
            "anchorRadius='3' anchorBorderColor='" + mortgage_anchorColor + "' formatNumberScale='0' " +
            "canvasBgColor='" + mortgage_bgColor + "' SYAxisMaxValue='" + mortgage_YMax + "' ";
    if(showlabel){
        strXML += "showDivLineValue='1' showLimits='1' shownames='1' showValues='0' "+
                "rotateValues='0' SYAxisName='Balance Amount' pyaxisname='Payment Amount' " +
                "xaxisname='Year'>\n";
    }
    else
    {
        strXML += "showDivLineValue='0' showLimits='0' shownames='0' showValues='0' "+
                "rotateValues='0' SYAxisName='' pyaxisname='' xaxisname=''>\n";
    }
    
    //Store <categories> and child <category> elements
    strXML += "<categories>\n";
    strXML += mortgage_sCategory;
    strXML += "</categories>\n";

    //Based on the products for which we've to generate data, generate XML
    strXML += "<dataset alpha='70' seriesName='Interest' color='" + mortgage_interestColor + "' >\n";
    strXML += mortgage_sInterest;
    strXML += "</dataset>\n";
    
    strXML += "<dataset alpha='70' seriesName='Principal' color='" + mortgage_principalColor + "' >\n";
    strXML += mortgage_sPrincipal;
    strXML += "</dataset>\n";
    
    strXML += "<dataset seriesName='Balance' color='" + mortgage_balanceColor + "' parentYAxis='S'>\n";
    strXML += mortgage_sBalance;
    strXML += "</dataset>\n";
    
    //Close <graph> element;
    strXML += "</graph>\n";

    //Return data
    return strXML;
}

function generateBuySellXML(showlabel){
    //Variable to store XML
    var strXML;
    strXML = "<graph numberPrefix='£' decimalPrecision='0' animation='1'" + 
            "lineThickness='6' numDivLines='5' formatNumberScale='0' " +
            "canvasBgColor='" + buysell_bgColor + "' ";
    if(showlabel){
        strXML += "showDivLineValue='1' showLimits='1' shownames='1' showValues='1' "+
                "rotateValues='1' YAxisName='Amount' xaxisname=''>\n";
    }
    else
    {
        strXML += "showDivLineValue='0' showLimits='0' shownames='0' showValues='1' "+
                "rotateValues='1' yaxisname='' xaxisname=''>\n";
    }
    
    //Store <categories> and child <category> elements
    strXML += "<categories>\n";
    strXML += "<category name='Other Agents' />\n";
    strXML += "<category name='Free Start' />\n";
    strXML += "<category name='Low Start' />\n";
    strXML += "<category name='Super Saver' />\n";
    strXML += "</categories>\n";

    //Based on the products for which we've to generate data, generate XML
    var hipPriceLow = parseFloat($("#tdProp_HipPriceLow").text().replace(/[£,%]/g,''));
    var hipPriceStandard = parseFloat($("#tdProp_HipPriceStandard").text().replace(/[£,%]/g,''));
    var hipPriceSuper = parseFloat($("#tdProp_HipPriceSuper").text().replace(/[£,%]/g,''));
    
    strXML += "<dataset alpha='70' seriesName='Includes the cost of HIP' color='" + buysell_withHipColor + "' >\n";
    var propPrice = parseFloat($("#txtProp_SalePrice").val().replace(/[£,%]/g,''));
    var commission = parseFloat($("#txtProp_Commission").val().replace(/[£,%]/g,''));
    var agentPrice = propPrice * (commission/100);
    strXML += "<set value='" + (agentPrice + hipPriceLow) * buysell_VAT + "' />";
    var lowStart = parseFloat($("#tdProp_LowStart").text().replace(/[£,%]/g,''));
    strXML += "<set value='" + lowStart * buysell_VAT + "' />";
    var standard = parseFloat($("#tdProp_Standard").text().replace(/[£,%]/g,''));
    strXML += "<set value='" + standard * buysell_VAT + "' />";
    var superSaver = parseFloat($("#tdProp_SuperSaver").text().replace(/[£,%]/g,''));
    strXML += "<set value='" + superSaver * buysell_VAT + "' />";
    strXML += "</dataset>\n";
    $('#spnSave').html((((agentPrice + hipPriceLow) * buysell_VAT)-(superSaver * buysell_VAT)).toFixed(0).toString().formatCurrency(2,'£'));
    
    strXML += "<dataset alpha='70' seriesName='Excludes the cost of HIP' color='" + buysell_withoutHipColor + "' >\n";
    strXML += "<set value='" + agentPrice * buysell_VAT + "' />";
    strXML += "<set value='" + (lowStart-hipPriceLow) * buysell_VAT + "' />";
    strXML += "<set value='" + (standard-hipPriceStandard) * buysell_VAT + "' />";
    strXML += "<set value='" + (superSaver-hipPriceSuper) * buysell_VAT + "' />";
    strXML += "</dataset>\n";
    
    //Close <graph> element;
    strXML += "</graph>\n";

    //Return data
    return strXML;
}



