(function() { $.ampliFund.vm = $.ampliFund.vm || {}; $.ampliFund.vm.PublicContactViewModel = function(data) { var self = this; data = data || {}; self.emailAddressId = data.emailAddressId; self.emailAddress = ko.observable(data.emailAddress).extend({ required: true, email: true }); self.addressId = data.addressId; self.addressLine1 = ko.observable(data.addressLine1); self.addressLine2 = ko.observable(data.addressLine2); self.city = ko.observable(data.city); self.state = ko.observable(data.state); self.postalCode = ko.observable(data.postalCode); self.phoneNumberId = data.phoneNumberId; self.phoneNumber = ko.observable(data.phoneNumber); }; $.ampliFund.vm.PublicOrganizationViewModel = function(data) { var self = this; data = data || {}; // base constructor call $.ampliFund.vm.PublicContactViewModel.call(self, data); self.organizationId = data.organizationId; self.name = ko.observable(data.name); self.name.extend({ required: true }); self.uniqueId = ko.observable(data.uniqueId); self.website = ko.observable(data.website); self._organizationType = ko.observable(data.organizationType); self.organizationType = ko.computed({ read: function() { return Number(self._organizationType()); }, write: function(newValue) { self._organizationType(newValue); } }); self.organizationTypeName = ko.computed(function() { switch (self.organizationType()) { case 6: return 'Corporation'; case 5: return 'Foundation'; case 4: return 'Local Government'; case 3: return 'State Government'; case 2: return 'Federal Government'; case 1: return 'Vendor'; default: return 'Other'; } }); self.addressLine1.extend({ required: true }); self.city.extend({ required: true }); self.state.extend({ required: true }); self.postalCode.extend({ required: true }); self.errors = ko.validation.group(self); self.save = function() { if (self.errors().length > 0) { self.errors.showAllMessages(); return; } var saveAndReturn = new $.ampliFund.deferredButton('save').spin(); $.ajax({ url: '/Public/PublicUser/EditAccountInformation', type: 'POST', dataType: 'JSON', data: ko.toJSON(self) }).done(function() { show_pnotify('success', 'Success', 'Account information saved successfully'); location.href = '/Public/PublicUser/AccountInformation' }).fail(function() { show_pnotify('error', 'Error', 'An error occurred while attempting to save your account information'); saveAndReturn.reject(); }); }; }; $.ampliFund.vm.PublicUserViewModel = function(source) { var self = this; source = source || {}; // base constructor call $.ampliFund.vm.PublicContactViewModel.call(self, source); self.id = source.id; self.uniqueId = source.uniqueId; self.role = ko.observable(source.role); self.roleName = ko.computed(function() { switch (self.role().toString()) { case "2": return "Administrator"; default: return "Editor"; } }); self.showChangeUserRole = source.showChangeUserRole; self.validUsername = ko.observable(true); self.checkingUsername = ko.observable(false); self.showCheck = ko.observable(false); self.invalidUsername = ko.computed(function () { return !!self.emailAddress() && !self.validUsername(); }); self.exists = function () { self.showCheck(false); if (!!self.emailAddress()) { self.checkingUsername(true); $.ajax({ url: "/Public/PublicUser/Exists", data: { username: self.emailAddress() }, type: "GET", contentType: "application/json" }).done(function (data) { self.validUsername(!data.exists); self.showCheck(!data.exists); }).complete(function () { self.checkingUsername(false); }); } }; // needed on keyup to hide the checkmark if field is marked invalid while they're typing self.clearCheck = function(model, e) { if (self.emailAddress.error()) { self.showCheck(false); } }; self.validateEmail = function () { if (self.emailAddress()) { if (!self.emailAddress.error()) { self.exists(); } else { self.showCheck(false); self.checkingUsername(false); } } }; self.isValid = function () { if (self.validUsername() && !self.checkingUsername()) { return true; } $("[data-role='spin']").each(function() { endPulse(this) }); return false; }; self.password = ko.observable(source.password); self.confirmPassword = ko.observable(source.confirmPassword); self.confirmPassword.extend({ equal: { params: self.password, message: 'Passwords do not match' } }); self.firstName = ko.observable(source.firstName); self.firstName.extend({ required: true }); self.middleName = ko.observable(source.middleName); self.lastName = ko.observable(source.lastName); self.lastName.extend({ required: true }); self.suffix = ko.observable(source.suffix); self.title = ko.observable(source.title); self.errors = ko.validation.group(self); self.save = function() { if (self.errors().length > 0) { self.errors.showAllMessages(); return; } var saveAndReturn = new $.ampliFund.deferredButton('save').spin(); $.ajax({ url: '/Public/PublicUser/Edit', type: 'POST', dataType: 'JSON', data: ko.toJSON(self) }).done(function() { show_pnotify('success', 'Success', 'Account information saved successfully'); location.href = '/Public/PublicUser/' }).fail(function() { show_pnotify('error', 'Error', 'An error occurred while attempting to save your user information'); saveAndReturn.reject(); }); }; self.invite = function() { if (self.errors().length > 0) { self.errors.showAllMessages(); return; } var invite = new $.ampliFund.deferredButton('invite').spin(); $.ajax({ url: '/Public/PublicUser/Create', type: 'POST', dataType: 'JSON', data: ko.toJSON(self) }).done(function() { show_pnotify('success', 'Success', 'Account information saved successfully'); location.href = '/Public/PublicUser/' }).fail(function() { show_pnotify('error', 'Error', 'An error occurred while attempting to invite the new user'); invite.reject(); }); }; }; $.ampliFund.vm.PublicOrganizationViewModel.prototype = Object.create($.ampliFund.vm.PublicContactViewModel.prototype); $.ampliFund.vm.PublicOrganizationViewModel.prototype.constructor = $.ampliFund.vm.PublicOrganizationViewModel; $.ampliFund.vm.PublicUserViewModel.prototype = Object.create($.ampliFund.vm.PublicContactViewModel.prototype); $.ampliFund.vm.PublicUserViewModel.prototype.constructor = $.ampliFund.vm.PublicUserViewModel; })();