$(function() {
  $('.error').hide();
  $('input.f1').css({backgroundColor:"#FFFFFF"});
  $('input.f1').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.f1').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });
  
  $('textarea.f1').css({backgroundColor:"#FFFFFF"});
  $('textarea.f1').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('textarea.f1').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });  

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var author = $("input#author").val();
		if (author == "") {
      $("label#author_error").show();
      $("input#author").focus();
      return false;
    }
    
		var fio = $("input#fio").val();
		if (fio == "") {
      $("label#fio_error").show();
      $("input#fio").focus();
      return false;
    }   
        
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    } else {
    
      re = new RegExp("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$", "g");
      parts = re.exec(email); 
      
		if (parts == null)
    	{
	      $("label#email_incorrect").show();
	      $("input#email").focus();
	      return false;
		}     
    }
    
		var tel = $("input#tel").val();
		if (tel == "") {
      $("label#tel_error").show();
      $("input#tel").focus();
      return false;
    }

		var commentz = $("textarea#commentz").val();
		if (commentz == "") {
      $("label#commentz_error").show();
      $("textarea#commentz").focus();
      return false;
    }   
    
    
		
		var dataString = 'author='+ author + '&email=' + email + '&tel=' + tel + '&fio=' + fio + '&commentz=' + commentz;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "/bin/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h1>Ваше сообщение успешно отправлено</h1>")
        .append("<p>Спасибо за внимание! Мы с Вами обязательно свяжемся.</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
