function validEmail(email) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(email);
}

function przeliczWalute()
{

	box = document.getElementById("p_waluta");
	waluta = box.options[box.selectedIndex].value;

	document.getElementById("p_ilosc").value = document.getElementById("p_ilosc").value.replace(new RegExp("[^0-9\.]{1,}", "gi"), '');

	if ( document.getElementById("p_ilosc").value > 0 )
	{

		document.getElementById("p_wynik").innerHTML = parseFloat(waluta * document.getElementById("p_ilosc").value).toFixed(4);

	}

}

function inputDefault(obj, text)
{

	if ( obj.value == text )
	{

		obj.value = "";

	}
	else if ( obj.value == "" )
	{

		obj.value = text;

	}

}

function userlogin(id)
{

	if ( document.getElementById("USR_login_login").value.indexOf('@', 0) == -1 )
	{

		alert("Błąd: login musi mieć postać adresu e-mail! Przykładowo: uzytkownik@domena.pl.");
		return false;

	}


	if ( document.getElementById("USR_login_passwd").value == "" )
	{

	    alert("Błąd: uzupełnij pole z hasłem!");
	    return false;

	}

	document.getElementById(id).submit();

}

function usrmessage_delete(id, user)
{

	var agree = confirm("Czy jesteś pewien, że chcesz usunąć wiadomość od użytkownika " + user + "? Będzie to operacja bezpowrotna!");

	if (agree)
	{

		location.href='?mod=user&do=inbox&deletemsg=' + id;

	}

}

function usrmessage_send(id)
{

	if ( document.getElementById("UMS_text").value.length < 4 )
	{

	    alert("Błąd: tekst wiadomości jest za krótki!");
	    return false;

	}

	document.getElementById(id).submit();

}

function delete_avatar(msg)
{

	var agree = confirm(msg);

	if (agree)
	{

		location.href='?mod=user&do=edit_account&avatar=delete';

	}

}

function edit_account(id)
{

	if ( document.getElementById("USR_passwd").value != "" )
	{

		if ( document.getElementById("USR_passwd").value.length < 6 )
		{

		    alert("Błąd: hasło musi zawierać przynajmniej 6 znaków!");
		    return false;

		}

		if ( document.getElementById("USR_passwd").value != document.getElementById("USR_passwd2").value )
		{

		    alert("Błąd: hasło i jego potwierdzenie się różnią!");
		    return false;

		}

	}

	if ( document.getElementById("USR_email").value != "" )
	{

		if ( document.getElementById("USR_email").value.indexOf('@', 0) == -1 )
		{

			alert("Błąd: niepoprawny adres poczty elektronicznej!");
			return false;
		}

	}

	if ( document.getElementById("USR_www").value != "" )
	{

		if ( document.getElementById("USR_www").value.indexOf('http://', 0) == -1 )
		{

			alert("Błąd: zły adres strony WWW. Prawidłowy ma postać: http://strona.pl!");
			return false;

		}

		if ( document.getElementById("USR_www").value.length < 12 )
		{

			alert("Błąd: adres strony WWW jest zbyt krótki!");
			return false;

		}

	}

	document.getElementById(id).submit();

}

function create_account(id)
{

	if ( !validEmail(document.getElementById("USR_login").value))
	{

	    alert("Błąd: niepoprawny adres poczty elektronicznej!");
	    return false;

	}

	if ( document.getElementById("USR_name").value.length < 2 )
	{

	    alert("Błąd: brak podpisu!");
	    return false;

	}

	if ( document.getElementById("USR_login").value.indexOf('@', 0) == -1 )
	{

		alert("Błąd: niepoprawny adres poczty elektronicznej!");
		return false;
	}

	if ( document.getElementById("USR_passwd").value.length < 6 )
	{

	    alert("Błąd: hasło musi zawierać przynajmniej 6 znaków!");
	    return false;

	}

	if ( document.getElementById("USR_passwd").value != document.getElementById("USR_passwd2").value )
	{

	    alert("Błąd: hasło i jego potwierdzenie się różnią!");
	    return false;

	}

	document.getElementById(id).submit();

}

function showText(type, child)
{

	advAJAX.get({
		url: "index.php",
		parameters : {
			"mod" : "listnews",
			"type" : type,
			"filter" : child
		},
		mimeType: 'text/plain',
		onLoading : function(obj) {
			document.getElementById("playground").innerHTML = "";
			document.getElementById("IMG_loading").style.display = "block";
		},
		onComplete : function(obj) {
			document.getElementById("IMG_loading").style.display = "none";
		},
		onSuccess: function(obj) {

			document.getElementById("playground").innerHTML = obj.responseText;
			for (i=1; i<=3; i++)
			{

				document.getElementById("boxtitle_" + i).className = "boxtitles boxtitlesnoactive";

			}

			document.getElementById("boxtitle_" + type).className = "boxtitles";

		}

	});

}

function sendForm(id)
{

	if ( document.getElementById("name").value.length < 2 )
	{

	    alert("Błąd: brak podpisu...");
	    return false;

	}

	if ( !validEmail(document.getElementById("email").value) )
	{

	    alert("Błąd: niepoprawny adres e-mail");
	    return false;

	}

	if ( document.getElementById("text").value.length < 10 )
	{

	    alert("Błąd, wiadomość jest za krótka...");
	    return false;

	}

	input = document.createElement("input");
	input.type = "hidden";
	input.value = id;
	input.name = "message_send";
	document.getElementById(id).appendChild(input);

	document.getElementById(id).submit();

}

function addComment(formid, nid)
{

	if ( document.getElementById("comment_name").value.length < 2 )
	{

	    alert("Błąd: brak podpisu...");
	    return false;

	}

	if ( document.getElementById("comment_text").value.length < 2 )
	{

	    alert("Błąd: komentarz nie zawiera treści...");
	    return false;

	}

	input = document.createElement("input");
	input.type = "hidden";
	input.value = nid;
	input.name = "comment_add";
	document.getElementById(formid).appendChild(input);

	document.getElementById("comment_form").submit();

}

jQuery.cookie = function (key, value, options) {

	if (arguments.length > 1 && String(value) !== "[object Object]") {
		options = jQuery.extend({}, options);

		if (value === null || value === undefined) {
			options.expires = -1;
		}

		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}

		value = String(value);

		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? value : encodeURIComponent(value),
			options.expires ? '; expires=' + options.expires.toUTCString() : '',
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(''));
	}

	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jQuery.banner = function(data)
{

	var data = jQuery.parseJSON($.toJSON(data));
	var size = data.size.split("x");

	if (!$.cookie(data.id) || data.src[$.cookie(data.id)] == undefined)
	{

		$.cookie(data.id, 0);

	}
	else
	{

		if (data.src[parseInt($.cookie(data.id)) + 1] == undefined)
		{

			$.cookie(data.id, 0);

		}
		else
		{

			$.cookie(data.id, parseInt($.cookie(data.id)) + 1);

		}

	}

	if (data.src[$.cookie(data.id)].indexOf(".swf") != -1)
	{

		swfobject.embedSWF(data.src[$.cookie(data.id)], data.id, size[0], size[1], "9.0.0", "", "{'wmode': 'transparent'}");

	}
	else
	{

		var img = data.src[$.cookie(data.id)].split("##");
		var url = '';

		if (img[1])
		{

			url = '<a href="'+ img[1] +'"><img src="'+ img[0] +'" alt="" /></a>';

		}
		else
		{

			url = '<img src="'+ data.src[$.cookie(data.id)] +'" alt="" />';

		}

		$("#" + data.id).html(url);

	}

}

$(document).ready(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast',
		showTitle: false,
		allowresize: false,
		hideflash: true,
		theme:'facebook'
	});

	$('#blogs-heads img').tipsy({gravity: 's'});
	// $('#sportsbanner_map area').tipsy({gravity: 's'});
	$('#sportozpn li').tipsy({gravity: 's'});

});

function VideoLaunch(params) {
	$("#VideoLink").attr('href', params);
	$("#VideoLink").trigger('click');
}
