var program50x50 = function(completion)
{
    this.name = 'Кредит 50/50+';
    this.programType = 2;
    this.codeName = '50x50';
    
    this.price = Math.round(completion.price);
    this.completionPrice = Math.round(completion.price);
    this.metallicPrice = completion.CostColorMetalic;
    this.metallicPriceUSD = completion.CostColorMetallicUSD;
    this.completion = completion;
}
extend(program50x50, programStandard);


program50x50.prototype.init = function(startData, programSwitch)
{
    if (typeof programSwitch == 'undefined')
    {
        programSwitch = false;
    }
    this.termMin = 60;
    this.termMax = 60;
    this.firstPayMin = 50;
    this.firstPayMax = 100;
    this.ltv = 0.6;
    
    $('#CalcCredit input[name=onSumCredit]').attr('disabled', false);
    facade.program.setTerm(60);
    facade.program.setFirstPay(50);
    facade.program.commission = 6000;
    this.insurance = 0;
    
    if (!programSwitch)
    {
        facade.program.currency = 'rub';
        this.USDRate = false;
    }
    
    facade.page.sliderTerm.slider('option', 'disabled', true);
    facade.page.sliderTermInner.slider('option', 'disabled', true);
    // facade.page.sliderFirstPay.slider('option', 'disabled', true);
    
    if (!programSwitch)
    {
        $('#currency_selector_rub').attr('checked', true);
        $('.currency_label').html('Руб');
        $('#metallic_off').attr('checked', true);
    }
    
    $('#onSumCredit_off').attr('checked', true);
    $('#CalcCredit input[name=SumCreditCur]').val('').attr('disabled', true);
    $('#CalcCredit input[name=SumCreditRate]').val('').attr('disabled', true);
    $('#LsTerm').hide();
    
    $('#FormExchangeRate').show();
    $('.standard_field').hide();
    $('.additional_field').hide();
    $('.program50_field').show();
    
    $('#standard_FirstPay').show();
    $('#program50_FirstPay').hide();
    
    $('.so_sign').hide();
    $('#so_sign_commission').show();
    $('#so_sign_monthly_50').show();
    $('.disclaimer p').hide();
    $('#so_notice_standart').show();
    $('#so_notice_50').show();
    
    
    
    $('#TotalIntegCost').val(Math.round(parseFloat(this.price)));
    facade.program.calc();
    facade.program.bindEvents();
}

program50x50.prototype.updateInterestRate = function()
{
    _self = this;
    
    if (_self.currency == 'usd')
    {
        _self.interestRate = 9.5;
    }
    
    if (_self.currency == 'rub')
    {
        _self.interestRate = 13.9;
    }
}

program50x50.prototype.calc = function()
{
    _self = this;
    _self.updateInterestRate();
    _self.loanAmount = _self.price * (1 - _self.firstPay / 100) + (_self.price * _self.insurance / 100);
    
    _self.term1 = 24;
    _self.term2 = 36;
    
    _self.checkCredit();
    
    var response = $.ajax(
    {
        url: 'calc.php?action=calcBackCredit',
        type: 'post',
        data: {loanAmount:_self.loanAmount, term1: _self.term1, term2: _self.term2, interestRate: _self.interestRate},
        dataType: 'json',
        success: _self.onCalcSuccess
    });
}


program50x50.prototype.onCalcSuccess = function(data)
{
    _self = facade.program;
    
    _self.monthlyPay1 = data.monthlyPay1.toFixed(2);
    _self.monthlyPay2 = data.monthlyPay2.toFixed(2);
    
    _self.totalPayRate1 = (_self.monthlyPay1 * _self.term1).toFixed(2);
    _self.totalPayRate2 = (_self.monthlyPay2 * _self.term2 - _self.loanAmount).toFixed(2);
    
    _self.summCredit = _self.loanAmount.toFixed(2);
    
    
    $('#InterestRate').html(_self.interestRate);
    $('#SummCredit').html(formatDigit(_self.summCredit));
    $('#Commission').html(formatDigit(_self.commission));
    
    $('#MonthlyPay_1').html(formatDigit(_self.monthlyPay1));
    $('#MonthlyPay_2').html(formatDigit(_self.monthlyPay2));
    $('#VisualMonthlyPay').html(formatDigit(_self.monthlyPay1));
    $('#TotalPayRate_1').html(formatDigit(_self.totalPayRate1));
    $('#TotalPayRate_2').html(formatDigit(_self.totalPayRate2));
}
