jQuery(function(){

  var ajax_loader = jQuery('<img class="ajax-loader" src="images/ajax-loader.gif" alt="loading..." />');
  ajax_loader.appendTo('body').hide();

  jQuery('#contact-form').submit(function(){
    var submit_button = jQuery('input[type=submit]');
    var data = {
      'name'      : jQuery('#name').val(),
      'company'   : jQuery('#company').val(),
      'phone'     : jQuery('#phone').val(),
      'email'     : jQuery('#email').val(),
      'address'   : jQuery('#address').val(),
      'city'      : jQuery('#city').val(),
      'zip'       : jQuery('#zip').val(),
      'state'     : jQuery('#state').val(),
      'comments'  : jQuery('#comments').val(),
      'subscribe' : jQuery("#mailing-list").attr('checked') ? 'yes' : 'no'
    };
    var contact_type = jQuery('input[name=contact_type]:checked').val();
    if (contact_type !== undefined && contact_type !== null) {
      data['contact_type'] = contact_type;
    }
    ajax_loader.insertAfter(submit_button).fadeIn(function(){
      submit_button.attr('disabled', 'disabled');
      jQuery.post('ajax_contact.php', data, function(response){
        ajax_loader.fadeOut(function(){
          switch (response.status) {
            case 'success':
              jQuery('form').slideUp(function(){
                alert(response.message);
                jQuery(this).remove();
              });
              break;
            case 'errors':
              alert(response.message);
              submit_button.attr('disabled','');
              break;
          }
        });
      }, 'json');
    });
    return false;
  });
});