




    var V12 = V12 ? V12 : { };

    (function (parent) {

        var settings = {
            retailerId: 10124,
            retailerGuid: "5c201a6b-cedb-4a85-b21b-719b466ccda5",
            applicationUrl: "https://apply.v12finance.com/javascriptgateway/retailer-scripts/10124/5c201a6b-cedb-4a85-b21b-719b466ccda5/submitusingformpost",
            version: "1.0.11"
            };

            var financeProducts = [
  {
    "productId": 27,
    "productGuid": "244b3e7a-0ffb-41f2-88d5-adf78b6a3d9e",
    "name": "Interest Free Finance (6 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 6,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF6",
    "altTag": "ONIF6",
    "settlementFee": 0.0
  },
  {
    "productId": 28,
    "productGuid": "8e0bd3a9-657f-457c-b488-dbfab37fac39",
    "name": "Interest Free Finance (12 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 12,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF12",
    "altTag": "ONIF12",
    "settlementFee": 0.0
  },
  {
    "productId": 46,
    "productGuid": "c3114266-125a-4ea1-ab10-cfb3ebd48b81",
    "name": "Interest Free Finance (18 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 18,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF18",
    "altTag": "ONIF18",
    "settlementFee": 0.0
  },
  {
    "productId": 29,
    "productGuid": "9d6fa148-eeb2-4345-b5c6-961902a8f0eb",
    "name": "Interest Free Finance (24 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 24,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF24",
    "altTag": "ONIF24",
    "settlementFee": 0.0
  },
  {
    "productId": 56,
    "productGuid": "1037063d-fdc4-46e6-a448-5df512a96c00",
    "name": "Interest Free Finance (30 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 30,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF30",
    "altTag": "ONIF30",
    "settlementFee": 0.0
  },
  {
    "productId": 30,
    "productGuid": "e931ab43-b8ae-431d-ac50-812d413fb5bb",
    "name": "Interest Free Finance (36 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 36,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF36",
    "altTag": "ONIF36",
    "settlementFee": 0.0
  },
  {
    "productId": 31,
    "productGuid": "e2d7b26b-d494-494a-aa50-a804c52942a0",
    "name": "Interest Free Finance (48 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 48,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF48",
    "altTag": "ONIF48",
    "settlementFee": 0.0
  },
  {
    "productId": 256,
    "productGuid": "d9bed807-1761-437e-a9b7-8e28cf22199b",
    "name": "Interest Free Finance (60 Months)",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 60,
    "monthlyRate": 0.0,
    "apr": 0.0,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "IFF60",
    "altTag": "ONIF60",
    "settlementFee": 0.0
  },
  {
    "productId": 54,
    "productGuid": "152fea32-be31-4b73-94d9-f23716c42aac",
    "name": "Classic Credit 24 Months 9.9%",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 24,
    "monthlyRate": 0.79,
    "apr": 9.9,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "CC24-9.9",
    "altTag": "ONIB24-9.9",
    "settlementFee": 0.0
  },
  {
    "productId": 44,
    "productGuid": "cc62bb86-f91b-4a85-97eb-94bb7ac4462f",
    "name": "Classic Credit 36 Months 9.9%",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 36,
    "monthlyRate": 0.79,
    "apr": 9.9,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "CC36-9.9",
    "altTag": "ONIB36-9.9",
    "settlementFee": 0.0
  },
  {
    "productId": 45,
    "productGuid": "5c0da208-597b-4dc8-9e42-2326601fa699",
    "name": "Classic Credit 48 Months 9.9%",
    "minLoan": 250.0,
    "maxLoan": 25000.0,
    "months": 48,
    "monthlyRate": 0.79,
    "apr": 9.9,
    "serviceFee": 0.0,
    "documentFee": 0.0,
    "documentFeeCollectionMonth": 0,
    "documentFeeMaximum": 0,
    "documentFeeMinimum": 0,
    "documentFeePercentage": 0.0,
    "optionPeriod": 0,
    "deferredPeriod": 0,
    "tag": "CC48-9.9",
    "altTag": "ONIB48-9.9",
    "settlementFee": 0.0
  }
];

            parent.getFinanceProduct = function(id) {
            for (var i = 0; i < financeProducts.length; i++) {
            if (financeProducts[i].productId == id || financeProducts[i].tag == id || financeProducts[i].altTag == id) {
            return financeProducts[i];
            }
            }
            return null;
            }

            parent.getFinanceProducts = function() {
            return financeProducts;
            }

            parent.calculateApr = function(loan, instalment, deferred, term) {
            var result = parseFloat(0);
            var high = parseFloat(200);
            var low = parseFloat(0);
            var n, x, j, q, r, y, x

            if (deferred > 1) {
            n = (term + deferred + 1);
            } else {
            n = (term + 1);
            }

            x = 1;

            while (x<20) {
                         result=(high + low) / 2;

            j = parseFloat(Math.pow((1.0000 + result / 100.0000), (1.0000 / 12.0000)));

            q = parseFloat((1.0000 / j));

            if (deferred<1) {
                            y=parseFloat((instalment * (1.0000 - Math.pow(q, n))) / (1 - q) - instalment);

            z = parseFloat(0.00);
            } else {
            y = parseFloat((instalment * (1.0000 - Math.pow(q, n - 1))) / (1 - q) - instalment);

            z = parseFloat((instalment * (1.0000 - Math.pow(q, deferred))) / (1 - q) - instalment);
            }

            if ((y - z) < loan) {
            high = result;
            } else {
            low = result;
            }

            x++;
            }

            return result;
            }

            parent.sumCashFlows = function(months, cashflows) {

            var total = parseFloat(0.00);

            for (var i=1; i < months; i++) {
            total = parseFloat(total + parseFloat(cashflows[i-1].cashFlows));
            }

            return total;
            }

            parent.earliestDate = function(cashflows, months) {

            var earliest = cashflows[0].dataDate;

            for (var i=1; i < months; i++) {
            if (moment(cashflows[i].dataDate).isBefore(earliest)) {
            earliest = cashflows[i].dataDate;
            }
            }

            return earliest;
            }

            parent.presentValue = function(cashflows, irr, loanTerm, checkdate, numdays) {

            var presValue = parseFloat(0.0);

            for (var i=0; i < loanTerm; i++) {
            var cf = parseFloat(cashflows[i].cashFlows);

            var diff = parseFloat(moment(checkdate).diff(cashflows[i].dataDate, 'days'));

            presValue += parseFloat(cf / Math.pow(1 + irr, diff) / numdays);
            }

            return presValue;
            }

            parent.XIRR = function(values, dates, guess) {
            // Credits: algorithm inspired by Apache OpenOffice

            // Calculates the resulting amount
            var irrResult = function(values, dates, rate) {
            var r = rate + 1;
            var result = values[0];
            for (var i = 1; i < values.length; i++) {
            result += values[i] / Math.pow(r, moment(dates[i]).diff(moment(dates[0]), 'days') / 365);
            }
            return result;
            }

            // Calculates the first derivation
            var irrResultDeriv = function(values, dates, rate) {
            var r = rate + 1;
            var result = 0;
            for (var i = 1; i < values.length; i++) {
            var frac = moment(dates[i]).diff(moment(dates[0]), 'days') / 365;
            result -= frac * values[i] / Math.pow(r, frac + 1);
            }
            return result;
            }

            // Check that values contains at least one positive value and one negative value
            var positive = false;
            var negative = false;
            for (var i = 0; i < values.length; i++) {
            if (values[i] > 0) positive = true;
            if (values[i] < 0) negative = true;
            }

            // Return error if values does not contain at least one positive value and one negative value
            if (!positive || !negative) return '#NUM!';

            // Initialize guess and resultRate
            var guess = (typeof guess === 'undefined') ? 0.1 : guess;
            var resultRate = guess;

            // Set maximum epsilon for end of iteration
            var epsMax = 1e-10;

            // Set maximum number of iterations
            var iterMax = 50;

            // Implement Newton's method
            var newRate, epsRate, resultValue;
            var iteration = 0;
            var contLoop = true;
            do {
            resultValue = irrResult(values, dates, resultRate);
            newRate = resultRate - resultValue / irrResultDeriv(values, dates, resultRate);
            epsRate = Math.abs(newRate - resultRate);
            resultRate = newRate;
            contLoop = (epsRate > epsMax) && (Math.abs(resultValue) > epsMax);
            } while(contLoop && (++iteration < iterMax));

            if(contLoop) return '#NUM!';

            // Return internal rate of return
            return resultRate;
            }


            parent.calculateAprFromIrr = function(loan, monthlyinstalment, loanTerm, documentfee, documentfeecollectionmonth) {
            var startDate = new Date();
            var incomeTable = [];
            var dateTable = [];

            var checkDate;
            var incomeObject = {cashFlows:0, dataDate:0};
            var irr, irrPrev, presentValuePrev, pv;

            if (documentfeecollectionmonth==0) {
            incomeTable.push(parseFloat(loan * -1) + documentfee);
            }else{
            incomeTable.push(parseFloat(loan * -1));
            }
            dateTable.push(startDate);

            for (var i=1; i<=loanTerm; i++) {
            var nextDate = moment(startDate).add('M',i);
            dateTable.push(nextDate);

            if ((i-1) == documentfeecollectionmonth && documentfeecollectionmonth>0) {
            incomeTable.push(parseFloat(monthlyinstalment + documentfee));
            } else {
            incomeTable.push(parseFloat(monthlyinstalment));
            }
            }
            var r=parent.XIRR(incomeTable, dateTable, 0.1);

            return Math.round(r*10000)/100;
            }

            parent.calculateFromProductCode = function(productCode, cashPrice, deposit) {
            return parent.calculate(parent.getFinanceProduct(productCode), cashPrice, deposit);
            }

            //Calculates all financial parameters
            parent.calculate = function (financeProduct, cashPrice, deposit) {

            var apr = parseFloat(financeProduct.apr);
            var monthlyrate = parseFloat(financeProduct.monthlyRate);
            var calculatedApr;
            var months = parseFloat(financeProduct.months);
            var serviceFee = parseFloat(financeProduct.serviceFee);
            var balancePayable = parseFloat(0.00);
            var documentFee = 0;

            cashPrice = parseFloat(cashPrice);

            deposit = parseFloat(deposit);

            var loanAmount = cashPrice - deposit;
            var initialPayments, finalPayment, balancePayable;

            balancePayable = loanAmount;

            documentFee = financeProduct.documentFee + (loanAmount * financeProduct.documentFeePercentage);

            if (financeProduct.documentFeeMinimum>0 && documentFee < financeProduct.documentFeeMinimum) {documentFee = financeProduct.documentFeeMinimum}
            if (financeProduct.documentFeeMaximum>0 && documentFee > financeProduct.documentFeeMaximum) {documentFee = financeProduct.documentFeeMaximum}

            if (monthlyrate == 0) {
            initialPayments = Math.round((loanAmount / months) * 100) / 100;

            if (initialPayments * months < loanAmount) {
            initialPayments += 0.01;
            }

            finalPayment = (loanAmount - (initialPayments * (months - 1)));

            calculatedApr = 0;
            } else {
            var yield = Math.pow(((apr) / 100) + 1, (1.00 / 12));

            var pv = loanAmount - serviceFee;

            if (financeProduct.deferredPeriod>1) {
            pv = (pv * Math.pow(yield, (financeProduct.deferredPeriod - 1)));
            }

            initialPayments = Math.floor(((0 - pv / ((Math.pow(yield, 0 - months) - 1) / (yield - 1)))) * 100) / 100;

            finalPayment = initialPayments;

            balancePayable = (initialPayments * months);

            calculatedApr = parent.calculateApr((loanAmount - financeProduct.serviceFee), initialPayments, financeProduct.deferredPeriod, months);
            }

            if (documentFee>0) {
            calculatedApr = parent.calculateAprFromIrr(loanAmount, initialPayments, months, parseFloat(documentFee), parseFloat(financeProduct.documentFeeCollectionMonth));
            }

            //balancePayable = initialPayments * (months - 1);
            //balancePayable += finalPayment;

            var interest = (balancePayable - loanAmount);

            var chargeForCredit = (interest + serviceFee + documentFee);

            var amountPayable = (balancePayable + serviceFee + documentFee + deposit);

            var productAvailable = true;
            var availabilityReason = "";

            if (loanAmount < financeProduct.minLoan) {
            productAvailable = false;

            availabilityReason = "Only available on loan amounts over £" + financeProduct.minLoan.toFixed(2);
            }
            else if (loanAmount > financeProduct.maxLoan) {
            productAvailable = false;

            availabilityReason = "Only available on loan amounts under £" + financeProduct.maxLoan.toFixed(2);
            }

            var interest = initialPayments * months - loanAmount

            var annualRate = ((interest/loanAmount)*100)/((months + financeProduct.deferredPeriod)/12)

            var financeCalculation = {
            initialPayments: initialPayments.toFixed(2),
            finalPayment: finalPayment.toFixed(2),
            balancePayable: balancePayable.toFixed(2),
            interest: interest.toFixed(2),
            chargeForCredit: chargeForCredit.toFixed(2),
            amountPayable: amountPayable.toFixed(2),
            cashPrice: cashPrice.toFixed(2),
            deposit: deposit.toFixed(2),
            loanAmount: loanAmount.toFixed(2),
            months: months,
            monthsDeferred: financeProduct.deferredPeriod,
            apr: calculatedApr.toFixed(2),
            productAvailable: productAvailable,
            availabilityReason: availabilityReason,
            productId: financeProduct.productId,
            productGuid: financeProduct.productGuid,
            name: financeProduct.name,
            settlementFee: financeProduct.settlementFee.toFixed(2),
            serviceFee: serviceFee.toFixed(2),
            documentFee: documentFee.toFixed(2),
            documentFeeMinimum: financeProduct.documentFeeMinimum,
            documentFeeMaximum: financeProduct.documentFeeMaximum,
            documentFeeCollectionMonth: financeProduct.documentFeeCollectionMonth,
            documentFeePercentage: financeProduct.documentFeePercentage,
            annualRate: annualRate.toFixed(2)
            };

            return financeCalculation;
            };


            })(V12);

            
