$(document).ready(function() {
	
	$('a.i-sendf').click(function(){
		$('div.overlay').show();
		$('div.pp-friend').show();
		
		$('div.overlay, div.pp-friend a.close').click(function(){
			closeOverlayFriend();
			return false;
		});
		
		return false;
	});
	
	$('#friend-form').submit(function(){
		$('#friend-result').hide();
		
		result = true;
		
		if (checkFieldFriend('friend_name', 'Имя друга')){
			result = false;
		} 
		if (checkMailFriend('friend_email')){
			result = false;
		} 
		
		if(!result)
		{
			showErrorResultFriend("Поля не заполнены или заполнены неверно!");
		}
		
		return result;
	});
});
	
function closeOverlayFriend()
{
	$('div.overlay').hide();
	$('div.pp-player embed').remove();
	$('div.pp-player').hide();
	$('div.pp-friend').hide();
	$('div.pp-restore').hide();
}

function checkFieldFriend(id, message){
	if($('#'+id).val()=='') {
		$('#'+id).css('border-color', 'red');
		return true;
	}
	else {
		if($('#'+id).val()==message) {
			$('#'+id).attr('style', 'border-color: red');
			return true;
		}
		else {
			$('#'+id).css('border-color', '#cdd9ec');
			return false;
		}
	}
}

function checkMailFriend(id){
	var pat=/^[\w-+\.]+@([\w-]+\.)+[\w-]{2,}$/i;
	if(pat.test($('#'+id).val())){
		$('#'+id).css('border-color', '#cdd9ec');
		return false;
	}
	else{
		$('#'+id).css('border-color', 'red');
		return true;
	}
}

function showSuccessResultFriend(text){
	$('#friend-result').html(text).show();
	
	$('#friend_name').val('Имя друга');
	$('#friend_email').val('E-mail друга');
}

function showErrorResultFriend(text){
	$('#friend-result').html(text).show();
}
