text changes to registration mail content
[namibia] / public / js / app / brochure-workspace / customervaluation.js
1 ;(function(){
2
3     window._w.customervaluation = {
4
5         itemId : null,
6         itemData : null,
7         formMeta : null,
8         ti : null,
9         limited : false,
10         newItem : false,
11         validations : 0,
12         highestOfferId : false,
13         priceGuideId : false,
14         publicCentreList : {},
15         currentStep : 1,
16
17         onCustomerValuationViewTemplateReady : function( template, event, eventData )
18         {
19             _w.customervaluation.itemData = null;
20             _w.customervaluation.ti = _t[template];
21             _w.customervaluation.limited = false;
22
23             _w.customervaluation.setValuationViewFormMeta();
24             // Retrieve initial view data.
25             _w.customervaluation.itemId = 0;
26
27             _w.customervaluation.newItem = true;
28             _w.customervaluation.itemData = {stock:{}};
29         },
30
31         onCustomerValuationViewPublished : function( template, event, eventData )
32         {
33
34             App.DataStore.loadSelectListData(
35                 'years', false, 'Stock', 'Year.SelectList', null,
36                 {}
37             );
38             App.DataStore.loadSelectListData(
39                 'categories', false, 'Stock', 'Category.SelectList', null,
40                 {}
41             );
42             App.DataStore.loadSelectListData(
43                 'fuelTypes', false, 'Stock', 'FuelType.SelectList', null,
44                 {}
45             );
46             App.DataStore.loadSelectListData(
47                 'transmissionTypes', false, 'Stock', 'TransmissionType.SelectList', null,
48                 {}
49             );
50             App.DataStore.loadSelectListData(
51                 'fsh', false, 'Stock', 'FullServiceHistory.SelectList', null,
52                 {}
53             );
54
55             App.DataStore.loadSelectListData(
56                 'regions', false, 'Location', 'Region.SelectListNames', null,
57                 {"Filter": {
58                     "country"   : 1,
59                     "name"              : ['Gauteng','Western Cape','KwaZulu-Natal']
60                 }}
61             );
62
63             App.API.getTask(
64                 'publicCentreList', 'Company', 'Company.SelectGrid', null, {},
65                 $.proxy(function()
66                 {
67                     App.API.execTask(
68                         'publicCentreList', {}, {},
69                         $.proxy(function(response)
70                         {
71                             _w.customervaluation.publicCentreList = {};
72                             for (var i in response.Data.DataSet)
73                             {
74                                 var company = response.Data.DataSet[i];
75                                 if (undefined == _w.customervaluation.publicCentreList[company.city.region.id])
76                                 {
77                                     _w.customervaluation.publicCentreList[company.city.region.id] = [];
78                                 }
79                                 _w.customervaluation.publicCentreList[company.city.region.id].push({
80                                     value: company.id,
81                                     label: company.name
82                                 });
83                             }
84                         }, this),
85                         _w.taskExecError
86                     );
87                 }, this),
88                 _w.taskContractError
89             );
90
91
92
93             App.API.getTask(
94                 'createCustomerValuation', 'Valuation', 'Valuation.CreateCustomerPublic',
95                 null, {}, function(){}, _w.taskContractError
96             );
97
98             App.DataStore.setData('sparekeys', [{"value":"1", "label":"Yes"}, {"value":"0", "label":"No"}]);
99
100
101             // Do a check here for the ie browser - if ie show upload file buttons
102             // IE browsers prevent submitting files when the input field didn't receive a direct click
103
104             if(navigator.appVersion.indexOf("MSIE ")!=-1)
105             {
106                 $('#terms').parent().addClass("IE");
107             }
108
109
110
111             _w.customervaluation.limited = false;
112             _w.customervaluation._setupValidation();
113
114             $('.back-btn').on('click', function(){
115                 window.history.back();
116             });
117
118             $('#btnContinue1').click(function () {
119
120                 $('.btn-next').click();
121             });
122
123             $('#btnContinue2').click(function () {
124
125                 $('.btn-next').click();
126             });
127
128             $('#btnContinue3').click(function () {
129
130                 var rules1 = {
131                     firstName: {required: true, minlength: 3},
132                     familyName: {required: true, minlength: 3},
133                     email: {required: true, email: true},
134                     confirmEmail: {required: true, email: true, equalTo: "#email"},
135                     mobile: {required: true, mobile: true},
136                     region: {required: true},
137                     city: {required: false},
138                     publicCentre: {required: true},
139                     street: {required: false},
140                     requiredPrice: {required: true, number: true, min: 0.01},
141                     vehicleYear: "required",
142                     vehicleCategory: "required",
143                     vehicleMake: "required",
144                     vehicleModel: "required",
145                     vehicleType: "required",
146                     vehicleFuelType: "required",
147                     vehicleTransmissionType: "required",
148                     registrationNumber: "required",
149                     fullServiceHistory: "required",
150                     km: { required: true, digits: true }
151                 };
152                 $('#frmCustomerValuationView').validate({
153                     "rules": rules1,
154                     messages: {
155                         firstName: {
156                             minlength: "This field is required."
157                         },
158                         familyName: {
159                             minlength: "This field is required."
160                         },
161                         email: {
162                             required: "This field is required.",
163                             email: "Please enter a valid<br/>email address."
164                         },
165                         confirmEmail: {
166                             required: "This field is required.",
167                             email: "Please enter a valid<br/>email address.",
168                             equalTo: "Please enter the same<br/>value again."
169                         },
170                         requiredPrice: {
171                             required: "This field is required.",
172                             number: "Please enter a valid amount.",
173                             min: "Please enter a<br/>valid amount."
174                         },
175                         vehicleYear : "This field is required.",
176                         vehicleCategory : "This field is required.",
177                         vehicleMake : "This field is required.",
178                         vehicleModel : "This field is required.",
179                         vehicleType : "This field is required.",
180                         vehicleFuelType : "This field is required.",
181                         vehicleTransmissionType : "This field is required.",
182                         fullServiceHistory : "This field is required.",
183                         registrationNumber : "This field is required.",
184                         km : {required: "This field is required."}
185                     },
186                     invalidHandler: function(event, validator) {
187                         var errors = validator.numberOfInvalids();
188                         if (0 < _w.customervaluation.validations && errors) {
189                             var message = errors == 1
190                                 ? 'You missed 1 field. It has been highlighted'
191                                 : 'You missed ' + errors + ' fields. They have been highlighted';
192                             alert(message);
193                         }
194                         _w.customervaluation.validations++;
195                     },
196                     submitHandler: function( form ) {
197                         return;
198                     }
199                 });
200                 try {
201                     _w.customervaluation.validations = 0;
202                     var formValid = $('#frmCustomerValuationView').valid();
203
204                     if(formValid)
205                     {
206                         var confirmationOfvehicleYear = $('#vehicleYear option:selected').text();
207                         var confirmationOfcategory = $('#vehicleCategory option:selected').text();
208                         var confirmationOfmake = $('#vehicleMake option:selected').text();
209                         var confirmationOfmodel = $('#vehicleModel option:selected').text();
210                         var confirmationOftype = $('#vehicleType option:selected').text();
211                         var confirmationOffullServiceHistory = $('#fullServiceHistory option:selected').text();
212                         var confirmationOffuelType = $('#vehicleFuelType option:selected').text();
213                         var confirmationOftransmissionType = $('#vehicleTransmissionType option:selected').text();
214                         var confirmationOfkm = $('#km').val();
215                         var confirmationOfregistrationNumber = $('#registrationNumber').val();
216                         var confirmationOffirstName = $('#firstName').val();
217                         var confirmationOffamilyName = $('#familyName').val();
218                         var confirmationOfmobile = $('#mobile').val();
219                         var confirmationOfemail = $('#email').val();
220                         var confirmationOfregion = $('#region option:selected').text();
221                         var confirmationOfpublicCentre = $('#publicCentre option:selected').text();
222                         var confirmationOfrequiredPrice = $('#requiredPrice').val();
223
224                         //var confirmationOfInfoHtml =
225                         //    '<div class="span5 offset1">'
226                         //    + '<h4>Customer information:<h4>'
227                         //    + '<br>'
228                         //    + '<h5>' + confirmationOffirstName + ' ' + confirmationOffamilyName + '<h5>'
229                         //    + '<br>'
230                         //    + '<h5>' + confirmationOfmobile + '<h5>'
231                         //    + '<br>'
232                         //    + '<h5>' + confirmationOfemail + '<h5>'
233                         //    + '</div>'
234                         //    + '<div class="span6">'
235                         //    + '<h4>Vehicle Information:<h4>'
236                         //    + '<br>'
237                         //    + '<h5>' + confirmationOfvehicleYear + '<h5>'
238                         //    + '<h5>' + confirmationOfmake + '<h5>'
239                         //    + '<h5>' + confirmationOfmodel + '<h5>'
240                         //    + '<h5>' + confirmationOftype + '<h5>'
241                         //    + '<h5>' + confirmationOfkm + ' Kms<h5>'
242                         //    + '<h5>' + confirmationOfregistrationNumber + '<h5>'
243                         //    + '<h5>' + confirmationOffuelType + '<h5>'
244                         //    + '<h5>' + confirmationOftransmissionType + '<h5>'
245                         //    + '<br>'
246                         //    + '</div>';
247
248                         var confirmationOfInfoHtml =
249                             '<div class="span5">'
250                             + '<h4><u>Customer information:</u><h4>'
251                             + '<br>'
252                             + '<div class="span12">'
253                             + '<span class="text-h4 span4">Customer: </span>'
254                             + '<span class="text-h5 span8">' + confirmationOffirstName + ' ' + confirmationOffamilyName + '</span>'
255                             + '</div>'
256                             + '<div class="span12">'
257                             + '<span class="text-h4 span4">Cell Number: </span>'
258                             + '<span class="text-h5 span8">+27' + confirmationOfmobile + '</span>'
259                             + '</div>'
260                             + '<div class="span12">'
261                             + '<span class="text-h4 span4">Email: </span>'
262                             + '<span class="text-h5 span8">' + confirmationOfemail + '</span>'
263                             + '</div>'
264                             + '</div>'
265                             + '<div class="span6">'
266                             + '<h4><u>Vehicle Information:</u><h4>'
267                             + '<br>'
268                             + '<div class="span12">'
269                             + '<span class="text-h4 span4">Year: </span>'
270                             + '<span class="text-h5 span8">' + confirmationOfvehicleYear + '</span>'
271                             + '</div>'
272                             + '<div class="span12">'
273                             + '<span class="text-h4 span4">Make: </span>'
274                             + '<span class="text-h5 span8">' + confirmationOfmake + '</span>'
275                             + '</div>'
276                             + '<div class="span12">'
277                             + '<span class="text-h4 span4">Model: </span>'
278                             + '<span class="text-h5 span8">' + confirmationOfmodel + '</span>'
279                             + '</div>'
280                             + '<div class="span12">'
281                             + '<span class="text-h4 span4">Mileage: </span>'
282                             + '<span class="text-h5 span8">' + confirmationOfkm + '</span>'
283                             + '</div>'
284                             + '<div class="span12">'
285                             + '<span class="text-h4 span4">Registration: </span>'
286                             + '<span class="text-h5 span8">' + confirmationOfregistrationNumber + '</span>'
287                             + '</div>'
288                             + '<div class="span12">'
289                             + '<span class="text-h4 span4">Transmission: </span>'
290                             + '<span class="text-h5 span8">' + confirmationOftransmissionType + '</span>'
291                             + '</div>'
292                             + '<div class="span12">'
293                             + '<span class="text-h4 span4">Fuel Type: </span>'
294                             + '<span class="text-h5 span8">' + confirmationOffuelType + '</span>'
295                             + '</div>'
296                             + '<br>'
297                             + '</div>';
298
299
300                         $('#confirmationOfInfo').html(confirmationOfInfoHtml);
301
302                         $('.btn-next').click();
303                     }
304
305                 } catch(err) {}
306                 $('label.error').remove();
307                 $('.valid').removeClass('valid');
308
309
310             });
311
312             $('#terms').change($.proxy(function () {
313
314                 console.log('terms checked',$('#terms').is(':checked'));
315
316                 if($('#terms').is(':checked'))
317                 {
318                     $('#btnContinue4').prop('disabled', false);
319                 }
320                 else
321                 {
322                     $('#btnContinue4').prop('disabled', true);
323                 }
324             }, this));
325
326
327             $('#btnContinue4').click(function () {
328                 //$('#frmCustomerValuationView').submit();
329                 _w.customervaluation._setupValidation();
330
331                 var data = _w.customervaluation._prepValuationUpdateData();
332                 App.API.execTask(
333                     'createCustomerValuation', data, {},
334                     _w.customervaluation._onValuationUpdated,
335                     _w.customervaluation._onValuationFail
336                 );
337             });
338             $('.btn-prev').click($.proxy(function () {
339                 _w.customervaluation.currentStep--;
340             }, this));
341             $('.btn-next').click($.proxy(function () {
342                 $('.btn-next').prop('disabled', true);
343                 _w.customervaluation.currentStep++;
344                 _w.customervaluation.validatePage(_w.customervaluation.currentStep);
345             }, this));
346             $('.btn-next').prop('disabled', true);
347             $('#btnContinue1').prop('disabled', true);
348             $('#btnContinue4').prop('disabled', true);
349
350
351             _w.customervaluation.ti.hydrate(_w.customervaluation.formMeta);
352
353         },
354
355         validatePage : function(step)
356         {
357             var errors = false;
358
359             switch (step)
360             {
361                 case 1:
362                     var fields = {
363                         '#vehicleYear': true,
364                         '#vehicleCategory': true,
365                         '#vehicleMake': true,
366                         '#vehicleModel': true,
367                         '#vehicleType': true
368                     };
369                     break;
370                 case 2:
371                     var fields = {
372                         '#vehicleFuelType': true,
373                         '#vehicleTransmissionType': true,
374                         '#fullServiceHistory': true,
375                         '#km': true
376                     };
377                     break;
378                 case 3:
379                     var fields = {
380                         '#firstName': true,
381                         '#familyName': true,
382                         '#email': true,
383                         '#mobile': true,
384                         '#region': true,
385                         '#publicCentre': true
386                     };
387                     break;
388             }
389
390
391             for (var field in fields)
392             {
393                 if ('' !== $(field).val() && null !== $(field).val())
394                 {
395                     if (!$(field).valid())
396                     {
397                         $('.btn-next').prop('disabled', true);
398                         $('#btnContinue' + step).prop('disabled', true);
399                         return;
400                     }
401                 }
402                 else if (fields[field])
403                 {
404                     $('.btn-next').prop('disabled', true);
405                     $('#btnContinue' + step).prop('disabled', true);
406                     return;
407                 }
408
409             }
410
411             if(4 != step)
412             {
413                 $('.btn-next').prop('disabled', false);
414                 $('#btnContinue' + step).prop('disabled', false);
415             }
416
417             //$('.btn-next').prop('disabled', false);
418             //$('#btnContinue' + step).prop('disabled', false);
419
420         },
421
422         setValuationViewFormMeta : function()
423         {
424             data = {"stock":{}};
425             _w.customervaluation.formMeta = {
426                 'vehicleYear' : {
427                     type        : 'select',
428                     id          : 'vehicleYear',
429                     title       : 'Year',
430                     dataStoreId : 'years',
431                     value       : data.stock.vehicleYear ? data.stock.vehicleYear : '',
432                     onChange    : function (evt) {
433                         if (!App.DataStore.getItem('BuildSelect:vehicleYear', false))
434                         {
435                             _w.customervaluation._listMakesModelsTypes();
436                         }
437                         var value = $(evt.currentTarget).val();
438                         if (null == value || '' == value)
439                         {
440                             return;
441                         }
442                         _w.customervaluation.validatePage(1);
443                     }
444                 },
445                 'category' : {
446                     type        : 'select',
447                     id          : 'vehicleCategory',
448                     title       : 'Category',
449                     dataStoreId : 'categories',
450                     value       : data.stock.type ? data.stock.type.category.id : '',
451                     onChange    : function (evt) {
452                         if (!App.DataStore.getItem('BuildSelect:vehicleCategory', false))
453                         {
454                             _w.customervaluation._listMakesModelsTypes();
455                         }
456                         var value = $(evt.currentTarget).val();
457                         if (null == value || '' == value)
458                         {
459                             return;
460                         }
461                         _w.customervaluation.validatePage(1);
462                     }
463                 },
464                 'make' : {
465                     type        : 'select',
466                     id          : 'vehicleMake',
467                     title       : 'Vehicle Make',
468                     dataStoreId : 'makes',
469                     value       : data.stock.type ? data.stock.type.model.make.id : '',
470                     onChange    : function (evt) {
471                         if (!App.DataStore.getItem('BuildSelect:vehicleMake', false))
472                         {
473                             _w.customervaluation._listMakesModelsTypes();
474                         }
475                         var value = $(evt.currentTarget).val();
476                         if (null == value || '' == value)
477                         {
478                             return;
479                         }
480                         _w.customervaluation.validatePage(1);
481                     }
482                 },
483                 'model' : {
484                     type        : 'select',
485                     id          : 'vehicleModel',
486                     title       : 'Vehicle Model',
487                     dataStoreId : 'models',
488                     value       : data.stock.type ? data.stock.type.model.id : '',
489                     onChange    : function (evt) {
490                         if (!App.DataStore.getItem('BuildSelect:vehicleModel', false))
491                         {
492                             _w.customervaluation._listMakesModelsTypes();
493                         }
494                         var value = $(evt.currentTarget).val();
495                         if (null == value || '' == value)
496                         {
497                             return;
498                         }
499                         _w.customervaluation.validatePage(1);
500                     }
501                 },
502                 'type' : {
503                     type        : 'select',
504                     id          : 'vehicleType',
505                     title       : 'Vehicle Type',
506                     dataStoreId : 'types',
507                     value       : data.stock.type ? data.stock.type.id : '',
508                     onChange    : function (evt) {
509                        var value = $(evt.currentTarget).val();
510                         if (null == value || '' == value)
511                         {
512                             return;
513                         }
514                         App.API.getTask(
515                             'TransUnionFromMmCode', 'Stock', 'Utility.TransUnionFromMmCode', null,
516                             {}, _w.customervaluation._onTuReady, _w.taskContractError
517                         );
518                         App.API.getTask(
519                             'GetMmCode' + $(this).val(), 'Stock', 'Type.GetMmCode', null,
520                             {id:$(this).val()}, function(response){
521                                 $('#mmCode').html(response.Data.mmCode);
522                             }, _w.taskContractError
523                         );
524                         _w.customervaluation.validatePage(1);
525                     }
526                 },
527
528
529                 'fuelType' : {
530                     type        : 'select',
531                     id          : 'vehicleFuelType',
532                     title       : 'Fuel Type',
533                     dataStoreId : 'fuelTypes',
534                     value       : data.stock.fuelType ? data.stock.fuelType : '',
535                     onChange    : function (evt) {
536                         var value = $(evt.currentTarget).val();
537                         if (null == value || '' == value)
538                         {
539                             return;
540                         }
541                         _w.customervaluation.validatePage(2);
542                     }
543                 },
544                 'transmissionType' : {
545                     type        : 'select',
546                     id          : 'vehicleTransmissionType',
547                     title       : 'Transmission Type',
548                     dataStoreId : 'transmissionTypes',
549                     value       : data.stock.transmissionType ? data.stock.transmissionType : '',
550                     onChange    : function (evt) {
551                         var value = $(evt.currentTarget).val();
552                         if (null == value || '' == value)
553                         {
554                             return;
555                         }
556                         _w.customervaluation.validatePage(2);
557                     }
558                 },
559                 'registrationNumber' : {
560                     type  : 'input',
561                     id    : 'registrationNumber',
562                     title : 'Registration no',
563                     value : data.stock.registrationNumber ? data.stock.registrationNumber : '',
564                     style       : 'capitalise',
565                     onChange    : function (evt) {
566                         var value = $(evt.currentTarget).val();
567                         if (null == value || '' == value)
568                         {
569                             return;
570                         }
571                         _w.customervaluation.validatePage(2);
572                     }
573                 },
574                 'km' : {
575                     type  : 'input',
576                     id    : 'km',
577                     title : 'Kms',
578                     value : data.stock.km ? data.stock.km : '',
579                     onChange    : function (evt) {
580                         var value = $(evt.currentTarget).val();
581                         if (null == value || '' == value)
582                         {
583                             return;
584                         }
585                         _w.customervaluation.validatePage(2);
586                     },
587                     onKeyUp : function (evt) { _w.customervaluation.validatePage(2);}
588                 },
589                 'fullServiceHistory' : {
590                     type        : 'select',
591                     id          : 'fullServiceHistory',
592                     title       : 'Full service history',
593                     dataStoreId : 'fsh',
594                     value       : data.stock.fullServiceHistory ? data.stock.fullServiceHistory : '',
595                     onChange    : function (evt) {
596                         var value = $(evt.currentTarget).val();
597                         if (null == value || '' == value)
598                         {
599                             return;
600                         }
601                         _w.customervaluation.validatePage(2);
602                     }
603                 },
604
605                 'firstName' : {
606                     type  : 'input',
607                     id    : 'firstName',
608                     title : 'Name',
609                     value : data.firstName ? data.firstName : '',
610                     onChange    : function (evt) {
611                         var value = $(evt.currentTarget).val();
612                         if (null == value || '' == value)
613                         {
614                             return;
615                         }
616                         _w.customervaluation.validatePage(3);
617                     }
618                 },
619                 'familyName' : {
620                     type  : 'input',
621                     id    : 'familyName',
622                     title : 'Surname',
623                     value : data.familyName ? data.familyName : '',
624                     onChange    : function (evt) {
625                         var value = $(evt.currentTarget).val();
626                         if (null == value || '' == value)
627                         {
628                             return;
629                         }
630                         _w.customervaluation.validatePage(3);
631                     }
632                 },
633                 'email' : {
634                     type  : 'input',
635                     id    : 'email',
636                     title : 'Email address',
637                     value : data.email ? data.email : '',
638                     onChange    : function (evt) {
639                         var value = $(evt.currentTarget).val();
640                         if (null == value || '' == value)
641                         {
642                             return;
643                         }
644                         _w.customervaluation.validatePage(3);
645                     }
646                 },
647                 'confirmEmail' : {
648                     type  : 'input',
649                     id    : 'confirmEmail',
650                     title : 'Confirm email address',
651                     value : '',
652                     maxlength : 255
653                 },
654                 'mobile' : {
655                     type  : 'mobile',
656                     id    : 'mobile',
657                     title : 'Mobile',
658                     value : data.mobile ? data.mobile : '',
659                     onChange    : function (evt) {
660                         var value = $(evt.currentTarget).val();
661                         if (null == value || '' == value)
662                         {
663                             return;
664                         }
665                         _w.customervaluation.validatePage(3);
666                     }
667                 },
668                 'region' : {
669                     type        : 'select',
670                     id          : 'region',
671                     title       : 'Region',
672                     dataStoreId : 'regions',
673                     value       : '',
674                     onChange    : $.proxy(function (evt) {
675                         if (!App.DataStore.getItem('BuildSelect:region', false))
676                         {
677                             /*App.DataStore.loadSelectListData(
678                              'cities', false, 'Location', 'Town.SelectList', null,
679                              {"Filter": {
680                              "region" : $(this).val()
681                              }}
682                              );*/
683
684                             var dataList = undefined != _w.customervaluation.publicCentreList[$('#region').val()]
685                                 ? _w.customervaluation.publicCentreList[$('#region').val()]
686                                 : [];
687                             App.DataStore.setData('publicCentre', dataList);
688                             if ($('#sameAsBusinessAddress').prop('checked'))
689                             {
690                                 $('#billingRegion').selectpicker('val', $('#region').val());
691                             }
692
693                             var value = $(evt.currentTarget).val();
694                             if (null == value || '' == value)
695                             {
696                                 return;
697                             }
698                             _w.customervaluation.validatePage(3);
699                         }
700                     }, this)
701                 },
702                 'city' : {
703                     type        : 'select',
704                     id          : 'city',
705                     title       : 'Town',
706                     dataStoreId : 'cities',
707                     value       : '',
708                     onChange    : function (evt) {
709                         var value = $(evt.currentTarget).val();
710                         if (null == value || '' == value)
711                         {
712                             return;
713                         }
714                         _w.customervaluation.validatePage(3);
715                     }
716                 },
717
718                 'publicCentre' : {
719                     type        : 'select',
720                     id          : 'publicCentre',
721                     title       : 'Public Center',
722                     dataStoreId : 'publicCentre',
723                     value       : '',
724                     onChange    : function (evt) {
725                         var value = $(evt.currentTarget).val();
726                         if (null == value || '' == value)
727                         {
728                             return;
729                         }
730                         _w.customervaluation.validatePage(3);
731                     }
732                 },
733
734
735                 'street' : {
736                     type  : 'input',
737                     id    : 'street',
738                     title : 'Street',
739                     value : '',
740                     maxlength : 75,
741                     onChange    : function (evt) {
742                         var value = $(evt.currentTarget).val();
743                         if (null == value || '' == value)
744                         {
745                             return;
746                         }
747                         _w.customervaluation.validatePage(3);
748                     }
749                 },
750                 'requiredPrice' : {
751                     type  : 'input',
752                     style : 'rand-input',
753                     id    : 'requiredPrice',
754                     title : 'Requested Price',
755                     value : '',
756                     onChange    : function (evt) {
757                         var value = $(evt.currentTarget).val();
758                         if (null == value || '' == value)
759                         {
760                             return;
761                         }
762                         _w.customervaluation.validatePage(3);
763                     }
764                 }
765             };
766         },
767
768         _listMakesModelsTypes : function( fromItemData, callback )
769         {
770             if (fromItemData)
771             {
772                 var year     = _w.customervaluation.itemData.stock.vehicleYear;
773                 var category = _w.customervaluation.itemData.stock.type.category.id;
774                 var make     = _w.customervaluation.itemData.stock.type.model.make.id;
775                 var model    = _w.customervaluation.itemData.stock.type.model.id;
776                 var type     = _w.customervaluation.itemData.stock.type.id;
777             }
778             else
779             {
780                 var year = $('#vehicleYear').val();
781                 var category = $('#vehicleCategory').val();
782                 var make = $('#vehicleMake').val();
783                 var model = $('#vehicleModel').val();
784                 var type = $('#vehicleType').val();
785                 var mmcode = $('#mmCode').val();
786             }
787             if (year && category)
788             {
789                 App.DataStore.loadSelectListData(
790                     'makes', false, 'Stock', 'Utility.ListMakes', null,
791                     {"Filter": {
792                         "category": category,
793                         "year": year
794                     }}, {}, $.proxy(function (categoryId)
795                     {
796                         if (8 == categoryId)
797                         {
798                             var data = App.DataStore.getData('makes');
799                             data.unshift({
800                                 value: -1,
801                                 label: 'Custom'
802                             });
803                             App.DataStore.setData('makes', data);
804                         }
805                     }, this, category)
806                 );
807             }
808             if (make && year && category)
809             {
810                 App.DataStore.loadSelectListData(
811                     'models', false, 'Stock', 'Utility.ListModels', null,
812                     {"Filter": {
813                         "make": make,
814                         "category": category,
815                         "year": year
816                     }}, {}, $.proxy(function (categoryId)
817                     {
818                         if (8 == categoryId)
819                         {
820                             var data = App.DataStore.getData('models');
821                             data.unshift({
822                                 value: -1,
823                                 label: 'Custom'
824                             });
825                             App.DataStore.setData('models', data);
826                         }
827                     }, this, category)
828                 );
829             }
830             if (model && year && category)
831             {
832                 App.DataStore.loadSelectListData(
833                     'types', false, 'Stock', 'Utility.ListTypes', null,
834                     {"Filter": {
835                         "model": model,
836                         "category": category,
837                         "year": year
838                     }}, {}, $.proxy(function (categoryId, callback)
839                     {
840                         if (8 == categoryId)
841                         {
842                             var data = App.DataStore.getData('types');
843                             data.unshift({
844                                 value: -1,
845                                 label: 'Custom'
846                             });
847                             App.DataStore.setData('types', data);
848                         }
849                         if (callback)
850                         {
851                             callback();
852                         }
853                     }, this, category, callback)
854                 );
855             }
856         },
857
858         _setupValidation : function ()
859         {
860             var rules = {
861                 firstName: {required: true, minlength: 3},
862                 familyName: {required: true, minlength: 3},
863                 email: {required: true, email: true},
864                 confirmEmail: {required: true, email: true, equalTo: "#email"},
865                 mobile: {required: true, mobile: true},
866                 region: {required: true},
867                 city: {required: false},
868                 publicCentre: {required: true},
869                 street: {required: false},
870                 requiredPrice: {required: false, number: true, min: 0.01},
871                 vehicleYear: "required",
872                 vehicleCategory: "required",
873                 vehicleMake: "required",
874                 vehicleModel: "required",
875                 vehicleType: "required",
876                 vehicleFuelType: "required",
877                 vehicleTransmissionType: "required",
878                 registrationNumber: "required",
879                 fullServiceHistory: "required",
880                 km: { required: true, digits: true }
881             };
882             $('#frmCustomerValuationView').validate({
883                 "rules": rules,
884                 messages: {
885                     firstName: {
886                         minlength: "This field is required."
887                     },
888                     familyName: {
889                         minlength: "This field is required."
890                     },
891                     email: {
892                         required: "This field is required.",
893                         email: "Please enter a valid<br/>email address."
894                     },
895                     confirmEmail: {
896                         required: "This field is required.",
897                         email: "Please enter a valid<br/>email address.",
898                         equalTo: "Please enter the same<br/>value again."
899                     },
900                     requiredPrice: {
901                         required: "This field is required.",
902                         number: "Please enter a valid amount.",
903                         min: "Please enter a<br/>valid amount."
904                     },
905                     vehicleYear : "This field is required.",
906                     vehicleCategory : "This field is required.",
907                     vehicleMake : "This field is required.",
908                     vehicleModel : "This field is required.",
909                     vehicleType : "This field is required.",
910                     vehicleFuelType : "This field is required.",
911                     vehicleTransmissionType : "This field is required.",
912                     fullServiceHistory : "This field is required.",
913                     registrationNumber : "This field is required.",
914                     km : {required: "This field is required."}
915                 },
916                 invalidHandler: function(event, validator) {
917                     var errors = validator.numberOfInvalids();
918                     if (0 < _w.customervaluation.validations && errors) {
919                         var message = errors == 1
920                             ? 'You missed 1 field. It has been highlighted'
921                             : 'You missed ' + errors + ' fields. They have been highlighted';
922                         alert(message);
923                     }
924                     _w.customervaluation.validations++;
925                 },
926                 submitHandler: function( form ) {
927                     return;
928                 }
929             });
930             try {
931                 _w.customervaluation.validations = 0;
932                 $('#frmCustomerValuationView').valid();
933             } catch(err) {}
934             $('label.error').remove();
935             $('.valid').removeClass('valid');
936         },
937
938         _onTuReady : function ()
939         {
940             App.API.execTask(
941                 'TransUnionFromMmCode', {
942                     Filter : {
943                         vehicleYear : parseInt($('#vehicleYear').val()),
944                         type : parseInt($('#vehicleType').val())
945                     }
946                 }, {},
947                 _w.customervaluation._onTuReceived, function(){alert('There were no trade and retail from TransUnion');}
948             );
949         },
950
951         _onTuReceived : function ( response )
952         {
953             if ('Success' != response.Data.Status)
954             {
955                 alert(response.Data.Status);
956                 return;
957             }
958             var vals = response.Data.VehicleDetails[0].Value;
959             $('#retailPrice').val(vals.RetailPrice);
960             $('#tradePrice').val(vals.TradePrice);
961             $('#listPrice').val(vals.NewPrice);
962             $('#' + _w.customervaluation.ti.tid + '_retailPrice').html('R ' + vals.RetailPrice + '.00');
963             $('#' + _w.customervaluation.ti.tid + '_tradePrice').html('R ' + vals.TradePrice + '.00');
964             $('#' + _w.customervaluation.ti.tid + '_listPrice').html('R ' + vals.NewPrice + '.00');
965         },
966
967         _prepValuationUpdateData : function()
968         {
969             function isValidEmailAddress(emailAddress) {
970                 var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
971                 return pattern.test(emailAddress);
972             };
973
974             var data = _w.customervaluation.ti.harvest();
975             data.km = '' == data.km
976                 ? 0
977                 : parseInt(data.km);
978             //data.retailPrice = 0.00;
979             //data.tradePrice = 0.00;
980             //data.listPrice = 0.00;
981             var retailPrice = $('#retailPrice').val();
982             var tradePrice = $('#tradePrice').val();
983             var listPrice = $('#listPrice').val();
984
985             data.retailPrice = ('' == retailPrice)
986                 ? 0.00
987                 : parseFloat(retailPrice);
988             data.tradePrice = ('' == tradePrice)
989                 ? 0.00
990                 : parseFloat(tradePrice);
991             data.listPrice = ('' == listPrice)
992                 ? 0.00
993                 : parseFloat(listPrice);
994
995             delete data.damageTotal;
996
997
998             var postData = {Stock:{}, Valuation:{}};
999             var valuationFields = [
1000                 "firstName","familyName","mobile",
1001                 "email","region","city","street","requiredPrice"," publicCentre"
1002             ];
1003             for (var field in data)
1004             {
1005                 if (-1 == valuationFields.indexOf(field))
1006                 {
1007                     postData.Stock[field] = data[field];
1008                 }
1009                 else
1010                 {
1011                     postData.Valuation[field] = data[field];
1012                 }
1013             }
1014
1015
1016             if('' == postData.Valuation.requiredPrice)
1017             {
1018                 postData.Valuation.requiredPrice = 0;
1019             }
1020
1021             if( !isValidEmailAddress( postData.Valuation.email ) )
1022             {
1023                 testEmail = postData.Valuation.email;
1024                 console.log("went through check", testEmail);
1025                 postData.Valuation.email = null;
1026             }
1027
1028
1029             if ('' == postData.Valuation.email)
1030             {
1031                 postData.Valuation.email = null;
1032             }
1033
1034             return postData;
1035         },
1036
1037         _onValuationUpdated : function( response )
1038         {
1039             //alert('Your vehicle has successfully been submitted.');
1040             window.location.hash = '/thankyouvaluation';
1041
1042             //send customer valuation email
1043         },
1044         _onValuationFail : function( response )
1045         {
1046             console.log('response',response );
1047             alert('Oops, something went wrong, please check your data carefully and try again.');
1048             //window.location.hash = '/public-landing';
1049         }
1050
1051
1052
1053     };
1054
1055     //this is for refreshing dropdowns if no data is populated on first load
1056 //    setTimeout(
1057 //        $.proxy(function(){
1058 ////                                            console.log('PageContent', $('#PageContent').children().length);
1059 ////            if ( $('#vehicleYear').children().length == 0 ) {
1060 ////                console.log ('PageContent no Year data - running task again...');
1061 ////                _w.customervaluation.setValuationViewFormMeta();
1062 ////            }
1063 ////
1064 ////            if ( $('#vehicleCategory').children().length == 0 ) {
1065 ////                _w.customervaluation.setValuationViewFormMeta();
1066 ////            }
1067 ////
1068 ////
1069 //            if ( $('#publicCenter').children().length == 0 ) {
1070 //                console.log ('PageContent no publicCenter data - running task again...');
1071 //                _w.customervaluation.setValuationViewFormMeta();
1072 //            }
1073 //
1074 //            if ( $('#vehicleYear').children().length == 0 ) {
1075 //                App.DataStore.loadSelectListData(
1076 //                    'years', false, 'Stock', 'Year.SelectList', null,
1077 //                    {}
1078 //                );
1079 //            }
1080 //            if ( $('#vehicleCategory').children().length == 0 ) {
1081 //                App.DataStore.loadSelectListData(
1082 //                    'categories', false, 'Stock', 'Category.SelectList', null,
1083 //                    {}
1084 //                );
1085 //            }
1086 //            if ( $('#fullServiceHistory').children().length == 0 ) {
1087 //                App.DataStore.loadSelectListData(
1088 //                    'fsh', false, 'Stock', 'FullServiceHistory.SelectList', null,
1089 //                    {}
1090 //                );
1091 //            }
1092 //            if ( $('#vehicleFuelType').children().length == 0 ) {
1093 //                App.DataStore.loadSelectListData(
1094 //                    'fuelTypes', false, 'Stock', 'FuelType.SelectList', null,
1095 //                    {}
1096 //                );
1097 //            }
1098 //            if ( $('#vehicleTransmissionType').children().length == 0 ) {
1099 //                App.DataStore.loadSelectListData(
1100 //                    'transmissionTypes', false, 'Stock', 'TransmissionType.SelectList', null,
1101 //                    {}
1102 //                );
1103 //            }
1104 //        }, this), 5000
1105 //    );
1106
1107     //setTimeout(
1108     //    $.proxy(function(){
1109     //        if ( $('#vehicleYear').children().length == 0 ) {
1110     //            location.reload();
1111     //            console.log('reload');
1112     //            alert('reload');
1113     //        }
1114     //    }, this), 10000
1115     //);
1116 })();