text changes to registration mail content
[namibia] / public / js / app / portal-workspace / changepinview.js
1 ;(function(){
2
3         window._w.changepinview = {
4
5                 itemId : null,
6                 itemData : null,
7                 formMeta : null,
8                 ti : null,
9                 limited : false,
10                 newItem : false,
11                 ownAccount : false,
12
13                 onViewTemplateReady : function( template, event, eventData )
14                 {
15                         _w.changepinview.ti = _t[template];
16                         _w.changepinview.itemData = null;
17                         _w.changepinview.limited = ('limiteduserview' == template)
18                                 ? true
19                                 : false;
20
21                         App.DataStore.loadSelectListData('permissions', true, 'User', 'Permissions.SelectList');
22                         App.DataStore.loadSelectListData('managers', true, 'User', 'Profile.ManagerList');
23
24
25
26                         _w.changepinview.setChangePinViewFormMeta();
27                         _w.changepinview.ti.hydrate(_w.changepinview.formMeta);
28
29                         // Retrieve initial view data.
30                         _w.changepinview.itemId = App.Util.getUrlParam('id');
31                         if (0 == _w.userview.itemId || null == _w.userview.itemId)
32                         {
33                                 _w.userview.newItem = true;
34                                 _w.userview.ownAccount = false;
35                                 _w.userview.itemData = {};
36                                 App.API.getTask(
37                                         'createUser', 'User', 'Profile.Create',
38                                         null, {}, function(){}, _w.taskContractError
39                                 );
40
41                         }
42                         else
43                         {
44                                 _w.userview.ownAccount = (App.userData.id == _w.userview.itemId)
45                                         ? true
46                                         : false;
47                                 _w.userview.newItem = false;
48                                 App.API.getTask(
49                                         'updateUser:' + _w.userview.itemId, 'User', 'Profile.Update', _w.userview.itemId,
50                                         {'id':_w.userview.itemId}, _w.userview._onUserViewDataReceived, _w.taskContractError
51                                 );
52                         }
53
54                 },
55
56                 onViewPublished : function( template, event, eventData )
57                 {
58                         $('#btnBack').click(function() {
59                                 window.location.hash = '/userview?id='+_w.changepinview.itemId;
60                         });
61
62
63                         $('#btnChangePin').click(function() {
64
65                                 $("#error").html();
66
67                                 if('' == $("#currentPin").val())
68                                 {
69                                         $("#currentPin").addClass("error");
70                                         $("#error").html("<font color='red'>Current PIN field is required</font>");
71                                         return;
72                                 }
73                                 else
74                                 {
75                                         if(!$.isNumeric($('#currentPin').val()))
76                                         {
77                                                 $("#currentPin").addClass("error");
78                                                 $("#error").html("<font color='red'>Current PIN field must be numeric</font>");
79                                                 return;
80                                         }
81
82                                         $("#currentPin").removeClass("error");
83                                         $("#currentPin").addClass("valid");
84                                 }
85
86                                 if('' == $("#newPin").val())
87                                 {
88                                         $("#newPin").addClass("error");
89                                         $("#error").html("<font color='red'>New PIN field is required</font>");
90                                         return;
91                                 }
92                                 else
93                                 {
94                                         if(!$.isNumeric($('#newPin').val()))
95                                         {
96                                                 $("#newPin").addClass("error");
97                                                 $("#error").html("<font color='red'>New PIN field must be numeric</font>");
98                                                 return;
99                                         }
100
101                                         //console.log($('#newPin').val().replace(/ /g,'').length);
102
103                                         if($('#newPin').val().replace(/ /g,'').length != 4)
104                                         {
105                                                 $("#newPin").addClass("error");
106                                                 $("#error").html("<font color='red'>New PIN must be a 4-digit pin</font>");
107                                                 return;
108                                         }
109
110                                         $("#newPin").removeClass("error");
111                                         $("#newPin").addClass("valid");
112                                 }
113
114                                 if('' == $("#pinVerify").val())
115                                 {
116                                         $("#pinVerify").addClass("error");
117                                         $("#error").html("<font color='red'>PIN confirmation field is required</font>");
118                                         return;
119                                 }
120
121                                 if($("#newPin").val() != $("#pinVerify").val())
122                                 {
123                                         $("#pinVerify").addClass("error");
124                                         $("#error").html("<font color='red'>PIN and confirmation pin do not match</font>");
125                                         return;
126                                 }
127
128                             App.API.getTask(
129                               'PinCange:' + App.userData.id, 'User', 'Profile.ChangePassword', App.userData.id,
130                               {}, function () {
131                                   App.API.execTask(
132                                                   'PinCange:' + App.userData.id, {
133                                                           Profile : {
134                                                                   oldPassword : $("#currentPin").val(),
135                                                                   newPassword : $("#newPin").val(),
136                                                           }
137                                                   },
138                                                   {},
139                                                   function() {
140                                                           alert('Pin changed successfully.');
141                                                           if (App.isNewAuctionUser)
142                                                           {
143                                                                   App.isNewAuctionUser = false;
144                                                                   window.location.hash = '/auction';
145                                                           }
146                                                           else
147                                                           {
148                                                                   window.location.hash = '/userview?id='+_w.changepinview.itemId;
149                                                           }
150                                                   },function( response ) {
151
152                                                           if('Current password does not match.'==response.StatusReason)
153                                                           {
154                                                                         $("#currentPin").removeClass("valid");
155                                                                         $("#currentPin").addClass("error");
156                                                                         $("#error").html("<font color='red'>"+response.StatusReason+"</font>");
157                                                                         return;
158                                                           }
159
160                                                  }
161                                   );
162                                 }, _w.taskContractError
163                             );
164                           });
165                 },
166
167
168                 setChangePinViewFormMeta : function( data )
169                 {
170                         if (!data)
171                         {
172                                 data = {};
173                         }
174                         _w.changepinview.formMeta = {
175                                 'currentPin' : {
176                                         type  : 'input',
177                                         id    : 'currentPin',
178                                         title : 'Current PIN',
179                                         value : '',
180                                         maxlength : 6
181                                 },
182                                 'newPin' : {
183                                         type  : 'input',
184                                         id    : 'newPin',
185                                         title : 'New PIN',
186                                         value : '',
187                                         maxlength : 6
188                                 },
189                                 'pinVerify' : {
190                                         type  : 'input',
191                                         id    : 'pinVerify',
192                                         title : 'Confirm new PIN',
193                                         value : ''
194                                 }
195                         };
196                 },
197
198
199
200                 _onDataAndTemplate : function() {
201
202                 },
203
204                 _onChangePinDataReceived : function( response )
205                 {
206                         _w.changepinview.itemId = response.Data.id;
207                         _w.changepinview.itemData = response.Data;
208                         _w.changepinview.setUserViewFormMeta(response.Data);
209                         _w.changepinview.ti.hydrate(_w.userview.formMeta);
210                         if (_w.changepinview.ti.published)
211                         {
212                                 _w.changepinview._onDataAndTemplate();
213                         }
214                 },
215
216                 _prepUserUpdateData : function()
217                 {
218                         var data = {Profile:_w.userview.ti.harvest()};
219                         if ('' == data.Profile.manager)
220                         {
221                                 data.Profile.manager = null;
222                         }
223                         return data;
224                 },
225
226                 _onUserUpdated : function( response )
227                 {
228                         if (_w.userview.ownAccount)
229                         {
230                                 App.API.getTask(
231                                                 'updateOffer:' + _w.offerview.offerId, 'User', 'Profile.ActiveAccount', null,
232                                                 {}, function (response) {
233                                                         App.permissions = response.Data.permissions;
234                                                         delete response.Data.permissions;
235                                                         App.companyData = response.Data.company;
236                                                         delete response.Data.company;
237                                                         App.userData = response.Data;
238                                                         window.location.hash = '/settings';
239                                                 }, _w.onActiveAccountFailure
240                                         );
241                         }
242                         else
243                         {
244                                 window.location.hash = '/settings';
245                         }
246                 }
247         };
248
249 })();