$(document).ready(function() {
  $('.error').hide();

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var name = $("input#ExpertFullName").val();
		if (name == "" || name == "Full name") {
      $("label#ExpertFullName_error").show();
      $("input#ExpertFullName").focus();
      return false;
    }
		var email = $("input#ExpertEmail").val();
		if (email == "" || email == "Email") {
      $("label#ExpertEmail_error").show();
      $("input#ExpertEmail").focus();
      return false;
    }
		var question = $("textarea#ExpertYourQuestion").val();
		if (question == "" || question == "Your question") {
      $("label#ExpertYourQuestion_error").show();
      $("textarea#ExpertYourQuestion").focus();
      return false;
    }
		
		var dataString = 'last_name='+ name + '&email=' + email + '&field0=' + question + '&form_id=2';
		// alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "/form/receive",
      data: dataString,
      success: function(data) {
        $('#contact_form').html("<div id='ask-expert-success'></div>");
        $('#ask-expert-success').html("<h3 id='hdr-ask-expert'>Question? Ask an Expert</h3>")
        .append("<div id='expert-success'>Thank you for your question. We will be in touch shortly with a response.</div>")
        .hide()
        .fadeIn(500, function() {
          $('#ask-expert-success');
        });
      }
     });
    return false;
	});
	
	$('.autoclear').autoClear();
});
// runOnLoad(function(){
  // $("input#ExpertFullName").select().focus();
// });

// Clears the default text when an input receives
// focus and reinstates it if it is left blank
(function($) {

    $.fn.autoClear = function() {	
        return this.each(function() {
            $(this).focus(function() {
                if( this.value == this.defaultValue ) {
                    this.value = "";
                }
            })
            .blur(function() {
                if( !this.value.length ) {
                    this.value = this.defaultValue;
                }
            });
        });
    };

})(jQuery);
