function get_home_url(){
    return $("base").attr("href");
}

function get_url_to_ajax(){
    return get_home_url() + "ajax/";
}

function getXMLHttpRequest()
{
  var request = false;
    
  try {
    request = new XMLHttpRequest();
  } catch(err1) {
    try {
      request = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(err2) {
      try {
        request = new ActiveXObject('Microsoft.XMLHTTP');                
      } catch(err3) {
        request = false;
      }
    }
  }
  return request;
}  

function dzial_user_letter(letter, dzial_id)
{
	$choosed_users_string="";
	
	$("#select_user_right .element").each(function()
		{$choosed_users_string+=","+$(this).attr("id");});
	
	$.post(get_url_to_ajax()+ "dzialUsersSelect.html",
	{ 
		akcja: "pobierz",
		letter: letter,
		dzial: dzial_id,
		users_string: $choosed_users_string
	},
	  function(data){
	  	$("#select_user_left").html(data);
	  });
	 
	 $("#arrow_left").attr("onClick", "javascript:dzial_user_pop('"+letter+"');"); 
	 
}

function dzial_user_push()
{
	
	$("#select_user_left .element").each(function()
	{ 
		if($(this).children("input").attr("checked"))
		{
			$wnetrze_div=$(this).html();
			$wnetrze_div = $wnetrze_div.replace('unreal_tab[]','users_array[]');
			$div_body='<div class="element" title="'+$(this).attr("title")+'" id="'+$(this).attr("id")+'">'+$wnetrze_div+'</div>';
			$("#select_user_right").append($div_body);
			$(this).remove();
		}
	});
}

function dzial_user_pop(letter)
{
	$("#select_user_right .element").each(function()
	{ 
		if($(this).children("input").attr("checked"))
		{
			if(letter==$(this).attr('title'))
			{
				$wnetrze_div=$(this).html();
				$wnetrze_div = $wnetrze_div.replace('users_array[]','unreal_tab[]');
				$div_body='<div class="element" title="'+$(this).attr("title")+'" id="'+$(this).attr("id")+'">'+$wnetrze_div+'</div>';
				$("#select_user_left").append($div_body);
			}
			$(this).remove();
		}
	});
}

function nothing()
{
	
}

function set_checkboxes(wsk)
{
	if($(wsk).attr("checked"))
	{
		$(".izba_part").removeAttr('disabled');
	}
	else
	{
		$(".izba_part").attr("disabled", "disabled");
		$(".izba_part").removeAttr('checked');
	}
}

function show_ok(link, text)
{
	$.post(get_url_to_ajax()+ "showOK.html",
	{ 
		akcja: "wyswietl",
		link: link,
		text: text
	},
	  function(data){
	  	$("#body_content").html(data);
	  });
}

function fckeditor_post(wsk, editor, id)
{
	$.post(get_url_to_ajax() + "postfckeditor.html",
	{ 
		akcja: "post",
		id: id
	},
	  function(data){
	  
		var begin_of_HTML ='<form action="post/edytuj/'+id+'.html" method="post">';
		editor = new FCKeditor('tresc'+id);
		editor.BasePath = 'fckeditor/';
		editor.Config['SkinPath'] = 'skins/silver/';
		editor.Value = data.tresc;
		editor.ToolbarSet="User";
		editor.Height = 400;
		editor.Width = 625;
		editor.Config['AutoDetectLanguage'] = false;
		editor.Config['DefaultLanguage'] = 'pl';
		var fckHTML=editor.CreateHtml();
		
		var selectHTML = '';
		if(data.dzialy) selectHTML = data.dzialy;
		
		var end_of_HTML = '<input type="submit" name="edytuj" value="zapisz" class="pole_button" /></form>';
		var sHTML = begin_of_HTML + fckHTML + selectHTML + end_of_HTML;
		$(wsk).parent().parent().children('.message').html(sHTML);
		
	  }, "json");
}

function cytuj_post(id){
	$.post(get_url_to_ajax() + "postfckeditor.html",
	{
		akcja: "cytuj",
		id: id
	},
	  function(data){
	  	var tresc = FCKeditorAPI.GetInstance('tresc').GetData();
	  	tresc += '<div class="cytat_autor">Użytkownik <strong>' + data.autor + '</strong> napisał/a:</div> <div class="cytat">' + data.tresc + '</div>';
	  	var editor = FCKeditorAPI.GetInstance('tresc');
	  	editor.SetHTML(tresc); 
	  	scroll(0,$(document).height());
	  }, "json");  
}

function fckeditor_faq(id)
{
	$.post(get_url_to_ajax() + "faqfckeditor.html",
	{ 
		akcja: "faq",
		id: id
	},
	  function(data){
		$('.masterbox').html(data);
		var feFCKeditor = new FCKeditor('tresc');
		feFCKeditor.BasePath = 'fckeditor/';
		feFCKeditor.ToolbarSet="Admin";
		feFCKeditor.Config['SkinPath'] = 'skins/silver/';
		feFCKeditor.Height = 400;
		feFCKeditor.Width = 625;
		feFCKeditor.Value = $('#content').val();
		feFCKeditor.Config['AutoDetectLanguage'] = false;
		feFCKeditor.Config['DefaultLanguage'] = 'pl';
		sHtml=feFCKeditor.CreateHtml();
		$('#fckeditor_faq').html(sHtml);
	  });
}

function szukaj_str(stron, ta_strona, wyrazy)
{
	$.post(get_home_url() + "obsluga/szukaj.html",
	{ 
		submit: "limit",
		licz_str: stron,
		szukanywyraz: wyrazy,
		id_str: ta_strona
	},
	  function(data){
	  	$("#body_content").html(data);
	  });
}

function temat_email(temat, miejsce)
{
	$.post(get_url_to_ajax() + "showYesNo.html",
	{ 
			akcja: 'del',
			text: "Czy chcesz otrzymywać raz dziennie maila z informacją o nowych postach w tym temacie?",
			yes_link: "temat/email/"+miejsce+"/"+temat+"/tak.html",
			no_link: "temat/email/"+miejsce+"/"+temat+"/nie.html"
	},
	  function(data){
	  	$("#body_content").html(data);
	  });
}

function users_for_private(letter)
{
		$.post(get_url_to_ajax() + "private_select.html",
		{ 
			akcja: 'select',
			letter: letter
		},
		  function(data){
		  	$("#select_users").html(data);
		  });		
}


function del_priv(wsk, id)
{
		var is_new=$(wsk).prev().attr("name");
		var text = is_new ? "Czy napewno chcesz usunąć nieprzeczytaną wiadomość?" : "Czy napewno chcesz usunąć tę wiadomość?";
		
		$.post(get_url_to_ajax() + "showYesNo.html",
		{ 
			akcja: 'del',
			text: text,
			yes_link: "obsluga/private/"+id+"/del.html",
			no_link: "obsluga/private.html",
			id: id
		},
		  function(data){
		  	$(".masterbox").html(data);
		  });	
}

function del_dzial(id)
{
		$.post(get_url_to_ajax() + "showYesNo.html",
		{ 
			akcja: 'del',
			text: "Czy napewno chcesz usunac ten dzial?",
			yes_link: "dzial/usun/"+id+".html",
			no_link: "",
			id: id
		},
		  function(data){
		  	$(".masterbox").html(data);
		  });		
}

function del_kategoria(id)
{
		$.post(get_url_to_ajax() + "showYesNo.html",
		{ 
			akcja: 'del',
			text: "Czy napewno chcesz usunac ta kategorie?",
			yes_link: "kategoria/usun/"+id+".html",
			no_link: "",
			id: id
		},
		  function(data){
		  	$(".masterbox").html(data);
		  });		
}

function del_temat(kat, id)
{
		$.post(get_url_to_ajax() + "showYesNo.html",
		{ 
			akcja: 'del',
			text: "Czy napewno chcesz usunac ten temat?",
			yes_link: "temat/usun/"+id+".html",
			no_link: "kategoria/default/" + kat + ".html",
			id: id
		},
		  function(data){
		  	$(".masterbox").html(data);
		  });		
}

function del_post(temat, id)
{
		$.post(get_url_to_ajax() + "showYesNo.html",
		{ 
			akcja: 'del',
			text: "Czy napewno chcesz usunac ten post?",
			yes_link: "post/usun/"+id+".html",
			no_link: "temat/default/" + temat + ".html",
			id: id
		},
		  function(data){
		  	$(".masterbox").html(data);
		  });		
}

function ban_post(temat, id_user)
{
		$.post(get_url_to_ajax() + "showYesNo.html",
		{ 
			akcja: 'del',
			text: "Czy napewno chcesz zbanować użytkownika za ten post? Jeśli użytkownik otrzyma 3 bany, jego konto zostanie zablokowane!",
			yes_link: "post/ban/"+temat+"/"+id_user+".html",
			no_link: "temat/default/" + temat + ".html",
			temat: temat,
			id: id_user
		},
		  function(data){
		  	$(".masterbox").html(data);
		  });		
}

function blokuj_temat(kat, id, status)
{
	if (status == 0) //gdy niezablokowany -> zablokuj
	{
		$.post(get_url_to_ajax() + "showYesNo.html", {
			akcja: 'del',
			text: "Czy napewno chcesz zablokowac ten temat?",
			yes_link: "temat/blokuj/" + id + "/off.html",
			no_link: "kategoria/default/" + kat + ".html",
			id: id
		}, function(data){
			$(".masterbox").html(data);
		});
	}
	else if (status == 1) //gdy zablokowany -> odblokuj
	{
		$.post(get_url_to_ajax() + "showYesNo.html", {
			akcja: 'del',
			text: "Czy napewno chcesz odblokowac ten temat?",
			yes_link: "temat/blokuj/" + id + "/on.html",
			no_link: "kategoria/default/" + kat + ".html",
			id: id
		}, function(data){
			$(".masterbox").html(data);
		});
	}
}

function dzial_valid()
{
	if ($("#nazwa").val()) 
		return true;
	else {
		$("#nazwa").prev().css("color", "#ff0000");
		$("#nazwa").prev().css("font-weight","bold");
		return false;
	}
}

function valid_wniosek_kat()
{
	if(!$("#dzial").val())
	{
		$(".opis_blad").html("Musisz wybrać dział!");
		return false;
	}
	else return true;
}

function valid_wniosek_inny()
{
	$return=true;
	if(!$("#tytul").val())
	{
		$("#tytul").next(".opis_blad").html("Musisz wpisać tytuł wniosku!");
		$return=false;
	}
	else
	{
		$("#tytul").next(".opis_blad").html("");
	}
	if(!$("#tresc").val())
	{
		$("#tresc").next(".opis_blad").html("Musisz wpisać treść wniosku!");
		$return=false;
	}
	else
	{
		$("#tresc").next(".opis_blad").html("");
	}
	return $return;
}

function rej_valid_ajax(result)
{
	var wpisany_nick=$("#nick").val();
	var wpisany_captcha=$("#captcha").val();
	
	$.post(get_url_to_ajax() + "rejestracja.html",
	{ 
		ajax_nick: wpisany_nick,
		ajax_captcha: wpisany_captcha,
		ajax_akcja: 'sprawdz'
	},
	  function(data){
		if (data.nick || data.captcha) {
			$("#nick").parent().next().html(data.nick);
			result = false;
		}
		$("#captcha").parent().next().html(data.captcha); //wypisanie bledu badz skasowanie tresci 
		if (result) {
			document.rej_form.submit();
		}
	  }, "json");
}

function rej_valid(){
	var result=true;
	
	var reg_nick = /^[a-zA-Z0-9_-]{2,10}$/;
	$nick=$("#nick").val();
	var correct_nick=reg_nick.test($nick);
	if(!correct_nick || $nick=="admin" || $nick=="moderator" || $nick=="gość" || 
			$nick=="avatar_guest" || $nick=="avatar_default") //niepoprawny
	{
		$("#nick").parent().prev().css("color","#ff0000");
		$("#nick").parent().prev().css("font-weight","bold");
		$("#nick").parent().next().html("Nick niepoprawny. Musi składać się z min. 2, a max. 10 znaków."+
		" Może zawierać litery (bez polskich znaków), liczby oraz znaki: - _ .");
		result=false;
	}
	else //poprawny
	{
		$("#nick").parent().prev().css("color","#000000");
		$("#nick").parent().prev().css("font-weight","normal");	
		$("#nick").parent().next().html("");	
	}
	
	var reg_imie = /^[a-zA-ZąćęłńóśźżŁÓŚŹŻ]{2,}( [a-zA-ZąćęłńóśźżŁÓŚŹŻ]{2,})?$/;
	var correct_imie=reg_imie.test($("#imie").val());
	if(!correct_imie && $("#imie").val()) //niepoprawny
	{
		$("#imie").parent().prev().css("color","#ff0000");
		$("#imie").parent().prev().css("font-weight","bold");
		$("#imie").parent().next().html("Imię niepoprawne");
		result=false;
	}
	else //poprawny
	{
		$("#imie").parent().prev().css("color","#000000");
		$("#imie").parent().prev().css("font-weight","normal");	
		$("#imie").parent().next().html("");	
	}

	var reg_nazwisko = /^[a-zA-ZąćęłńóśźżŁÓŚŹŻ]{2,}(-[a-zA-ZąćęłńóśźżŁÓŚŹŻ]{2,})?$/;
	var correct_nazwisko=reg_nazwisko.test($("#nazwisko").val());
	if(!correct_nazwisko && $("#nazwisko").val()) //niepoprawny
	{
		$("#nazwisko").parent().prev().css("color","#ff0000");
		$("#nazwisko").parent().prev().css("font-weight","bold");
		$("#nazwisko").parent().next().html("Nazwisko niepoprawne");
		result=false;
	}
	else //poprawny
	{
		$("#nazwisko").parent().prev().css("color","#000000");
		$("#nazwisko").parent().prev().css("font-weight","normal");	
		$("#nazwisko").parent().next().html("");	
	}	
	
	var reg_haslo = /^.{4,}$/;
	var correct_haslo=reg_haslo.test($("#haslo").val());
	if(!correct_haslo) //niepoprawny
	{
		$("#haslo").parent().prev().css("color","#ff0000");
		$("#haslo").parent().prev().css("font-weight","bold");
		$("#haslo").parent().next().html("Hasło niepoprawne. Musi składać się z min. 4 znaków");
		result=false;
	}
	else //poprawny
	{
		$("#haslo").parent().prev().css("color","#000000");
		$("#haslo").parent().prev().css("font-weight","normal");	
		$("#haslo").parent().next().html("");	
	}
	

	if($("#haslo").val()!=$("#rep_haslo").val()) //niepoprawny
	{
		$("#rep_haslo").parent().prev().css("color","#ff0000");
		$("#rep_haslo").parent().prev().css("font-weight","bold");
		$("#rep_haslo").parent().next().html("Wpisane hasła różnią się");
		result=false;
	}
	else //poprawny
	{
		$("#rep_haslo").parent().prev().css("color","#000000");
		$("#rep_haslo").parent().prev().css("font-weight","normal");	
		$("#rep_haslo").parent().next().html("");	
	}
	
	var reg_email = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	var correct_email=reg_email.test($("#email").val());
	if(!correct_email ) //niepoprawny
	{
		$("#email").parent().prev().css("color","#ff0000");
		$("#email").parent().prev().css("font-weight","bold");
		$("#email").parent().next().html("Wpisany email jest niepoprawny");
		result=false;
	}
	else //poprawny
	{
		$("#email").parent().prev().css("color","#000000");
		$("#email").parent().prev().css("font-weight","normal");	
		$("#email").parent().next().html("");	
	}
	
	/*
	var reg_branza = /^[a-zA-ZąćęłńóśźżŁÓŚŹŻ -]{2,}?$/;
	var correct_branza=reg_branza.test($("#branza").val());
	if(!correct_branza && $("#branza").val()) //niepoprawny
	{
		$("#branza").parent().prev().css("color","#ff0000");
		$("#branza").parent().prev().css("font-weight","bold");
		$("#branza").parent().next().html("Branża niepoprawna");
		result=false;
	}
	else //poprawny
	{
		$("#branza").parent().prev().css("color","#000000");
		$("#branza").parent().prev().css("font-weight","normal");	
		$("#branza").parent().next().html("");	
	}	*/
	
	var reg_city = /^[a-zA-ZąćęłńóśźżŁÓŚŹŻ -]{2,}?$/;
	var correct_city=reg_city.test($("#city").val());
	if(!correct_city && $("#city").val()) //niepoprawny
	{
		$("#city").parent().prev().css("color","#ff0000");
		$("#city").parent().prev().css("font-weight","bold");
		$("#city").parent().next().html("Miejscowość niepoprawna");
		result=false;
	}
	else //poprawny
	{
		$("#city").parent().prev().css("color","#000000");
		$("#city").parent().prev().css("font-weight","normal");	
		$("#city").parent().next().html("");	
	}
	
	/*
	var reg_division = /^[a-zA-ZąćęłńóśźżŁÓŚŹŻ-]{2,}?$/;
	var correct_division=reg_division.test($("#division").val());
	if(!correct_division && $("#division").val()) //niepoprawny
	{
		$("#division").parent().prev().css("color","#ff0000");
		$("#division").parent().prev().css("font-weight","bold");
		$("#division").parent().next().html("Województwo niepoprawna");
		result=false;
	}
	else //poprawny
	{
		$("#division").parent().prev().css("color","#000000");
		$("#division").parent().prev().css("font-weight","normal");	
		$("#division").parent().next().html("");	
	}*/
	
	rej_valid_ajax(result);
}

function priv_mess_toggle(wsk)
{
    $(".show_message").hide();
    $(wsk).next().next().show();
	var priv_id = $(wsk).attr("name"); //for new privs
	
	if(priv_id)
	{
		$(wsk).children(".privatelist").children(".titlebox").css("font-weight","normal");
		$.post(get_url_to_ajax() + "privRead.html",
		{ 
			id: priv_id
		},
		  function(data){
		  	$(wsk).attr("name", "");
		  });	
	}
}

$(document).ready(function(){
	
	$(".lista_dzialow").tooltip({showURL:false});


    $(".sliding").click(function(){$(this).parent().next().slideToggle("fast");}).toggle(
	function(){
		$(this).html('<img src="dom/gfx/icons/roll_down.gif" title="rozwin"/');
	},
	function(){
		$(this).html('<img src="dom/gfx/icons/roll_up.gif" title="zwin"/');
	});
    
    $(".show_message").hide();
	
	$("#hidden_select").hide();
	
	$(".centericon *").tooltip({
		delay: 0,  
	 	showURL: false 
 	});
 	
 	$(".delete_priv_message *").tooltip({
		delay: 0,  
	 	showURL: false 
 	});
 	
 	$(".icony_dol *").tooltip({
		delay: 0,  
	 	showURL: false 
 	});
	
});
