$(document).ready(function(){	
	$('.feedback .button').bind("click", function(){
		 feedback();
	});
});

function feedback()
{
	oObject = $('.feedback #object');
	oName = $('.feedback #name');
	oEmail = $('.feedback #email');
	oPhone = $('.feedback #phone');
	oText = $('.feedback #text');

	object = trim($('.feedback #object').val());
	name = trim($('.feedback #name').val());
	email = trim($('.feedback #email').val());
	phone = trim($('.feedback #phone').val());
	text = trim($('.feedback #text').val());
	
	if (name == '' || !is_email(email) || !is_phone(phone) || text == '')
	{
		if (name == '')
		{
			oName.stop();
			oName.animate({ backgroundColor: '#FF0000' }, 1000).animate({ backgroundColor: '#FFFFFF' }, 1000);
		}
		
		if (!is_email(email))
		{
			oEmail.stop();
			oEmail.animate({ backgroundColor: '#FF0000' }, 1000).animate({ backgroundColor: '#FFFFFF' }, 1000);
		}
		
		if (!is_phone(phone))
		{
			oPhone.stop();
			oPhone.animate({ backgroundColor: '#FF0000' }, 1000).animate({ backgroundColor: '#FFFFFF' }, 1000);
		}
		
		if (text == '')
		{
			oText.stop();
			oText.animate({ backgroundColor: '#FF0000' }, 1000).animate({ backgroundColor: '#FFFFFF' }, 1000);
		}
	}
	else
	{
		$('.feedback').fadeOut(300);

		$('.feedback').load('/ajax/feedback.php', {object: object, name: name, email: email, phone: phone, text: text}, function () {
			$('.feedback').fadeIn(300).delay(3000).fadeOut(300, function () {
				$('.feedback').load('/ajax/feedback.tpl').fadeIn(300).bind("click", function(){
					feedback();
				});;
			});
		});
	}
}

function trim(str, chars) {
	return str == null ? null : ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str == null ? null : str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str == null ? null : str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function is_email(email){
	var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
	if(result > -1){ return true; } else { return false; }
}

function is_phone(phone){
	return trim(phone) == '' ? false : true;
	/*var result = phone.search(/^(\+\d{3,8})?(\(\d+\))?(\d{5,12})(\-\d{3,8})?$/);
	if(result > -1){ return true; } else { return false; }*/
	
}
