c66dfd5a15df09395f3ed9cf27864b0ed8a41442
[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
1158                         App.API.getTask(
1159                                         'getActiveAccount', 'User', 'Profile.ActiveAccount', null,
1160                                         {}, function() {
1161                                                 if ('bid4cars.nirph.com' != window.location.hostname
1162                                                                 && 'bid4cars.local' != window.location.hostname
1163                                                                 && 'bid4cars.com.na' != window.location.hostname)
1164                                                 {
1165
1166                                                         alert(window.location.hostname);
1167                                                         alert('Please click ok to refresh this page.');
1168                                                         location.reload();
1169                                                 }
1170                                                 console.log('------------ Task Contract Error ------------');
1171                                                 console.log(response);
1172                                                 console.log('--------------------------------------');
1173                                         },_w.onActiveAccountFailure
1174                         );
1175
1176                 },
1177
1178                 taskExecError : function( response )
1179                 {
1180                         App.API.getTask(
1181                                         'getActiveAccount', 'User', 'Profile.ActiveAccount', null,
1182                                         {}, function() {
1183                                                 if ('bid4cars.nirph.com' != window.location.hostname
1184                                                                 && 'bid4cars.local' != window.location.hostname
1185                                                                 && 'bid4cars.com.na' != window.location.hostname)
1186                                                 {
1187                                                         alert('Please click ok to refresh this page.');
1188                                                         alert(window.location.hostname);
1189
1190                                                         location.reload();
1191                                                 }
1192                                                 console.log('------------ Task Exec Error ------------');
1193                                                 console.log(response);
1194                                                 console.log('--------------------------------------');
1195                                         }, _w.onActiveAccountFailure
1196                         );
1197                 },
1198
1199                 onForgotPageTemplatePublished : function( event, eventData )
1200                 {
1201                         $('#email').val(App.Util.getCookie('UserEmail', ''));
1202                         $('#frmReset').validate({
1203                                 rules: {
1204                                         email: {
1205                                                 email: true
1206                                         },
1207                                         mobile: {
1208                                                 mobile: true
1209                                         }
1210                                 },
1211                                 submitHandler: function( form ) {
1212                                         if ('' == $('#email').val() && '' == $('#mobile').val())
1213                                         {
1214                                                 alert('Please provide email or mobile number.');
1215                                                 return;
1216                                         }
1217                                         App.API.getTask(
1218                                                         'reset', 'User', 'Profile.ForgotPassword', null,
1219                                                         {}, function() {
1220                                                                 var mobile = $('#mobile').val();
1221                                                                 mobile = ('' == mobile)
1222                                                                         ? ''
1223                                                                         : '+264' + mobile;
1224                                                                 var data = {
1225                                                                         "Reset":{
1226                                                                             "email"  : $('#email').val(),
1227                                                                             "mobile" : mobile
1228                                                                   }};
1229                                                                 App.API.execTask(
1230                                                                         'reset', data, {},
1231                                                                         function(response) {
1232                                                                                 $('#reset-fail').popover('hide');
1233                                                                                 alert('Please check your email for your new pin.');
1234                                                                                 window.location = '/#/login';
1235                                                                         }, function (data) {
1236                                                                                 $('#reset-fail').attr('data-content', data.Message);
1237                                                                                 $('#reset-fail').popover('show');
1238                                                                         }
1239                                                                 );
1240                                                         }, _w.taskContractError
1241                                         );
1242                                 }
1243                         });
1244                 },
1245
1246                 onLoginPageTemplatePublished : function( event, eventData )
1247                 {
1248                         App.Util.updateCheckboxStyles($('#frmLogin'));
1249                         $('#email').val(App.Util.getCookie('UserEmail', ''));
1250                         $('#frmLogin').validate({
1251                                 rules: {
1252                                         email: {
1253                                                 email: true,
1254                                                 required: true
1255                                         },
1256                                         pin: {
1257                                                 minlength: 3,
1258                                                 required: true
1259                                         },
1260                                         mpin: {
1261                                                 minlength: 3,
1262                                                 required: true
1263                                         }
1264                                 },
1265                                 submitHandler: function( form ) {
1266                                         App.API.getTask(
1267                                                         'login', 'User', 'Profile.Login', null,
1268                                                         {}, function() {
1269                                                                 var data = {
1270                                                                         "Login":{
1271                                                                             "email"      : $('#email').val(),
1272                                                                             "pin"        : $('#pin').val()
1273                                                                   }};
1274                                                                 App.API.execTask(
1275                                                                         'login', data, {},
1276                                                                         function(response) {
1277                                                                                 $('#login-fail').popover('hide');
1278                                                                                 var rem = $('#rememberMe').is(':checked')
1279                                                                                         ? $('#email').val()
1280                                                                                         : '';
1281                                                                                 App.Util.setCookie('UserEmail', rem, 30);
1282                                                                                 _w.onActiveAccount(response);
1283                                                                         }, function (data) {
1284                                                                                 $('#login-fail').attr('data-content', data.StatusReason);
1285                                                                                 $('#login-fail').popover('show');
1286                                                                         }
1287                                                                 );
1288                                                         }, _w.taskContractError
1289                                         );
1290                                 }
1291                         });
1292                 },
1293
1294                 synchServerTime : function()
1295                 {
1296                         var dt = new Date();
1297                         App.timeDiffStart = Math.round(dt.getTime() / 1000) + 1;
1298                         $.ajax({
1299                                 type            : 'GET',
1300                                 dataType        : 'json',
1301                                 url             : window.location.protocol
1302                                                         + '//' + window.location.hostname
1303                                                         + '//servertime.php?v=' + dt.getTime()
1304                         })
1305                         .done( function(data) {
1306                                 var dt = new Date();
1307                                 var now = Math.round(dt.getTime() / 1000);
1308                                 if ((now - App.timeDiffStart) < 4)
1309                                 {
1310                                         App.timeDiff = now - data.time;
1311                                         setTimeout(_w.synchServerTime, 600000);
1312                                 }
1313                                 else
1314                                 {
1315                                         setTimeout(_w.synchServerTime, 5000);
1316                                 }
1317                         });
1318                 },
1319
1320                 onActiveAccount : function( response )
1321                 {
1322                         /*
1323                          * Request list of public holidays.
1324                          */
1325                         $('#login_mobi').hide();
1326                         App.API.getTask(
1327                                         'listPublicHoliday', 'Auction', 'PublicHoliday.List', null,
1328                                         {}, function() {
1329                                                 App.API.execTask(
1330                                                                 'listPublicHoliday', {}, {},
1331                                                                 function(response) {
1332                                                                         App.DataStore.setData('publicHolidays', response.Data);
1333                                                                 }, _w.taskExecError
1334                                                                 );
1335                                         }, _w.taskContractError
1336                                         );
1337
1338                         /*
1339                          * User is authenticated.
1340                          */
1341                         App.Authenticated = true;
1342                         App.serverConfig = response.Data.config;
1343                         delete response.Data.config;
1344                         App.permissions = response.Data.permissions;
1345                         delete response.Data.permissions;
1346                         App.companyData = response.Data.company;
1347                         App.companyData.tC = App.companyData.tradeCenter && App.companyData.tradeCenter.id
1348                                 ? App.companyData.tradeCenter.id
1349                                 : false;
1350                         delete response.Data.company;
1351                         App.Sudo = response.Data.Sudo;
1352                         delete response.Data.Sudo;
1353                         App.userData = response.Data;
1354
1355                         //console.log("serverConfig", App.serverConfig);
1356                         //console.log("permissions", App.permissions);
1357                         //console.log("companyData", App.companyData);
1358                         //console.log("Sudo", App.Sudo);
1359                         //console.log("userData", App.userData);
1360
1361
1362                         App.openDays = response.Data.auctionOpenDays;
1363                         delete response.Data.auctionOpenDays;
1364                         if ('Auction Buyer' == App.permissions.name
1365                                 || 'Auction Buyer & Seller' == App.permissions.name)
1366                         {
1367                                 if (App.companyData.group != null){
1368                                         App.companyData.group.landingPage = 'auction';
1369                                 }
1370
1371                         }
1372                         if ('Auction Buyer' == App.permissions.name
1373                                 || 'Auction Buyer & Seller' == App.permissions.name)
1374                         {
1375                                 App.isNewAuctionUser = true;
1376                         }
1377
1378
1379                         /*
1380                          * Show and hook up logout button
1381                          */
1382                         $('.b_logout').show();
1383                         $('.b_logout').click(function()
1384                         {
1385
1386                                 $('#PortalMenu').remove();
1387
1388                                 App.Authenticated = false;
1389                                 App.permissions = {};
1390                                 App.companyDat  = {};
1391                                 App.Sudo        = {};
1392                                 App.userData    = {};
1393                                 App.API.getTask(
1394                                                 'logout:', 'User', 'Profile.Logout', null,
1395                                                 {}, function() { window.location='/'; }, function() {}
1396                                         );
1397                         });
1398
1399                         /*
1400                          * Show user details
1401                          */
1402                         $('#user-details').removeClass('invisible');
1403                         $('.auth-username').html(
1404                                         App.userData.firstName + ' ' + App.userData.familyName
1405                         );
1406                         $('.auth-name').html(
1407                                         App.userData.firstName
1408                         );
1409
1410                         /*
1411                          * Init permissions and page listeners.
1412                          */
1413                         var isMobile = 'none' == $('#mobiCheck').css('display');
1414                         if (true == App.permissions.accessPortalModule
1415                                         && false == App.permissions.accessAdminModule)
1416                         {
1417                                 _w.initPortal();
1418
1419                                 if (null == App.userData.lastLogin || '' == App.userData.lastLogin)
1420                                 {
1421                                         window.location.hash = '/changepinview?id=' + App.userData.id;
1422                                 }
1423                                 else if (App.redirect && '' != App.redirect && '#/' != App.redirect)
1424                                 {
1425                                         window.location =  '/' + App.redirect;
1426                                         delete App.redirect;
1427                                 }
1428                                 else
1429                                 {
1430
1431                                         // check for mobi redirect here
1432
1433                                         if (isMobile)
1434                                         {
1435
1436                                                 // if permissions are ok show the buttons
1437                                                 if (false == App.permissions.valuationPage)
1438                                                 {
1439                                                         $("#mobiValuationsBtn").remove();
1440                                                 }
1441                                                 if (false == App.permissions.pgPage)
1442                                                 {
1443                                                         $("#mobiPriceGuideBtn").remove();
1444                                                 }
1445                                                 if (false == App.permissions.auctionPage)
1446                                                 {
1447                                                         $("#mobiAuctionBtn").remove();
1448                                                 }
1449
1450                                                 // do a redirect to valid pages
1451                                                 if (App.permissions.valuationPage)
1452                                                 {
1453                                                         window.location.hash = '/valuations';
1454                                                 }
1455                                                 else if (App.permissions.pgPage )
1456                                                 {
1457                                                         window.location.hash = '/priceguide';
1458                                                 }
1459                                                 else if (App.permissions.auctionPage)
1460                                                 {
1461                                                         window.location.hash = '/auction';
1462                                                 }
1463
1464                                                 else
1465                                                 {
1466                                                         alert("Sorry you dont have any mobi permissions");
1467                                                         App.Authenticated = false;
1468                                                         App.permissions = {};
1469                                                         App.companyDat  = {};
1470                                                         App.Sudo        = {};
1471                                                         App.userData    = {};
1472                                                         App.API.getTask(
1473                                                                         'logout:', 'User', 'Profile.Logout', null,
1474                                                                         {}, function() { window.location='/'; }, function() {}
1475                                                                 );
1476                                                 }
1477                                         }
1478
1479                                         else
1480                                         {
1481
1482                                                 if (App.companyData.group == null)
1483                                                 {
1484
1485                                                         window.location.hash = '/settings';
1486
1487                                                 }
1488
1489
1490
1491                                                 else if (false == App.permissions.valuationPage && 'valuations' == App.companyData.group.landingPage)
1492                                                 {
1493                                                         window.location.hash = '/settings';
1494                                                 }
1495                                                 else if (false == App.permissions.pgPage && 'priceguide' == App.companyData.group.landingPage)
1496                                                 {
1497                                                         window.location.hash = '/settings';
1498                                                 }
1499                                                 else if (false == App.permissions.auctionPage && 'auction' == App.companyData.group.landingPage)
1500                                                 {
1501                                                         window.location.hash = '/settings';
1502                                                 }
1503                                                 else if ('' != App.companyData.group.landingPage && null != App.companyData.group.landingPage)
1504                                                 {
1505                                                         window.location.hash = '/' + App.companyData.group.landingPage;
1506                                                 }
1507                                                 else if (null == App.companyData.group.landingPage)
1508                                                 {
1509                                                         window.location.hash = '/settings';
1510                                                 }
1511                                                 else
1512                                                 {
1513                                                         window.location.hash = '/settings';
1514                                                 }
1515
1516                                         }
1517                                 }
1518                         }
1519                         else if (false == App.permissions.accessPortalModule
1520                                         && true == App.permissions.accessAdminModule)
1521                         {
1522                                 _w.initAdmin();
1523                                 if ('bid4cars.local' != window.location.hostname)
1524                                 {
1525                                         window.location.hash = '/admin';
1526                                 }
1527                                 else
1528                                 {
1529                                         if(App.redirect)
1530                                         {
1531                                                 window.location =  '/' + App.redirect;
1532                                                 delete App.redirect;
1533                                         }
1534                                         else
1535                                         {
1536                                                 window.location.hash = '/admin';
1537                                         }
1538                                 }
1539                         }
1540                         else if (true == App.permissions.accessPortalModule
1541                                         && true == App.permissions.accessAdminModule)
1542                         {
1543                                 App.Event.listen(
1544                                         'onSwitchTemplatePublished', 'Controller.Published:switch',
1545                                         $.proxy( _w.onSwitchTemplatePublished, this ), 'Recurring'
1546                                         );
1547                                 window.location.hash = '/switch';
1548                         }
1549                         else
1550                         {
1551                                 // No permissions at all
1552                                 window.location.hash = '/home';
1553                         }
1554                         _w.synchServerTime();
1555
1556                 },
1557
1558                 onActiveAccountFailure : function()
1559                 {
1560                         /*
1561                          * User is not authenticated.
1562                          */
1563                         App.Authenticated = false;
1564                         $('#login_mobi').show();
1565
1566                         /*
1567                          * Hide nav
1568                          */
1569                         $('#b_logout').hide();
1570                         $('#b_stock').hide();
1571                         $('#b_tcstock').hide();
1572                         $('#b_valuations').hide();
1573                 $('#b_historicaldata').hide();
1574                         $('#b_settings').hide();
1575                         $('#b_priceGuide').hide();
1576
1577                         /*
1578                          * Hide user details
1579                          */
1580                         $('#user-details').addClass("invisible");
1581
1582                         /*
1583                          * Initiate brochure.
1584                          */
1585                         _w.initBrochure();
1586
1587                         _w.synchServerTime();
1588
1589                 }
1590
1591         };
1592
1593 })();