$(document).ready(function(){
	$isOpend = window.location.href.indexOf('#kontakt');

    if ($isOpend > 0) {
        $("#emailForm").fadeIn(400);
        $('#sendMail').html('ukryj formularz');
    }

	$('#sendMail').click(function() {
        
		if($("#emailForm").css("display") == 'none'){
			$("#emailForm").fadeIn(400);
			$('#sendMail').html('ukryj formularz');
		}else{
			$("#emailForm").fadeOut(400);
			$('#sendMail').html('wyślij wiadomość e-mail');
		}
		
	});
	
	
	$('button#send').click(function(){
		$('#mailFormError').html('');
		var error = false;
		if($('input#name').val().length < 1) 
		{ 
			error = true;
			$('#mailFormError').append('<p>[Imię i nazwisko] Pole nie może być puste</p>'); 
		}
		if($('input#email').val().length < 1) 
		{
			error = true;
			$('#mailFormError').append('<p>[Twój adres e-mail] Pole nie może być puste</p>'); 
		}else{
			var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
			if(!regex.test($('input#email').val())){
				error = true;
				$('#mailFormError').append('<p>[Twój adres e-mail] Podany adres jest niepoprawny</p>'); 
			}
		}
		if($('textarea#message').val().length < 1) 
		{
			error = true;
			$('#mailFormError').append('<p>[Wiadomość] Pole nie może być puste</p>'); 
		}
		if($('div#recivers').html().length < 29)
		{
			error = true;
			$('#mailFormError').append('<p>[Odbiorcy] Pole nie może być puste'); 
		}
		
		if (error) 
		{
			return false
		}	
	});

	$('a#actualReciverAdd').click(function(){
		$('div#recivers').append('<div class="actualReciver" id="'+ $('#inform_page').attr('class') +'"> &raquo; '+ $('#main-title').html() +'<input type="hidden" name="recivers[]" value="'+$('#actualEmailAddress').html()+'"/><span style="margin-left:10px;"><a onclick=\'deleteEmailAddress('+ $('#inform_page').attr('class') +')\' href="javascript:void(0)">usuń</a></span></div>');
		$('a#actualReciverAdd').css('display','none');
	});
	
	$('a#addAllRecivers').click(function(){
		$.ajax({
	        url: '/slOffers/index/getallfavorites',
	        type: 'GET',
	        success: function (html) {
				$('div#recivers span#favorites').html(html);  
	        }
	    });
	});
	
	
});

function deleteEmailAddress(id)
{
	if($('div#'+id).attr('class') == 'actualReciver'){
		$('a#actualReciverAdd').css('display','block');
	}
	$('div#'+id).remove();
}

