jQuery( document ).ready( function() { jQuery( '.iwacontact' ).iwacontactForm(); } ); jQuery.fn.iwacontactForm = function() { return this.each( function() { the_form = new iwacontactConstruct( jQuery( this ) ); the_form.init(); } ); } function iwacontactConstruct( jQuery_obj ) { return { _dom_elem: null, init: function() { this._dom_elem = jQuery_obj; var contactForm = this._dom_elem; var contactObj = this; // Remove the iwac_no_js field contactForm.find( 'input[name=iwac_no_js]' ).remove(); // Make sure the honeypot anti-bot validation fields are hidden contactForm.find( 'input.iwac_abval' ).css( { width: '1px !important', height: '1px !important', padding: '0px !important', border: 'none !important', } ); // Bind our ajax submit function to our form submit action this._dom_elem.submit( function( e ) { // Prevent form from submitting e.preventDefault(); // Clear any errors from the last submission contactObj._clearFormError(); // Validate the form submission if ( !contactObj._validateSubmission() ) { contactObj._displayFormError( objectL10n.validationError ); return false; } // Submit the form contactObj._submitForm( contactForm.find( 'input[name=iwac_adminajax]' ).val(), contactForm.find( ':input').serialize() ); // Return false so the browser does not submit the form twice return false; } ); }, _validateSubmission: function() { var contactForm = this._dom_elem; var contactObj = this; var validationError = false; var firstError = true; contactForm.find( ':input' ).each( function( i ) { jQuery( this ).removeClass( 'error' ); jQuery( this ).parent().children( '.ajax-feedback' ).remove(); if ( jQuery( this ).hasClass( 'required-field' ) && ( jQuery( this ).val() == '' || jQuery( this ).val() == null ) ) { validationError = true; jQuery( this ).addClass( 'error' ); jQuery( this ).after( '' + objectL10n.requiredField + '' ); jQuery( this ).parent().children( '.ajax-feedback' ).fadeIn(); } else if ( jQuery( this ).hasClass( 'validate-email' ) && !jQuery( this ).val().match( /^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$/i ) ) { validationError = true; jQuery( this ).addClass( 'error' ); jQuery( this ).after( '' + objectL10n.enterValidAddress + '' ); jQuery( this ).parent().children( '.ajax-feedback' ).fadeIn(); } // Focus on the first error field if ( validationError && firstError ) { this.focus(); firstError = false; } } ); return validationError ? false : true; }, _submitForm: function( submitURL, postData ) { var contactForm = this._dom_elem; var contactObj = this; contactObj._startLoading(); jQuery.ajax( { type: 'POST', data: postData, url: submitURL, dataType: 'text json', complete: function() { contactObj._stopLoading(); }, success: function( json ) { if ( json.status == 'success' ) { contactForm.find( '.buttons .ajax-result' ).html( objectL10n.success ); contactForm.find( '.buttons .ajax-result' ).fadeIn(); contactForm.find( ':input' ).attr( 'disabled', 'disabled' ); contactForm.find( '.buttons input[name=iwac_submit]' ).addClass( 'disabled' ); if ( json.redirect != null ) window.location = json.redirect; } else if ( json.status == 'fail' ) { contactObj._displayFormError( json.reason ); // Reload ReCAPTCHA if it's attached to this form if ( contactForm.find( 'table.recaptchatable' ).length > 0 ) Recaptcha.reload(); } else console.log( json ); } } ); }, _displayFormError: function( message ) { this._dom_elem.find( '.buttons .ajax-result' ).addClass( 'error' ); this._dom_elem.find( '.buttons .ajax-result' ).html( message ); this._dom_elem.find( '.buttons .ajax-result' ).fadeIn(); }, _clearFormError: function() { this._dom_elem.find( '.buttons .ajax-result' ).hide(); this._dom_elem.find( '.buttons .ajax-result' ).removeClass( 'error' ); }, _startLoading: function() { this._dom_elem.find( '.ajax-loading' ).show(); }, _stopLoading: function() { this._dom_elem.find( '.ajax-loading' ).hide(); } } }