(function( $ ) { $('body').append(''); $('form input[placeholder*="postcode"]:not(.avm-postcode), form input[placeholder*="Postcode"]:not(.avm-postcode), form input.postcode:not(.avm-postcode), form input[name*="postcode"]:not(.avm-postcode)').each(function(){ //Add class to identify + prevent reprocessing $(this).addClass('avm-postcode'); if( $(this).attr('name') == 'input_12' && $('input[name="input_32"]').length && $('input[name="input_32"]').val() !== '' ){ $(this).parent().css('display','none'); }else{ //Identify the parent form the field belongs to var theForm = $(this).parents('form'); var theMsg = "Please enter your postcode..."; //Add a property_id field to the form if there isn't already one. if( !theForm.find('input[name="property_id"]').length ){ var dataAttrib = (typeof($(this).attr('data-id')) !== 'undefined') ? 'data-id="'+$(this).attr('data-id').split('_postcode').join('')+'property_id" ' : ''; theForm.append($('')); } if( !$(this).parent().find('.avm-msg').length ){ $(this).parent().addClass('avm-valid').css('position','relative'); $(this).parent().append(''); } //Prevent the form from submitting without a valid postcode + property ID theForm.on('submit',function(e){ console.log("Submit called...", $(this).find('input[name="property_id"]'), $(this).find('input[name="property_id"]').val() ); if( $(this).attr('id') == 'gform_13' || $(this).find('input[name="property_id"]').val() !== '' ){ //Good to go console.log("Submit"); }else{ //Show error if(e!==null){ e.preventDefault(); } $(this).find('.avm-postcode').val('').addClass('error').removeClass('valid'); $(this).find('.avm-msg > div').text(theMsg); $(this).find('.avm-msg').css('display','').removeClass('has-list'); $('body').removeClass('avm-open'); } }); //Hide the selector if you click somewhere else on the page $(document).click(function(event) { var $target = $(event.target); if( !$target.closest('.avm-msg').length && !$target.closest('.avm-postcode').length && ( $('.avm-msg.has-list').length || $('.avm-msg').css('display') !== 'none') ) { $('.avm-msg').css('display','none'); $('body').removeClass('avm-open'); } }); //Show the selector if you refocus on the postcode field again $(this).on('focus click', function(){ if( $(this).parent().find('.avm-msg').length ){ $(this).parent().find('.avm-msg').css('display',''); $('body').addClass('avm-open'); } }) $(this).on('change keyup', function(){ var formData = $(this).val(); var msgEl = $(this).parent().find('.avm-msg') msgEl.css('display','').css('height',''); var msgElTxt = $(this).parent().find('.avm-msg > div'); var pattern = /\d{1}[A-Z]{2}/; if( formData.split(' ').join('').length >= 5 && pattern.test(formData.split(' ').join('').toUpperCase().slice(-3)) ){ $(msgEl).css('display',''); $(this).removeClass('error').addClass('valid').addClass('spinner'); var postCodeField = $(this); $.post( "/avmprod.php?action=postcode", "data="+escape(formData), function(data) { $(postCodeField).removeClass('spinner'); var response = $.parseJSON(data); if( response.status && response.properties.length > 0 ){ //var validPostcode = response.properties[0].fullAddress.split(',').at(-1).trim(); $(msgElTxt).text(''); if( $(msgElTxt).find('ul').length){ $(msgElTxt).find('ul').empty(); }else{ $(msgElTxt).prepend(''); } var theList = $(msgElTxt).find("ul"); response.properties.forEach(function(item){ var LI = $('
  • '+item.fullAddress+'
  • '); LI.on('click', function(){ //Set the form and post var pId = $(this).attr('data-property-id'); var pAddress = $(this).text().split(','); $(this).parents('form').find('input[name="property_id"]').val(pId); $(this).parents('form').find('input[placeholder*="Address line 1"],input[value="address_line_1"],input[name*="address1"],input[name*="address_1"],input[name*="address_line_1"]').val( pAddress[0].trim() ); $(this).parents('form').find('input[placeholder*="Address line 2"],input[value="address_line_2"],input[name*="address2"],input[name*="address_2"],input[name*="address_line_2"]').val( pAddress[1].trim() ); $(this).parents('form').find('input[placeholder*="Town"],input[value="town"],input[name*="town"],input[name*="city"]').val( pAddress[2].trim() ); if(pAddress.length>4){ $(this).parents('form').find('input[placeholder*="County"],input[value="county"],input[name*="county"],input[name*="state"]').val( pAddress[pAddress.length-2].trim() ); }else{ $(this).parents('form').find('input[placeholder*="County"],input[value="county"]').val(''); } $(msgEl).removeClass('has-list'); if( !$(msgEl).parents('form').find('input[type="email"]').length ){ //Short form, submit it $(msgElTxt).text('Submitting...'); $(msgEl).parent('form').submit(); }else{ //Long form, don't submit $(msgElTxt).text('Address selected'); $(msgEl).css('display','none'); if( $(postCodeField).parent().hasClass('ginput_container') ){ $(postCodeField).parent().addClass('hidden'); } } }); $(theList).append(LI); }); $(msgEl).addClass('has-list'); $('body').addClass('avm-open'); }else{ $(msgElTxt).find('ul').remove(); $(msgElTxt).text('No results...'); $(msgEl).removeClass('has-list'); $('body').removeClass('avm-open'); } } ); }else if( formData.split(' ').join('').length >= 5 ){ $(this).removeClass('valid').addClass('error'); $(msgElTxt).text('Please enter your FULL postcode...'); }else{ $(this).removeClass('valid').removeClass('error'); $(msgElTxt).text(theMsg); } }); } }); })(jQuery);