jQuery( document ).ready( function() {
if ( jQuery( '.iwacontact' ).length > 0 ) {
jQuery( '.iwacontact' ).submit( function() {
var currentForm = jQuery( this );
currentForm.find( '.buttons .ajax-result' ).hide();
currentForm.find( '.buttons .ajax-result' ).removeClass( 'error' );
var validationError = 0;
var firstError = 1;
var botAttempt = 0;
currentForm.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 = 1;
jQuery( this ).addClass( 'error' );
jQuery( this ).after( 'This field is required' );
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 = 1;
jQuery( this ).addClass( 'error' );
jQuery( this ).after( 'Please enter a valid email address' );
jQuery( this ).parent().children( '.ajax-feedback' ).fadeIn();
}
else if ( ( jQuery( this ).hasClass( 'iwac_abval' ) || jQuery( this ).hasClass( 'iwac_abval_two' ) ) && jQuery( this ).val().length > 0 ) {
// One or more of our concealed anti-bot validation fields have been
// given values, this means a bot is trying to use our form!
validationError = 1;
botAttempt = 1;
jQuery( this ).addClass( 'error' );
jQuery( this ).after( 'You are a bot!' );
jQuery( this ).parent().children( '.ajax-feedback' ).fadeIn();
}
if ( validationError && firstError && !botAttempt ) {
this.focus();
firstError = 0;
}
} );
if ( validationError ) {
currentForm.find( '.buttons .ajax-result' ).addClass( 'error' );
currentForm.find( '.buttons .ajax-result' ).html( "There was an error processing your request" );
currentForm.find( '.buttons .ajax-result' ).fadeIn();
return false;
}
currentForm.find( '.ajax-loading' ).show();
var postData = currentForm.find( ':input').serialize();
var submitURL = currentForm.attr( 'action' );
jQuery.ajax( {
type: "POST",
url: submitURL,
data: postData,
complete: function( html ) {
currentForm.find( '.ajax-loading' ).hide();
currentForm.find( '.buttons .ajax-result' ).html( "Your message has been sent successfully!" );
currentForm.find( '.buttons .ajax-result' ).fadeIn();
currentForm.find( ':input' ).attr( 'disabled', 'disabled' );
currentForm.find( '.buttons button[type=submit]' ).addClass( 'disabled' );
}
} );
return false;
} );
}
} );