$(function() {
			  
	$('input.single').focus(function(){
		if( $(this).attr('value') == $(this).attr('title') )
		{
			$(this).attr('value','');
		}
	});
	
	$('input.single').blur(function(){
		if( !$(this).attr('value') )
		{
			$(this).attr('value',$(this).attr('title'));
		}
	});

	$('#feedback').submit(function(){
			
			checkForm($($("input[type='text']")[0]));
			checkForm($($("input[type='text']")[1]));
			//checkForm($("textarea"));
			
			if( $( ".inerror", $(this) ).size() )
			{	
				alert("Проверьте правильность заполнения формы!");
				return false;
			}
			else
			{
				data = {};
				action = $(this).attr( 'action' );
				
				$( "input[type='text'], textarea", $(this) ).each(function(i){
			
					var e = $(this);
					var name = e.attr('name');
					var val = e.val();
					data[name] = val;
					
				})
				
				$(this).animate( {opacity: "hide"}, 'fast', function(){
						$("#sendpanel").animate( {opacity: "show"}, 'fast', function(){
								$.post( action, data, function(){
										$("#sendpanel").animate({opacity: "hide"}, 'fast', function(){
											$(this).text("Спасибо! Мы обязательно вам ответим.");
											$(this).fadeIn("3000");
										});
								} );
						} );
				} );

				return false;
			}
	});
	
	function checkForm(e)
	{
		emailRegExp = /^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;
		nameRegExp = /^[a-zа-я\s]{2,20}$/i;
		comRegExp = /.{5,}/i;
		
		var v = e.val();
		var name = e.attr('name');
			
		if( 
			( name == "name" && nameRegExp.test(v) ) ||
			( name == "mail" && emailRegExp.test(v) ) ||
			( name == "question" && v != 'Ваше сообщение*' && comRegExp.test(v) )
		){
			e.removeClass('inerror');
			return 0;
		}
		else
		{
			e.addClass('inerror');
			return 1;
		}
	
	}
	
	function resetForm(form)
	{
		$( "input[name='name']", $(this) ).val('Ваше имя*');
		$( "input[name='mail']", $(this) ).val('Ваше e-mail*');
		$( "textarea[name='question']", $(this) ).val('Ваше сообщение*');
	}

	$( "input[type='text']", $("#feedback") ).blur(function(){checkForm($(this))});
	
	$( "input[type='reset']", $("#feedback") ).blur(function(){resetForm($("#feedback"))});


});