jQuery( document ).ready( function() {
if ( jQuery( '#iwacontactform' ).length ) {
jQuery( '#iwacontactform' ).submit( function() {
jQuery( '#iwacontactform .buttons .ajax-result' ).hide();
jQuery( '#iwacontactform .buttons .ajax-result' ).removeClass( 'error' );
var validationError = 0;
var firstError = 1;
jQuery( '#iwacontactform :input' ).each( function( i ) {
jQuery( this ).removeClass( 'error' );
jQuery( this ).parent().children( '.ajax-feedback' ).remove();
if ( jQuery( this ).hasClass( 'required-field' ) && jQuery( this ).val() == '' ) {
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();
}
if ( validationError && firstError ) {
this.focus();
firstError = 0;
}
} );
if ( validationError ) {
jQuery( '#iwacontactform .buttons .ajax-result' ).addClass( 'error' );
jQuery( '#iwacontactform .buttons .ajax-result' ).html( "There was an error processing your request" );
jQuery( '#iwacontactform .buttons .ajax-result' ).fadeIn();
return false;
}
jQuery( '#iwacontactform .ajax-loading' ).show();
var postData = jQuery( '#iwacontactform :input').serialize();
var submitURL = jQuery( '#iwacontactform' ).attr( 'action' );
jQuery.ajax( {
type: "POST",
url: submitURL,
data: postData,
success: function( html ) {
jQuery( '#iwacontactform .ajax-loading' ).hide();
jQuery( '#iwacontactform .buttons .ajax-result' ).html( "Your message has been sent successfully!" );
jQuery( '#iwacontactform .buttons .ajax-result' ).fadeIn();
jQuery( '#iwacontactform :input' ).attr( 'disabled', 'disabled' );
jQuery( '#iwacontactform .buttons #submit' ).addClass( 'disabled' );
},
error: function( html, textStatus, errorThrown ) {
jQuery( '#iwacontactform .ajax-loading' ).hide();
jQuery( '#iwacontactform .buttons .ajax-result' ).addClass( 'error' );
jQuery( '#iwacontactform .buttons .ajax-result' ).html( "[" + textStatus + "] There was an error processing your request" );
jQuery( '#iwacontactform .buttons .ajax-result' ).fadeIn();
}
} );
return false;
} );
}
} );