$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#ffede6"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $('textarea.text-input').css({backgroundColor:"#FFFFFF"});
  $('textarea.text-input').focus(function(){
    $(this).css({backgroundColor:"#ffede6"});
  });
  $('textarea.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
		var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }
	
	var messagetext = $("textarea#message").val();
		if (messagetext == "") {
			$("label#message_error").show();
			$("textarea#message").focus();
			return false;
		}
		
		var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&messagetext=' + messagetext;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "scripts/form.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message-send'></div>");
        $('#message-send').html("<img src='http://www.zawady.eu/images/mail_48.gif' style='float:left; padding-right:8px; vertical-align:middle' /><h3>Wiadomość została wysłana!</h3>")
        .append("<p>Odezwiemy się najszybciej jak to możliwe.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message-send').append("");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
