$(document).ready
(
	function()
	{
		$(document).pngFix();
		
		$('.tooltip').tooltip
		(
			{
				track: true,
				delay: 0,
				showURL: false,
				showBody: " - ",
				fade: 150
			}
		);
		
		$('#searchfield').focus
		(
			function ()
			{
				if ($(this).val() == 'zadajte hľadaný výraz')
				{
					$(this).val('');
				}
			}
		);
		
		$('#searchfield').blur
		(
			function ()
			{
				if ($(this).val() == '')
				{
					$(this).val('zadajte hľadaný výraz');
				}
			}
		);
		
		$('#search_form').submit
		(
			function ()
			{
				var submit = true;
				
				if (submit && ($('#searchfield').val() == 'zadajte hľadaný výraz' || $('#searchfield').val() == ''))
				{
					alert('Zadajte hľadaný výraz');
					$('#searchfield').focus();
					submit = false;
				}
				
				if (submit)
				{
					disable_button('search_submit');
				}
				
				return submit;
			}
		);
		
		$('#newsletter_submit').submit
		(
			function ()
			{
				var submit = true;
				
				if (submit && $('#first_name').val() == '')
				{
					alert('Zadajte svoje meno');
					$('#first_name').focus();
					submit = false;
				}
				
				if (submit && $('#surname').val() == '')
				{
					alert('Zadajte svoje priezvisko');
					$('#surname').focus();
					submit = false;
				}
				
				if (submit && $('#e_mail').val() == '')
				{
					alert('Zadajte svoju e-mailovú adresu');
					$('#e_mail').focus();
					submit = false;
				}
				
				if (submit && !check_email_address($('#e_mail').val()))
				{
					alert('Skontrolujte zadanú e-mailovú adresu');
					$('#e_mail').focus();
					submit = false;
				}
				
				return submit;
			}
		);
		
		$('#bonusove_materialy').click
		(
			function ()
			{
				$.ajax
				(
					{
						type: 'GET',
						url: '/editor/scripts/ajax/boxy/check_e_mail.ajax.php',
						data: '',
						success: function(req)
						{
							if (req == 'T')
							{
								window.location.href = '/502';
							}
							else
							{
								Boxy.load
								(
									'/editor/scripts/ajax/boxy/form.ajax.php',
									{
										modal: true,
										closeable: true,
										title: 'Zadajte svoju e-mailovú adresu',
										closeText: '[ Zavrieť ]'
									}
								);
							}
						}
					}
				);
				return false;
			}
		);
		
		$('#bonus_submit').click
		(
			function ()
			{
				var submit = true;
				$('#e_book_0').val($('input[name="e_book"]:radio:checked').val());
				
				if ($('#e_book_0').val() > '0')
				{
					if (submit == true && ($('#nl1').val() == '' || !check_email_address($('#nl1').val())))
					{
						alert('Zadajte prvú e-mailovú adresu.');
						$('#nl1').focus();
						submit = false;
					}
					
					if (submit == true && $('#e_mail_1').val() == '0')
					{
						alert('Vyberte bonus pre prvú e-mailovú adresu.');
						$('#e_mail_1').focus();
						submit = false;
					}
					
					if (submit == true && ($('#nl2').val() == '' || !check_email_address($('#nl2').val())))
					{
						alert('Zadajte druhú e-mailovú adresu.');
						$('#nl2').focus();
						submit = false;
					}
					
					if (submit == true && $('#e_mail_2').val() == '0')
					{
						alert('Vyberte bonus pre druhú e-mailovú adresu.');
						$('#e_mail_2').focus();
						submit = false;
					}
					
					if (submit == true && ($('#nl3').val() == '' || !check_email_address($('#nl3').val())))
					{
						alert('Zadajte tretiu e-mailovú adresu.');
						$('#nl3').focus();
						submit = false;
					}
					
					if (submit == true && $('#e_mail_3').val() == '0')
					{
						alert('Vyberte bonus pre tretiu e-mailovú adresu.');
						$('#e_mail_3').focus();
						submit = false;
					}
					
					if (submit == true && $('#nl4').val() != '')
					{
						if (submit == true && !check_email_address($('#nl4').val()))
						{
							alert('Zadajte štrvtú e-mailovú adresu.');
							$('#nl4').focus();
							submit = false;
						}
						
						if (submit == true && $('#e_mail_4').val() == '0')
						{
							alert('Vyberte bonus pre štrvtú e-mailovú adresu.');
							$('#e_mail_4').focus();
							submit = false;
						}
					}
					
					if (submit == true && $('#nl5').val() != '')
					{
						if (submit == true && !check_email_address($('#nl5').val()))
						{
							alert('Zadajte piatu e-mailovú adresu.');
							$('#nl5').focus();
							submit = false;
						}
						
						if (submit == true && $('#e_mail_5').val() == '0')
						{
							alert('Vyberte bonus pre piatu e-mailovú adresu.');
							$('#e_mail_5').focus();
							submit = false;
						}
					}
					
					if (submit == true)
					{
						$.ajax
						(
							{
								type: 'GET',
								url: '/editor/scripts/ajax/boxy/check_e_mail_3.ajax.php',
								data: 'e_mail_1=' + $('#nl1').val() + '&e_mail_2=' + $('#nl2').val() + '&e_mail_3=' + $('#nl3').val() + '&e_mail_4=' + $('#nl4').val() + '&e_mail_5=' + $('#nl5').val(),
								success: function(req)
								{
									var state = req.getElementsByTagName('state')[0].childNodes[0].nodeValue;
									var remove = req.getElementsByTagName('remove')[0].childNodes[0].nodeValue;
									
									if (state == 'T')
									{
										$('#bonus_form').submit();
									}
									else
									{
										if (remove != '0')
										{
											alert('Nasledovné e-mailové adresy sú už registrované: ' + remove);
										}
									}
								}
							}
						);
					}
				}
				else
				{
					alert('Vyberte svoj bonusový dokument.');
				}
				
				return submit;
			}
		);
		
		$('.emoticons img').click
		(
			function ()
			{
				$('#message').val($('#message').val() + ' ' + $(this).attr('alt') + ' ');
				$('#message').focus();
			}
		);
		
		$('#guestbook_form').submit
		(
			function ()
			{
				var submit = true;
				
				if (submit && $('#name').val() == '')
				{
					alert('Zadajte svoje meno');
					$('#name').focus();
					submit = false;
				}
				
				if (submit && $('#subject').val() == '')
				{
					alert('Zadajte predmet spávy');
					$('#subject').focus();
					submit = false;
				}
				
				if (submit && $('#e_mail').val() == '')
				{
					alert('Zadajte e-mailovú adresu.');
					$('#e_mail').focus();
					submit = false;
				}
				
				if (submit && !check_email_address($('#e_mail').val()))
				{
					alert('Skontrolujte svoju e-mailovú adresu');
					$('#e_mail').focus();
					submit = false;
				}
				
				if (submit && $('#tmp_captcha_code').val() == '')
				{
					alert('Prepíšte text z obrázku do poľa!');
					$('#tmp_captcha_code').focus();
					submit = false;
				}
				
				if (submit)
				{
					disable_button('guestbook_submit');
				}
				
				return submit;
			}
		);
		
		$('a[rel="lightbox"]').lightBox();
	}
);

function disable_button(button_id)
{
	$('#' + button_id).attr('disabled', 'disabled');
	$('#' + button_id).val('Čakajte prosím');
}

function check_email_address(email)
{
	var re = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	if (!re.test(email))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function e_mail1_submit()
{
	var e_mail = document.getElementById('e_mail1');
	var form_content = document.getElementById('form_content');
	
	if (e_mail.value != '' && check_email_address(e_mail.value))
	{
		$.ajax
		(
			{
				type: 'GET',
				url: '/editor/scripts/ajax/boxy/check_e_mail_2.ajax.php',
				data: 'e_mail=' + e_mail.value,
				success: function(req)
				{
					if (req == 'T')
					{
						window.location.href = '/502';
					}
					else
					{
						alert('Nie ste registrovaný v našom newslettri, vyplňte prosím registračný formulár.');
					}
				}
			}
		);
	}
	else
	{
		alert('Skontrolujte zadanú adresu.');
	}
}