0088d3016d6131743cb188fe86e209be51417813
[namibia] / public / js / app / workspace.js
1 ;(function(){
2
3         //-- Ensure that we have a usable console object so that we don't crash finicky browsers.
4         if (!window.console)
5         {
6                 var names = [
7                             "log", "debug", "info", "warn", "error",
8                             "assert", "dir", "dirxml", "group", "groupEnd", "time",
9                             "timeEnd", "count", "trace", "profile", "profileEnd"
10                     ],
11                     i,
12                     l = names.length,
13                     noOp = function () {};
14                 window.console = {};
15                 for (i = 0; i < l; i = i + 1)
16                 {
17                         window.console[names[i]] = noOp;
18                 }
19         }
20
21         window._w = {
22
23                 itemId    : null,
24                 itemData  : null,
25                 formMeta  : null,
26                 apeLoaded : false,
27                 apeLive   : false,
28                 checkBoxChanged : false,
29
30
31                 onSwitchTemplatePublished : function()
32                 {
33                         $('.navbar li').hide();
34                         $('#su_container').hide();
35                         $('#btnPortal').click(function() {
36                                 _w.initPortal();
37                                 if (App.companyData.group == '' || App.companyData.group == null)
38                                 {
39                                         window.location.hash = '/valuations';
40                                 }
41                                 window.location.hash = '/' + App.companyData.group.landingPage;
42                         });
43                         $('#btnAdmin').click(function() {
44                                 _w.initAdmin();
45                                 window.location.hash = '/admin';
46                         });
47                 },
48
49                 onAdminTemplatePublished : function()
50                 {
51                         // nothing to do here yet, empty landing page for now.
52                 },
53
54                 initBrochure : function()
55                 {
56                         // show brochure menu
57
58                         /*$('#MobiPublicMenu').hide();
59                         $('#MobiBrochureMenu').show();
60                         $('#MobiPortalMenu').remove();*/
61
62                         App.Event.listen(
63                                 'onLoginPageTemplatePublished', 'Controller.Published:login',
64                                 $.proxy( _w.onLoginPageTemplatePublished, this ), 'Recurring'
65                                 );
66                         App.Event.listen(
67                                 'onForgotPageTemplatePublished', 'Controller.Published:forgot',
68                                 $.proxy( _w.onForgotPageTemplatePublished, this ), 'Recurring'
69                                 );
70                         App.Event.listen(
71                                 'onTemplateReady', 'Template.Ready:register',
72                                 $.proxy( _w.register.onTemplateReady, this, 'register' ), 'Recurring'
73                                 );
74                         App.Event.listen(
75                                 'onTemplatePublished', 'Controller.Published:register',
76                                 $.proxy( _w.register.onTemplatePublished, this, 'register' ), 'Recurring'
77                                 );
78
79                         App.Event.listen(
80                                 'onTemplateReady', 'Template.Ready:dealerhome',
81                                 $.proxy( _w.home.onTemplateReady, this, 'dealerhome' ), 'Recurring'
82                                 );
83                         App.Event.listen(
84                                 'onTemplatePublished', 'Controller.Published:dealerhome',
85                                 $.proxy( _w.home.onTemplatePublished, this, 'dealerhome' ), 'Recurring'
86                                 );
87
88                         // Public Valuation
89                         App.Event.listen(
90                                 'onValuationViewTemplateReadyMain', 'Template.Ready:publicvaluation',
91                                 $.proxy( _w.publicvaluation.onValuationViewTemplateReady, this, 'publicvaluation' ), 'Recurring'
92                                 );
93                     App.Event.listen(
94                                 'onValuationViewPublishedMain', 'Controller.Published:publicvaluation',
95                                 $.proxy( _w.publicvaluation.onValuationViewPublished, this, 'publicvaluation' ), 'Recurring'
96                                 );
97
98             App.Event.listen(
99                 'onCustomerValuationViewTemplateReadyMain', 'Template.Ready:customervaluation',
100                 $.proxy( _w.customervaluation.onCustomerValuationViewTemplateReady, this, 'customervaluation' ), 'Recurring'
101             );
102             App.Event.listen(
103                 'onCustomerValuationViewPublishedMain', 'Controller.Published:customervaluation',
104                 $.proxy( _w.customervaluation.onCustomerValuationViewPublished, this, 'customervaluation' ), 'Recurring'
105             );
106
107             App.Event.listen(
108                 'onCustomerCompleteValuationViewTemplateReadyMain', 'Template.Ready:customercompletevaluation',
109                 $.proxy( _w.customercompletevaluation.onCustomerCompleteValuationViewTemplateReady, this, 'customercompletevaluation' ), 'Recurring'
110             );
111             App.Event.listen(
112                 'onCustomerCompleteValuationViewPublishedMain', 'Controller.Published:customercompletevaluation',
113                 $.proxy( _w.customercompletevaluation.onCustomerCompleteValuationViewPublished, this, 'customercompletevaluation' ), 'Recurring'
114             );
115
116                 },
117
118                 initAdmin : function()
119                 {
120
121                         $('#PublicMenu').remove();
122                         $('#MobiPublicMenu').remove();
123                         $('#BrochureMenu').remove();
124                         $('#MobiBrochureMenu').remove();
125                         $('#PortalMenu').show();
126
127                         /*
128                          * Setup permissions
129                          */
130                         App.allowedPages = {};
131                         true == App.permissions.adminPage
132                                 ? $('.b_permissions').show() && (App.allowedPages.permissions = true)
133                                 : $('.b_permissions').hide() && (App.allowedPages.permissions = false);
134
135                         true == App.permissions.adminRegistration
136                                 ? $('.b_registration').show() && (App.allowedPages.registration = true)
137                                 : $('.b_registration').hide() && (App.allowedPages.registration = false);
138
139                         true == App.permissions.adminReport
140                                 ? $('.b_reports').show() && (App.allowedPages.reports = true)
141                                 : $('.b_reports').hide() && (App.allowedPages.reports = false);
142
143                         true == App.permissions.adminDealer
144                                 ? $('.b_dealers').show() && (App.allowedPages.dealers = true)
145                                 : $('.b_dealers').hide() && (App.allowedPages.dealers = false);
146                         true == App.permissions.adminDealer
147                                 ? $('.b_groups').show() && (App.allowedPages.dealers = true)
148                                 : $('.b_groups').hide() && (App.allowedPages.dealers = false);
149                         true == App.permissions.adminDealer
150                                 ? $('.b_divisions').show() && (App.allowedPages.dealers = true)
151                                 : $('.b_divisions').hide() && (App.allowedPages.dealers = false);
152
153                         true == App.permissions.adminCommunication
154                                 ? $('.b_communication').show() && (App.allowedPages.communication = true)
155                                 : $('.b_communication').hide() && (App.allowedPages.communication = false);
156
157                         true == App.permissions.adminSettings
158                                 ? $('.b_systemsettings').show() && (App.allowedPages.systemsettings = true)
159                                 : $('.b_systemsettings').hide() && (App.allowedPages.systemsettings = false);
160
161                         true == App.permissions.adminUser
162                                 ? $('.b_systemusers').show() && (App.allowedPages.systemusers = true)
163                                 : $('.b_systemusers').hide() && (App.allowedPages.systemusers = false);
164
165                         true == App.permissions.adminAuction
166                                 ? $('.b_auctions').show() && (App.allowedPages.systemauctions = true)
167                                 : $('.b_auctions').hide() && (App.allowedPages.systemauctions = false);
168
169                         true == App.permissions.adminPage
170                                 ? $('.b_newsletters').show() && (App.allowedPages.newsletters = true)
171                                 : $('.b_newsletters').hide() && (App.allowedPages.newsletters = false);
172
173
174                         /*
175                          * Setup page listeners.
176                          */
177                         App.Event.listen(
178                                 'onAdminTemplatePublished', 'Controller.Published:admin',
179                                 $.proxy( _w.onAdminTemplatePublished, this, 'admin' ), 'Recurring'
180                                 );
181                         if (true == App.permissions.adminPage)
182                         {
183                                 // Grid
184                                 App.Event.listen(
185                                         'onTemplateReady', 'Template.Ready:userpermissions',
186                                         $.proxy( _w.userpermissions.onTemplateReady, this, 'userpermissions' ), 'Recurring'
187                                         );
188                                 App.Event.listen(
189                                         'onTemplatePublished', 'Controller.Published:userpermissions',
190                                         $.proxy( _w.userpermissions.onTemplatePublished, this, 'userpermissions' ), 'Recurring'
191                                         );
192
193                                 // Edit
194                                 App.Event.listen(
195                                         'onTemplateReady', 'Template.Ready:viewuserpermissions',
196                                         $.proxy( _w.viewuserpermissions.onTemplateReady, this, 'viewuserpermissions' ), 'Recurring'
197                                         );
198                                 App.Event.listen(
199                                         'onTemplatePublished', 'Controller.Published:viewuserpermissions',
200                                         $.proxy( _w.viewuserpermissions.onTemplatePublished, this, 'viewuserpermissions' ), 'Recurring'
201                                         );
202
203                                 // Grid
204                                 App.Event.listen(
205                                         'onTemplateReady', 'Template.Ready:newsletters',
206                                         $.proxy( _w.newsletters.onTemplateReady, this, 'newsletters' ), 'Recurring'
207                                         );
208                                 App.Event.listen(
209                                         'onTemplatePublished', 'Controller.Published:newsletters',
210                                         $.proxy( _w.newsletters.onTemplatePublished, this, 'newsletters' ), 'Recurring'
211                                         );
212
213                                 // Edit
214                                 App.Event.listen(
215                                         'onTemplateReady', 'Template.Ready:newsletterview',
216                                         $.proxy( _w.newsletterview.onTemplateReady, this, 'newsletterview' ), 'Recurring'
217                                         );
218                                 App.Event.listen(
219                                         'onTemplatePublished', 'Controller.Published:newsletterview',
220                                         $.proxy( _w.newsletterview.onTemplatePublished, this, 'newsletterview' ), 'Recurring'
221                                         );
222                                 App.Event.listen(
223                                         'onTemplateDestruct', 'Controller.RemovingPage:newsletterview',
224                                         $.proxy( _w.newsletterview.onTemplateDestruct, this, 'newsletterview' ), 'Recurring'
225                                         );
226                         }
227                         if (true == App.permissions.adminDealer)
228                         {
229                                 // Grid
230                                 App.Event.listen(
231                                         'onTemplateReady', 'Template.Ready:dealers',
232                                         $.proxy( _w.dealers.onTemplateReady, this, 'dealers' ), 'Recurring'
233                                         );
234                                 App.Event.listen(
235                                         'onTemplatePublished', 'Controller.Published:dealers',
236                                         $.proxy( _w.dealers.onTemplatePublished, this, 'dealers' ), 'Recurring'
237                                         );
238
239                                 // Edit
240                                 App.Event.listen(
241                                         'onTemplateReady', 'Template.Ready:systemdealerview',
242                                         $.proxy( _w.systemdealerview.onTemplateReady, this, 'systemdealerview' ), 'Recurring'
243                                         );
244                                 App.Event.listen(
245                                         'onTemplatePublished', 'Controller.Published:systemdealerview',
246                                         $.proxy( _w.systemdealerview.onTemplatePublished, this, 'systemdealerview' ), 'Recurring'
247                                         );
248                         }
249                         if (true == App.permissions.adminSettings)
250                         {
251                                 // XmlRpc Request Grid
252                                 App.Event.listen(
253                                                 'onTemplateReady', 'Template.Ready:xmlrpcrequestlog',
254                                                 $.proxy( _w.xmlrpcrequestlog.onTemplateReady, this, 'xmlrpcrequestlog' ), 'Recurring'
255                                 );
256                                 App.Event.listen(
257                                                 'onTemplatePublished', 'Controller.Published:xmlrpcrequestlog',
258                                                 $.proxy( _w.xmlrpcrequestlog.onTemplatePublished, this, 'xmlrpcrequestlog' ), 'Recurring'
259                                 );
260                                 // XmlRpc Callback Grid
261                                 App.Event.listen(
262                                                 'onTemplateReady', 'Template.Ready:xmlrpccallbacklog',
263                                                 $.proxy( _w.xmlrpccallbacklog.onTemplateReady, this, 'xmlrpccallbacklog' ), 'Recurring'
264                                 );
265                                 App.Event.listen(
266                                                 'onTemplatePublished', 'Controller.Published:xmlrpccallbacklog',
267                                                 $.proxy( _w.xmlrpccallbacklog.onTemplatePublished, this, 'xmlrpccallbacklog' ), 'Recurring'
268                                 );
269                                 // XmlRpc Client Grid
270                                 App.Event.listen(
271                                                 'onTemplateReady', 'Template.Ready:xmlrpcclients',
272                                                 $.proxy( _w.xmlrpcclients.onTemplateReady, this, 'xmlrpcclients' ), 'Recurring'
273                                 );
274                                 App.Event.listen(
275                                                 'onTemplatePublished', 'Controller.Published:xmlrpcclients',
276                                                 $.proxy( _w.xmlrpcclients.onTemplatePublished, this, 'xmlrpcclients' ), 'Recurring'
277                                 );
278                                 // XmlRpc Client Edit
279                                 App.Event.listen(
280                                                 'onTemplateReady', 'Template.Ready:xmlrpcclientview',
281                                                 $.proxy( _w.systemxmlrpcclientview.onTemplateReady, this, 'xmlrpcclientview' ), 'Recurring'
282                                 );
283                                 App.Event.listen(
284                                                 'onTemplatePublished', 'Controller.Published:xmlrpcclientview',
285                                                 $.proxy( _w.systemxmlrpcclientview.onTemplatePublished, this, 'xmlrpcclientview' ), 'Recurring'
286                                 );
287
288                                 // System Config
289                                 App.Event.listen(
290                                         'onTemplateReady', 'Template.Ready:configview',
291                                         $.proxy( _w.configview.onTemplateReady, this, 'configview' ), 'Recurring'
292                                         );
293                                 App.Event.listen(
294                                         'onTemplatePublished', 'Controller.Published:configview',
295                                         $.proxy( _w.configview.onTemplatePublished, this, 'configview' ), 'Recurring'
296                                         );
297
298                                 // Accessories
299                                 App.Event.listen(
300                                         'onTemplateReady', 'Template.Ready:accessories',
301                                         $.proxy( _w.accessories.onTemplateReady, this, 'accessories' ), 'Recurring'
302                                         );
303                                 App.Event.listen(
304                                         'onTemplatePublished', 'Controller.Published:accessories',
305                                         $.proxy( _w.accessories.onTemplatePublished, this, 'accessories' ), 'Recurring'
306                                         );
307
308                                 // Damages
309                                 App.Event.listen(
310                                         'onTemplateReady', 'Template.Ready:damages',
311                                         $.proxy( _w.damages.onTemplateReady, this, 'damages' ), 'Recurring'
312                                         );
313                                 App.Event.listen(
314                                         'onTemplatePublished', 'Controller.Published:damages',
315                                         $.proxy( _w.damages.onTemplatePublished, this, 'damages' ), 'Recurring'
316                                         );
317
318                                 // Increments
319                                 App.Event.listen(
320                                                 'onTemplateReady', 'Template.Ready:increments',
321                                                 $.proxy( _w.increments.onTemplateReady, this, 'increments' ), 'Recurring'
322                                 );
323                                 App.Event.listen(
324                                                 'onTemplatePublished', 'Controller.Published:increments',
325                                                 $.proxy( _w.increments.onTemplatePublished, this, 'increments' ), 'Recurring'
326                                 );
327
328                                 // Groups
329                                 App.Event.listen(
330                                                 'onTemplateReady', 'Template.Ready:groups',
331                                                 $.proxy( _w.groups.onTemplateReady, this, 'groups' ), 'Recurring'
332                                 );
333                                 App.Event.listen(
334                                                 'onTemplatePublished', 'Controller.Published:groups',
335                                                 $.proxy( _w.groups.onTemplatePublished, this, 'groups' ), 'Recurring'
336                                 );
337
338                                 // Divisions
339                                 App.Event.listen(
340                                                 'onTemplateReady', 'Template.Ready:divisions',
341                                                 $.proxy( _w.divisions.onTemplateReady, this, 'divisions' ), 'Recurring'
342                                 );
343                                 App.Event.listen(
344                                                 'onTemplatePublished', 'Controller.Published:divisions',
345                                                 $.proxy( _w.divisions.onTemplatePublished, this, 'divisions' ), 'Recurring'
346                                 );
347
348                                 // Public Holidays
349                                 App.Event.listen(
350                                                 'onTemplateReady', 'Template.Ready:holidays',
351                                                 $.proxy( _w.holidays.onTemplateReady, this, 'holidays' ), 'Recurring'
352                                 );
353                                 App.Event.listen(
354                                                 'onTemplatePublished', 'Controller.Published:holidays',
355                                                 $.proxy( _w.holidays.onTemplatePublished, this, 'holidays' ), 'Recurring'
356                                 );
357
358                                 // Regional Manager
359                                 App.Event.listen(
360                                                 'onTemplateReady', 'Template.Ready:regionalmanager',
361                                                 $.proxy( _w.regionalmanager.onTemplateReady, this, 'regionalmanager' ), 'Recurring'
362                                 );
363                                 App.Event.listen(
364                                                 'onTemplatePublished', 'Controller.Published:regionalmanager',
365                                                 $.proxy( _w.regionalmanager.onTemplatePublished, this, 'regionalmanager' ), 'Recurring'
366                                 );
367
368                                 // Open Days
369                                 App.Event.listen(
370                                                 'onTemplateReady', 'Template.Ready:opendays',
371                                                 $.proxy( _w.opendays.onTemplateReady, this, 'opendays' ), 'Recurring'
372                                 );
373                                 App.Event.listen(
374                                                 'onTemplatePublished', 'Controller.Published:opendays',
375                                                 $.proxy( _w.opendays.onTemplatePublished, this, 'opendays' ), 'Recurring'
376                                 );
377                         }
378                         if (true == App.permissions.adminUser)
379                         {
380                                 // Grid
381                                 App.Event.listen(
382                                         'onTemplateReady', 'Template.Ready:systemusers',
383                                         $.proxy( _w.systemusers.onTemplateReady, this, 'systemusers' ), 'Recurring'
384                                         );
385                                 App.Event.listen(
386                                         'onTemplatePublished', 'Controller.Published:systemusers',
387                                         $.proxy( _w.systemusers.onTemplatePublished, this, 'systemusers' ), 'Recurring'
388                                         );
389
390                                 // Edit
391                                 App.Event.listen(
392                                         'onTemplateReady', 'Template.Ready:systemuserview',
393                                         $.proxy( _w.systemuserview.onTemplateReady, this, 'systemuserview' ), 'Recurring'
394                                         );
395                                 App.Event.listen(
396                                         'onTemplatePublished', 'Controller.Published:systemuserview',
397                                         $.proxy( _w.systemuserview.onTemplatePublished, this, 'systemuserview' ), 'Recurring'
398                                         );
399                                 App.Event.listen(
400                                         'onTemplatePublished', 'Controller.Published:changepinview',
401                                         $.proxy( _w.systemuserview.onTemplatePublished, this, 'changepinview' ), 'Recurring'
402                                         );
403
404                                 // Permissions
405                                 App.Event.listen(
406                                         'onTemplateReady', 'Template.Ready:override',
407                                         $.proxy( _w.override.onTemplateReady, this, 'override' ), 'Recurring'
408                                         );
409                                 App.Event.listen(
410                                         'onTemplatePublished', 'Controller.Published:override',
411                                         $.proxy( _w.override.onTemplatePublished, this, 'override' ), 'Recurring'
412                                         );
413                         }
414                         if (true == App.permissions.adminAuction)
415                         {
416                                 // Grid
417                                 App.Event.listen(
418                                         'onTemplateReady', 'Template.Ready:systemauctions',
419                                         $.proxy( _w.systemauctions.onTemplateReady, this, 'systemauctions' ), 'Recurring'
420                                         );
421                                 App.Event.listen(
422                                         'onTemplatePublished', 'Controller.Published:systemauctions',
423                                         $.proxy( _w.systemauctions.onTemplatePublished, this, 'systemauctions' ), 'Recurring'
424                                         );
425
426                                 // Edit
427                                 App.Event.listen(
428                                         'onTemplateReady', 'Template.Ready:systembidview',
429                                         $.proxy( _w.systembidview.onTemplateReady, this, 'systembidview' ), 'Recurring'
430                                         );
431                                 App.Event.listen(
432                                         'onTemplatePublished', 'Controller.Published:systembidview',
433                                         $.proxy( _w.systembidview.onTemplatePublished, this, 'systembidview' ), 'Recurring'
434                                         );
435
436                                 // Dispute Grid
437                                 App.Event.listen(
438                                         'onTemplateReady', 'Template.Ready:systemdisputes',
439                                         $.proxy( _w.systemdisputes.onTemplateReady, this, 'systemdisputes' ), 'Recurring'
440                                         );
441                                 App.Event.listen(
442                                         'onTemplatePublished', 'Controller.Published:systemdisputes',
443                                         $.proxy( _w.systemdisputes.onTemplatePublished, this, 'systemdisputes' ), 'Recurring'
444                                         );
445
446                                 // Edit Dispute
447                                 App.Event.listen(
448                                         'onTemplateReady', 'Template.Ready:systemdisputeview',
449                                         $.proxy( _w.systemdisputeview.onTemplateReady, this, 'systemdisputeview' ), 'Recurring'
450                                         );
451                                 App.Event.listen(
452                                         'onTemplatePublished', 'Controller.Published:systemdisputeview',
453                                         $.proxy( _w.systemdisputeview.onTemplatePublished, this, 'systemdisputeview' ), 'Recurring'
454                                         );
455                         }
456                 },
457
458                 initPortal : function()
459                 {
460             App.Template.retrieve( 'page', 'valuationview');
461                         /*
462                          * Setup permissions
463                          */
464                         App.allowedPages = {};
465                         App.allowedPages.contact = true;
466                         true == App.permissions.auctionPage
467                                 ? $('.b_auction').show() && (App.allowedPages.auction = true)
468                                 : $('.b_auction').hide() && (App.allowedPages.auction = false);
469
470                         true == App.permissions.stockPage
471                                 ? $('.b_stock').show() && (App.allowedPages.stock = true)
472                                 : $('.b_stock').hide() && (App.allowedPages.stock = false);
473
474                         App.companyData.dealerType == 'Trade Center'
475                                 ? $('.b_tcstock').show() && (App.allowedPages.tcstock = true)
476                                 : $('.b_tcstock').hide() && (App.allowedPages.tcstock = false);
477
478                         true == App.permissions.valuationPage
479                                 ? $('.b_valuations').show() && (App.allowedPages.valuations = true)
480                                 : $('.b_valuations').hide() && (App.allowedPages.valuations = false);
481         
482                  true == App.permissions.historicalPage
483                                 ? $('.b_historicaldata').show() && (App.allowedPages.historicaldata = true)
484                                 : $('.b_historicaldata').hide() && (App.allowedPages.historicaldata = false);
485
486                         true == App.permissions.settingsPage
487                                 ? $('.b_settings').show() && (App.allowedPages.settings = true)
488                                 : $('.b_settings').hide() && (App.allowedPages.settings = false);
489
490                         true == App.permissions.pgPage
491                                 ? $('.b_priceGuide').show() && (App.allowedPages.priceguide = true)
492                                 : $('.b_priceGuide').hide() && (App.allowedPages.priceguide = false);
493
494             //if(true == App.companyData.publicCentre && true == App.permissions.valuationPage && true == App.permissions.valuationCalendar)
495             if(true == App.permissions.valuationCalendar)
496             {
497                 console.log('show b_valuationcalandar');
498
499                 $('.b_valuationCalendar').show();
500
501                 $('.b_valuationCalendar').removeClass('hide');
502
503                 App.Event.listen(
504                     'onValuationCalendarTemplateReady', 'Template.Ready:valuationcalendar',
505                     $.proxy( _w.valuationcalendar.onValuationCalendarTemplateReady, this, 'valuationcalendar' ), 'Recurring'
506                 );
507                 App.Event.listen(
508                     'onValuationCalendarPublished', 'Controller.Published:valuationcalendar',
509                     $.proxy( _w.valuationcalendar.onValuationCalendarPublished, this, 'valuationcalendar' ), 'Recurring'
510                 );
511
512                 App.Event.listen(
513                     'onValuationAppointmentEditTemplateReady', 'Template.Ready:appointmentedit',
514                     $.proxy( _w.appointmentedit.onValuationAppointmentEditTemplateReady, this, 'appointmentedit' ), 'Recurring'
515                 );
516                 App.Event.listen(
517                     'onValuationAppointmentEditPublished', 'Controller.Published:appointmentedit',
518                     $.proxy( _w.appointmentedit.onValuationAppointmentEditPublished, this, 'appointmentedit' ), 'Recurring'
519                 );
520             }
521
522
523                         $('#PublicMenu').remove();
524                         $('#MobiPublicMenu').remove();
525                         $('#BrochureMenu').remove();
526                         $('#MobiBrochureMenu').remove();
527                         $('#PortalMenu').show();
528                         $('#MobiPortalMenu').show();
529
530                         /*
531                          * Setup page listeners.
532                          */
533       if (true == App.permissions.historicalPage)
534                         {
535         App.Event.listen(
536                                         'onHistoricaldataPageTemplateReady', 'Template.Ready:historicaldata',
537                                         $.proxy( _w.historicaldata.onHistoricaldataPageTemplateReady, this, 'historicaldata' ), 'Recurring'
538                                         );
539         App.Event.listen(
540                                         'onHistoricGridPublished', 'Controller.Published:historicaldata',
541                                         $.proxy( _w.historicaldata.onHistoricGridPublished, this, 'historicaldata' ), 'Recurring'
542                                         );
543                                         // Auction History View
544                 App.Event.listen(
545                                         'onHistoryViewTemplateReady', 'Template.Ready:historyview',
546                                         $.proxy( _w.historyview.onHistoryViewTemplateReady, this, 'historyview' ), 'Recurring'
547                                         );
548                                 App.Event.listen(
549                                         'onHistoryViewPublished', 'Controller.Published:historyview',
550                                         $.proxy( _w.historyview.onHistoryViewPublished, this, 'historyview' ), 'Recurring'
551                                         );
552
553          }
554       
555                         if (true == App.permissions.auctionPage)
556                         {
557                                 // Auction Grid
558                                 App.Event.listen(
559                                         'onAuctionPageTemplateReady', 'Template.Ready:auction',
560                                         $.proxy( _w.auction.onAuctionPageTemplateReady, this, 'auction' ), 'Recurring'
561                                         );
562                                 App.Event.listen(
563                                         'onAuctionGridPublished', 'Controller.Published:auction',
564                                         $.proxy( _w.auction.onAuctionGridPublished, this, 'auction' ), 'Recurring'
565                                         );
566                                 App.Event.listen(
567                                         'onAuctionDestruct', 'Controller.RemovingPage:auction',
568                                         $.proxy( _w.auction.onPageDestruct, this, 'auction' ), 'Recurring'
569                                         );
570
571                                 // Auction Bid View
572                                 App.Event.listen(
573                                         'onBidViewTemplateReady', 'Template.Ready:bidview',
574                                         $.proxy( _w.bidview.onBidViewTemplateReady, this, 'bidview' ), 'Recurring'
575                                         );
576                                 App.Event.listen(
577                                         'onBidViewPublished', 'Controller.Published:bidview',
578                                         $.proxy( _w.bidview.onBidViewPublished, this, 'bidview' ), 'Recurring'
579                                         );
580
581                                 // Auction Question View
582                                 App.Event.listen(
583                                                 'onViewTemplateReady', 'Template.Ready:questionview',
584                                                 $.proxy( _w.questionview.onViewTemplateReady, this, 'questionview' ), 'Recurring'
585                                 );
586                                 App.Event.listen(
587                                                 'onViewPublished', 'Controller.Published:questionview',
588                                                 $.proxy( _w.questionview.onViewPublished, this, 'questionview' ), 'Recurring'
589                                 );
590
591                                 // Auction Reply View
592                                 App.Event.listen(
593                                                 'onViewTemplateReady', 'Template.Ready:replyview',
594                                                 $.proxy( _w.replyview.onViewTemplateReady, this, 'replyview' ), 'Recurring'
595                                 );
596                                 App.Event.listen(
597                                                 'onViewPublished', 'Controller.Published:replyview',
598                                                 $.proxy( _w.replyview.onViewPublished, this, 'replyview' ), 'Recurring'
599                                 );
600
601                                 // Auction Retail View
602                                 App.Event.listen(
603                                                 'onViewTemplateReady', 'Template.Ready:retailview',
604                                                 $.proxy( _w.retailview.onViewTemplateReady, this, 'retailview' ), 'Recurring'
605                                 );
606                                 App.Event.listen(
607                                                 'onViewPublished', 'Controller.Published:retailview',
608                                                 $.proxy( _w.retailview.onViewPublished, this, 'retailview' ), 'Recurring'
609                                 );
610                         }
611                         // they dont have rights on auctionPage so buzz off to settings page
612                         else
613                         {
614                                 // Auction Grid
615                                 App.Event.listen(
616                                         'onAuctionPageTemplateReady', 'Template.Ready:auction',
617                                         function(){ window.location.hash = '/settings'; }, 'Recurring'
618                                         );
619                                 App.Event.listen(
620                                         'onAuctionGridPublished', 'Controller.Published:auction',
621                                         function(){ window.location.hash = '/settings'; }, 'Recurring'
622                                         );
623                                 App.Event.listen(
624                                         'onAuctionDestruct', 'Controller.RemovingPage:auction',
625                                         function(){ window.location.hash = '/settings'; }, 'Recurring'
626                                         );
627
628                                 // Auction Bid View
629                                 App.Event.listen(
630                                         'onBidViewTemplateReady', 'Template.Ready:bidview',
631                                         function(){ window.location.hash = '/settings'; }, 'Recurring'
632                                         );
633                                 App.Event.listen(
634                                         'onBidViewPublished', 'Controller.Published:bidview',
635                                         function(){ window.location.hash = '/settings'; }, 'Recurring'
636                                         );
637
638
639                                 // Auction Question View
640                                 App.Event.listen(
641                                                 'onViewTemplateReady', 'Template.Ready:questionview',
642                                                 function(){ window.location.hash = '/settings'; }, 'Recurring'
643                                 );
644                                 App.Event.listen(
645                                                 'onViewPublished', 'Controller.Published:questionview',
646                                                 function(){ window.location.hash = '/settings'; }, 'Recurring'
647                                 );
648
649                                 // Auction Reply View
650                                 App.Event.listen(
651                                                 'onViewTemplateReady', 'Template.Ready:replyview',
652                                                 function(){ window.location.hash = '/settings'; }, 'Recurring'
653                                 );
654                                 App.Event.listen(
655                                                 'onViewPublished', 'Controller.Published:replyview',
656                                                 function(){ window.location.hash = '/settings'; }, 'Recurring'
657                                 );
658
659                                 // Auction Retail View
660                                 App.Event.listen(
661                                                 'onViewTemplateReady', 'Template.Ready:retailview',
662                                                 function(){ window.location.hash = '/settings'; }, 'Recurring'
663                                 );
664                                 App.Event.listen(
665                                                 'onViewPublished', 'Controller.Published:retailview',
666                                                 function(){ window.location.hash = '/settings'; }, 'Recurring'
667                                 );
668
669                         }
670
671
672                         if (App.companyData.dealerType == 'Trade Center')
673                         {
674                                 // TC Stock Grid
675                                 App.Event.listen(
676                                                 'onStockPageTemplateReady', 'Template.Ready:tcstock',
677                                                 $.proxy( _w.tcstock.onStockPageTemplateReady, this, 'tcstock' ), 'Recurring'
678                                 );
679                                 App.Event.listen(
680                                                 'onStockPublished', 'Controller.Published:tcstock',
681                                                 $.proxy( _w.tcstock.onStockPublished, this, 'tcstock' ), 'Recurring'
682                                 );
683                                 App.Event.listen(
684                                                 'onStockDestruct', 'Controller.RemovingPage:tcstock',
685                                                 $.proxy( _w.tcstock.onPageDestruct, this, 'tcstock' ), 'Recurring'
686                                 );
687
688                                 // TC Stock View
689                                 App.Event.listen(
690                                                 'onStockViewTemplateReady', 'Template.Ready:tcstockview',
691                                                 $.proxy( _w.tcstockview.onStockViewTemplateReady, this, 'tcstockview' ), 'Recurring'
692                                 );
693                                 App.Event.listen(
694                                                 'onStockViewPublished', 'Controller.Published:tcstockview',
695                                                 $.proxy( _w.tcstockview.onStockViewPublished, this, 'tcstockview' ), 'Recurring'
696                                 );
697
698                                 // Limited TC Stock View
699                                 App.Event.listen(
700                                         'onStockViewTemplateReady', 'Template.Ready:limitedtcstockview',
701                                         $.proxy( _w.tcstockview.onStockViewTemplateReady, this, 'limitedtcstockview' ), 'Recurring'
702                                         );
703                             App.Event.listen(
704                                         'onStockViewPublished', 'Controller.Published:limitedtcstockview',
705                                         $.proxy( _w.tcstockview.onStockViewPublished, this, 'limitedtcstockview' ), 'Recurring'
706                                         );
707                         }
708                         if (true == App.permissions.stockPage)
709                         {
710                                 // Stock Grid
711                                 App.Event.listen(
712                                                 'onStockPageTemplateReady', 'Template.Ready:stock',
713                                                 $.proxy( _w.stock.onStockPageTemplateReady, this, 'stock' ), 'Recurring'
714                                 );
715                                 App.Event.listen(
716                                                 'onStockPublished', 'Controller.Published:stock',
717                                                 $.proxy( _w.stock.onStockPublished, this, 'stock' ), 'Recurring'
718                                 );
719                                 App.Event.listen(
720                                                 'onStockDestruct', 'Controller.RemovingPage:stock',
721                                                 $.proxy( _w.stock.onPageDestruct, this, 'stock' ), 'Recurring'
722                                 );
723
724                                 // Stock View
725                                 App.Event.listen(
726                                         'onStockViewTemplateReady', 'Template.Ready:stockview',
727                                         $.proxy( _w.stockview.onStockViewTemplateReady, this, 'stockview' ), 'Recurring'
728                                         );
729                             App.Event.listen(
730                                         'onStockViewPublished', 'Controller.Published:stockview',
731                                         $.proxy( _w.stockview.onStockViewPublished, this, 'stockview' ), 'Recurring'
732                                         );
733
734                                 // Limited Stock View
735                                 App.Event.listen(
736                                         'onStockViewTemplateReady', 'Template.Ready:limitedstockview',
737                                         $.proxy( _w.stockview.onStockViewTemplateReady, this, 'limitedstockview' ), 'Recurring'
738                                         );
739                             App.Event.listen(
740                                         'onStockViewPublished', 'Controller.Published:limitedstockview',
741                                         $.proxy( _w.stockview.onStockViewPublished, this, 'limitedstockview' ), 'Recurring'
742                                         );
743                         }
744
745                         if (true == App.permissions.valuationPage)
746                         {
747                                 // Valuation Grid
748                                 App.Event.listen(
749                                         'onValuationPageTemplateReady', 'Template.Ready:valuations',
750                                         $.proxy( _w.valuation.onValuationPageTemplateReady, this, 'valuations' ), 'Recurring'
751                                         );
752                                 App.Event.listen(
753                                         'onValuationsPublished', 'Controller.Published:valuations',
754                                         $.proxy( _w.valuation.onValuationsPublished, this, 'valuations' ), 'Recurring'
755                                         );
756                                 App.Event.listen(
757                                         'onValuationsDestruct', 'Controller.RemovingPage:valuations',
758                                         $.proxy( _w.valuation.onPageDestruct, this, 'valuations' ), 'Recurring'
759                                         );
760
761                                 // Valuation View
762                                 App.Event.listen(
763                                         'onValuationViewTemplateReadyMain', 'Template.Ready:valuationview',
764                                         $.proxy( _w.valuationview.onValuationViewTemplateReady, this, 'valuationview' ), 'Recurring'
765                                         );
766                             App.Event.listen(
767                                         'onValuationViewPublishedMain', 'Controller.Published:valuationview',
768                                         $.proxy( _w.valuationview.onValuationViewPublished, this, 'valuationview' ), 'Recurring'
769                                         );
770
771                                 // Limited Valuation View
772                                 App.Event.listen(
773                                         'onValuationViewTemplateReadyLimited', 'Template.Ready:limitedvaluationview',
774                                         $.proxy( _w.valuationview.onValuationViewTemplateReady, this, 'limitedvaluationview' ), 'Recurring'
775                                         );
776                             App.Event.listen(
777                                         'onValuationViewPublishedLimited', 'Controller.Published:limitedvaluationview',
778                                         $.proxy( _w.valuationview.onValuationViewPublished, this, 'limitedvaluationview' ), 'Recurring'
779                                         );
780                         }
781
782                         if (true == App.permissions.pgPage)
783                         {
784                                 // Price Guide Grid
785                                 App.Event.listen(
786                                         'onPriceGuidePageTemplateReady', 'Template.Ready:priceguide',
787                                         $.proxy( _w.priceguide.onPriceGuidePageTemplateReady, this, 'priceguide' ), 'Recurring'
788                                         );
789                                 App.Event.listen(
790                                         'onPriceGuideGridPublished', 'Controller.Published:priceguide',
791                                         $.proxy( _w.priceguide.onPriceGuideGridPublished, this, 'priceguide' ), 'Recurring'
792                                         );
793                                 App.Event.listen(
794                                         'onPriceGuideDestruct', 'Controller.RemovingPage:priceguide',
795                                         $.proxy( _w.priceguide.onPageDestruct, this, 'priceguide' ), 'Recurring'
796                                         );
797
798                                 // Price Guide Offer View
799                                 App.Event.listen(
800                                         'onOfferViewTemplateReady', 'Template.Ready:offerview',
801                                         $.proxy( _w.offerview.onOfferViewTemplateReady, this, 'offerview' ), 'Recurring'
802                                         );
803                                 App.Event.listen(
804                                         'onOfferViewPublished', 'Controller.Published:offerview',
805                                         $.proxy( _w.offerview.onOfferViewPublished, this, 'offerview' ), 'Recurring'
806                                         );
807                         }
808
809                         if (true == App.permissions.settingsPage)
810                         {
811                                 // Settings
812                                 App.Event.listen(
813                                         'onViewTemplateReady', 'Template.Ready:settings',
814                                         $.proxy( _w.settings.onViewTemplateReady, this, 'settings' ), 'Recurring'
815                                         );
816                             App.Event.listen(
817                                         'onViewPublished', 'Controller.Published:settings',
818                                         $.proxy( _w.settings.onViewPublished, this, 'settings' ), 'Recurring'
819                                         );
820                                 App.Event.listen(
821                                         'onSettingsDestruct', 'Controller.RemovingPage:settings',
822                                         $.proxy( _w.settings.onPageDestruct, this, 'settings' ), 'Recurring'
823                                         );
824
825                             // User View
826                             App.Event.listen(
827                                         'onViewTemplateReady', 'Template.Ready:userview',
828                                         $.proxy( _w.userview.onViewTemplateReady, this, 'userview' ), 'Recurring'
829                             );
830                             App.Event.listen(
831                                         'onViewPublished', 'Controller.Published:userview',
832                                         $.proxy( _w.userview.onViewPublished, this, 'userview' ), 'Recurring'
833                             );
834
835                             // Change pin
836                             App.Event.listen(
837                                         'onViewTemplateReady', 'Template.Ready:changepinview',
838                                         $.proxy( _w.changepinview.onViewTemplateReady, this, 'changepinview' ), 'Recurring'
839                             );
840                             App.Event.listen(
841                                         'onViewPublished', 'Controller.Published:changepinview',
842                                         $.proxy( _w.changepinview.onViewPublished, this, 'changepinview' ), 'Recurring'
843                             );
844
845                             // Dealer View
846                             App.Event.listen(
847                                         'onViewTemplateReady', 'Template.Ready:dealerview',
848                                         $.proxy( _w.dealerview.onViewTemplateReady, this, 'dealerview' ), 'Recurring'
849                             );
850                             App.Event.listen(
851                                         'onViewPublished', 'Controller.Published:dealerview',
852                                         $.proxy( _w.dealerview.onViewPublished, this, 'dealerview' ), 'Recurring'
853                             );
854
855                             // Membership View
856                             App.Event.listen(
857                                         'onViewTemplateReady', 'Template.Ready:membershipview',
858                                         $.proxy( _w.membershipview.onViewTemplateReady, this, 'membershipview' ), 'Recurring'
859                             );
860                             App.Event.listen(
861                                         'onViewPublished', 'Controller.Published:membershipview',
862                                         $.proxy( _w.membershipview.onViewPublished, this, 'membershipview' ), 'Recurring'
863                             );
864
865                             // Club View
866                             App.Event.listen(
867                                         'onViewTemplateReady', 'Template.Ready:clubview',
868                                         $.proxy( _w.clubview.onViewTemplateReady, this, 'clubview' ), 'Recurring'
869                             );
870                             App.Event.listen(
871                                         'onViewPublished', 'Controller.Published:clubview',
872                                         $.proxy( _w.clubview.onViewPublished, this, 'clubview' ), 'Recurring'
873                             );
874
875                             // Club Member View
876                             App.Event.listen(
877                                         'onViewTemplateReady', 'Template.Ready:memberview',
878                                         $.proxy( _w.memberview.onViewTemplateReady, this, 'memberview' ), 'Recurring'
879                             );
880                             App.Event.listen(
881                                         'onViewPublished', 'Controller.Published:memberview',
882                                         $.proxy( _w.memberview.onViewPublished, this, 'memberview' ), 'Recurring'
883                             );
884
885                             // User View
886                             App.Event.listen(
887                                         'onViewTemplateReady', 'Template.Ready:disputeview',
888                                         $.proxy( _w.disputeview.onViewTemplateReady, this, 'disputeview' ), 'Recurring'
889                             );
890                         }
891
892
893                         /*
894                          * SU filters
895                          */
896                         if(true == App.permissions.suAccess)
897                         {
898                                 App.DataStore.loadSelectListData('sugroups', true, 'Company', 'Group.SelectList');
899                             App.DataStore.setData('sudivisions', []);
900                             App.DataStore.setData('sucompanies', []);
901                             var sugroupMeta = {
902                                                 type        : 'select',
903                                                 id          : 'sugroup',
904                                                 selectEmpty : 'All',
905                                                 dataStoreId : 'sugroups',
906                                                 value       : false == App.permissions.suGroupFull
907                                                                                 ? App.companyData.group.id
908                                                                                 : (App.Sudo && App.Sudo.GroupId ? App.Sudo.GroupId : ''),
909                                                 onChange    : function () {
910                                                         if (!App.DataStore.getItem('BuildSelect:sugroup', false))
911                                                         {
912                                                                 if (true == App.permissions.suGroupFull)
913                                                             {
914                                                                         var leVal = $(this).val();
915
916                                                                         if ("" == leVal)
917                                                                         {
918                                                                                 App.DataStore.setData('sudivisions', []);
919                                                                                 App.DataStore.setData('sucompanies', []);
920                                                                                 $('#x_sudivision').hide();
921                                                                                 $('#x_sucompany').hide();
922                                                                         }
923                                                                         else
924                                                                         {
925                                                                                 App.DataStore.loadSelectListData(
926                                                                                                 'sudivisions', false, 'Company', 'GroupDivision.SelectList',
927                                                                                                 null, {Filter:{group:$(this).val()}}
928                                                                                                 );
929                                                                                 App.DataStore.setData('sucompanies', []);
930                                                                                 $('#sudivision').selectpicker('val', "");
931                                                                                 $('#sucompany').selectpicker('val', "");
932                                                                                 $('#x_sudivision').show();
933                                                                                 $('#x_sucompany').hide();
934                                                                         }
935
936                                                                         var leVal = '' == $(this).val() ? 0 : $(this).val();
937                                                                         App.API.execTask(
938                                                                                         'setSu', {Sudo:{Group:leVal}}, {},
939                                                                                         function(){
940                                                                                                 App.Event.trigger('SuChange', {});
941                                                                                                 App.DataStore.loadSelectListGroup([
942                                                                                                   {dataId: 'sales', isStatic: false, workspace: 'User', task: 'Profile.SalesList'},
943                                                                                                   {dataId: 'managers', isStatic: false, workspace: 'User', task: 'Profile.ManagerList'},
944                                                                                                 ]);
945                                                                                         }, _w.taskExecError
946                                                                                 );
947                                                             }
948
949
950                                                         }
951                                                         else
952                                                         {
953                                                                 if ("" != $(this).val())
954                                                                 {
955                                                                         App.DataStore.loadSelectListData(
956                                                                                         'sudivisions', false, 'Company', 'GroupDivision.SelectList',
957                                                                                         null, {Filter:{group:$(this).val()}}
958                                                                                         );
959                                                                         $('#x_sudivision').show();
960                                                                 }
961                                                         }
962                                                 }
963                                         };
964
965                             var sudivisionMeta = {
966                                                 type        : 'select',
967                                                 id          : 'sudivision',
968                                                 selectEmpty : 'All',
969                                                 dataStoreId : 'sudivisions',
970                                                 value       : false == App.permissions.suGroupFull && false == App.permissions.suDivisionFull
971                                                                                         ? (null != App.companyData.groupDivision ? App.companyData.groupDivision.id : null)
972                                                                                         : (App.Sudo && App.Sudo.DivisionId ? App.Sudo.DivisionId : ''),
973                                                 onChange    : function () {
974                                                         if (!App.DataStore.getItem('BuildSelect:sudivision', false))
975                                                         {
976
977                                                                 if (true == App.permissions.suGroupFull
978                                                                                 || true == App.permissions.suDivisionFull)
979                                                                 {
980                                                                         var leVal = $(this).val();
981
982                                                                         if ("" == leVal)
983                                                                         {
984                                                                                 App.DataStore.setData('sucompanies', []);
985                                                                                 $('#x_sucompany').hide();
986                                                                         }
987                                                                         else
988                                                                         {
989                                                                                 App.DataStore.loadSelectListData(
990                                                                                                 'sucompanies', false, 'Company', 'Company.SelectList',
991                                                                                                 null, {Filter:{groupDivision:$(this).val()}}
992                                                                                                 );
993                                                                                 $('#x_sucompany').show();
994                                                                                 $('#sucompany').selectpicker('val', "");
995                                                                         }
996
997                                                                         var leVal = '' == $(this).val() ? 0 : $(this).val();
998                                                                         App.API.execTask(
999                                                                                         'setSu', {Sudo:{Division:leVal}}, {},
1000                                                                                         function(){
1001                                                                                                 App.Event.trigger('SuChange', {});
1002                                                                                                 App.DataStore.loadSelectListGroup([
1003                                                                                                   {dataId: 'sales', isStatic: false, workspace: 'User', task: 'Profile.SalesList'},
1004                                                                                                   {dataId: 'managers', isStatic: false, workspace: 'User', task: 'Profile.ManagerList'},
1005                                                                                                 ]);
1006                                                                                         }, _w.taskExecError
1007                                                                                 );
1008                                                                 }
1009
1010
1011                                                         }
1012                                                         else
1013                                                         {
1014                                                                 if("" != $(this).val())
1015                                                                 {
1016                                                                         App.DataStore.loadSelectListData(
1017                                                                                         'sucompanies', false, 'Company', 'Company.SelectList',
1018                                                                                         null, {Filter:{groupDivision:$(this).val()}}
1019                                                                                         );
1020                                                                         $('#x_sucompany').show();
1021                                                                 }
1022                                                         }
1023                                                 }
1024                                         };
1025
1026                             var sucompanyMeta = {
1027                                                 type        : 'select',
1028                                                 id          : 'sucompany',
1029                                                 selectEmpty : 'All',
1030                                                 dataStoreId : 'sucompanies',
1031                                                 value       : false == App.permissions.suGroupFull
1032                                                                                 && false == App.permissions.suDivisionFull
1033                                                                                 && false == App.permissions.suCompanyFull
1034                                                         ? App.companyData.id
1035                                                         : (App.Sudo && App.Sudo.CompanyId ? App.Sudo.CompanyId : ''),
1036                                                 onChange    : function () {
1037                                                         if (!App.DataStore.getItem('BuildSelect:sucompany', false))
1038                                                         {
1039                                                                 if (true == App.permissions.suGroupFull
1040                                                                                 || true == App.permissions.suDivisionFull
1041                                                                                 || true == App.permissions.suCompanyFull)
1042                                                                 {
1043                                                                         var leVal = '' == $(this).val() ? 0 : $(this).val();
1044                                                                         App.API.execTask(
1045                                                                                         'setSu', {Sudo:{Company:leVal}}, {},
1046                                                                                         function(){
1047                                                                                                 App.Event.trigger('SuChange', {});
1048                                                                                                 App.DataStore.loadSelectListGroup([
1049                                                                                                   {dataId: 'sales', isStatic: false, workspace: 'User', task: 'Profile.SalesList'},
1050                                                                                                   {dataId: 'managers', isStatic: false, workspace: 'User', task: 'Profile.ManagerList'},
1051                                                                                                 ]);
1052                                                                                         }, _w.taskExecError
1053                                                                                 );
1054                                                                 }
1055                                                         }
1056                                                 }
1057                                         };
1058
1059                             $('#x_sugroup').html(App.ElementLibrary.Field.html(sugroupMeta, null));
1060                             App.ElementLibrary.Field.bind( 'x', sugroupMeta );
1061                             if (false == App.permissions.suGroupFull)
1062                             {
1063                                 $('#sugroup').attr('disabled', true);
1064                             }
1065
1066                             $('#x_sudivision').html(App.ElementLibrary.Field.html(sudivisionMeta, null));
1067                             App.ElementLibrary.Field.bind( 'x', sudivisionMeta );
1068                             if (false == App.permissions.suGroupFull
1069                                         && false == App.permissions.suDivisionFull)
1070                             {
1071                                 $('#sudivision').attr('disabled', true);
1072                             }
1073
1074                             $('#x_sucompany').html(App.ElementLibrary.Field.html(sucompanyMeta, null));
1075                             App.ElementLibrary.Field.bind( 'x', sucompanyMeta );
1076                             if (false == App.permissions.suGroupFull
1077                                         && false == App.permissions.suDivisionFull
1078                                         && false == App.permissions.suCompanyFull)
1079                             {
1080                                 $('#sucompany').attr('disabled', true);
1081                             }
1082
1083                             $('#x_sudivision').hide();
1084                                 $('#x_sucompany').hide();
1085                                 $('#su_container').show();
1086                         }
1087                 },
1088
1089                 filterIfnotEmpty : function( filter, element, field, op, append, defaultValue )
1090                 {
1091                         append = (append)
1092                                 ? append
1093                                 : '';
1094                         if ('' != $('#' + element).val() && null != $('#' + element).val())
1095                         {
1096                                 op = op ? op + ' ' : '';
1097                                 var val = $('#' + element).val();
1098
1099                                 if ($('#' + element).hasClass('selectpicker'))
1100                                 {
1101                                         op = '';
1102                                 }
1103                                 else
1104                                 {
1105                                         //val = ('' == val || !isNaN(val) || '' != op)
1106                                         //      ? val
1107                                         //      : '%' + val + '%';
1108
1109                     val = ('' == val || '' != op)
1110                         ? val
1111                         : '%' + val + '%';
1112                                 }
1113                                 if (filter.filters[field])
1114                                 {
1115                                         var nested = {};
1116                                         nested[field] = op + val + append;
1117                                         filter.filters[field + '-to'] = nested;
1118                                 }
1119                                 else
1120                                 {
1121                                         filter.filters[field] = op + val + append;
1122                                 }
1123                                 filter.count++;
1124                         }
1125                         return filter;
1126                 },
1127
1128                 populateFilters : function( filters, map )
1129                 {
1130                         for (var field in map)
1131                         {
1132                                 var nested = field.split(':');
1133                                 if (nested[1])
1134                                 {
1135                                         if (filters[nested[0]] && filters[nested[0]][nested[1]])
1136                                         {
1137                                                 filters[field] = isNaN(filters[nested[0]][nested[1]]) && '%' == filters[nested[0]][nested[1]].substring(0,1)
1138                                                         ? filters[nested[0]][nested[1]].substring(1, filters[nested[0]][nested[1]].length - 1)
1139                                                         : filters[nested[0]][nested[1]];
1140                                                 $('#' + map[field]).val(filters[field]);
1141                                                 $('#' + map[field] + '.selectpicker').selectpicker('refresh');
1142                                         }
1143                                 }
1144                                 else if (filters[field])
1145                                 {
1146                                         filters[field] = isNaN(filters[field]) && '%' == filters[field].substring(0,1)
1147                                                 ? filters[field].substring(1, filters[field].length - 1)
1148                                                 : filters[field];
1149                                         $('#' + map[field]).val(filters[field]);
1150                                         $('#' + map[field] + '.selectpicker').selectpicker('refresh');
1151                                 }
1152                         }
1153                 },
1154
1155                 taskContractError : function( response )
1156                 {
1157                         console.log(response);
1158                         console.log(window.location.hostname);
1159                         App.API.getTask(
1160                                         'getActiveAccount', 'User', 'Profile.ActiveAccount', null,
1161                                         {}, function() {
1162                                                 if ('namibia.nirph.com' != window.location.hostname
1163                                                                 && 'namibia.local' != window.location.hostname
1164                                                                 && 'www.bid4cars.com.na' != window.location.hostname)
1165                                                 {
1166                                                         alert('Please click ok to refresh this page.');
1167                                                         location.reload();
1168                                                 }
1169                                                 console.log('------------ Task Contract Error ------------');
1170                                                 console.log(response);
1171                                                 console.log('--------------------------------------');
1172                                         },_w.onActiveAccountFailure
1173                         );
1174
1175                 },
1176
1177                 taskExecError : function( response )
1178                 {
1179                         console.log(response);
1180                         console.log(window.location.hostname);
1181                         App.API.getTask(
1182                                         'getActiveAccount', 'User', 'Profile.ActiveAccount', null,
1183                                         {}, function() {
1184                                                 if ('namibia.nirph.com' != window.location.hostname
1185                                                                 && 'namibia.local' != window.location.hostname
1186                                                                 && 'www.bid4cars.com.na' != window.location.hostname)
1187                                                 {
1188                                                         alert('Please click ok to refresh this page.');
1189                                                         location.reload();
1190                                                 }
1191                                                 console.log('------------ Task Exec Error ------------');
1192                                                 console.log(response);
1193                                                 console.log('--------------------------------------');
1194                                         }, _w.onActiveAccountFailure
1195                         );
1196                 },
1197
1198                 onForgotPageTemplatePublished : function( event, eventData )
1199                 {
1200                         $('#email').val(App.Util.getCookie('UserEmail', ''));
1201                         $('#frmReset').validate({
1202                                 rules: {
1203                                         email: {
1204                                                 email: true
1205                                         },
1206                                         mobile: {
1207                                                 mobile: true
1208                                         }
1209                                 },
1210                                 submitHandler: function( form ) {
1211                                         if ('' == $('#email').val() && '' == $('#mobile').val())
1212                                         {
1213                                                 alert('Please provide email or mobile number.');
1214                                                 return;
1215                                         }
1216                                         App.API.getTask(
1217                                                         'reset', 'User', 'Profile.ForgotPassword', null,
1218                                                         {}, function() {
1219                                                                 var mobile = $('#mobile').val();
1220                                                                 mobile = ('' == mobile)
1221                                                                         ? ''
1222                                                                         : '+264' + mobile;
1223                                                                 var data = {
1224                                                                         "Reset":{
1225                                                                             "email"  : $('#email').val(),
1226                                                                             "mobile" : mobile
1227                                                                   }};
1228                                                                 App.API.execTask(
1229                                                                         'reset', data, {},
1230                                                                         function(response) {
1231                                                                                 $('#reset-fail').popover('hide');
1232                                                                                 alert('Please check your email for your new pin.');
1233                                                                                 window.location = '/#/login';
1234                                                                         }, function (data) {
1235                                                                                 $('#reset-fail').attr('data-content', data.Message);
1236                                                                                 $('#reset-fail').popover('show');
1237                                                                         }
1238                                                                 );
1239                                                         }, _w.taskContractError
1240                                         );
1241                                 }
1242                         });
1243                 },
1244
1245                 onLoginPageTemplatePublished : function( event, eventData )
1246                 {
1247                         App.Util.updateCheckboxStyles($('#frmLogin'));
1248                         $('#email').val(App.Util.getCookie('UserEmail', ''));
1249                         $('#frmLogin').validate({
1250                                 rules: {
1251                                         email: {
1252                                                 email: true,
1253                                                 required: true
1254                                         },
1255                                         pin: {
1256                                                 minlength: 3,
1257                                                 required: true
1258                                         },
1259                                         mpin: {
1260                                                 minlength: 3,
1261                                                 required: true
1262                                         }
1263                                 },
1264                                 submitHandler: function( form ) {
1265                                         App.API.getTask(
1266                                                         'login', 'User', 'Profile.Login', null,
1267                                                         {}, function() {
1268                                                                 var data = {
1269                                                                         "Login":{
1270                                                                             "email"      : $('#email').val(),
1271                                                                             "pin"        : $('#pin').val()
1272                                                                   }};
1273                                                                 App.API.execTask(
1274                                                                         'login', data, {},
1275                                                                         function(response) {
1276                                                                                 $('#login-fail').popover('hide');
1277                                                                                 var rem = $('#rememberMe').is(':checked')
1278                                                                                         ? $('#email').val()
1279                                                                                         : '';
1280                                                                                 App.Util.setCookie('UserEmail', rem, 30);
1281                                                                                 _w.onActiveAccount(response);
1282                                                                         }, function (data) {
1283                                                                                 $('#login-fail').attr('data-content', data.StatusReason);
1284                                                                                 $('#login-fail').popover('show');
1285                                                                         }
1286                                                                 );
1287                                                         }, _w.taskContractError
1288                                         );
1289                                 }
1290                         });
1291                 },
1292
1293                 synchServerTime : function()
1294                 {
1295                         var dt = new Date();
1296                         App.timeDiffStart = Math.round(dt.getTime() / 1000) + 1;
1297                         $.ajax({
1298                                 type            : 'GET',
1299                                 dataType        : 'json',
1300                                 url             : window.location.protocol
1301                                                         + '//' + window.location.hostname
1302                                                         + '//servertime.php?v=' + dt.getTime()
1303                         })
1304                         .done( function(data) {
1305                                 var dt = new Date();
1306                                 var now = Math.round(dt.getTime() / 1000);
1307                                 if ((now - App.timeDiffStart) < 4)
1308                                 {
1309                                         App.timeDiff = now - data.time;
1310                                         setTimeout(_w.synchServerTime, 600000);
1311                                 }
1312                                 else
1313                                 {
1314                                         setTimeout(_w.synchServerTime, 5000);
1315                                 }
1316                         });
1317                 },
1318
1319                 onActiveAccount : function( response )
1320                 {
1321                         /*
1322                          * Request list of public holidays.
1323                          */
1324                         $('#login_mobi').hide();
1325                         App.API.getTask(
1326                                         'listPublicHoliday', 'Auction', 'PublicHoliday.List', null,
1327                                         {}, function() {
1328                                                 App.API.execTask(
1329                                                                 'listPublicHoliday', {}, {},
1330                                                                 function(response) {
1331                                                                         App.DataStore.setData('publicHolidays', response.Data);
1332                                                                 }, _w.taskExecError
1333                                                                 );
1334                                         }, _w.taskContractError
1335                                         );
1336
1337                         /*
1338                          * User is authenticated.
1339                          */
1340                         App.Authenticated = true;
1341                         App.serverConfig = response.Data.config;
1342                         delete response.Data.config;
1343                         App.permissions = response.Data.permissions;
1344                         delete response.Data.permissions;
1345                         App.companyData = response.Data.company;
1346                         App.companyData.tC = App.companyData.tradeCenter && App.companyData.tradeCenter.id
1347                                 ? App.companyData.tradeCenter.id
1348                                 : false;
1349                         delete response.Data.company;
1350                         App.Sudo = response.Data.Sudo;
1351                         delete response.Data.Sudo;
1352                         App.userData = response.Data;
1353
1354                         //console.log("serverConfig", App.serverConfig);
1355                         //console.log("permissions", App.permissions);
1356                         //console.log("companyData", App.companyData);
1357                         //console.log("Sudo", App.Sudo);
1358                         //console.log("userData", App.userData);
1359
1360
1361                         App.openDays = response.Data.auctionOpenDays;
1362                         delete response.Data.auctionOpenDays;
1363                         if ('Auction Buyer' == App.permissions.name
1364                                 || 'Auction Buyer & Seller' == App.permissions.name)
1365                         {
1366                                 if (App.companyData.group != null){
1367                                         App.companyData.group.landingPage = 'auction';
1368                                 }
1369
1370                         }
1371                         if ('Auction Buyer' == App.permissions.name
1372                                 || 'Auction Buyer & Seller' == App.permissions.name)
1373                         {
1374                                 App.isNewAuctionUser = true;
1375                         }
1376
1377
1378                         /*
1379                          * Show and hook up logout button
1380                          */
1381                         $('.b_logout').show();
1382                         $('.b_logout').click(function()
1383                         {
1384
1385                                 $('#PortalMenu').remove();
1386
1387                                 App.Authenticated = false;
1388                                 App.permissions = {};
1389                                 App.companyDat  = {};
1390                                 App.Sudo        = {};
1391                                 App.userData    = {};
1392                                 App.API.getTask(
1393                                                 'logout:', 'User', 'Profile.Logout', null,
1394                                                 {}, function() { window.location='/'; }, function() {}
1395                                         );
1396                         });
1397
1398                         /*
1399                          * Show user details
1400                          */
1401                         $('#user-details').removeClass('invisible');
1402                         $('.auth-username').html(
1403                                         App.userData.firstName + ' ' + App.userData.familyName
1404                         );
1405                         $('.auth-name').html(
1406                                         App.userData.firstName
1407                         );
1408
1409                         /*
1410                          * Init permissions and page listeners.
1411                          */
1412                         var isMobile = 'none' == $('#mobiCheck').css('display');
1413                         if (true == App.permissions.accessPortalModule
1414                                         && false == App.permissions.accessAdminModule)
1415                         {
1416                                 _w.initPortal();
1417
1418                                 if (null == App.userData.lastLogin || '' == App.userData.lastLogin)
1419                                 {
1420                                         window.location.hash = '/changepinview?id=' + App.userData.id;
1421                                 }
1422                                 else if (App.redirect && '' != App.redirect && '#/' != App.redirect)
1423                                 {
1424                                         window.location =  '/' + App.redirect;
1425                                         delete App.redirect;
1426                                 }
1427                                 else
1428                                 {
1429
1430                                         // check for mobi redirect here
1431
1432                                         if (isMobile)
1433                                         {
1434
1435                                                 // if permissions are ok show the buttons
1436                                                 if (false == App.permissions.valuationPage)
1437                                                 {
1438                                                         $("#mobiValuationsBtn").remove();
1439                                                 }
1440                                                 if (false == App.permissions.pgPage)
1441                                                 {
1442                                                         $("#mobiPriceGuideBtn").remove();
1443                                                 }
1444                                                 if (false == App.permissions.auctionPage)
1445                                                 {
1446                                                         $("#mobiAuctionBtn").remove();
1447                                                 }
1448
1449                                                 // do a redirect to valid pages
1450                                                 if (App.permissions.valuationPage)
1451                                                 {
1452                                                         window.location.hash = '/valuations';
1453                                                 }
1454                                                 else if (App.permissions.pgPage )
1455                                                 {
1456                                                         window.location.hash = '/priceguide';
1457                                                 }
1458                                                 else if (App.permissions.auctionPage)
1459                                                 {
1460                                                         window.location.hash = '/auction';
1461                                                 }
1462
1463                                                 else
1464                                                 {
1465                                                         alert("Sorry you dont have any mobi permissions");
1466                                                         App.Authenticated = false;
1467                                                         App.permissions = {};
1468                                                         App.companyDat  = {};
1469                                                         App.Sudo        = {};
1470                                                         App.userData    = {};
1471                                                         App.API.getTask(
1472                                                                         'logout:', 'User', 'Profile.Logout', null,
1473                                                                         {}, function() { window.location='/'; }, function() {}
1474                                                                 );
1475                                                 }
1476                                         }
1477
1478                                         else
1479                                         {
1480
1481                                                 if (App.companyData.group == null)
1482                                                 {
1483
1484                                                         window.location.hash = '/settings';
1485
1486                                                 }
1487
1488
1489
1490                                                 else if (false == App.permissions.valuationPage && 'valuations' == App.companyData.group.landingPage)
1491                                                 {
1492                                                         window.location.hash = '/settings';
1493                                                 }
1494                                                 else if (false == App.permissions.pgPage && 'priceguide' == App.companyData.group.landingPage)
1495                                                 {
1496                                                         window.location.hash = '/settings';
1497                                                 }
1498                                                 else if (false == App.permissions.auctionPage && 'auction' == App.companyData.group.landingPage)
1499                                                 {
1500                                                         window.location.hash = '/settings';
1501                                                 }
1502                                                 else if ('' != App.companyData.group.landingPage && null != App.companyData.group.landingPage)
1503                                                 {
1504                                                         window.location.hash = '/' + App.companyData.group.landingPage;
1505                                                 }
1506                                                 else if (null == App.companyData.group.landingPage)
1507                                                 {
1508                                                         window.location.hash = '/settings';
1509                                                 }
1510                                                 else
1511                                                 {
1512                                                         window.location.hash = '/settings';
1513                                                 }
1514
1515                                         }
1516                                 }
1517                         }
1518                         else if (false == App.permissions.accessPortalModule
1519                                         && true == App.permissions.accessAdminModule)
1520                         {
1521                                 _w.initAdmin();
1522                                 if ('namibia.local' != window.location.hostname)
1523                                 {
1524                                         window.location.hash = '/admin';
1525                                 }
1526                                 else
1527                                 {
1528                                         if(App.redirect)
1529                                         {
1530                                                 window.location =  '/' + App.redirect;
1531                                                 delete App.redirect;
1532                                         }
1533                                         else
1534                                         {
1535                                                 window.location.hash = '/admin';
1536                                         }
1537                                 }
1538                         }
1539                         else if (true == App.permissions.accessPortalModule
1540                                         && true == App.permissions.accessAdminModule)
1541                         {
1542                                 App.Event.listen(
1543                                         'onSwitchTemplatePublished', 'Controller.Published:switch',
1544                                         $.proxy( _w.onSwitchTemplatePublished, this ), 'Recurring'
1545                                         );
1546                                 window.location.hash = '/switch';
1547                         }
1548                         else
1549                         {
1550                                 // No permissions at all
1551                                 window.location.hash = '/home';
1552                         }
1553                         _w.synchServerTime();
1554
1555                 },
1556
1557                 onActiveAccountFailure : function()
1558                 {
1559                         /*
1560                          * User is not authenticated.
1561                          */
1562                         App.Authenticated = false;
1563                         $('#login_mobi').show();
1564
1565                         /*
1566                          * Hide nav
1567                          */
1568                         $('#b_logout').hide();
1569                         $('#b_stock').hide();
1570                         $('#b_tcstock').hide();
1571                         $('#b_valuations').hide();
1572                 $('#b_historicaldata').hide();
1573                         $('#b_settings').hide();
1574                         $('#b_priceGuide').hide();
1575
1576                         /*
1577                          * Hide user details
1578                          */
1579                         $('#user-details').addClass("invisible");
1580
1581                         /*
1582                          * Initiate brochure.
1583                          */
1584                         _w.initBrochure();
1585
1586                         _w.synchServerTime();
1587
1588                 }
1589
1590         };
1591
1592 })();