var MainViewModel={};MainViewModel.userLoggedIn=ko.observable(!1);MainViewModel.userName=ko.observable("");MainViewModel.password=ko.observable("");MainViewModel.token=ko.observable("");MainViewModel.redirectAfterLoginSuccess=ko.observable("");MainViewModel.splashscreen=!0;
MainViewModel.logInCommand=function(){Settings.logInMode()==LogInModes.Automatic&&(Settings.userName(MainViewModel.userName()),Settings.password(MainViewModel.password()));MainViewModel.logIn(MainViewModel.userName(),MainViewModel.password(),null)};MainViewModel.logInCommandToHome=function(){Settings.logInMode()==LogInModes.Automatic&&(Settings.userName(MainViewModel.userName()),Settings.password(MainViewModel.password()));MainViewModel.logIn(MainViewModel.userName(),MainViewModel.password(),function(){$.mobile.changePage("Home.html")})};
MainViewModel.editCalculationCommand=function(){PageParameters.calculationId=0;window.CurrentlyEditedCalculation.reset();Settings.defaultTaxMode()!=TaxModes.Select&&Settings.defaultObjectType()!=ObjectTypes.Select&&Settings.defaultCalculationType()!=CalculationTypes.Select?$.mobile.changePage("EditCalculation2.html"):$.mobile.changePage("EditCalculation.html")};MainViewModel.clearRedirect=function(){MainViewModel.redirectAfterLoginSuccess("");return!0};
MainViewModel.handleLostSession=function(c,e){Settings.logInMode()==LogInModes.Automatic?(MainViewModel.userLoggedIn(!1),MainViewModel.redirectAfterLoginSuccess(c),MainViewModel.logIn(MainViewModel.userName(),MainViewModel.password(),e)):(MainViewModel.logOutCommand(),LostSessionDialog.show(!0,c))};
MainViewModel.checkInternetConnection=function(){if(void 0!=navigator.connection)if(Log("Sprawdzanie internetu"),navigator.connection.type==Connection.NONE)Log("Brak internetu"),Overlay.create(!1,""),Overlay.showTextOnlyLoader(Culture.translations().stringTextInternetConnectionProblem),setTimeout(function(){Overlay.destroy();return!1},2E3);else return!0;else{if(window.navigator.onLine)return!0;Log("Brak internetu");Overlay.create(!1,"");Overlay.showTextOnlyLoader(Culture.translations().stringTextInternetConnectionProblem);
setTimeout(function(){Overlay.destroy();return!1},2E3)}};MainViewModel.checkDevice=function(c){if(1==$("#FirefoxOSHomePage").length)alert(Culture.translations().stringMsgTheFunctionWillBeAddedSoon+".");else{if(void 0!=navigator.notification||c)return!0;alert(Culture.translations().stringMsgFunctionIsAvailableOnlyOnMobileDevices+".");return!1}};
MainViewModel.logIn=function(c,e,d){function g(a){for(var f={},b=0;b<a.ContractTypes.length;b++)f[a.ContractTypes[b].Code]=a.ContractTypes[b];a.ContractTypes=f;f={};for(b=0;b<a.ObjectTypes.length;b++)f[a.ObjectTypes[b].Code]=a.ObjectTypes[b];a.ObjectTypes=f;return a}!MainViewModel.userLoggedIn()&&MainViewModel.checkInternetConnection()&&(Overlay.create(!0,Culture.translations().stringMsgLoging),ServiceDataProvider.logIn(c,e).done(function(a){MainViewModel.token(a)}).always(function(){this.state()!=
ASyncState.Resolved&&(Overlay.hideLoader(),Overlay.showTextOnlyLoader(Culture.translations().stringErrorMsgLoggingInFailed))}).done(function(){Overlay.changeLoader(Culture.translations().stringMsgUpdatingSettings);MainViewModel.userLoggedIn(!0);ServiceDataProvider.downloadCalculationSettings().done(function(a){Settings.calculationSettings(g(a));Overlay.changeLoader(Culture.translations().stringMsgUpdatingCars);ServiceDataProvider.downloadCars().done(function(a){Log(a);CarsViewModel.data=a;DataProvider.saveCars(a);
Overlay.changeLoader(Culture.translations().stringMsgUpdatingResidualValueCharts);ServiceDataProvider.downloadResidualValueCharts().done(function(a){Overlay.hideLoader();Overlay.showTextOnlyLoader(Culture.translations().stringMsgUpdateSuccessful);Log(a);Settings.residualValueCharts(a);DataProvider.saveResidualValueCharts(a);void 0!=d&&null!=d&&d()}).always(function(){setTimeout(function(){Overlay.destroy();setTimeout(function(){0<MainViewModel.redirectAfterLoginSuccess().length&&($.mobile.changePage(MainViewModel.redirectAfterLoginSuccess()),
MainViewModel.redirectAfterLoginSuccess(""))},1)},2E3)})})})}).fail(function(){setTimeout(function(){Overlay.destroy()},2E3)}))};MainViewModel.logOutCommand=function(){this.userLoggedIn(!1);Settings.logInMode(LogInModes.Manual);MainViewModel.userName("");MainViewModel.password("");MainViewModel.token("")};
MainViewModel.isAvailable=function(c,e){if(MainViewModel.userLoggedIn())c(e);else{var d=Culture.translations().stringTextPleaseLogIn,g=Culture.translations().stringMsgOptionAfterLogIn;navigator.notification?navigator.notification.alert(d,void 0,g,Culture.translations().stringTextOk):alert(d)}};
