String.prototype.trim = function()
{
	return this.replace(/(^\s+)|(\s+$)/g, "");
}

$.fn.checkForm = function() 
{ 
	var surname = $("input[name='surname']").attr("value");
	var name = $("input[name='name']").attr("value");
	var email = $("input[name='email']").attr("value");
	var distance = ($("input[name='distance']").attr("value"));
	var country = $("input[name='country']").attr("value");
	var city = $("input[name='city']").attr("value");
	
	surname = surname.trim();
	name = name.trim();
	email = email.trim();
	country = country.trim();
	city = city.trim();
	
	distance = parseInt(distance);
	if (distance == '' || isNaN(distance)) 
	{
		distance = 0;  
	}                             
	
	var emailPattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	var success = true;
	
	if (surname == '' || surname == ' ')
	{
		$("#notifications .surnameError").slideDown();
		success = false;
	}
	else
	{
		$("#notifications .surnameError").slideUp();
	}
	
	if (name == '' || name == ' ')
	{
		$("#notifications .nameError").slideDown();
		success = false;
	}
	else
	{
		$("#notifications .nameError").slideUp();
	}
	
	if (!email.match(emailPattern))
	{
		$("#notifications .emailError").slideDown();
		success = false;
	}
	else
	{
		$("#notifications .emailError").slideUp();
	}
	
	if (distance < 1 || distance > 100)
	{
		$("#notifications .distanceError").slideDown();
		success = false;
	}
	else
	{
		$("#notifications .distanceError").slideUp();
	}
	
	if (country == '' || country == ' ')
	{
		$("#notifications .countryError").slideDown();
		success = false;
	}
	else
	{
		$("#notifications .countryError").slideUp();
	}
	
	if (city == '' || city == ' ')
	{
		$("#notifications .cityError").slideDown();
		success = false;
	}
	else
	{
		$("#notifications .cityError").slideUp();
	}
	
	return success;
}

$.fn.immediateCheck = function() 
{ 
	$("input[name='surname']").keyup(function () {
		$("form").checkForm()  
	});
	
	$("input[name='name']").keyup(function () {
		$("form").checkForm()  
	});
	
	$("input[name='email']").keyup(function () {
		$("form").checkForm()  
	});
	
	$("input[name='distance']").keyup(function () {
		$("form").checkForm()  
	});
	
	$("input[name='country']").keyup(function () {
		$("form").checkForm()  
	});
	
	$("input[name='city']").keyup(function () {
		$("form").checkForm()  
	});
}

$(document).ready(function(){
	
	$("#menVsWomen").click(function() {
		$("#youTube").slideToggle(50);
	});
	
	$("textarea[name='comment']").keyup(function () {
		var value = $("textarea[name='comment']").val();
		var charsLeft = 60 - value.length;
		$("#charsLeft").text("Осталось " + charsLeft);
		
		if (value.length > 60) 
		{
			$("textarea[name='comment']").val(value.substring(0, 60));
			$("#charsLeft").text("Осталось 0");
		} 
	});
	
	$("#faqSwitcher").click(function() {
		$(".faq").slideToggle(50);
	});
	
	$(".list .delete").click(function() {
		$id = $(this).find("input").attr("value");
		$kilometer = $(this).parent();
		
		$(this).children("img").attr("src", "/images/ajax-loader.gif");
		
		$.getJSON("delete/" + $id + "/", function(data) {
			if (data.success == 'true')
			{
				$kilometer.hide();
			}  
		});    
	});
	
	$("form").submit(function(e) {
		if ($("form").checkForm())
		{
			return true;
		}
		else
		{
			$("form").immediateCheck();
			return false;
		}
	});
});
 

