initial commit
[namibia] / public / js / app / brochure-workspace / publicvaluation.js
1 ;(function(){
2
3         window._w.publicvaluation = {
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
16                 onValuationViewTemplateReady : function( template, event, eventData )
17                 {
18                         _w.publicvaluation.itemData = null;
19                         _w.publicvaluation.ti = _t[template];
20                         _w.publicvaluation.limited = false;
21
22
23                         App.API.getTask(
24                                         'publicCentreList', 'Company', 'Company.SelectGrid', null, {},
25                                         $.proxy(function()
26                                         {
27                                                 App.API.execTask(
28                                                                 'publicCentreList', {}, {},
29                                                                 $.proxy(function(response)
30                                                                 {
31                                                                         _w.publicvaluation.publicCentreList = {};
32                                                                         for (var i in response.Data.DataSet)
33                                                                         {
34                                                                                 var company = response.Data.DataSet[i];
35                                                                                 if (undefined == _w.publicvaluation.publicCentreList[company.city.region.id])
36                                                                                 {
37                                                                                         _w.publicvaluation.publicCentreList[company.city.region.id] = [];
38                                                                                 }
39                                                                                 _w.publicvaluation.publicCentreList[company.city.region.id].push({
40                                                                                         value: company.id,
41                                                                                         label: company.name
42                                                                                 });
43                                                                         }
44                                                                 }, this),
45                                                                 _w.taskExecError
46                                                         );
47                                         }, this),
48                                         _w.taskContractError
49                                         );
50
51                         App.DataStore.setData('sparekeys', [{"value":"1", "label":"Yes"}, {"value":"0", "label":"No"}]);
52                         App.DataStore.loadSelectListData('categories', false, 'Stock', 'Category.SelectList', null, {}, {}, function(){
53                                 var data = App.DataStore.getData('categories');
54                                 data.splice(data.length - 1, 1);
55                                 App.DataStore.setData('categories', data);
56                         });
57                         App.DataStore.loadSelectListGroup([
58                {dataId: 'years', isStatic: true, workspace: 'Stock', task: 'Year.SelectList'},
59                {dataId: 'conditions', isStatic: true, workspace: 'Stock', task: 'Condition.SelectList'},
60                {dataId: 'exteriorColours', isStatic: true, workspace: 'Stock', task: 'ExteriorColour.SelectList'},
61                {dataId: 'interiorColours', isStatic: true, workspace: 'Stock', task: 'InteriorColour.SelectList'},
62                {dataId: 'upholsteries', isStatic: true, workspace: 'Stock', task: 'Upholstery.SelectList'},
63                {dataId: 'papers', isStatic: true, workspace: 'Stock', task: 'Paper.SelectList'},
64                {dataId: 'natis', isStatic: true, workspace: 'Stock', task: 'Natis.SelectList'},
65                {dataId: 'fsh', isStatic: true, workspace: 'Stock', task: 'FullServiceHistory.SelectList'},
66                {dataId: 'fuelTypes', isStatic: true, workspace: 'Stock', task: 'FuelType.SelectList'},
67                {dataId: 'transmissionTypes', isStatic: true, workspace: 'Stock', task: 'TransmissionType.SelectList'},
68                {dataId: 'damages', isStatic: true, workspace: 'Stock', task: 'Damage.SelectList'},
69                {dataId: 'accessories', isStatic: true, workspace: 'Stock', task: 'Accessory.SelectList'}
70             ]);
71                         App.DataStore.loadSelectListData(
72                                         'regions', false, 'Location', 'Region.SelectListNames', null,
73                                         {"Filter": {
74                                                 "country"       : 1,
75                                                 "name"          : ['Gauteng','Western Cape','KwaZulu-Natal']
76                                                 }}
77                                         );
78
79
80                         _w.publicvaluation.setValuationViewFormMeta();
81                         _w.publicvaluation.ti.hydrate(_w.publicvaluation.formMeta);
82                         // Retrieve initial view data.
83                         _w.publicvaluation.itemId = 0;
84
85                         _w.publicvaluation.newItem = true;
86                         _w.publicvaluation.itemData = {stock:{}};
87                         App.API.getTask(
88                                 'createPublicValuation', 'Valuation', 'Valuation.CreatePublic',
89                                 null, {}, function(){}, _w.taskContractError
90                         );
91                 },
92
93                 onValuationViewPublished : function( template, event, eventData )
94                 {
95
96                         // Do a check here for the ie browser - if ie show upload file buttons
97                         // IE browsers prevent submitting files when the input field didn't receive a direct click
98
99                          if(navigator.appVersion.indexOf("MSIE ")!=-1)
100                          {
101                                  //alert( "This is msie IE!" );
102                                  // Main
103                                  $("#btn_mainImage").show();
104                                  $("#mainImage").show();
105
106                                  // Front
107                                  $("#btn_frontImage").show();
108                                  $("#frontImage").show();
109
110                                  // Left
111                                  $("#btn_leftImage").show();
112                                  $("#leftImage").show();
113
114                                  // Right
115                                  $("#btn_rightImage").show();
116                                  $("#rightImage").show();
117
118                                  // Back
119                                  $("#btn_backImage").show();
120                                  $("#backImage").show();
121
122                                  // Interior
123                                  $("#btn_interiorImage").show();
124                                  $("#interiorImage").show();
125
126                                  // Engine
127                                  $("#btn_engineImage").show();
128                                  $("#engineImage").show();
129
130                                  // Copy Of Natis
131                                  $("#btn_natisImage").show();
132                                  $("#natisImage").show();
133
134                                  // accept text IE
135                                  $(".acceptText").hide();
136                                  $(".acceptTextIE").show();
137
138                                  $('#terms').parent().addClass("IE");
139                          }
140
141
142                         _w.publicvaluation.limited = false;
143                         _w.publicvaluation._setupValidation();
144                         $('#terms').change(function() {
145                                 $('#btnSaveNewValuation').prop('disabled', !$(this).is(':checked'));
146                         });
147                         $('#btnSaveNewValuation').prop('disabled', true);
148                         $('#btnSaveNewValuation').show();
149                         $('#btnSaveNewValuation').click(function () {
150                                 $('#btnSaveNewValuation').prop('disabled', true);
151                                 $('#terms').prop('disabled', true);
152                                 if (!$('#frmValuationView').valid())
153                                 {
154                                         $('#btnSaveNewValuation').prop('disabled', false);
155                                         return;
156                                 }
157
158                                 //Check for mobi photo's
159                                 // the IE doesn't support matchMedia ...
160                                 var isMobile = 'none' == $('#customerTitle').css('display');
161
162                                 // check for IE as well // if (!isMobile.matches && !navigator.appVersion.indexOf("MSIE ")!=-1 )
163                                 if (!isMobile )
164                                 {
165                                         var data = _w.publicvaluation.ti.harvest();
166                                         var images = 8;
167                                         if ('' == data.mainImage) { images--; }
168                                         if ('' == data.leftImage) { images--; }
169                                         if ('' == data.backImage) { images--; }
170                                         if ('' == data.engineImage) { images--; }
171                                         if ('' == data.frontImage) { images--; }
172                                         if ('' == data.rightImage) { images--; }
173                                         if ('' == data.interiorImage) { images--; }
174                                         if ('' == data.natisImage) { images--; }
175
176                                         if (images < 2)
177                                         {
178                                                 $('#btnSaveNewValuation').prop('disabled', false);
179                                                 alert('Please upload at least 2 images.');
180                                                 return;
181                                         }
182                                  }
183
184                                 var data = _w.publicvaluation._prepValuationUpdateData();
185                                 App.API.execTask(
186                                         'createPublicValuation', data, {},
187                                         _w.publicvaluation._onValuationUpdated,
188                                         _w.publicvaluation._onValuationFail
189                                 );
190                         });
191
192                         $('.back-btn').on('click', function(){
193                                 window.history.back();
194                         });
195                 },
196
197                 setValuationViewFormMeta : function()
198                 {
199                         data = {"stock":{}};
200                                 _w.publicvaluation.formMeta = {
201                                 'vehicleYear' : {
202                                         type        : 'select',
203                                         id          : 'vehicleYear',
204                                         title       : 'Year',
205                                         dataStoreId : 'years',
206                                         value       : data.stock.vehicleYear ? data.stock.vehicleYear : '',
207                                         onChange    : function () {
208                                                 if (!App.DataStore.getItem('BuildSelect:vehicleYear', false))
209                                                 {
210                                                         _w.publicvaluation._listMakesModelsTypes();
211                                                 }
212                                         }
213                                 },
214                                 'category' : {
215                                         type        : 'select',
216                                         id          : 'vehicleCategory',
217                                         title       : 'Category',
218                                         dataStoreId : 'categories',
219                                         value       : data.stock.type ? data.stock.type.category.id : '',
220                                         onChange    : function () {
221                                                 if (!App.DataStore.getItem('BuildSelect:vehicleCategory', false))
222                                                 {
223                                                         _w.publicvaluation._listMakesModelsTypes();
224                                                 }
225                                         }
226                                 },
227                                 'make' : {
228                                         type        : 'select',
229                                         id          : 'vehicleMake',
230                                         title       : 'Vehicle Make',
231                                         dataStoreId : 'makes',
232                                         value       : data.stock.type ? data.stock.type.model.make.id : '',
233                                         onChange    : function () {
234                                                 if (!App.DataStore.getItem('BuildSelect:vehicleMake', false))
235                                                 {
236                                                         _w.publicvaluation._listMakesModelsTypes();
237                                                 }
238                                         }
239                                 },
240                                 'model' : {
241                                         type        : 'select',
242                                         id          : 'vehicleModel',
243                                         title       : 'Vehicle Model',
244                                         dataStoreId : 'models',
245                                         value       : data.stock.type ? data.stock.type.model.id : '',
246                                         onChange    : function () {
247                                                 if (!App.DataStore.getItem('BuildSelect:vehicleModel', false))
248                                                 {
249                                                         _w.publicvaluation._listMakesModelsTypes();
250                                                 }
251                                         }
252                                 },
253                                 'type' : {
254                                         type        : 'select',
255                                         id          : 'vehicleType',
256                                         title       : 'Vehicle Type',
257                                         dataStoreId : 'types',
258                                         value       : data.stock.type ? data.stock.type.id : ''
259                                 },
260                                 'fuelType' : {
261                                         type        : 'select',
262                                         id          : 'vehicleFuelType',
263                                         title       : 'Fuel Type',
264                                         dataStoreId : 'fuelTypes',
265                                         value       : data.stock.fuelType ? data.stock.fuelType : ''
266                                 },
267                                 'transmissionType' : {
268                                         type        : 'select',
269                                         id          : 'vehicleTransmissionType',
270                                         title       : 'Transmission Type',
271                                         dataStoreId : 'transmissionTypes',
272                                         value       : data.stock.transmissionType ? data.stock.transmissionType : ''
273                                 },
274                                 'registrationNumber' : {
275                                         type  : 'input',
276                                         id    : 'registrationNumber',
277                                         title : 'Registration no',
278                                         value : data.stock.registrationNumber ? data.stock.registrationNumber : '',
279                                         style   : 'capitalise'
280                                 },
281                                 'vinNumber' : {
282                                         type  : 'input',
283                                         id    : 'vinNumber',
284                                         title : 'Vin no',
285                                         value : data.stock.vinNumber ? data.stock.vinNumber : '',
286                                         style   : 'capitalise'
287                                 },
288                                 'engineNumber' : {
289                                         type  : 'input',
290                                         id    : 'engineNumber',
291                                         title : 'Eng. no',
292                                         value : data.stock.engineNumber ? data.stock.engineNumber : '',
293                                         style   : 'capitalise'
294                                 },
295                                 'km' : {
296                                         type  : 'input',
297                                         id    : 'km',
298                                         title : 'Kms',
299                                         value : data.stock.km ? data.stock.km : ''
300                                 },
301                                 'condition' : {
302                                         type        : 'select',
303                                         id          : 'vehicleCondition',
304                                         title       : 'Condition',
305                                         dataStoreId : 'conditions',
306                                         value       : data.stock.condition ? data.stock.condition : ''
307                                 },
308                                 'exteriorColour' : {
309                                         type        : 'select',
310                                         id          : 'exteriorColour',
311                                         title       : 'Main ext. colour',
312                                         dataStoreId : 'exteriorColours',
313                                         value       : data.stock.exteriorColour ? data.stock.exteriorColour : ''
314                                 },
315                                 'interiorColour' : {
316                                         type  : 'select',
317                                         id    : 'interiorColour',
318                                         title : 'Main int. colour',
319                                         dataStoreId : 'interiorColours',
320                                         value : data.stock.interiorColour ? data.stock.interiorColour : ''
321                                 },
322                                 'upholstery' : {
323                                         type        : 'select',
324                                         id          : 'vehicleUpholstery',
325                                         title       : 'Upholstery',
326                                         dataStoreId : 'upholsteries',
327                                         value       : data.stock.upholstery ? data.stock.upholstery : ''
328                                 },
329                                 'papers' : {
330                                         type        : 'select',
331                                         id          : 'vehiclePapers',
332                                         title       : 'Papers',
333                                         dataStoreId : 'papers',
334                                         value       : data.stock.papers ? data.stock.papers : ''
335                                 },
336                                 'natis' : {
337                                         type        : 'select',
338                                         id          : 'vehicleNatis',
339                                         title       : 'Natis',
340                                         dataStoreId : 'natis',
341                                         value       : data.stock.natis ? data.stock.natis : ''
342                                 },
343                                 'spareKeys' : {
344                                         type        : 'select',
345                                         id          : 'spareKeys',
346                                         title       : 'Spare keys',
347                                         dataStoreId : 'sparekeys',
348                                         value       : undefined != data.stock.spareKeys && null != data.stock.spareKeys
349                                                                         ? (data.stock.spareKeys == true ? "1" : "0")
350                                                                         : null
351                                 },
352                                 'fullServiceHistory' : {
353                                         type        : 'select',
354                                         id          : 'fullServiceHistory',
355                                         title       : 'Full service history',
356                                         dataStoreId : 'fsh',
357                                         value       : data.stock.fullServiceHistory ? data.stock.fullServiceHistory : ''
358                                 },
359                                 'fshNotes' : {
360                                         type  : 'textarea-small',
361                                         id    : 'fshNotes',
362                                         title : 'FSH notes',
363                                         value : data.stock.fshNotes ? data.stock.fshNotes : ''
364                                 },
365                                 'damages' : {
366                                         type        : 'damageDetails',
367                                         disabled        : false,
368                                         id          : 'damages',
369                                         dataStoreId : 'damages',
370                                         data        : data.stock.damages ? data.stock.damages : {}
371                                 },
372                                 'damageTotal' : 'R 0.00',
373                                 'damageNotes' : {
374                                         type  : 'textarea-styled',
375                                         disabled        : false,
376                                         id    : 'damageNotes',
377                                         title : 'Damages comments: i.e. Scratches, Dents, Paintwork, etc.',
378                                         style : 'damages-comments1',
379                                         value : data.stock.damageNotes ? data.stock.damageNotes : ''
380                                 },
381                                 'previousRepairsNoted' : {
382                                         type  : 'checkbox',
383                                         id    : 'previousRepairsNoted',
384                                         title : 'Previous repairs',
385                                         style : 'damages-comments',
386                                         value : data.stock.previousRepairsNoted ? data.stock.previousRepairsNoted : false
387                                 },
388                                 'previousRepairsNotes' : {
389                                         type  : 'textarea-styled',
390                                         disabled        : false,
391                                         id    : 'previousRepairsNotes',
392                                         title : 'Previous repairs notes',
393                                         style : 'damages-comments',
394                                         value : data.stock.previousRepairsNotes ? data.stock.previousRepairsNotes : '',
395                                         onChange: function(){
396                                                 var haveData = ('' != $(this).val() ? true : false);
397                                                 $('#previousRepairsNoted').prop('checked', haveData);
398                                         }
399                                 },
400                                 'accessories' : {
401                                         type        : 'checkGroup',
402                                         id          : 'accessories',
403                                         dataStoreId : 'accessories',
404                                         itemName    : 'accessory',
405                                         data        : data.stock.accessories ? data.stock.accessories : {}
406                                 },
407                                 'accessoryNotes' : {
408                                         type      : 'textarea-styled',
409                                         id        : 'accessoryNotes',
410                                         title     : 'Comments',
411                                         style     : 'comment mobile-comment',
412                                         areaStyle : 'comment-text-box',
413                                         value     : data.stock.accessoryNotes ? data.stock.accessoryNotes : ''
414                                 },
415                                 'mainImage' : {
416                                         type  : 'image',
417                                         id    : 'mainImage',
418                                         baseImage : 'main_img_car.jpg',
419                                         view  : false,
420                                         value : data.stock.mainImage ? data.stock.mainImage : ''
421                                 },
422                                 'frontImage' : {
423                                         type  : 'image',
424                                         id    : 'frontImage',
425                                         baseImage : 'front_car.jpg',
426                                         view  : false,
427                                         value : data.stock.frontImage ? data.stock.frontImage : ''
428                                 },
429                                 'leftImage' : {
430                                         type  : 'image',
431                                         id    : 'leftImage',
432                                         baseImage : 'left_car.jpg',
433                                         view  : false,
434                                         value : data.stock.leftImage ? data.stock.leftImage : ''
435                                 },
436                                 'rightImage' : {
437                                         type  : 'image',
438                                         id    : 'rightImage',
439                                         baseImage : 'right_car.jpg',
440                                         view  : false,
441                                         value : data.stock.rightImage ? data.stock.rightImage : ''
442                                 },
443                                 'backImage' : {
444                                         type  : 'image',
445                                         id    : 'backImage',
446                                         baseImage : 'back_car.jpg',
447                                         view  : false,
448                                         value : data.stock.backImage ? data.stock.backImage : ''
449                                 },
450                                 'interiorImage' : {
451                                         type  : 'image',
452                                         id    : 'interiorImage',
453                                         baseImage : 'interior_car.jpg',
454                                         view  : false,
455                                         value : data.stock.interiorImage ? data.stock.interiorImage : ''
456                                 },
457                                 'engineImage' : {
458                                         type  : 'image',
459                                         id    : 'engineImage',
460                                         baseImage : 'engine_car.jpg',
461                                         view  : false,
462                                         value : data.stock.engineImage ? data.stock.engineImage : ''
463                                 },
464                                 'natisImage' : {
465                                         type  : 'image',
466                                         id    : 'natisImage',
467                                         baseImage : 'copy_of_natis_car.jpg',
468                                         view  : false,
469                                         value : data.stock.natisImage ? data.stock.natisImage : ''
470                                 },
471
472                                 'firstName' : {
473                                         type  : 'input',
474                                         id    : 'firstName',
475                                         title : 'Name',
476                                         value : data.firstName ? data.firstName : ''
477                                 },
478                                 'familyName' : {
479                                         type  : 'input',
480                                         id    : 'familyName',
481                                         title : 'Surname',
482                                         value : data.familyName ? data.familyName : ''
483                                 },
484                                 'email' : {
485                                         type  : 'input',
486                                         id    : 'email',
487                                         title : 'Email address',
488                                         value : data.email ? data.email : ''
489                                 },
490                                 'confirmEmail' : {
491                                         type  : 'input',
492                                         id    : 'confirmEmail',
493                                         title : 'Confirm email address',
494                                         value : '',
495                                         maxlength : 255
496                                 },
497                                 'mobile' : {
498                                         type  : 'mobile',
499                                         id    : 'mobile',
500                                         title : 'Mobile',
501                                         value : data.mobile ? data.mobile : ''
502                                 },
503                                 'region' : {
504                                         type        : 'select',
505                                         id          : 'region',
506                                         title       : 'Region',
507                                         dataStoreId : 'regions',
508                                         value       : '',
509                                         onChange    : $.proxy(function () {
510                                                 if (!App.DataStore.getItem('BuildSelect:region', false))
511                                                 {
512                                                         /*App.DataStore.loadSelectListData(
513                                                                         'cities', false, 'Location', 'Town.SelectList', null,
514                                                                         {"Filter": {
515                                                                                 "region" : $(this).val()
516                                                                                 }}
517                                                                         );*/
518
519                                                         var dataList = undefined != _w.publicvaluation.publicCentreList[$('#region').val()]
520                                                                 ? _w.publicvaluation.publicCentreList[$('#region').val()]
521                                                                 : [];
522                                                         App.DataStore.setData('publicCentre', dataList);
523                                                         if ($('#sameAsBusinessAddress').prop('checked'))
524                                                         {
525                                                                 $('#billingRegion').selectpicker('val', $('#region').val());
526                                                         }
527                                                 }
528                                         }, this)
529                                 },
530                                 'city' : {
531                                         type        : 'select',
532                                         id          : 'city',
533                                         title       : 'Town',
534                                         dataStoreId : 'cities',
535                                         value       : ''
536                                 },
537
538                                 'publicCentre' : {
539                                         type        : 'select',
540                                         id          : 'publicCentre',
541                                         title       : 'Public Center',
542                                         dataStoreId : 'publicCentre',
543                                         value       : ''
544                                 },
545
546
547                                 'street' : {
548                                         type  : 'input',
549                                         id    : 'street',
550                                         title : 'Street',
551                                         value : '',
552                                         maxlength : 75
553                                 },
554                                 'requiredPrice' : {
555                                         type  : 'input',
556                                         style : 'rand-input',
557                                         id    : 'requiredPrice',
558                                         title : 'Required price',
559                                         value : ''
560                                 }
561                         };
562                 },
563
564                 _listMakesModelsTypes : function( fromItemData, callback )
565                 {
566                         if (fromItemData)
567                         {
568                                 var year     = _w.publicvaluation.itemData.stock.vehicleYear;
569                                 var category = _w.publicvaluation.itemData.stock.type.category.id;
570                                 var make     = _w.publicvaluation.itemData.stock.type.model.make.id;
571                                 var model    = _w.publicvaluation.itemData.stock.type.model.id;
572                                 var type     = _w.publicvaluation.itemData.stock.type.id;
573                         }
574                         else
575                         {
576                                 var year     = $('#vehicleYear').val();
577                                 var category = $('#vehicleCategory').val();
578                                 var make     = $('#vehicleMake').val();
579                                 var model    = $('#vehicleModel').val();
580                                 var type     = $('#vehicleType').val();
581                         }
582                         if (year && category)
583                         {
584                                 App.DataStore.loadSelectListData(
585                                                 'makes', false, 'Stock', 'Utility.ListMakes', null,
586                                                 {"Filter": {
587                                                         "category" : category,
588                                                         "year" : year
589                                                         }}
590                                                 );
591                         }
592                         if (make && year && category)
593                         {
594                                 App.DataStore.loadSelectListData(
595                                                 'models', false, 'Stock', 'Utility.ListModels', null,
596                                                 {"Filter": {
597                                                         "make" : make,
598                                                         "category" : category,
599                                                         "year" : year
600                                                         }}
601                                                 );
602                         }
603                         if (model && year && category)
604                         {
605                                 App.DataStore.loadSelectListData(
606                                                 'types', false, 'Stock', 'Utility.ListTypes', null,
607                                                 {"Filter": {
608                                                         "model" : model,
609                                                         "category" : category,
610                                                         "year" : year
611                                                         }}, {}, callback
612                                                 );
613                         }
614                 },
615
616                 _setupValidation : function ()
617                 {
618                         App.DataStore.listen('regValidation', 'damages', function(id, damages) {
619                                 var rules = {
620                                         firstName: {required: true, minlength: 3},
621                                         familyName: {required: true, minlength: 3},
622                                         email: {required: true, email: true},
623                                         confirmEmail: {required: true, email: true, equalTo: "#email"},
624                                         mobile: {required: true, mobile: true},
625                                         region: {required: true},
626                                         city: {required: false},
627                                         publicCentre: {required: true},
628                                         street: {required: false},
629                                         requiredPrice: {required: true, number: true, min: 0.01},
630                                         vehicleYear: "required",
631                                         vehicleCategory: "required",
632                                         vehicleMake: "required",
633                                         vehicleModel: "required",
634                                         vehicleType: "required",
635                                         vehicleFuelType: "required",
636                                         vehicleTransmissionType: "required",
637                                         registrationNumber: "required",
638
639                                         vehicleCondition: "required",
640                                         exteriorColour: "required",
641                                         interiorColour: "required",
642                                         vehicleUpholstery: "required",
643                                         vehiclePapers: {required: false},
644                                         vehicleNatis: {required: false},
645                                         spareKeys: "required",
646                                         fullServiceHistory: "required",
647                                         km: { required: true, digits: true }
648                                 };
649                                 for (var i = 0; i < damages.length; i++)
650                                 {
651                                         rules['damage' + damages[i].value + 'Amount'] = {number: true};
652                                 }
653                                 $('#frmValuationView').validate({
654                                         "rules": rules,
655                                         messages: {
656                                                 firstName: {
657                                                         minlength: "This field is required."
658                                                 },
659                                                 familyName: {
660                                                         minlength: "This field is required."
661                                                 },
662                                                 email: {
663                                                         required: "This field is required.",
664                                                         email: "Please enter a valid<br/>email address."
665                                                 },
666                                                 confirmEmail: {
667                                                         required: "This field is required.",
668                                                         email: "Please enter a valid<br/>email address.",
669                                                         equalTo: "Please enter the same<br/>value again."
670                                                 },
671                                                 requiredPrice: {
672                                                         required: "This field is required.",
673                                                         number: "Please enter a valid amount.",
674                                                         min: "Please enter a<br/>valid amount."
675                                                 },
676                                                 vehicleYear : "This field is required.",
677                                                 vehicleCategory : "This field is required.",
678                                                 vehicleMake : "This field is required.",
679                                                 vehicleModel : "This field is required.",
680                                                 vehicleType : "This field is required.",
681                                                 vehicleFuelType : "This field is required.",
682                                                 vehicleTransmissionType : "This field is required.",
683                                                 registrationNumber : "This field is required.",
684                                                 vehicleCondition : "This field is required.",
685                                                 exteriorColour : "This field is required.",
686                                                 interiorColour : "This field is required.",
687                                                 vehicleUpholstery : "This field is required.",
688                                                 //vehiclePapers : "This field is required.",
689                                                 //vehicleNatis : "This field is required.",
690                                                 spareKeys : "This field is required.",
691                                                 fullServiceHistory : "This field is required.",
692                                                 km : {required: "This field is required."}
693                                         },
694                                         invalidHandler: function(event, validator) {
695                                             var errors = validator.numberOfInvalids();
696                                             if (0 < _w.publicvaluation.validations && errors) {
697                                               var message = errors == 1
698                                                 ? 'You missed 1 field. It has been highlighted'
699                                                 : 'You missed ' + errors + ' fields. They have been highlighted';
700                                               alert(message);
701                                             }
702                                             _w.publicvaluation.validations++;
703                                         },
704                                         submitHandler: function( form ) {
705                                                 return;
706                                         }
707                                 });
708                                 try {
709                                         _w.publicvaluation.validations = 0;
710                                         $('#frmValuationView').valid();
711                                 } catch(err) {}
712                                 $('label.error').remove();
713                                 $('.valid').removeClass('valid');
714                         });
715                 },
716
717                 _prepValuationUpdateData : function()
718                 {
719                         function isValidEmailAddress(emailAddress) {
720                             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);
721                             return pattern.test(emailAddress);
722                         };
723
724                         var data = _w.publicvaluation.ti.harvest();
725                         data.km = '' == data.km
726                                 ? 0
727                                 : parseInt(data.km);
728                         data.retailPrice = 0.00;
729                         data.tradePrice = 0.00;
730             data.listPrice = 0.00;
731                         delete data.damageTotal;
732
733                         if ('' == data.mainImage)
734                         {
735                                 delete data.mainImage;
736                         }
737                         if ('' == data.leftImage)
738                         {
739                                 delete data.leftImage;
740                         }
741                         if ('' == data.backImage)
742                         {
743                                 delete data.backImage;
744                         }
745                         if ('' == data.engineImage)
746                         {
747                                 delete data.engineImage;
748                         }
749                         if ('' == data.frontImage)
750                         {
751                                 delete data.frontImage;
752                         }
753                         if ('' == data.rightImage)
754                         {
755                                 delete data.rightImage;
756                         }
757                         if ('' == data.interiorImage)
758                         {
759                                 delete data.interiorImage;
760                         }
761                         if ('' == data.natisImage)
762                         {
763                                 delete data.natisImage;
764                         }
765
766
767                         var postData = {Stock:{}, Valuation:{}};
768                         var valuationFields = [
769                                                "firstName","familyName","mobile",
770                                                "email","region","city","street","requiredPrice"," publicCentre"
771                                                ];
772                         for (var field in data)
773                         {
774                                 if (-1 == valuationFields.indexOf(field))
775                                 {
776                                         postData.Stock[field] = data[field];
777                                 }
778                                 else
779                                 {
780                                         postData.Valuation[field] = data[field];
781                                 }
782                         }
783                         if ('' == postData.Valuation.idNumber)
784                         {
785                                 delete postData.Valuation.idNumber;
786                         }
787
788                         if( !isValidEmailAddress( postData.Valuation.email ) )
789                         {
790                                 testEmail = postData.Valuation.email;
791                                 //console.log("went through check", testEmail);
792                                 postData.Valuation.email = null;
793                         }
794
795
796                         if ('' == postData.Valuation.email)
797                         {
798                                 postData.Valuation.email = null;
799                         }
800
801                         return postData;
802                 },
803
804                 _onValuationUpdated : function( response )
805                 {
806                         //alert('Your vehicle has successfully been submitted.');
807                         window.location.hash = '/thankyouvaluation';
808                 },
809                 _onValuationFail : function( response )
810                 {
811                         alert('Oops, something went wrong, please check your data carefully and try again.');
812                         //window.location.hash = '/public-landing';
813                 }
814
815         };
816
817 })();