var V12=V12?V12:{};(function(parent){var settings={retailerId:13691,retailerGuid:"516ceb36-c8ac-4ea1-8d11-dbf5ba150659",applicationUrl:"https://apply.v12finance.com/javascriptgateway/retailer-scripts/13691/516ceb36-c8ac-4ea1-8d11-dbf5ba150659/submitusingformpost",version:"1.0.11"};var financeProducts=[{"productId":43,"productGuid":"20125e19-f2c2-42f4-a230-ec668f776296","name":"Interest Free Finance (9 Months)","minLoan":250.0,"maxLoan":15000.0,"months":9,"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":"IFF9","altTag":"ONIF9","settlementFee":0.0},{"productId":294,"productGuid":"f0418d9e-464f-4b4a-ae06-6dc3cf34f705","name":"Interest Free Finance (15 Months)","minLoan":250.0,"maxLoan":15000.0,"months":15,"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":"IFF15","altTag":"ONIF15","settlementFee":0.0},{"productId":46,"productGuid":"c3114266-125a-4ea1-ab10-cfb3ebd48b81","name":"Interest Free Finance (18 Months)","minLoan":625.0,"maxLoan":15000.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":1000.0,"maxLoan":15000.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":30,"productGuid":"e931ab43-b8ae-431d-ac50-812d413fb5bb","name":"Interest Free Finance (36 Months)","minLoan":2000.0,"maxLoan":15000.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":64,"productGuid":"1401bd54-9a22-4ce7-8f7c-61a3ebb93639","name":"Classic Credit 24 Months 15.9%","minLoan":250.0,"maxLoan":15000.0,"months":24,"monthlyRate":1.237,"apr":15.9,"serviceFee":0.0,"documentFee":0.0,"documentFeeCollectionMonth":0,"documentFeeMaximum":0,"documentFeeMinimum":0,"documentFeePercentage":0.0,"optionPeriod":0,"deferredPeriod":0,"tag":"CC24-15.9","altTag":"ONIB24-15.9","settlementFee":0.0},{"productId":65,"productGuid":"bbe76da6-c60e-4881-83fc-328e415f0a5a","name":"Classic Credit 36 Months 15.9%","minLoan":250.0,"maxLoan":15000.0,"months":36,"monthlyRate":1.237,"apr":15.9,"serviceFee":0.0,"documentFee":0.0,"documentFeeCollectionMonth":0,"documentFeeMaximum":0,"documentFeeMinimum":0,"documentFeePercentage":0.0,"optionPeriod":0,"deferredPeriod":0,"tag":"CC36-15.9","altTag":"ONIB36-15.9","settlementFee":0.0},{"productId":66,"productGuid":"3fd9bcde-bc26-47d2-bd4f-df453bc6f1a1","name":"Classic Credit 48 Months 15.9%","minLoan":250.0,"maxLoan":15000.0,"months":48,"monthlyRate":1.237,"apr":15.9,"serviceFee":0.0,"documentFee":0.0,"documentFeeCollectionMonth":0,"documentFeeMaximum":0,"documentFeeMinimum":0,"documentFeePercentage":0.0,"optionPeriod":0,"deferredPeriod":0,"tag":"CC48-15.9","altTag":"ONIB48-15.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){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;}
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;}
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;}
if(!positive||!negative)return'#NUM!';var guess=(typeof guess==='undefined')?0.1:guess;var resultRate=guess;var epsMax=1e-10;var iterMax=50;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 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);}
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));}
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);