function RepaymentCalculationViewModel(){var a=this;a.isContract=ko.observable(!1);a.initializing=!0;a.calculationTimeout=null;a.calculatingContraint=!1;a.ObjectDescription=ko.observable("");a.Id=ko.observable(0);a.RedemptionDate=ko.observable("");a.contractDate=ko.observable("");a.residualValue=ko.observable(0).extend({validateFloat:{}});a.monthlyPayment=ko.observable(0).extend({validateFloat:{}});a.interestRate=ko.observable(0).extend({onlyValidateInterestRate:{}});a.maxMonthsCountOptions=ko.observableArray([0]);
a.instalmentCountOptions=ko.observableArray([0]);a.RedemptionDateOptions=ko.observableArray([]);a.months=ko.observable(0);a.maxMonths=ko.observable();a.calculationType=ko.observable(Settings.defaultCalculationType()==CalculationTypes.Select?CalculationTypes.Leasing:Settings.defaultCalculationType());a.RedemptionValue=ko.observable(0);a.slidersVisible=ko.observable(Settings.useSliders());a.RedemptionDateId=ko.observable(0);a.calculationTypes=ko.observableArray([{id:CalculationTypes.Leasing,label:function(){return Culture.translations().stringTextLeasing}},
{id:CalculationTypes.Credit,label:function(){return Culture.translations().stringTextCredit}}]);Log("calcTypes");Log(a.calculationTypes());a.constraints={};a.constraints.duration={min:ko.observable(0),max:ko.observable(0)};a.constraints.durationStep=ko.observable();a.constraints.months={min:ko.observable(0),max:ko.observable(0)};a.constraints.monthsStep=ko.observable();a.constraints.maxMonths={min:ko.observable(0),max:ko.observable(0)};a.constraints.interestRateStep=ko.observable(0.001);var h=Settings.calculationSettings().ContractTypes[a.calculationType()];
a.constraints.residualValue={min:ko.observable(h.ResidualValue.Min),max:ko.observable(h.ResidualValue.Max)};a.setValues=function(b){a.enableCalculate=!1;a.constraints.duration.min(1);a.constraints.durationStep(1);a.constraints.maxMonths.min(1);a.constraints.maxMonths.max(83);a.maxMonthsCountOptions(f(83),!1);if(a.isContract()){a.Id(b.Id);a.ObjectDescription(b.ObjectDescription);a.contractDate(moment(b.End,Constants.FullDataFormat).format(Constants.LongDataFormat));var d=moment().format(Constants.LongDataFormat);
a.RedemptionDate(d);a.RedemptionDate.valueHasMutated();a.maxMonths(g(a.contractDate()));a.constraints.duration.max(a.maxMonths());a.instalmentCountOptions(f(a.maxMonths(),!0));a.RedemptionDateOptions(i(a.maxMonths(),a.RedemptionDate(),a.contractDate()));a.months(g(a.contractDate(),a.RedemptionDate()));a.residualValue(b.LastPayment);a.monthlyPayment(b.MonthlyPayment);a.interestRate(b.InterestRate);a.calculationType(b.ContractType.toUpperCase())}else a.RedemptionDate(moment().format(Constants.LongDataFormat)),
a.maxMonths(24),a.contractDate(moment().add(Constants.Months,a.maxMonths()).format(Constants.LongDataFormat)),a.instalmentCountOptions(f(a.maxMonths(),!0)),a.RedemptionDateOptions(i(a.maxMonths(),a.RedemptionDate(),a.contractDate())),a.constraints.duration.max(a.maxMonths()),a.months(24),a.residualValue(2E4),a.monthlyPayment(600),a.interestRate(Settings.defaultInterestRate());a.initializing=!1;a.calculatingContraint=!1};a.calculate=function(){Log(a);a.initializing||(a.calculationTimeout&&clearTimeout(a.calculationTimeout),
a.calculationTimeout=setTimeout(function(){Log("Calculating");a.residualValue.isValid()&&(a.monthlyPayment.isValid()&&a.interestRate.isValid())&&a.RedemptionValue(CalculateAmount(a.months(),12,a.interestRate(),a.residualValue(),a.monthlyPayment(),a.getMode(),0))},500))};a.RedemptionDate.subscribe(function(){""!=a.contractDate()&&(""!=a.RedemptionDate()&&!a.initializing)&&(a.calculatingContraint=!0,a.months(g(a.contractDate(),a.RedemptionDate())),a.calculatingContraint=!1)});a.residualValue.subscribe(a.calculate);
a.monthlyPayment.subscribe(a.calculate);a.interestRate.subscribe(a.calculate);a.months.subscribe(a.calculate);a.calculationType.subscribe(a.calculate);a.months.subscribe(function(){if(!a.calculatingContraint&&!a.initializing){a.calculatingContraint=!0;var b=moment(a.contractDate(),Constants.LongDataFormat);a.RedemptionDate(b.add(Constants.Months,-1*a.months()).format(Constants.LongDataFormat));a.calculatingContraint=!1}});a.maxMonths.subscribe(function(){!a.isContract()&&!a.initializing&&(a.calculatingContraint=
!0,a.instalmentCountOptions(f(a.maxMonths())),a.contractDate(moment().add(Constants.Months,a.maxMonths()).format(Constants.LongDataFormat)),a.constraints.duration.max(a.maxMonths()),a.months()>a.maxMonths()&&a.months(a.maxMonths()),a.calculatingContraint=!1)});a.lastPaymentLabelPopUp=ko.computed(function(){switch(a.calculationType()){default:case CalculationTypes.Leasing:return Culture.translations().stringTextResidualValuePopUp;case CalculationTypes.Credit:return Culture.translations().stringTextLastPaymentPopUp}});
var g=function(a,d){var e=void 0==d?moment():moment(d,Constants.LongDataFormat);return moment(a,Constants.LongDataFormat).add(Constants.Months,-1).diff(e,Constants.Months)},f=function(a,d){var e=Array(a);if(d)for(var c=0;c<a;c++)e[a-c-1]=c+1;else for(c=0;c<a;c++)e[c]=c+1;return e},i=function(a,d,e){var d=Array(a),c=moment(e,Constants.LongDataFormat);c.add(Constants.Months,-1);Log("test");Log(e);Log(c);for(a-=1;0<=a;a--,c.add(Constants.Months,-1))d[a]={id:c.format(Constants.LongDataFormat),label:c.format(msf.LC._my).toString()};
Log(Culture);return d};a.getMode=function(){return a.calculationType()==CalculationTypes.Leasing?1:0};a.sendEmail=ko.computed(function(){if(""!=a.RedemptionDate()&&""!=a.contractDate()){var b;b=""+String.format("{0}:  {1}\n",Culture.translations().stringTextNumber,a.Id());b+=String.format("{0}:  {1}\n",Culture.translations().stringTextDescription,a.ObjectDescription());b+=String.format("{0}:  {1}\n",Culture.translations().stringTextCalculationType,CalculationTypes.getText(a.calculationType()));b+=
String.format("{0}:  {1}\n",Culture.translations().StringTextRedemptionDate,String.format("{0:d}",moment(a.RedemptionDate(),Constants.LongDataFormat).toDate()));b+=String.format("{0}:  {1}\n",Culture.translations().StringTextContractEnd,String.format("{0:d}",moment(a.contractDate(),Constants.LongDataFormat).toDate()));b+=String.format("{0}:  {1}\n",Culture.translations().stringTextRemainingDuration,a.months());b+=String.format("{0}:  {1}\n",a.lastPaymentLabelPopUp(),a.residualValue().formatCurrency());
b+=String.format("{0}:  {1}\n",Culture.translations().stringTextMonthlyPayment,a.monthlyPayment().formatCurrency());a.isContract()||(b+=String.format("{0}:  {1}\n",Culture.translations().stringTextInterestRate,a.interestRate().formatPercentage()));b+=String.format("{0}:  {1}\n",Culture.translations().stringTextRedemptionValue,a.RedemptionValue().formatCurrency());b=encodeURIComponent(b);return String.format(Constants.EmailFormat,ApplicationSettings.defaultEmail,Culture.translations().stringTextEmailContractSubject+
a.Id(),b)}});a.calculationInvalid=ko.computed(function(){return!(a.residualValue.isValid()&&a.monthlyPayment.isValid()&&a.interestRate.isValid())})};
