$(document).ready(function() {
  //delay setting the cursor until window is loaded
  $('#submit').click(send_form).css('cursor', 'pointer');
});

function send_form() {
  //try to send message
  //update status indicator
  $("#error_message").hide();
  $("#submit").unbind('click', send_form).css('cursor', 'default')
    .attr("src", "pics/contact_form_activity_indicator.gif");
    
  //send message
  $.ajax({ url: 'contact_form_processor.php',
           type:'POST',
           cache: false,
           data: { name: $("input.input[name=name]").val(),
                   phonenumber: $("input.input[name=phonenumber]").val(),
                   email: $("input.input[name=email]").val(),
                   message: $("#message").val() 
                   },
           dataType: 'json',
           success: ajax_complete,
           error: ajax_complete 
  });
}

function ajax_complete(data, status) {
  if (!(data instanceof Object))
    data = new Object();
  if (!data.success || status != 'success')
  {
    //error sending message
    //update status indicators
    var error_text = data.error || 'Error; Please try again.';
    $("#error_message").html(error_text).show();
    $("#submit").attr("src", "pics/submit.png")
      .click(send_form).css('cursor', 'pointer');
  }
  else
  {
    //successfully sent message
    $("#submit").hide();
    $("#error_message").css('color', '#3d1f0d')
      .html("THANK YOU! Your message has been successfully sent.")
      .show();
    $(".input").add("#message").val("");
  }
}

