PageSourceSearch

https://moverdb.com/wp-content/plugins/mymovingloads-leads-form/assets/js/mml_functions.min.js?ver=7.1.2

js moverdb.com collected 2026-10-02 14:29:58 UTC 42,346 bytes, 1,216 lines download raw bytes

1/* 
2 * Version: 1.1
3 */
4
5var use_google = false;
6function gm_authFailure() {
7	console.log('Unauthorized API key');
8	use_google = false;
9};
10
11function testApiCredentials(){
12
13	//Just in case because we are using external library
14	try
15	{
16		var a = new google.maps.places.AutocompleteService;
17		var request = {
18			input: 'new york, ny, us',
19			types:['geocode']
20		};
21
22		a.getQueryPredictions(request, function(data, response){
23			if(response == 'OK'){
24				//console.log('we can init google autocomplete');
25				use_google = true;
26			} else if(response == 'REQUEST_DENIED') {
27				console.log('Places API is not enabled.');
28			} else {
29				console.log('Problem with Google Places API.');
30			}
31		});
32	} catch(error) {
33		console.error(error);
34	}
35}
36
37
38
39(function($) {
40	$(document).ready(function() {
41
42		/*
43		 * REFRESH NONCE ON PAGE LOAD
44		 * Prevents 403 errors on sites with aggressive caching.
45		 * Makes a fresh REST API call to get a valid nonce even if the page is cached.
46		 */
47		if (typeof mml_object !== 'undefined' && typeof mml_ajax_url !== 'undefined') {
48			// Only attempt nonce refresh if REST API URL is available
49			if (mml_object.nonce_refresh_url) {
50				$.ajax({
51					url: mml_object.nonce_refresh_url,
52					method: 'GET',
53					async: false, // Synchronous to ensure nonce is ready before form submission
54					success: function(response) {
55						if (response.nonce) {
56							mml_object.nonsense = response.nonce;
57							mml_ajax_url.ajaxurl = response.ajaxurl;
58						}
59					},
60					error: function(xhr, status, error) {
61						// Fall back to cached nonce - will work if page cache is fresh or no caching
62					}
63				});
64			}
65		}
66
67		var autocomplete_countries = ['at', 'ch', 'de', 'gb', 'nz'];
68		var autocomplete_states = ['au', 'ca', 'us'];
69
70		/*
71		 * PREFILL by cookie
72		 * Checks if cookie is set and prefills info. It does not set cookies
73		 * This function uses Jquery Cookie Plugin
74		 */
75		try {
76			if ($.cookie('mml_name') !== 'undefined') {
77				var value = $.cookie('mml_name');
78				$('input[name=mml_customer_name]').val(value);
79			}
80			if ($.cookie('mml_email') !== 'undefined') {
81				var value = $.cookie('mml_email');
82				$('input[name=mml_customer_email]').val(value);
83			}
84			if ($.cookie('mml_email') !== 'undefined') {
85				var value = $.cookie('mml_phone');
86				$('input[name=mml_customer_phone]').val(value);
87			}
88		} catch(err) {
89			console.log('You are missing jquery.cookie.js');
90		}
91		
92
93		// GOOGLE API Autocomplete
94		function autocomplete_ini(input, country) {
95			if (use_google) {
96				if (!country) { country = ''};
97				var options = {
98					types: ['(cities)'],
99					componentRestrictions: {country: country}
100				};
101				var autocomplete = new google.maps.places.Autocomplete(input.get(0), options);
102				var autocompleteLst = google.maps.event.addListener(autocomplete, 'place_changed', function() { getCityOnly(input); });
103		 	}
104		}
105
106		function getCityOnly(input) {
107			try {
108				var field = input;
109				var value = field.val().split(',');
110				field.val(value[0]);
111			} catch (err) {
112				console.log(err);
113			}
114		}
115
116		/*
117		 * PREDEFINED RESUABLE VARIABLES
118		 * Here are all the variables that are used more then once
119		 */
120
121		var today = new Date();
122		var tomorrow = new Date(today.getTime() + 1000 * 60 * 60 * 24);
123		var source = [];
124
125		/* Google API variables */
126		var start_location = '';
127		var end_location = '';
128
129		/*
130		 * REUSABLE HANDLERS
131		 * Some functions that are used more then once
132		 */
133
134		var changeDate = function () {
135			var field = $(this);
136			var form = field.closest('form.mml_form');
137
138			var target = form.find($('input[name=mml_dateiso]'));
139			var day_start = new Date();
140			var day_end = new Date(target.val());
141			var total_days = (day_end.getTime() - day_start.getTime()) / (1000 * 60 * 60 * 24);
142			if (!isNaN(total_days)) {
143				if (Math.round(total_days) > 0) {
144					field.parent().find('.mml_days_left_wrap').attr('style', 'opacity:1;');
145					//field.parent().find('.mml_days_left').text(Math.ceil(total_days)); 
146					if (Math.round(total_days) == 1) {
147						field.parent().find('.mml_days_left_wrap').html('About <span class="mml_days_left">' +  Math.round(total_days) + '</span><br/>day left');
148					} else {
149						field.parent().find('.mml_days_left_wrap').html('About <span class="mml_days_left">' +  Math.round(total_days) + '</span><br/>days left');	
150					}
151				} else {
152					field.parent().find('.mml_days_left_wrap').attr('style', 'opacity:1;');
153					field.parent().find('.mml_days_left_wrap').html('<span class="mml_days_left">Soon</span>');
154				}
155			}
156		}
157
158		/*
159		 * PAGE LOAD TRACKING
160		 * If you have a google analytics code and have turned on the feature from the settings
161		 * this will send an event when the form loads with the coresponding abriviation.
162		 * Hiding the form will still count as seen.
163		 */
164		if (mml_object.totrack == '1') {
165			var abr = $('form.mml_form').attr('data-abr');
166			try {
167				if (typeof gtag == 'function') {
168					// console.log('gtag');
169					gtag('event', abr, {'event_category': 'MoveAdvisor Form', 'event_label': 'Successfully-Viewed'});
170				} else if (typeof ga == 'function') {
171					// console.log('ga');
172					ga('send', 'event', 'MoveAdvisor Form', abr , 'Successfully-Viewed');
173				} else if (typeof _gaq == 'function') {
174					// console.log('_gaq');
175					_gaq.push(['_trackEvent', 'MoveAdvisor Form', abr, 'Successfully-Viewed']);
176				}
177			} catch(err) {
178				console.log('No Google Analytics active');
179			}
180		}
181
182		/*
183		 * LOAD FORM TEMPLATE FUNCTION
184		 * This loads the template file with the required form.
185		 * 		form_abr    -> the new abbreviation the form will take, and file it has to load
186		 * 		form 	    -> the form object, used to target one form only on page
187		 * 		change_to   -> the abbreviation that the form needs to change back to
188		 * 		switch_text -> the text of the switch back link, implemented because of multi-lingual forms
189		 * This function allows to load new templates but the submit functionality still need to be implemented.
190		 */
191
192
193		function loadForm ( form_abr, form, change_to, switch_text ) {
194			form.find('.mml_body-wrap').slideUp('100');
195			form.find('.mml_body-wrap').html();
196
197			// language can be the preffered language en or de
198			var language = mml_object.language;
199			if (mml_object.language == 'en'){
200                var form_url = mml_object.plugin + 'includes/templates/en/mml_leadform_' + form_abr + '.html.php';
201			}
202			else {
203                var form_url = mml_object.plugin + 'includes/templates/en/mml_leadform_' + form_abr + '.html.php';
204			}
205			
206			$.get(form_url, function(data, status) {
207
208				if (status == 'success') {
209					form.find('.mml_body-wrap').html(data);
210					form.find('.mml_body-wrap').slideDown('100');
211
212					form.attr('data-abr', form_abr);
213				
214					if ( form_abr == 'int') {
215						form.find('select[name="mml_origin_country"] > option').each(function() {
216							if (mml_object.user_country == this.value.toLowerCase()) {
217								$(this).attr('selected','selected');
218								form.find('select[name="mml_origin_country"]').trigger('change');
219								return false;
220							};
221						});
222					}
223
224					form.find('div.mml_switch_int-wrap').show();
225					form.find('a.mml_switch')
226						.attr('data-to', change_to)
227						.removeClass('mml_switch_' + form_abr)
228						.addClass('mml_switch_'+ change_to)
229						.html(switch_text);
230
231					return true;
232				} else {
233					form.find('.mml_body-wrap').html('No form for this country. Please refresh page.');
234				}
235
236			});
237
238		}
239
240		/*
241		 * SWITCH FORM LINK
242		 * This is the link that switches the form body when clicked,
243		 * between the first loaded form and the international one,
244		 * and allows the option to switch back.
245		 */
246		$('.mml_leadform_wrapper').on('click', 'a.mml_switch', function(e) {
247			var switch_link = $(this);
248			var form = $(this).closest('div.mml_leadform_wrapper').find('form.mml_form');
249			var user_cntr = form.attr('data-abr');
250			var switch_to = switch_link.attr('data-to');
251
252			switch (user_cntr) {
253				case 'us': switch_text = 'Switch to United States form'; break;
254				case 'uk': switch_text = 'Switch to United Kingdom form'; break;
255				case 'ca': switch_text = 'Switch to Canada form'; break;
256				case 'au': switch_text = 'Switch to Australia form'; break;
257				case 'nz': switch_text = 'Switch to New Zealand form'; break;
258				case 'de': switch_text = 'Deutschland?'; break;
259				default: user_cntr = 'int'; switch_text = 'Switch to international form'; break;
260			}
261
262			$('input[name="mml_dateiso"]').val('');	// Clear if there was inputted date
263
264			loadForm ( switch_to, form, user_cntr, switch_text ); // load the new form
265
266			// Google Analytics tracking code
267			if (mml_object.totrack == '1') {
268				try {
269					if (typeof gtag == 'function') {
270						// console.log('gtag');
271						gtag('event', user_cntr, {'event_category': 'MoveAdvisor Form', 'event_label': 'Successfully-Viewed'});
272					} else if (typeof ga == 'function') {
273						// console.log('ga');
274						ga('send', 'event', 'MoveAdvisor Form', user_cntr , 'Successfully-Viewed');
275					} else if (typeof _gaq == 'function') {
276						// console.log('_gaq');
277						_gaq.push(['_trackEvent', 'MoveAdvisor Form', user_cntr, 'Successfully-Viewed']);
278					}
279				} catch(err) {
280					console.log('No Google Analytics active');
281				}
282			}
283			e.preventDefault();
284		});
285		/*
286		 * FORM RELATED CHANGES
287		 * These changes are mainly for the frontend of the form
288		 */
289
290		 /* Date Picker (ALL FORMS) */
291
292		$('form.mml_form').on('click', '.mml_moving_date', function(){
293			var date_field = $(this);
294
295			if ( date_field.hasClass('mml_date_format_uk') ) {
296				date_field.datepicker({
297					altFormat: 'yy-mm-dd',
298					altField: 'input[name=mml_dateiso]',
299					showAnim: 'slideDown',
300					ignoreReadonly: true,
301					minDate: tomorrow,
302					maxDate: '+6m',
303					showOn:'focus',
304					firstDay: 1,
305					dateFormat: 'dd/mm/yy',
306					beforeShow: function(input, instance) {
307						var inputOffset = $(input).offset();
308						var inputH = $(input).outerHeight();
309						var top = inputOffset.top;
310						var left= inputOffset.left;
311						setTimeout(function(){
312							instance.dpDiv.css({
313								top: top + inputH,
314								left: left
315							});
316						}, 0);
317					}
318				}).focus();
319			} else if (date_field.hasClass('mml_date_format_int')) {
320				date_field.datepicker({
321					altFormat: 'yy-mm-dd',
322					altField: 'input[name=mml_dateiso]',
323					showAnim: 'slideDown',
324					ignoreReadonly: true,
325					minDate: tomorrow,
326					maxDate: '+6m',
327					showOn:'focus',
328					firstDay: 1,
329					dateFormat: 'd M yy',
330					beforeShow: function(input, instance) {
331						var inputOffset = $(input).offset();
332						var inputH = $(input).outerHeight();
333						var top = inputOffset.top;
334						var left= inputOffset.left;
335						setTimeout(function(){
336							instance.dpDiv.css({
337								top: top + inputH,
338								left: left
339							});
340						}, 0);
341					}
342				}).focus();
343			} else {
344				date_field.datepicker({
345					altFormat: 'yy-mm-dd',
346					altField: 'input[name=mml_dateiso]',
347					showAnim: 'slideDown',
348					ignoreReadonly: true,
349					minDate: tomorrow,
350					maxDate: '+6m',
351					showOn:'focus',
352					firstDay: 0,
353					dateFormat: 'mm/dd/yy',
354					beforeShow: function(input, instance) {
355						var inputOffset = $(input).offset();
356						var inputH = $(input).outerHeight();
357						var top = inputOffset.top;
358						var left= inputOffset.left;
359						setTimeout(function(){
360							instance.dpDiv.css({
361								top: top + inputH,
362								left: left
363							});
364						}, 0);
365					}
366				}).focus();
367			}
368		});
369
370		$('form.mml_form').on('blur', '.mml_moving_date', changeDate);
371		$('form.mml_form').on('change', '.mml_moving_date', changeDate);
372
373		/*
374		 * CALCULATE DISTANCE
375		 * Using Google Maps, calculate the distance between
376		 * the origin location and the delivery location.
377		 */	
378		function calculateDistances(start_location, end_location, metric) {
379			origin = start_location; //Get the source string
380			destination = end_location; //Get the destination string
381			if ( metric ) {
382				unit_system = google.maps.UnitSystem.METRIC;
383			} else {
384				unit_system = google.maps.UnitSystem.IMPERIAL;
385			}
386			var service = new google.maps.DistanceMatrixService(); //initialize the distance service
387			service.getDistanceMatrix(
388			{
389				origins: [origin], 							// Set origin, you can specify multiple sources here
390				destinations: [destination], 				// Set destination, you can specify multiple destinations here
391				travelMode: google.maps.TravelMode.DRIVING, // Set the travelmode
392				unitSystem: unit_system, 	// The unit system to use when displaying distance
393				avoidHighways: false,
394				avoidTolls: false
395			}, calcDistance); // here calcDistance is the call back function
396		}
397
398		function calcDistance(response, status) {
399			if (status == google.maps.DistanceMatrixStatus.OK) { // check if there is valid result
400				var origins = response.originAddresses;
401				var destinations = response.destinationAddresses;
402
403				results = response.rows;
404				result = results[0].elements[0]
405				result_status = result.status;
406
407				var distance_wrap = $('span.mml_distance');
408				
409				if (result_status == 'OK') {
410					var distance = result.distance.text;
411					distance_wrap.text('About ' + distance);
412				} else {
413					//console.log('No results found');
414				}
415
416			} else {
417				//console.log('Status: ' + status);
418			}
419
420		}
421
422		$('form.mml_form').on('focusout', 'input[name="mml_delivery_city"]', function(){
423			if ( mml_object.googleAPI.length > 2 ) {
424				var field = $(this);
425				var form = field.closest('form.mml_form');
426				var abr = form.attr('data-abr');
427				var city, state, country;
428
429				switch (abr) {
430					case 'us':
431						city = field.val();
432						state = form.find('select[name="mml_delivery_state"]').val();
433						country = 'US';
434						end_location = city + ', ' + state + ', ' + country;
435						break;
436					case 'uk':
437						city = field.val();
438						country = 'UK';
439						end_location = city + ', ' + country;
440						break;
441					case 'ca':
442						city = field.val();
443						state = form.find('select[name="mml_delivery_state"]').val();
444						country = 'CA';
445						end_location = city + ', ' + state + ', ' + country;
446						break;
447					case 'au':
448						city = field.val();
449						state = form.find('select[name="mml_delivery_state"]').val();
450						country = 'AU';
451						end_location = city + ', ' + state + ', ' + country;
452					case 'nz':
453						city = field.val();
454						country = 'NZ';
455						end_location = city + ', ' + country;
456						break;
457					default:
458						//console.log('Error');
459						break;
460				}
461
462				if (start_location !== '' && end_location !== '') {
463					if (use_google) {
464						if (abr == 'us') {
465							calculateDistances(start_location, end_location, false);						
466						} else {
467							calculateDistances(start_location, end_location, true);
468						}
469					}
470				}
471			}
472		});
473
474		// For international only
475		$('form.mml_form').on('change', 'input[name="mml_destination_city"]', function(){
476			var field = $(this);
477			var form = field.closest('form.mml_form');
478			var abr = form.attr('data-abr');
479
480
481			var start_city = form.find('input[name="mml_origin_city"]').val();
482			var start_country = form.find('select[name="mml_origin_country"]').val();
483			var start_state = '';
484
485			switch (start_country) {
486				case 'US':
487				start_state = form.find('select[name="mml_origin_state_us"]').val();
488				start_location = start_city + ', ' + start_state + ', ' + start_country;
489				break;
490
491				case 'CA':
492				start_state = form.find('select[name="mml_origin_state_ca"]').val();
493				start_location = start_city + ', ' + start_state + ', ' + start_country;
494				break;
495
496				case 'AU':
497				start_state = form.find('select[name="mml_origin_state_au"]').val();
498				start_location = start_city + ', ' + start_state + ', ' + start_country;
499				break;
500
501				default:
502				start_location = start_city + ', ' + start_country;
503				break;
504			}
505
506			var end_city = form.find('input[name="mml_destination_city"]').val();
507			var end_country = form.find('select[name="mml_destination_country"]').val();
508
509			switch (end_country) {
510				case 'US':
511				end_state = form.find('select[name="mml_delivery_state_us"]').val();
512				end_location = end_city + ', ' + end_state + ', ' + end_country;
513				break;
514
515				case 'CA':
516				end_state = form.find('select[name="mml_delivery_state_ca"]').val();
517				end_location = end_city + ', ' + end_state + ', ' + end_country;
518				break;
519
520				case 'AU':
521				end_state = form.find('select[name="mml_delivery_state_au"]').val();
522				end_location = end_city + ', ' + end_state + ', ' + end_country;
523				break;
524
525				default:
526				end_location = end_city + ', ' + end_country;
527				break;
528			}
529
530			if (start_location !== '' && end_location !== '') {
531				if (use_google) {
532					calculateDistances(start_location, end_location);
533				}
534			}
535		});
536
537		$('.mml_close').click(function(){
538			var parent = $(this).parent();
539			parent.slideUp();
540		});
541
542		$('form.mml_form').submit(function (e) {
543			var form = $(this);
544
545			var abr = form.attr('data-abr');
546
547			var post_id = form.attr('data-post');
548
549			// Fields
550			var field_required		= form.find('input[required=true]');
551			var field_error			= form.find('div.mml_error');
552			var field_fails			= form.find('input[name="failed"]');
553			var honey_field			= form.find('input[type="email"].mml_leadform_3198637');
554
555			var terms_field			= form.find('input[type="checkbox"].mml_termstoagree');
556
557			var inputArray;
558			
559			proceed = true;			// State of the proccess.
560
561			// Reset errors on submit attempt
562			field_error.html();
563
564			if (honey_field.val().length > 0) {
565				$('form.mml_form').fadeOut();
566				$('.mml_leadform_success').fadeIn();
567				proceed = false;
568				console.log('Go away, bot.');
569			} else {
570
571				var value_moving_date = form.find('input[name="mml_dateiso"]').val();		// Data format in ISO
572				var value_size = form.find('select[name="mml_size"]').val();				// Moving Size
573				var value_cust_name = form.find('input[name="mml_customer_name"]').val();	// Customer Name
574				var value_cust_ip = form.find('input[name="user_id"]').val();				// User IP
575
576				var value_cust_phone = form.find('input[name="mml_customer_phone"]').val();	// Customer Phone
577				var field_phone = form.find('input[name="mml_customer_phone"]');
578				// Phone number validation (Tier 1)
579				var value_cust_phone = value_cust_phone.replace(/\D/g,'');
580				if (abr == 'us' || abr == 'ca') {
581					// With US/CA remove the country code
582					if (value_cust_phone[0] == 1) {
583						value_cust_phone = value_cust_phone.substr(1);
584					}
585					// US/CA phones are always 10 digits
586					if (value_cust_phone.length == 10) {
587						field_phone.removeClass('mml_error_field');		// remove any previous error
588					} else {
589						proceed = false;
590						field_fails.val(field_fails.val() + "|Phone - Not 10 digits");
591						field_phone.addClass('mml_error_field');
592						field_error.html('Please enter valid phone.');
593					}
594				}
595
596				var value_cust_email = form.find('input[name="mml_customer_email"]').val();	// Customer Email
597				var field_email = form.find('input[name="mml_customer_email"]');
598				// Email validation (Tier 1)
599				if ( value_cust_email.length < 13 ) {
600					proceed = false;
601					field_fails.val(field_fails.val() + "|Email - Email too short");
602					field_email.addClass('mml_error_field');
603					field_error.html('Please enter valid email.');
604				} else {
605					field_email.removeClass('mml_error_field');
606				}
607
608				var value_failed = form.find('input[name="failed"]').val();					// Record errors
609
610				if (abr !== 'int' && abr !== 'de') {
611					var value_from_zip = form.find('input[name="mml_from_zip"]').val();		// Take ZIP code
612
613					if ( abr !== 'uk' || abr !== 'nz' ) {													// UK SPECIFIC
614						var value_to_state = form.find('select[name="mml_delivery_state"]').val();	// Take Delivery State
615					} else {
616						var value_to_state = '';
617					}
618
619					var value_delivery_city = form.find('input[name="mml_delivery_city"]').val();
620					if (value_delivery_city == 'undefined') {
621						proceed = false;
622						field_email.addClass('mml_error_field');
623						field_error.html('Please enter valid city name.');
624					}
625
626					var value_from_country = form.find('input[name="from_to_country"]').val();
627					if ( abr == 'ca') {																// CANADA SPECIFIC
628						var value_to_country = form.find('input[name="to_country"]').val();
629					} else {
630						var value_to_country = form.find('input[name="from_to_country"]').val();
631					}
632
633
634					// Fill the data NOT INT
635					post_data = {
636						customer_name : value_cust_name,
637						customer_phone : value_cust_phone,
638						customer_email : value_cust_email,
639						from_country : value_from_country,
640						from_postal_code : value_from_zip,
641						to_country : value_to_country,
642						to_state : value_to_state,
643						to_place : $('input[name="mml_delivery_city"]').val().match(/[0-9]/) ? null : value_delivery_city,
644						to_postal_code : $('input[name="mml_delivery_city"]').val().match(/[0-9]/) ? value_delivery_city : null,
645						size : value_size,
646						moving_date : value_moving_date,
647						parent_webpage: mml_object.ref_page,
648						failed: value_failed,
649						post_id: post_id
650					}
651
652				} else {
653					// Is INT form or DE form
654					var value_from_country = form.find('select[name="mml_origin_country"]').val();
655					var value_from_place = form.find('input[name="mml_origin_city"]').val();
656					if (value_from_place == 'undefined') {
657						proceed = false;
658						field_email.addClass('mml_error_field');
659						field_error.html('Please enter valid city name.');
660					}
661
662					var value_from_state = '';
663					if (value_from_country == 'US') {
664						value_from_state = form.find('select[name="mml_origin_state_us"]').val();
665					} else if (value_from_country == 'CA') {
666						value_from_state = form.find('select[name="mml_origin_state_ca"]').val();
667					} else if (value_from_country == 'AU') {
668						value_from_state = form.find('select[name="mml_origin_state_au"]').val();
669					} else {
670						value_from_state = '';
671					}
672
673					var value_to_country = form.find('select[name="mml_destination_country"]').val();
674					var value_delivery_city = form.find('input[name="mml_destination_city"]').val();
675					if (value_delivery_city == 'undefined') {
676						proceed = false;
677						field_email.addClass('mml_error_field');
678						field_error.html('Please enter valid city name.');
679					}
680					var value_to_state = '';
681					if (value_to_country == 'US') {
682						value_to_state = form.find('select[name="mml_delivery_state_us"]').val();
683					} else if (value_to_country == 'CA') {
684						value_to_state = form.find('select[name="mml_delivery_state_ca"]').val();
685					} else if (value_to_country == 'AU') {
686						value_to_state = form.find('select[name="mml_delivery_state_au"]').val();
687					} else {
688						value_to_state = '';
689					}
690
691					// Fill the data Int
692					post_data = {
693						customer_name : value_cust_name,
694						customer_phone : value_cust_phone,
695						customer_email : value_cust_email,
696						from_country : value_from_country,
697						from_state: value_from_state,
698						from_place : value_from_place,
699						to_country : value_to_country,
700						to_place : $('input[name="mml_destination_city"]').val().match(/[0-9]/) ? null : value_delivery_city,
701						to_postal_code : $('input[name="mml_destination_city"]').val().match(/[0-9]/) ? value_delivery_city : null,
702						to_state : value_to_state,
703						size : value_size,
704						moving_date : value_moving_date,
705						parent_webpage: mml_object.ref_page,
706						failed: value_failed,
707						post_id: post_id
708					}
709				}
710
711				// Terms of Service
712				if (terms_field.length > 0) {
713					if (!terms_field.is(':checked')) {
714						terms_field.closest('label').addClass('mml_error_field');
715						field_error.html('You must agree on terms of service.');
716						proceed = false;
717					} else {
718						terms_field.closest('label').removeClass('mml_error_field');
719					}
720				}
721
722				if (!proceed) {
723					// General error settings for this form
724					form.effect( 'shake' );
725					console.log('Something is wrong with the form');
726				} else {
727
728					field_error.html('');
729					var old_submit_value = form.find('input[type=submit]').val();
730					form.find('input[type=submit]').val('Sending...').prop('disabled', true).addClass('disabled');
731
732
733					if (form.hasClass('mml_isadmin')) {
734						$('form.mml_form').fadeOut();
735						$('.mml_leadform_success').fadeIn();
736						if ($('.mml_switch_int-wrap').length > 0 ) { $('.mml_switch_int-wrap').remove(); }
737						$('.mml_admin_block').slideDown().html('This test lead was not submitted to MoveAdvisor.<br/>Please note that only real-time leads by visitors are approved as valid leads according to our affiliate agreement.<br/>Your visitors will not see this message.<br/><br/><a href="">Click to visit your MoveAdvisor affiliate account.</a>');
738					} else {
739						data = {
740							action: 'mml_ajax_action',
741							security: mml_object.nonsense,
742							form_data: post_data
743						}
744
745						$.ajax({
746							url: mml_ajax_url.ajaxurl,
747							async: true,
748							type: 'POST',
749							data: data,
750							dataType: 'json', // added data type
751
752							success: function(res) {
753								if ( res.status !== 'error') {
754
755									// Google Analytics tracking code
756									if (mml_object.totrack == '1') {
757										try {
758											if (typeof gtag == 'function') {
759												// console.log('gtag');
760												gtag('event', abr, {'event_category': 'MoveAdvisor Form', 'event_label': 'Successfully-Sent'});
761											} else if (typeof ga == 'function') {
762												// console.log('ga');
763												ga('send', 'event', 'MoveAdvisor Form', abr , 'Successfully-Sent');
764											} else if (typeof _gaq == 'function') {
765												// console.log('_gaq');
766												_gaq.push(['_trackEvent', 'MoveAdvisor Form', abr, 'Successfully-Sent']);
767											}
768										} catch(err) {
769											console.log('No Google Analytics active');
770										}
771									}
772									
773									$('form.mml_form').fadeOut();
774									if (res.ic && res.ic === 1) {
775
776										let agentNumber = "";
777										switch (abr) {
778											case 'uk':
779												agentNumber = '0800 086 9039';
780												break;
781											case 'au':
782												agentNumber = '1800 952 370';
783												break;
784											case 'nz':
785												agentNumber = '0800 244 266';
786												break;
787											case 'ie':
788												agentNumber = '1 800 81 6551';
789												break;
790											default:
791												agentNumber = '+1 (800) 680-6439';
792												break;
793										}
794
795										$('#call-transfer-number-success').text(agentNumber);
796
797										$('.mml_leadform_phonecall_success').fadeIn();
798									} else {
799										$('.mml_leadform_success').fadeIn();
800									}
801									if ($('.mml_switch_int-wrap').length > 0 ) { $('.mml_switch_int-wrap').remove(); }
802								
803									// If it is a demo test
804									if (res.text === 'Demo Test Successful.') {
805										$('.mml_leadform_success').append('<div class="demo_warning">This is a Demo Test.<br/>Please contact your MoveAdvisor representative to get an active API key.</div>');
806									}
807								} else {
808									// Handle API errors response
809									var errors = res.errors;
810									for(i = 0; i < errors.length; i++) {
811										if (errors[i].indexOf('name') !== -1) {
812											field_fails.val(field_fails.val() + "|Name - " + errors[i] + " (" + value_cust_name + ")");
813										} else if(errors[i].indexOf('phone') !== -1) {
814											field_fails.val(field_fails.val() + "|Phone - " + errors[i] + " (" + value_cust_phone + ")");
815										} else if (errors[i].indexOf('E-Mail') !== -1) {
816											field_fails.val(field_fails.val() + "|Email - " + errors[i] + " (" + value_cust_email + ")");
817										} else {
818											field_fails.val(field_fails.val() + "|" + errors[i]);
819										}
820									}
821									form.effect( 'shake' );
822									field_error.html(errors[0]);
823								}
824								form.find('input[type=submit]').val(old_submit_value).prop('disabled', false).removeClass('disabled');
825							},
826							error: function (jqXHR, textStatus, errorThrown) {
827								console.log('AJAX Error:', jqXHR, textStatus, errorThrown);
828								form.effect( 'shake' );
829								
830								// Check if we have a JSON error response with errors array
831								var errorMessage = 'An error occurred. Please try again.';
832								if (jqXHR.responseJSON && jqXHR.responseJSON.errors && Array.isArray(jqXHR.responseJSON.errors) && jqXHR.responseJSON.errors.length > 0) {
833									errorMessage = jqXHR.responseJSON.errors[0] + ' Please try again.';
834								} else if (jqXHR.responseJSON && jqXHR.responseJSON.errors) {
835									// Handle case where errors might be a string
836									errorMessage = jqXHR.responseJSON.errors + ' Please try again.';
837								} else if (textStatus === 'timeout') {
838									errorMessage = 'Request timed out. Please try again.';
839								} else if (textStatus === 'error' && errorThrown) {
840									errorMessage = errorThrown + ' Please try again.';
841								}
842								
843								field_error.html(errorMessage);
844								form.find('input[type=submit]').val(old_submit_value).prop('disabled', false).removeClass('disabled');
845							}
846						});	
847					}
848
849				}
850			}
851			
852			e.preventDefault();
853		});
854
855
856		var key_pressed_timeout = null;
857		function findZip(abr, field){
858
859			var country = '';
860			form = field.closest('form.mml_form');
861			switch(abr) {
862				case 'uk': country = 'GB'; break;
863				case 'us': country = 'US'; break;
864				case 'ca': country = 'CA'; break;
865				case 'au': country = 'AU'; break;
866				case 'nz': country = 'NZ'; break;
867				case 'usa': country = 'US'; break;
868			}
869			postal_code = $('.mml_from_zip').val();
870			address = $('.mml_from_zip').val() + ', ' + ((country == 'GB') ? '
870UK' : country);
871
872
873
874			geocoder = new google.maps.Geocoder();
875			geocoder.geocode( { 'address': address, 'country': country}, function(results, status) {
876				var found = false;
877				var country_match = false;
878				var postal_match = false;
879				var locality;
880				var state;
881				var print_address;
882
883				if (status == "OK") {
884
885					$.each(results, function(key, result) {
886						country_match = false;
887						postal_match = false;
888						$.each(result['address_components'], function(key,component) {
889							if (component['types'][0] == 'postal_code') {
890								if (component['short_name'] == postal_code.toUpperCase()) {
891									postal_match = true;
892								} else if (component['short_name'] == postal_code.substring(0, 3).toUpperCase() && country !== 'US') {
893									postal_match = true;
894								}
895							}
896							if (component['types'][0] == 'country' && component['short_name'] == country) {
897								country_match = true;
898							}
899							if (component['types'][0] == 'administrative_area_level_1') {
900								state = component['short_name'];
901							}
902							if (country == 'US' || country == 'CA' || country == 'AU') {
903								if (component['types'][0] == 'locality') {
904									locality = component['long_name'];
905								}
906							} else if (country == 'GB') {
907								if (component['types'][0] == 'postal_town') {
908									locality = component['long_name'];
909								}
910							}
911
912						});
913
914						if ( country_match == true && postal_match == true) {
915							print_address = ((locality !== undefined) ? locality + ', ' : '') + ((state !== undefined) ? state : '');
916							form.find('.mml_from-city').html(print_address);
917							start_location = result['formatted_address'];
918							found = true;
919							return;
920						}
921
922					});
923				}
924
925				if(!found) {
926					$('.mml_from-city').empty();
927					start_location = '';
928				}
929			});
930
931		};
932
933		$('form.mml_form').on('keypress', '.mml_from_zip', function(){
934
935			if ( mml_object.googleAPI.length > 2 ) {	// shows that there is no google api entered in the 
936				var field = $(this);
937				var abr = field.attr('data-abr');
938
939				if(key_pressed_timeout != null) {
940					clearTimeout(key_pressed_timeout);
941					key_pressed_timeout = null;
942				}
943				key_pressed_timeout = setTimeout(function(){ findZip(abr, field); }, 500);
944			}
945
946		});
947
948		// International Form - Origin: Show states for countries with states
949		$('form.mml_form').on('change', 'select[name=mml_origin_country]', function() {
950			var form = $(this).closest('form.mml_form');
951			var select_wrap = $(this).parent();
952			if ($(this).val() == 'US') {
953				select_wrap.removeClass('mml_col-xs-12').addClass('mml_col-xs-6');
954				form.find('.mml_originstate_wrap.mml_int_us').show();
955				form.find('.mml_originstate_wrap.mml_int_ca').hide();
956				form.find('.mml_originstate_wrap.mml_int_au').hide();
957				form.find('.mml_originstate_wrap.mml_int_us > select').prop('required', true);
958				form.find('.mml_originstate_wrap.mml_int_ca > select').prop('required', false);
959				form.find('.mml_originstate_wrap.mml_int_au > select').prop('required', false);
960			} else if ($(this).val() == 'CA') {
961				select_wrap.removeClass('mml_col-xs-12').addClass('mml_col-xs-6');
962				form.find('.mml_originstate_wrap.mml_int_us').hide();
963				form.find('.mml_originstate_wrap.mml_int_ca').show();
964				form.find('.mml_originstate_wrap.mml_int_au').hide();
965				form.find('.mml_originstate_wrap.mml_int_us > select').prop('required', false);
966				form.find('.mml_originstate_wrap.mml_int_ca > select').prop('required', true);
967				form.find('.mml_originstate_wrap.mml_int_au > select').prop('required', false);
968			} else if ($(this).val() == 'AU') {
969				select_wrap.removeClass('mml_col-xs-12').addClass('mml_col-xs-6');
970				form.find('.mml_originstate_wrap.mml_int_us').hide();
971				form.find('.mml_originstate_wrap.mml_int_ca').hide();
972				form.find('.mml_originstate_wrap.mml_int_au').show();
973				form.find('.mml_originstate_wrap.mml_int_us > select').prop('required', false);
974				form.find('.mml_originstate_wrap.mml_int_ca > select').prop('required', false);
975				form.find('.mml_originstate_wrap.mml_int_au > select').prop('required', true);
976			} else {
977				select_wrap.removeClass('mml_col-xs-6').addClass('mml_col-xs-12');
978				form.find('.mml_originstate_wrap.mml_int_us').hide();
979				form.find('.mml_originstate_wrap.mml_int_ca').hide();
980				form.find('.mml_originstate_wrap.mml_int_au').hide();
981				form.find('.mml_originstate_wrap.mml_int_us > select').prop('required', false);
982				form.find('.mml_originstate_wrap.mml_int_ca > select').prop('required', false);
983				form.find('.mml_originstate_wrap.mml_int_au > select').prop('required', false);
984			}
985		});
986
987		// International Form - Delivery: Show states for countries with states
988		$('form.mml_form').on('change', 'select[name=mml_destination_country]', function() {
989			var form = $(this).closest('form.mml_form');
990			var select_wrap = $(this).parent();
991			if ($(this).val() == 'US') {
992				select_wrap.removeClass('mml_col-xs-12').addClass('mml_col-xs-6');
993				form.find('.mml_deliverystate_wrap.mml_int_us').show();
994				form.find('.mml_deliverystate_wrap.mml_int_ca').hide();
995				form.find('.mml_deliverystate_wrap.mml_int_au').hide();
996				form.find('.mml_deliverystate_wrap.mml_int_us > select').prop('required', true);
997				form.find('.mml_deliverystate_wrap.mml_int_ca > select').prop('required', false);
998				form.find('.mml_deliverystate_wrap.mml_int_au > select').prop('required', false);
999			} else if ($(this).val() == 'CA') {
1000				select_wrap.removeClass('mml_col-xs-12').addClass('mml_col-xs-6');
1001				form.find('.mml_deliverystate_wrap.mml_int_us').hide();
1002				form.find('.mml_deliverystate_wrap.mml_int_ca').show();
1003				form.find('.mml_deliverystate_wrap.mml_int_au').hide();
1004				form.find('.mml_deliverystate_wrap.mml_int_us > select').prop('required', false);
1005				form.find('.mml_deliverystate_wrap.mml_int_ca > select').prop('required', true);
1006				form.find('.mml_deliverystate_wrap.mml_int_au > select').prop('required', false);
1007			} else if ($(this).val() == 'AU') {
1008				select_wrap.removeClass('mml_col-xs-12').addClass('mml_col-xs-6');
1009				form.find('.mml_deliverystate_wrap.mml_int_us').hide();
1010				form.find('.mml_deliverystate_wrap.mml_int_ca').hide();
1011				form.find('.mml_deliverystate_wrap.mml_int_au').show();
1012				form.find('.mml_deliverystate_wrap.mml_int_us > select').prop('required', false);
1013				form.find('.mml_deliverystate_wrap.mml_int_ca > select').prop('required', false);
1014				form.find('.mml_deliverystate_wrap.mml_int_au > select').prop('required', true);
1015			} else {
1016				select_wrap.removeClass('mml_col-xs-6').addClass('mml_col-xs-12');
1017				form.find('.mml_deliverystate_wrap.mml_int_us').hide();
1018				form.find('.mml_deliverystate_wrap.mml_int_ca').hide();
1019				form.find('.mml_deliverystate_wrap.mml_int_au').hide();
1020				form.find('.mml_deliverystate_wrap.mml_int_us > select').prop('required', false);
1021				form.find('.mml_deliverystate_wrap.mml_int_ca > select').prop('required', false);
1022				form.find('.mml_deliverystate_wrap.mml_int_au > select').prop('required', false);
1023			}
1024		});
1025
1026		// NB! Canada Form - On delivery, take if the state is in CA or US
1027		$('form.mml_form').on('change', 'select[name=mml_delivery_state]', function() {
1028			var form = $(this).closest('form.mml_form');
1029			var state = $(this).val().toLowerCase();
1030
1031			var typeahead_source_url = '';
1032			if (form.attr('data-abr') == 'ca') {
1033				var group_selection = $("option:selected", this).closest('optgroup').attr('id');
1034				if (group_selection == "US") {
1035					form.find('input[name="to_country"]').val('US');
1036					typeahead_source_url = mml_object.plugin + 'assets/typeahead/us/'+state+'.json';
1037				} else {
1038					form.find('input[name="to_country"]').val('CA');
1039					typeahead_source_url = mml_object.plugin + 'assets/typeahead/ca/'+state+'.json';
1040				}
1041
1042			} else if (form.attr('data-abr') == 'us') {
1043				typeahead_source_url = mml_object.plugin + 'assets/typeahead/us/'+state+'.json';
1044			} else if (form.attr('data-abr') == 'au') {
1045				typeahead_source_url = mml_object.plugin + 'assets/typeahead/au/'+state+'.json';
1046			} else {
1047				//console.log(form.attr('data-abr'));
1048			}
1049
1050			$.get( typeahead_source_url, function( data ) {
1051				source = data;
1052			}).fail(function(error){ console.log(error); source = [];});
1053
1054			var input = form.find('input[name="mml_delivery_city"]');
1055			$(input).replaceWith(input.clone());
1056		});
1057
1058		// States in international forms
1059		$('form.mml_form').on('change', '.mml_stateselect', function() {
1060			var form = $(this).closest('form.mml_form');
1061			var state = $(this).val().toLowerCase();
1062
1063			if ($(this).parent().hasClass('mml_originstate_wrap'))
1064			{
1065				country = form.find('select[name=mml_origin_country]').val();
1066				input = form.find('input[name="mml_origin_city"]');
1067			} else {
1068				country = form.find('select[name=mml_destination_country]').val();
1069				input = form.find('input[name="mml_destination_city"]');
1070			}
1071
1072			var typeahead_source_url = '';
1073
1074			typeahead_source_url = mml_object.plugin + 'assets/typeahead/'+country.toLowerCase()+'/'+state+'.json';
1075
1076			$.get( typeahead_source_url, function( data ) {
1077				source = data;
1078			}).fail(function(error){ console.log(error); source = [];});
1079
1080			$(input).replaceWith(input.clone());
1081		});
1082
1083		// Sets for countries without state and resets input autocomplete/typeahead
1084		$('form.mml_form').on('change', '.mml_countryselect', function() {
1085			var form = $(this).closest('form.mml_form');
1086			
1087			if ($(this).parent().hasClass('mml_origincountry_wrap'))
1088			{
1089				input = form.find('input[name="mml_origin_city"]');
1090			} else {
1091				input = form.find('input[name="mml_destination_city"]');
1092			}
1093
1094			var country_val = $(this).val().toLowerCase();
1095
1096			if($.inArray(country_val, autocomplete_countries) != -1) {
1097				typeahead_source_url = mml_object.plugin + 'assets/typeahead/'+country_val+'.json';
1098				$.get( typeahead_source_url, function( data ) {
1099					source = data;
1100				}).fail(function(error){ console.log(error); source = [];});		
1101			}
1102
1103			$(input).replaceWith(input.clone());
1104		});
1105
1106		// For NZ only, since it does not have states
1107		$('form.mml_form').on('focusin', '.mml_to_city_nz > input', function() {
1108			var form = $(this).closest('form.mml_form');
1109			var typeahead_source_url = mml_object.plugin + 'assets/typeahead/nz.json';
1110			var input = $(this);
1111
1112			$.get( typeahead_source_url, function( data ) {
1113				source = data;
1114			})
1115			.fail(function(error){ console.log(error); source = [];})
1116			.done(function(){
1117				try {
1118					input.mml_typeahead({
1119						source: source,
1120						items: 4
1121					});
1122				} catch ( err) {
1123					console.log(err);
1124				}
1125
1126			});
1127		});
1128
1129		$('form.mml_form').on('focus', 'input[name="mml_delivery_city"]', function(){
1130			var form = $(this).closest('form.mml_form');
1131			var abr = form.attr('data-abr');
1132
1133			var input = $(this);
1134			if (source.length > 0 ) {
1135				if (abr !== 'uk') {
1136					// Typeahead Bootstrap 2
1137					try {
1138						input.mml_typeahead({
1139							source: source,
1140							items: 4
1141						});
1142					} catch (error) {
1143						// No Typeahead loaded
1144						console.log(error);
1145					}
1146				}
1147			}
1148
1149		});
1150
1151		$('form.mml_form').on('focusin', 'input.mml_cityfield', function(){
1152			var form = $(this).closest('form.mml_form');
1153			var input = $(this);
1154
1155			if (input.attr('name') == 'mml_origin_city') {
1156				var country = form.find('select[name="mml_origin_country"]').val();
1157			} else {
1158				var country = form.find('select[name="mml_destination_country"]').val();
1159			}
1160
1161			if (country !== null) {
1162				country = country.toLowerCase();
1163			}
1164			if ( $.inArray(country, autocomplete_countries) != -1 || $.inArray(country, autocomplete_states) != -1) {
1165				if (source.length > 0 ) {
1166
1167					// Typeahead Bootstrap 2
1168					try {
1169						input.mml_typeahead({
1170							source: source,
1171							items: 4
1172						});
1173					} catch (error) {
1174						// No Typeahead loaded
1175						console.log(error);
1176					}
1177				}
1178			} else {
1179				autocomplete_ini(input, country);				
1180			}
1181		});
1182
1183		/*
1184		 * PRIVACY POLICY POPUP
1185		 */
1186		$('form.mml_form').on('click', '.mml_infoicon', function(e) {
1187			var form = $(this).closest('form.mml_form');
1188			var popup = form.find('.mml_popup.mml_popup_termsofservice');
1189			var link = $('.mml_popup.mml_popup_termsofservice span.mml_linkservices');
1190			var element;
1191
1192			if (mml_object.default_privacy == '1') {
1193				element = '<a href="' + mml_object.plugin + 'assets/files/mml-tou-pp.htm" target="_blank" rel="nofollow">Read terms of service.</a>';
1194			} else {
1195				element = '<strong>Read terms of service below.</strong>';
1196			}
1197			link.html(element);
1198
1199			if (popup.css('display') == 'none') {
1200				popup.fadeIn(350);				
1201			} else {
1202				popup.fadeOut(350);
1203			}
1204
1205			e.preventDefault();
1206		});
1207		$(document).on('click', function(e) {
1208			var target = $(e.target);
1209			if (!target.is('.mml_infoicon')) {
1210				$('.mml_popup.mml_popup_termsofservice').fadeOut(350);
1211			}
1212		});
1213
1214	}); // Close document Ready
1215
1216}(jQuery));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.