(function() { $.ampliFund.vm = $.ampliFund.vm || {}; $.ampliFund.vm.RegisterViewModel = function(data) { var self = this; self.returnUrl = data.returnUrl; self.user = new $.ampliFund.vm.PublicUserViewModel(data.user); self.organization = new $.ampliFund.vm.PublicOrganizationViewModel(data.organization); self.user.password.extend({ required: true }); self.user.confirmPassword.extend({ required: true }); self.sameInfo = ko.observable(false); self.uniqueId = ko.observable(data.organization?.uniqueId); self.showExistingOrganization = ko.computed(function() { return !!self.user.uniqueId || !!self.uniqueId(); }); self.sameInfo.subscribe(function(newValue) { if (newValue && !self.showExistingOrganization()) { self.organization.emailAddress(self.user.emailAddress()); self.organization.addressLine1(self.user.addressLine1()); self.organization.addressLine2(self.user.addressLine2()); self.organization.city(self.user.city()); self.organization.state(self.user.state()); self.organization.postalCode(self.user.postalCode()); self.organization.phoneNumber(self.user.phoneNumber()); } }); self.errors = ko.validation.group(self); self.allErrors = ko.computed(function() { var errors = [] self.errors().forEach(function(error) { errors.push(error); }); self.user.errors().forEach(function(error) { errors.push(error); }); self.organization.errors().forEach(function(error) { errors.push(error); }); return errors; }); self.register = function() { if (self.allErrors().length > 0) { self.errors.showAllMessages(); self.user.errors.showAllMessages(); self.organization.errors.showAllMessages(); return; } var register = new $.ampliFund.deferredButton('register').spin(); $.ajax({ url: '/Public/PublicUser/Register', type: 'POST', dataType: 'JSON', data: ko.toJSON(self) }).done(function(result) { show_pnotify('success', 'Success', 'Account information saved successfully'); if (result.navigateToReturnUrl) { location.href = self.returnUrl; } else { location.href = '/'; } }).fail(function() { show_pnotify('error', 'Error', 'An error occurred while attempting to save your account information'); register.reject(); }); }; }; })();