/**
 * Snepo contact JS
 *
 * Here we provide an AJAX implementation for the contact form, with a normal POST fallback
 */
$(function() {
  
  // Capture the Form submission
  $( 'form.snepo_contact_form' ).submit(function() {
    // Disable the form and notify the user we're sending
    $( this ).find( 'input[type=submit]' )
             .attr( 'disabled', 'disabled' )
             .val( 'Hold on!' );
    
    // Get form data
    var data = $( this ).serialize();
    
    // Fix scope
    var form_element = this;
    
    // POST and receive
    $.ajax({
      type: 'POST',
      url: $( this ).attr( 'action' ),
      data: data,
      cache: false,
      dataType: 'json',
      success: function( data ) {
        // Check for success
        if ( data.success )
        {
          // Success message
          $( form_element ).find( 'input[type="submit"]' ).val('Thanks!');
        } else {
          // Error message
          $( form_element ).find( 'input[type="submit"]' ).val('Oops!');
        }
      },
      error: function() {
        // Error message
        $( form_element ).find( 'input[type="submit"]' ).val('Oops!');
      },
      complete: function() {
        // Enable the form
        $( form_element ).find( 'input[type="submit"]' ).removeAttr('disabled');
      }
    });
    
    // Return false to stop submission
    return false
  });
  
});
