(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('