function CalculationViewModel(){function l(b){Log(b);console.log("success");a.imageURL("");a.imageURL(b)}function m(){}var a=this;a.calculating=!1;a.calculationTimeout=null;a.initializing=!1;a.taxRate=Settings.defaultTaxModeValue()/100;a.taxRateObservable=ko.observable(a.taxRate);a.labelTaxRate=ko.observable(String.format(Culture.translations().stringTextTax,100*a.taxRate));a.sendCalculationLock=!1;a.id=ko.observable();a.updateTime=ko.observable();a.desc=ko.observable();a.description=ko.computed({read:function(){return a.desc()},
write:function(b){a.desc(b)}}).extend({validateStringLength:{min:ApplicationSettings.minDescriptionLength,max:ApplicationSettings.maxDescriptionLength,validateOnInitiate:!1}});a.make=ko.observable("");a.model=ko.observable("");a.type=ko.observable("");a.carName=ko.computed(function(){return a.make()+" "+a.model()+" "+a.type()});a.imageURL=ko.observable();a.oldImageURL=ko.observable();a.percentResidualValue=ko.observable();a.residualValueName=ko.observable();a.roundDifference=ko.observable(0);a.presentValue=
ko.observable().extend({validateFloat:{}});a.oneOffPayment=ko.observable().extend({validateFloat:{allowPercentageOf:a.presentValue}});a.finalValue=ko.observable().extend({validateFloat:{allowPercentageOf:a.presentValue}});a.monthlyPayment=ko.observable().extend({validateFloat:{}});a.numberOfMonths=ko.observable();a.calculationType=ko.observable();a.selectedObjectType=ko.observable();a.mileage=ko.observable();a.baseValue=ko.observable().extend({validateFloat:{}});a.accessoriesValue=ko.observable().extend({validateFloat:{}});
a.finalBaseValue=ko.observable().extend({validateFloat:{allowPercentageOf:a.presentValue}});a.finalCorrectValue=ko.observable().extend({validateFloat:{}});a.residualValueChart=ko.observable();a.carsResidualValueChartId=ko.observable();a.slidersVisible=ko.observable();a.calculationMode=ko.observable();a.calculationTypes=ko.observableArray([{id:CalculationTypes.Leasing,label:function(){return Culture.translations().stringTextLeasing}},{id:CalculationTypes.Credit,label:function(){return Culture.translations().stringTextCredit}}]);
a.objectTypes=ko.observableArray([{id:ObjectTypes.Undefined,label:function(){return Culture.translations().stringTextUndefined}},{id:ObjectTypes.Car,label:function(){return Culture.translations().stringTextCar}}]);a.objectTypes.destroy(function(a){return a.id==ObjectTypes.ObPool});a.usages=ko.observableArray([{id:Usages.Business,label:function(){return Culture.translations().stringTextNetto}},{id:Usages.Private,label:function(){return Culture.translations().stringTextBrutto}}]);a.selectedUsage=ko.observable();
a.instalmentCountOptions=ko.observableArray();a.mileageCountOptions=ko.observableArray([5E3,1E4,15E3,2E4,25E3,3E4,35E3,4E4,45E3,5E4,55E3,6E4,65E3,7E4,75E3,8E4]);a.contractTypesDisabled=ko.observable();a.objectTypesDisabled=ko.observable();a.usagesDisabled=ko.observable();a.interestRate=ko.observable().extend({validateFloat:{}});a.netAmount=ko.computed({read:function(){return a.selectedUsage()==Usages.Private?CalculateNetValue(a.presentValue(),a.taxRateObservable()):a.presentValue()},write:function(b){a.selectedUsage()==
Usages.Private?a.presentValue(CalculateGrossValue(b,a.taxRateObservable())):a.presentValue(b)}}).extend({validateFloat:{defaultValue:0}});a.grossAmount=ko.computed({read:function(){return a.selectedUsage()==Usages.Private?a.presentValue():CalculateGrossValue(a.presentValue(),a.taxRateObservable())},write:function(b){a.selectedUsage()==Usages.Private?a.presentValue(b):a.presentValue(CalculateNetValue(b,a.taxRateObservable()))}}).extend({validateFloat:{defaultValue:0}});a.taxValue=ko.computed(function(){return CalculateTax(a.netAmount(),
a.taxRateObservable())});a.netMonthlyPayment=ko.computed({read:function(){return a.selectedUsage()==Usages.Private?CalculateNetValue(a.monthlyPayment(),a.taxRateObservable()):a.monthlyPayment()},write:function(b){a.selectedUsage()==Usages.Private?a.monthlyPayment(CalculateGrossValue(b,a.taxRateObservable())):a.monthlyPayment(b)}}).extend({validateFloat:{defaultValue:0}});a.grossMonthlyPayment=ko.computed({read:function(){return a.selectedUsage()==Usages.Private?a.monthlyPayment():CalculateGrossValue(a.monthlyPayment(),
a.taxRateObservable())},write:function(b){a.selectedUsage()==Usages.Private?a.monthlyPayment(b):a.monthlyPayment(CalculateNetValue(b,a.taxRateObservable()))}}).extend({validateFloat:{defaultValue:0}});a.taxMonthlyPaymentValue=ko.computed(function(){return CalculateTax(a.netMonthlyPayment(),a.taxRateObservable())});a.reset=function(){a.calculating=!1;a.calculationTimeout=null;a.initializing=!1;a.taxRate=Settings.defaultTaxModeValue()/100;a.taxRateObservable(a.taxRate);a.labelTaxRate(String.format(Culture.translations().stringTextTax,
100*a.taxRate));a.sendCalculationLock=!1;a.id(0);a.updateTime(moment());a.desc(Culture.translations().stringTextNewCalculation);a.make("");a.model("");a.type("");a.percentResidualValue(0);a.residualValueName("");a.imageURL("");a.oldImageURL("");a.objectTypes([{id:ObjectTypes.Undefined,label:function(){return Culture.translations().stringTextUndefined}},{id:ObjectTypes.Car,label:function(){return Culture.translations().stringTextCar}}]);a.calculationType(Settings.defaultCalculationType()==CalculationTypes.Select?
CalculationTypes.Leasing:Settings.defaultCalculationType());a.selectedObjectType(Settings.defaultObjectType()==ObjectTypes.Select?ObjectTypes.Car:Settings.defaultObjectType());a.mileage(2E4);a.baseValue(1E5);a.accessoriesValue(0);a.finalBaseValue(25E3);a.finalCorrectValue(0);a.residualValueChart(Settings.getDefaultResidualValueChart());a.slidersVisible(Settings.useSliders());a.calculationMode(CalculationModes.monthlyPayment);a.selectedUsage(Settings.defaultTaxMode()==TaxModes.Select?Usages.Private:
Settings.defaultTaxMode());a.interestRate(Settings.defaultInterestRate());a.roundDifference(0);a.presentValue(1E5);a.oneOffPayment(2E4);a.finalValue(25E3);a.monthlyPayment(0);a.numberOfMonths(48);a.contractTypesDisabled(!1);a.objectTypesDisabled(!1);a.usagesDisabled(!1)};a.reset();a.firstPaymentLabel=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextOneOffPayment;case CalculationTypes.Credit:return Culture.translations().stringTextFirstPayment}});
a.firstPaymentLabelPopUp=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextOneOffPaymentPopUp;case CalculationTypes.Credit:return Culture.translations().stringTextFirstPaymentPopUp}});a.firstPaymentLabelPDF=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextOneOffPaymentPDF;case CalculationTypes.Credit:return Culture.translations().stringTextFirstPaymentPDF}});
a.firstPaymentDescriptionPopUp=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextOneOffPaymentDescription+".";case CalculationTypes.Credit:return Culture.translations().stringTextFirstpaymentDescription+"."}});a.lastPaymentLabel=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextResidualValue;case CalculationTypes.Credit:return Culture.translations().stringTextLastPayment}});
a.lastPaymentLabelPopUp=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextResidualValuePopUp;case CalculationTypes.Credit:return Culture.translations().stringTextLastPaymentPopUp}});a.lastPaymentLabelPDF=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextResidualValuePDF;case CalculationTypes.Credit:return Culture.translations().stringTextLastPaymentPDF}});
a.lastPaymentDescriptionPopUp=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextResidualValueDescription+".";case CalculationTypes.Credit:return Culture.translations().stringTextLastPaymentDescription+"."}});a.monthlyPaymentDescriptionPopUp=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextMonthlyPaymentLeasingDescription+".";case CalculationTypes.Credit:return Culture.translations().stringTextMonthlyPaymentCreditDescription+
"."}});a.residualValueAlgorithmDescription=ko.computed(function(){return String.format(Culture.translations().stringTextResidualValueAlgorithmDescription,'"'+a.carName()+'"',a.residualValueName(),a.numberOfMonths(),a.mileage(),a.percentResidualValue())+"."});a.emailUrl=ko.computed(function(){var b=a.description(),c;c=""+String.format("{0}\n\n",a.description());if((a.selectedObjectType()==ObjectTypes.Car||a.selectedObjectType()==ObjectTypes.ObPool)&&a.make().length&&a.model().length&&a.type().length)c+=
String.format("{0}:  {1}\n",Culture.translations().stringTextDescription,a.carName()),b=a.carName();c+=String.format("{0}:  {1}\n",Culture.translations().stringTextCalculationType,CalculationTypes.getText(a.calculationType()));c+=String.format("{0}:  {1}\n",Culture.translations().stringTextObjectType,ObjectTypes.getText(a.selectedObjectType()));c+=String.format("{0}:  {1}\n",Culture.translations().stringTextUsage,TaxModes.getText(a.selectedUsage()));c+=String.format("{0}:  {1}\n",Culture.translations().stringTextPrice,
a.presentValue().formatCurrency());c+=String.format("{0}:  {1}\n",a.firstPaymentLabelPopUp(),a.oneOffPayment().formatCurrency());c+=String.format("{0}:  {1} {2}\n",Culture.translations().stringTextPeriod,a.numberOfMonths(),Culture.translations().stringTextMonths);c+=String.format("{0}:  {1}\n",a.lastPaymentLabelPopUp(),a.finalValue().formatCurrency());c+=String.format("{0}:  {1}\n",Culture.translations().stringTextMonthlyPayment,a.monthlyPayment().formatCurrency());c+=String.format("{0}:  {1}\n",
Culture.translations().stringTextInterestRate,a.interestRate().formatPercentage());c=encodeURIComponent(c);return String.format(Constants.EmailFormat,ApplicationSettings.defaultEmail,Culture.translations().stringTextEmailCalculationSubject+b,c)});a.emailCommand=function(){a.saveCommand();return!0};a.constraints={};a.constraints.oneOff={min:ko.observable(0),max:ko.observable(0)};a.constraints.oneOffPercentage={min:ko.observable(0),max:ko.observable(0)};a.constraints.residualValue={min:ko.observable(0),
max:ko.observable(0)};a.constraints.residualValuePercentage={min:ko.observable(0),max:ko.observable(0)};a.constraints.amount={min:ko.observable(0),max:ko.observable(0)};a.constraints.duration={min:ko.observable(0),max:ko.observable(0)};a.constraints.durationStep=ko.observable();a.constraints.monthlyPayment={min:ko.observable(0),max:ko.observable(0)};a.constraints.oneOffMerged=ko.observable();a.constraints.residualValueMerged=ko.observable();var e=function(){function b(a,b,c){a.min(Math.max(b.Min,
c.Min));a.max(Math.min(b.Max,c.Max))}Log(a.selectedObjectType());Log(Settings.calculationSettings().ObjectTypes[a.selectedObjectType()]);var c=Settings.calculationSettings().ObjectTypes[a.selectedObjectType()],d=Settings.calculationSettings().ContractTypes[a.calculationType()];b(a.constraints.duration,c.Duration,d.Duration);a.constraints.durationStep(Math.max(c.DurationStep,d.DurationStep));var f=a.numberOfMonths();a.instalmentCountOptions([]);for(var k=a.constraints.duration.min();k<=a.constraints.duration.max();k+=
a.constraints.durationStep())a.instalmentCountOptions.push(k);a.numberOfMonths(f);b(a.constraints.oneOff,c.OneOff,d.OneOff);b(a.constraints.oneOffPercentage,c.OneOffPercentage,d.OneOffPercentage);b(a.constraints.residualValue,c.ResidualValue,d.ResidualValue);b(a.constraints.residualValuePercentage,c.ResidualValuePercentage,d.ResidualValuePercentage);b(a.constraints.amount,c.Amount,d.Amount);a.constraints.monthlyPayment.min(0);a.constraints.monthlyPayment.max(Math.max(d.Amount.Max,c.Amount.Max));Log(a.constraints)};
a.selectedObjectType.subscribe(e);a.calculationType.subscribe(e);e();e=function(){a.initializing||(a.calculationTimeout&&clearTimeout(a.calculationTimeout),a.calculationTimeout=setTimeout(function(){a.calculate()},300))};a.presentValue.subscribe(e);a.oneOffPayment.subscribe(e);a.finalValue.subscribe(e);a.monthlyPayment.subscribe(e);a.numberOfMonths.subscribe(e);a.calculationType.subscribe(e);a.calculationMode.subscribe(e);a.interestRate.subscribe(e);a.monthlyPayment.subscribe(function(){a.calculationMode()!=
CalculationModes.monthlyPayment&&a.roundDifference(0)});a.presentValue.disableUpdates=!1;e=function(){!a.presentValue.disableUpdates&&(a.baseValue.isValid()&&a.accessoriesValue.isValid())&&(a.presentValue.disableUpdates=!0,a.presentValue(a.baseValue()+a.accessoriesValue()),a.presentValue.disableUpdates=!1)};a.presentValue.subscribe(function(){!a.presentValue.disableUpdates&&a.presentValue.isValid()&&(a.presentValue.disableUpdates=!0,a.baseValue(a.presentValue()),a.accessoriesValue(0),a.presentValue.disableUpdates=
!1)});a.baseValue.subscribe(e);a.accessoriesValue.subscribe(e);a.finalValue.disableUpdates=!1;var j=function(){if(!a.finalValue.disableUpdates&&a.finalCorrectValue.isValid()&&!a.initializing)if((a.selectedObjectType()==ObjectTypes.Car||a.selectedObjectType()==ObjectTypes.ObPool)&&a.calculationType()==CalculationTypes.Leasing){if(void 0===a.residualValueChart())a.residualValueChart(Settings.getDefaultResidualValueChart());else if(!(null===a.carsResidualValueChartId()||void 0===a.carsResidualValueChartId())&&
a.carsResidualValueChartId()!==a.residualValueChart().Id){var b=Settings.getResidualValueChartById(a.carsResidualValueChartId());null===b?a.residualValueChart(Settings.getDefaultResidualValueChart()):a.residualValueChart(b)}var c=ko.utils.arrayFilter(a.residualValueChart().Values,function(b){return b.Mileage===a.mileage()&&b.Period===a.numberOfMonths()}),b=0;0!=c.length&&(b=c[0].ResidualValue);c=void 0==a.residualValueChart().Name?Culture.translations().stringTextUndefined:a.residualValueChart().Name;
a.finalValue.disableUpdates=!0;a.initializing=!0;a.percentResidualValue(b);a.residualValueName(c);a.finalBaseValue(a.presentValue()*0.01*b);a.finalValue.percentageLock=!0;a.finalValue(a.finalBaseValue()+a.finalCorrectValue());a.finalValue.percentageLock=!1;a.initializing=!1;a.finalValue.disableUpdates=!1}else!a.finalValue.disableUpdates&&(a.finalBaseValue.isValid()&&a.finalCorrectValue.isValid()&&a.finalValue.isValid())&&(a.finalValue.disableUpdates=!0,a.finalValue(a.finalBaseValue()+a.finalCorrectValue()),
a.finalValue.disableUpdates=!1)};a.finalValue.subscribe(function(){!a.finalValue.disableUpdates&&a.finalValue.isValid()&&(a.finalValue.disableUpdates=!0,a.finalBaseValue.percentageLock=!0,a.finalBaseValue(a.finalValue()),a.finalCorrectValue(0),a.finalValue.disableUpdates=!1,a.finalBaseValue.percentageLock=!1)});a.finalBaseValue.subscribe(j);a.numberOfMonths.subscribe(j);a.finalCorrectValue.subscribe(j);a.mileage.subscribe(j);a.selectedUsage.subscribe(function(b){a.calculating=!0;a.oneOffPayment.percentageLock=
!0;a.finalValue.percentageLock=!0;b==Usages.Private?(a.presentValue(CalculateGrossValue(a.presentValue(),a.taxRate)),a.oneOffPayment(CalculateGrossValue(a.oneOffPayment(),a.taxRate)),a.finalValue(CalculateGrossValue(a.finalValue(),a.taxRate)),a.monthlyPayment(CalculateGrossValue(a.monthlyPayment(),a.taxRate))):b==Usages.Business&&(a.presentValue(CalculateNetValue(a.presentValue(),a.taxRate)),a.oneOffPayment(CalculateNetValue(a.oneOffPayment(),a.taxRate)),a.finalValue(CalculateNetValue(a.finalValue(),
a.taxRate)),a.monthlyPayment(CalculateNetValue(a.monthlyPayment(),a.taxRate)));a.calculating=!1;a.oneOffPayment.percentageLock=!1;a.finalValue.percentageLock=!1;a.calculate()});a.selectedObjectType.subscribe(function(){if(a.selectedObjectType()==ObjectTypes.Car||a.selectedObjectType()==ObjectTypes.ObPool)(a.calculationMode()==CalculationModes.presentValue||a.calculationMode()==CalculationModes.finalValue&&a.calculationType()==CalculationTypes.Leasing)&&a.calculationMode(CalculationModes.monthlyPayment)});
a.calculationType.subscribe(function(){(a.selectedObjectType()==ObjectTypes.Car||a.selectedObjectType()==ObjectTypes.ObPool)&&a.calculationMode()==CalculationModes.finalValue&&a.calculationType()==CalculationTypes.Leasing&&a.calculationMode(CalculationModes.monthlyPayment)});a.buttonsDisabled=ko.computed(function(){return a.description.isValid()?!1:!0});a.openAmountDetailsCommand=function(){a.selectedObjectType()==ObjectTypes.ObPool?(PageParameters.createNewCalculation=!1,PageParameters.downloadPoolObjects=
!0,MainViewModel.isAvailable($.mobile.changePage,"PoolObject.html")):a.selectedObjectType()==ObjectTypes.Car?(PageParameters.createNewCalculation=!1,CarsViewModel.reset(),$.mobile.changePage("CarConfigurator.html","none",!0,!0)):$.mobile.changePage("AmountDetails.html","none",!0,!0)};a.openFinalValueDetailsCommand=function(){$.mobile.changePage("FinalValueDetails.html","none",!0,!0)};a.openMonthlyPaymentDetailsCommand=function(){$.mobile.changePage("MonthlyPaymentDetails.html","none",!0,!0)};a.saveCommand=
function(){Overlay.create(!0,Culture.translations().stringMsgSavingCalculation);a.RepairCalculation();a.imageURL()!=a.oldImageURL()?(a.removePicture(a.oldImageURL()),a.savePicture(a.imageURL(),function(b){Log(b);var c=a.imageURL();a.clenUpFolder(c);a.updateTime(moment());a.imageURL(b);a.oldImageURL(a.imageURL());DataProvider.saveCalculation(a);Overlay.hideLoader();Overlay.showTextOnlyLoader(Culture.translations().stringMsgCalculationSaved);setTimeout(function(){Overlay.destroy();PageParameters.calculationId=
a.id()},2E3)})):(a.updateTime(moment()),DataProvider.saveCalculation(a),Overlay.hideLoader(),Overlay.showTextOnlyLoader(Culture.translations().stringMsgCalculationSaved),setTimeout(function(){Overlay.destroy();PageParameters.calculationId=a.id()},2E3))};a.printCommand=function(){a.saveCommand();if(void 0!=cordova&&void 0!=cordova.exec){var b=moment();PdfViewer.showPdf(a.desc()+" "+b.format(Constants.FullDataFormatWithDots)+".pdf",a.generatePdf().output())}else window.open(a.generatePdf().output("datauristring"))};
a.sendCalculationCommand=function(){a.sendCalculationLock||(a.sendCalculationLock=!0,a.saveCommand(),Overlay.create(!0,Culture.translations().stringMsgSendingCalculation),ServiceDataProvider.addCalculation({Id:a.id(),Date:a.updateTime().format(Constants.FullDataFormat),Description:a.description(),CalculationType:a.calculationType(),ObjectType:a.selectedObjectType(),Price:a.presentValue(),OneOffPayment:a.oneOffPayment(),Period:a.numberOfMonths(),LastPayment:a.finalValue(),MonthlyPayment:a.monthlyPayment(),
InterestRate:a.interestRate()}).done(function(){setTimeout(function(){Overlay.destroy()},2E3)}).always(function(b){Overlay.hideLoader();if(this.state()==ASyncState.Resolved)b=Culture.translations().stringMsgTheCalculationHasBeenSuccessfullySent;else switch(b){case 3015:case 3016:MainViewModel.handleLostSession("EditCalculation3.html",a.sendCalculationCommand);return;default:b=Culture.translations().stringErrorMsgTheCalculationCouldNotBeSent}a.sendCalculationLock=!1;Overlay.hideLoader();Overlay.showTextOnlyLoader(b)}).fail(function(){a.sendCalculationLock=
!1;setTimeout(function(){Overlay.destroy()},2E3)}))};a.gotoLastPageCommand=function(){a.calculate();if(a.presentValue.isValid()&&a.presentValue.isInRange()&&a.oneOffPayment.isValid()&&a.oneOffPayment.isInRange()&&a.finalValue.isValid()&&a.finalValue.isInRange()&&a.monthlyPayment.isValid()&&a.monthlyPayment.isInRange())$.mobile.changePage("EditCalculation3.html");else{var b=Culture.translations().stringErrorMsgSomeFieldsHaveWrongValues+". "+Culture.translations().stringMsgCorrectTheValuesToProceed+
".",c=Culture.translations().stringTextError;navigator.notification?navigator.notification.alert(b,void 0,c,Culture.translations().stringTextOk):alert(b)}};a.takePhotoCommand=function(){void 0!=navigator.camera&&navigator.camera.getPicture(l,m,{quality:85,destinationType:Camera.DestinationType.FILE_URI,sourceType:Camera.PictureSourceType.CAMERA,allowEdit:!0,encodingType:Camera.EncodingType.JPEG,targetWidth:800,targetHeight:600,correctOrientation:!0})};a.removePicture=function(a){void 0!=navigator.notification&&
""!=a&&(Log(a),window.resolveLocalFileSystemURI(a,function(a){a.remove(function(){Log("Picture removed")},function(){Log("Picture not removed")})},function(){Log("resolveLocalFileSystemURI failed")}))};a.clenUpFolder=function(a){Log("<<<>>><<<   "+a);void 0!=navigator.notification&&""!=a&&window.resolveLocalFileSystemURI(a,function(a){a.getParent(function(a){Log(a.name);Log(a.fullPath);a.createReader().readEntries(function(a){var b;for(b=0;b<a.length;b++)console.log(a[b].name),a[b].remove(function(){Log("Cache picture removed")},
function(){Log("Cache picture not removed")})},function(){Log("failed")})},function(){Log("failed2")})},function(){Log("failed3")})};a.savePicture=function(a,c){void 0!=navigator.notification&&""!=a?(Log(a),window.requestFileSystem(LocalFileSystem.PERSISTENT,0,function(d){Log("root: "+d.root.fullPath);d.root.getDirectory("ExCalc",{create:!0,exclusive:!1},function(d){window.resolveLocalFileSystemURI(a,function(e){e.copyTo(d,a.split("/").pop(),function(){Log(d.toURL()+"/"+a.split("/").pop());Log("success");
c(d.toURL()+"/"+a.split("/").pop())},function(){Log("failed");c(a)})},function(){Log("resolveLocalFileSystemURI failed");c(a)})},function(){Log("getDirectory failed");c(a)})},function(){Log("requestFileSystem failed");c(a)})):c(a)};a.commandNewCalculation=function(){Overlay.create(!1);Overlay.showTextOnlyLoader(Culture.translations().stringTextNewCalculationCreated);a.reset();setTimeout(function(){Overlay.destroy()},2E3)};a.generatePdf=function(){var b=new jsPDF;b.setFont("Helvetica","");b.setFontSize(30);
b.text(a.description(),20,20);b.setFontSize(20);var c=35;if((a.selectedObjectType()==ObjectTypes.Car||a.selectedObjectType()==ObjectTypes.ObPool)&&a.make().length&&a.model().length&&a.type().length)b.text(Culture.translations().stringTextDescriptionPDF,20,c),b.text(a.carName(),100,c),c+=10;b.text(Culture.translations().stringTextCalculationTypePDF,20,c);b.text(CalculationTypes.getText(a.calculationType()),100,c);c+=10;b.text(Culture.translations().stringTextObjectTypePDF,20,c);b.text(ObjectTypes.getText(a.selectedObjectType()),
100,c);c+=10;b.text(Culture.translations().stringTextUsagePDF,20,c);b.text(TaxModes.getText(a.selectedUsage()),100,c);c+=10;b.text(Culture.translations().stringTextPricePDF,20,c);b.text(a.presentValue().formatCurrency(),100,c);c+=10;b.text(a.firstPaymentLabelPDF(),20,c);b.text(a.oneOffPayment().formatCurrency(),100,c);c+=10;b.text(Culture.translations().stringTextPeriodPDF,20,c);b.text(a.numberOfMonths()+"",100,c);c+=10;b.text(a.lastPaymentLabelPDF(),20,c);b.text(a.finalValue().formatCurrency(),100,
c);c+=10;b.text(Culture.translations().stringTextMonthlyPaymentPDF,20,c);b.text(a.monthlyPayment().formatCurrency(),100,c);c+=10;b.text(Culture.translations().stringTextInterestRatePDF,20,c);b.text(a.interestRate().formatPercentage(),100,c);return b};a.calculate=function(){function b(a,b){var c={min:ko.observable(a*b.min()/100),max:ko.observable(a*b.max()/100)};Log(a);Log(b);Log(c);return c}function c(a,b){Log(a);Log(b);return a>=b.min()&&a<=b.max()}if(!a.calculating){a.calculating=!0;Log("Calculating...");
Log(a.finalValue());j();Log(a.finalValue());var d=a.roundDifference(),f=a.numberOfMonths(),e=a.presentValue(),g=a.oneOffPayment(),h=a.finalValue(),i=a.monthlyPayment();(!a.interestRate.isValid()||0>=a.interestRate())&&a.interestRate(Settings.defaultInterestRate());Log("----- Input values -----");Log("RoundDifferenceself"+a.roundDifference());Log("Present value: "+e);Log("One-off payment: "+g);Log("Number of months: "+f);Log("Final value: "+h);Log("Monthly payment: "+i);Log("-------- Result --------");
a.constraints.oneOffMerged({min:Math.max(a.constraints.oneOff.min(),a.constraints.oneOffPercentage.min()*a.presentValue()/100),max:Math.min(a.constraints.oneOff.max(),a.constraints.oneOffPercentage.max()*a.presentValue()/100)});a.constraints.residualValueMerged({min:Math.max(a.constraints.residualValue.min(),a.constraints.residualValuePercentage.min()*a.presentValue()/100),max:Math.min(a.constraints.residualValue.max(),a.constraints.residualValuePercentage.max()*a.presentValue()/100)});a.presentValue.isInRange(c(e,
a.constraints.amount)||!a.presentValue.isValid()||a.selectedObjectType()==ObjectTypes.Car||a.selectedObjectType()==ObjectTypes.ObPool);a.oneOffPayment.isInRange(c(g,a.constraints.oneOff)&&c(g,b(a.presentValue(),a.constraints.oneOffPercentage))||!a.oneOffPayment.isValid());a.finalValue.isInRange(c(h,a.constraints.residualValue)&&c(h,b(a.presentValue(),a.constraints.residualValuePercentage))||!a.finalValue.isValid()||a.selectedObjectType()==ObjectTypes.Car||a.selectedObjectType()==ObjectTypes.ObPool);
a.monthlyPayment.isInRange(c(i,a.constraints.monthlyPayment));Log(a.presentValue.isInRange());switch(a.calculationMode()){case CalculationModes.presentValue:d=CalculateAmountRounded(f,12,a.interestRate(),h,i,a.getMode(),g,d,Culture.currentCulture().currencyDecimalDigits);f=a.oneOffPayment.isValid()&&a.finalValue.isValid()&&a.monthlyPayment.isValid();a.presentValue(f?d:0);Log("Present value: "+d+" [valid: "+f+"]");break;case CalculationModes.oneOffPayment:d=CalculateOneOffRounded(f,12,a.interestRate(),
h,i,a.getMode(),e,d,Culture.currentCulture().currencyDecimalDigits);f=a.presentValue.isValid()&&a.finalValue.isValid()&&a.monthlyPayment.isValid();a.oneOffPayment(f?d:0);Log(f+" - "+d);Log("One-off payment: "+d+" [valid: "+f+"]");break;case CalculationModes.finalValue:d=CalculateResidualValueRounded(f,12,a.interestRate(),e,g,i,a.getMode(),d,Culture.currentCulture().currencyDecimalDigits);f=a.presentValue.isValid()&&a.oneOffPayment.isValid()&&a.monthlyPayment.isValid();a.finalValue(f?d:0);Log("Final value: "+
d+" [valid: "+f+"]");break;case CalculationModes.monthlyPayment:d=CalculateInstallmentRounded(f,12,a.interestRate(),e,g,h,a.getMode(),Culture.currentCulture().currencyDecimalDigits),f=a.presentValue.isValid()&&a.oneOffPayment.isValid()&&a.finalValue.isValid(),a.monthlyPayment(f?d[0]:0),a.roundDifference(f?d[1]:0),Log("Monthly payment: "+d+" [valid: "+f+"]")}a.calculating=!1}};a.getMode=function(){return a.calculationType()==CalculationTypes.Leasing?1:0};a.setValues=function(b){a.initializing=!0;a.taxRate=
b.PriceTaxMode?b.PriceTaxMode:Settings.defaultTaxModeValue()/100;a.taxRateObservable(a.taxRate);a.labelTaxRate(String.format(Culture.translations().stringTextTax,100*a.taxRate));a.desc($.trim(b.Make+" "+b.Model+" "+b.Type));a.make(b.Make);a.model(b.Model);a.type(b.Type);a.imageURL("");a.calculating=!1;a.objectTypes([{id:ObjectTypes.Undefined,label:function(){return Culture.translations().stringTextUndefined}},{id:ObjectTypes.Car,label:function(){return Culture.translations().stringTextCar}},{id:ObjectTypes.ObPool,
label:function(){return Culture.translations().stringTextPoolObject}}]);b.ObjectType!=ObjectTypes.Select&&(a.selectedObjectType(b.ObjectType),a.objectTypesDisabled(!0));b.ContractType!=CalculationTypes.Select&&(a.calculationType(b.ContractType),a.contractTypesDisabled(!0));b.PriceTaxMode&&(a.selectedUsage(b.PriceTaxMode),a.usagesDisabled(!0));a.presentValue(b.Price);a.oneOffPayment.percentageLock=!0;a.oneOffPayment(b.FirstPayment);a.oneOffPayment.percentageLock=!1;a.finalValue.percentageLock=!0;a.finalValue(b.LastPayment);
a.finalValue.percentageLock=!1;a.monthlyPayment(b.MonthlyPayment);a.numberOfMonths(b.NumberOfMonths);Log(b.ObjectType);a.selectedUsage()==Usages.Private?a.baseValue(b.Price):a.baseValue(CalculateNetValue(b.Price,a.taxRate));a.finalBaseValue(a.finalValue());a.residualValueChart(Settings.getDefaultResidualValueChart());a.mileage(b.Mileage);a.interestRate(b.InterestRate);a.initializing=!1};a.setModelValues=function(b){a.initializing=!0;a.id(b.id());a.taxRate=b.taxRate;a.taxRateObservable(a.taxRate);
a.labelTaxRate(String.format(Culture.translations().stringTextTax,100*a.taxRate));a.desc(b.desc());a.make(b.make());a.model(b.model());a.type(b.type());a.mileage(b.mileage());a.imageURL(b.imageURL());a.oldImageURL(b.imageURL());a.calculating=!1;b.selectedObjectType()==ObjectTypes.ObPool&&a.objectTypes([{id:ObjectTypes.Undefined,label:function(){return Culture.translations().stringTextUndefined}},{id:ObjectTypes.Car,label:function(){return Culture.translations().stringTextCar}},{id:ObjectTypes.ObPool,
label:function(){return Culture.translations().stringTextPoolObject}}]);a.selectedObjectType(b.selectedObjectType());a.objectTypesDisabled(b.objectTypesDisabled());a.calculationType(b.calculationType());a.contractTypesDisabled(b.contractTypesDisabled());a.selectedUsage(b.selectedUsage());a.usagesDisabled(b.usagesDisabled());a.calculationMode(b.calculationMode());a.presentValue(b.presentValue());a.oneOffPayment.percentageLock=!0;a.oneOffPayment(b.oneOffPayment());a.oneOffPayment.percentageLock=!1;
a.finalValue.percentageLock=!0;a.finalBaseValue(b.finalBaseValue());a.finalCorrectValue(b.finalCorrectValue());a.finalValue.percentageLock=!1;a.monthlyPayment(b.monthlyPayment());a.baseValue(b.baseValue());a.accessoriesValue(b.accessoriesValue());a.residualValueChart(b.residualValueChart());a.mileage(b.mileage());a.interestRate(b.interestRate());a.numberOfMonths(b.numberOfMonths());a.initializing=!1};a.RepairCalculation=function(){var b=function(a){return null==a||isNaN(a)?0:a};a.presentValue(b(a.presentValue()));
a.oneOffPayment(b(a.oneOffPayment()));a.numberOfMonths(b(a.numberOfMonths()));a.finalBaseValue(b(a.finalBaseValue()));a.finalCorrectValue(b(a.finalCorrectValue()));a.monthlyPayment(b(a.monthlyPayment()));a.interestRate(b(a.interestRate()));a.mileage(b(a.mileage()));console.log(a.accessoriesValue());a.accessoriesValue(b(a.accessoriesValue()));a.baseValue(b(a.baseValue()))}};
