var validMail=/^[a-zA-Z0-9._-]+\@([a-zA-Z0-9_-]+\.){1,}[\w]{1,5}$/;
var validPass=/^[a-zA-Z0-9]+$/;
var validZdjecie=/^(.+)\.(jpg|JPG|png|PNG|gif|GIF)$/;

$(document).ready(function(){

	 /*$('.sifr').flash(
		{
			src: '/js/sifr.swf',
			wmode:'opaque' ,
			flashvars: {
				css: [
					'* {font-weight:bold;color:#1a1d24}'
				].join(' ')
			}
		},
		{ version: 7 },
		function(htmlOptions) {

			htmlOptions.flashvars.txt = this.innerHTML;//.toUpperCase();
			this.innerHTML = '<div style="height:26px;">'+this.innerHTML+'</div>';

			var $alt = $(this.firstChild);
			htmlOptions.height = $alt.height();
			htmlOptions.width = $alt.width();
			$alt.addClass('alt');
			$(this)
				.addClass('flash-replaced')
				.prepend($.fn.flash.transform(htmlOptions));
		}
	);*/

	if(document.getElementById("powrot_do_listy_firm")!=undefined)
        if(history.length>1)document.getElementById("powrot_do_listy_firm").innerHTML='<a href="javascript:history.go(-1)">powrót do listy</a>';
        else document.getElementById("powrot_do_listy_firm").innerHTML = '<a href="/firmy-i-instytucje.html">powrót do listy</a>';

	if(document.getElementById("powrot_do_listy_ogl")!=undefined)
        if(history.length>1)document.getElementById("powrot_do_listy_ogl").innerHTML='<a href="javascript:history.go(-1)">powrót do listy</a>';
        else document.getElementById("powrot_do_listy_ogl").innerHTML = '<a href="/ogloszenia.html">powrót do listy</a>';


	if(document.getElementById("formularz_ogl")!=undefined)
	{
		document.getElementById("formularz_ogl").innerHTML = '<h2>Formularz kontaktowy</h2>'
		+'<table>'
			+'<tr>'
			+'	<td style="width:100px;">Adres e-mail:</td>'
			+'	<td><input class="pole" maxlength="100" id="adres_email" type="text"></td>'
		+'	</tr>'
		+'	<tr>'
		+'		<td>Nr telefonu:</td>'
		+'		<td><input class="pole" maxlength="20" id="nr_telefonu" type="text"></td>'
		+'	</tr>'
		+'	<tr>'
		+'		<td colspan="2"><div style="float:right;font-size:10px;color:#b3b3b3">wymagany nr telefonu lub e-mail</div>Wiadomość:<br>'
		+'		<textarea id="wiadomosc"></textarea></td>'
		+'	</tr>'
		+'	<tr>'
		+'		<td colspan="2" style="text-align:right"><img id="ajax_loader" src="/images/ajax-loader.gif" style="display:none;vertical-align:middle;margin-right:5px" alt="wysyłanie..."> <input id="wyslij" type="submit" value="wyślij"></td>'
		+'	</tr>'
		+'</table>';
	}

	$("#wyslij").click(function(){
		msg = '';
		if(document.getElementById("adres_email").value && document.getElementById("adres_email").value.search(validMail) == -1)
		msg += "- Podaj poprawny adres e-mail\n";
		if(!document.getElementById("adres_email").value && !document.getElementById("nr_telefonu").value)
		msg += "- Podaj adres e-mail lub nr telefonu\n";
		if(!document.getElementById("wiadomosc").value)
		msg += "- Podaj treść wiadomości\n";

		if(msg)
		{
			alert("Popraw dane:\n"+msg);
		}
		else
		{
			// wysylanie
			document.getElementById("adres_email").disabled = 'disabled';
			document.getElementById("adres_email").style.background = '#ededed';
			document.getElementById("nr_telefonu").disabled = 'disabled';
			document.getElementById("nr_telefonu").style.background = '#ededed';
			document.getElementById("wiadomosc").disabled = 'disabled';
			document.getElementById("wiadomosc").style.background = '#ededed';
			document.getElementById("wyslij").disabled = 'disabled';
			document.getElementById("wyslij").style.background = '#696969';
			document.getElementById("ajax_loader").style.display = '';

			$.post("/ogloszenia/formularz.html",
			{
				ogl_id: document.getElementById("ogl_id").value,
				adres_email: document.getElementById("adres_email").value,
				nr_telefonu: document.getElementById("nr_telefonu").value,
				wiadomosc: document.getElementById("wiadomosc").value
			}, function(data) {
				document.getElementById("formularz_ogl").style.background='white';
				document.getElementById("formularz_ogl").style.padding='20px 0 0 0';
				document.getElementById("formularz_ogl").style.border=0;

				document.getElementById("formularz_ogl").innerHTML = data;
			});
		}
		return false;
	});


	/*
	$("#submit_sb").click(function() {
		document.getElementById('wysyla').style.display = '';
        var name    = $("#name_sb").val();
        var message = $("#message_sb").val();
       $.post("/shout.php",
		{	name: name,
			message: message
		}, function(r) {
			$("#shout").html(r);
			$("#message_sb").val("");
			document.getElementById('wysyla').style.display = 'none';
		});
        return false;
    });

	refresh_shoutbox();
    setInterval("refresh_shoutbox()", 15000);
	if(document.getElementById('wysyla')!=undefined) document.getElementById('wysyla').style.display = 'none';
	*/

});

function refresh_shoutbox() {
	$.post("/shout.php",
	{	refresh: 1
	}, function(r) {
		$("#shout").html(r);
	});
}

function loadNewsComsForm()
{
	$.get("/news-coms-form.html?news_id="+news_id, function(data) {
		document.getElementById('comments_form').innerHTML = data;
		$('textarea#comment').autoResize({
			// On resize:
			/*
			onResize : function() {
				$(this).css({opacity:0.8});
			},
			// After resize:
			animateCallback : function() {
				$(this).css({opacity:1});
			},*/
			// Quite slow animation:
			animateDuration : 300,
			// More extra space:
			extraSpace : 16
		});
	});
}
var news_com_count = 1;
function addNewsCom()
{
	var msg = '';
	if(document.add_com_form.content.value=="") msg += "- Podaj treść komentarza\n";
	if(document.add_com_form.nick.value=="") msg += "- Podaj swój nick\n";

	if(msg)
	{
		alert("Błędne dane:\n"+msg);
		return false;
	}

	$.post("/news-coms-form.html",
		{
			news_id: news_id,
			nick: document.add_com_form.nick.value,
			user_id: document.add_com_form.user_id.value,
			content: document.add_com_form.content.value
		}, function(data) {

		coms_count++;
		document.getElementById('div_coms_count').innerHTML = coms_count;

		document.getElementById('news_com'+news_com_count).style.display = 'none';
		document.getElementById('news_com'+news_com_count).innerHTML = data;
		$('#news_com'+news_com_count).slideDown('slow',function(){});

		document.getElementById('news_com_msg').innerHTML = 'Dodano!';

		news_com_count++;
		if(document.add_com_form.user_id.value==0) document.add_com_form.nick.value = '';
		document.add_com_form.content.value = '';
	});

	return false;
}

function in_array( what, where ){
	var a=false;
	for(var i=0;i<where.length;i++){
	  if(what == where[i]){
	    a=true;
        break;
	  }
	}
	return a;
}

wielkie = new Array('Z','X','C','V','B','N','M','L','K','J','H','G','F','D','S','A','Q','W','E','R','T','Y','U','I','O','P');


function validDodajOgl()
{
	var msg = '';
	
	var wybrano = 0;
	boxes = document.myform["cat_id[]"].length
	for (i = 0; i < boxes; i++) {
		if (document.myform["cat_id[]"][i].checked) {
			wybrano++;
		}
	}
	if(wybrano==0) msg += "- Wybierz kategorię\n";
	else if (wybrano>3) msg += "- Wybierz maksymalnie 3 kategorie\n";

	wybrano = 0;
	boxes = document.myform["typ"].length
	for (i = 0; i < boxes; i++) {
		if (document.myform["typ"][i].checked) {
			wybrano++;
		}
	}
	if(wybrano==0) msg += "- Wybierz typ\n";

	if(document.myform.title.value == "")
		msg += "- Podaj tytuł\n";
	else
	{
		dl = document.myform.title.value.length;
		w = 0;
		for(i=0;i<dl;i++)
		{
			if(in_array(document.myform.title.value.charAt(i),wielkie))
			w++;
		}
		if(w/dl>0.4) msg += "- Zmniejsz liczbę WIELKICH LITER w tytule\n";
		
	}


	if(document.myform.tresc.value == "")
		msg += "- Podaj treść\n";
	else
	{
		dl = document.myform.tresc.value.length;
		w = 0;
		for(i=0;i<dl;i++)
		{
			if(in_array(document.myform.tresc.value.charAt(i),wielkie))
			w++;
		}
		if(w/dl>0.4) msg += "- Zmniejsz liczbę WIELKICH LITER w treści\n";
	}



	if(document.myform.miniatura!=undefined)
	if(document.myform.miniatura.value && document.myform.miniatura.value.search(validZdjecie) == -1)
		msg += "- Plik miniatury musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(document.myform.zdjecie1!=undefined)
	if(document.myform.zdjecie1.value && document.myform.zdjecie1.value.search(validZdjecie) == -1)
		msg += "- Plik zdjęcia nr 1 musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(document.myform.zdjecie2!=undefined)
	if(document.myform.zdjecie2.value && document.myform.zdjecie2.value.search(validZdjecie) == -1)
		msg += "- Plik zdjęcia nr 2 musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(document.myform.zdjecie3!=undefined)
	if(document.myform.zdjecie3.value && document.myform.zdjecie3.value.search(validZdjecie) == -1)
		msg += "- Plik zdjęcia nr 3 musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(msg)
	{
		alert("Popraw dane:\n"+msg);
		return false;
	}
	return true;

}

function validDodajFir()
{
	var msg = '';

	if(document.myform.nazwa_firmy.value == "")
		msg += "- Podaj nazwę firmy/instytucji\n";

	if(document.myform.title.value == "")
		msg += "- Podaj tytuł\n";
	else
	{
		dl = document.myform.title.value.length;
		w = 0;
		for(i=0;i<dl;i++)
		{
			if(in_array(document.myform.title.value.charAt(i),wielkie))
			w++;
		}
		if(w/dl>0.4) msg += "- Zmniejsz liczbę WIELKICH LITER w tytule\n";
	}


	if(document.myform.tresc.value == "")
		msg += "- Podaj treść\n";
	else
	{
		dl = document.myform.tresc.value.length;
		w = 0;
		for(i=0;i<dl;i++)
		{
			if(in_array(document.myform.tresc.value.charAt(i),wielkie))
			w++;
		}
		if(w/dl>0.4) msg += "- Zmniejsz liczbę WIELKICH LITER w treści\n";
	}


	if(document.myform.miniatura!=undefined)
	if(document.myform.miniatura.value && document.myform.miniatura.value.search(validZdjecie) == -1)
		msg += "- Plik miniatury musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(document.myform.zdjecie1!=undefined)
	if(document.myform.zdjecie1.value && document.myform.zdjecie1.value.search(validZdjecie) == -1)
		msg += "- Plik zdjęcia nr 1 musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(document.myform.zdjecie2!=undefined)
	if(document.myform.zdjecie2.value && document.myform.zdjecie2.value.search(validZdjecie) == -1)
		msg += "- Plik zdjęcia nr 2 musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(document.myform.zdjecie3!=undefined)
	if(document.myform.zdjecie3.value && document.myform.zdjecie3.value.search(validZdjecie) == -1)
		msg += "- Plik zdjęcia nr 3 musi być obrazkiem typu JPG, PNG lub GIF\n";

	if(document.myform.zdjecie4!=undefined)
	if(document.myform.zdjecie4.value && document.myform.zdjecie4.value.search(validZdjecie) == -1)
		msg += "- Plik zdjęcia nr 4 musi być obrazkiem typu JPG, PNG lub GIF\n";

	var wybrano = 0;
	boxes = document.myform["cat_id[]"].length
	for (i = 0; i < boxes; i++) {
		if (document.myform["cat_id[]"][i].checked) {
			wybrano++;
		}
	}
	if(wybrano==0) msg += "- Wybierz kategorię\n";
	else if (wybrano>3) msg += "- Wybierz maksymalnie 3 kategorie\n";

	if(msg)
	{
		alert("Popraw dane:\n"+msg);
		return false;
	}
	return true;

}
