var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";

return "Odznacz"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Zaznacz"; }
}

function zliczaj_znaki_m() {
var a=document.forms["komentowanie_m"].tresc_m.value.length;
var b=2000;
var c = b - a;

   if(a < 2001)
  {
      document.forms["komentowanie_m"].znak_m.value=c;
   }
   else
   {
      alert('Przekroczono dozwoloną ilość znaków !!!');
      document.forms["komentowanie_m"].tresc_m.value=document.forms["komentowanie_m"].tresc_m.value.substring(0,2000);
   }
}

function zliczaj_znaki2() {
var a=document.forms["komentowanie2"].tresc.value.length;
var b=2000;
var c = b - a;

   if(a < 2001)
  {
      document.forms["komentowanie2"].znak2.value=c;
   }
   else
   {
      alert('Przekroczono dozwoloną ilość znaków !!!');
      document.forms["komentowanie2"].tresc.value=document.forms["komentowanie2"].tresc.value.substring(0,2000);
   }
}

function displayWindow(url, name, width, height)
{ 
	if (width > screen.availWidth - 30)
		width = screen.availWidth - 30;
	if (height > screen.availheight - 30)
		height = screen.availHeight - 30;
			
	var Win = window.open(url,'', 'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=no');
if (Win)
	{
	try{ Win.moveTo((screen.availWidth - width) / 2, (screen.availHeight - height) / 2); } catch(e){}
  if(typeof event != "undefined") event.returnValue = false;
		return false;
	}
}
// zmienia kolorek
function ChangeBkColor(o,c)
{
o.style.backgroundColor=c;
} 
// shout
function $(obj) {
return (typeof obj == "string") ? document.getElementById(obj) : obj;
}

// szukaj podpowiedzi
var xmlHttp;
var txt="";
var pozycja=0;
var ile;

function Zapytanie(adres){
      if(xmlHttp==null){ //w zależności od przeglądarki tworzymy obiekt XMLHTTP
         if(window.ActiveXObject)xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //dla IE
	 else if(window.XMLHttpRequest)xmlHttp = new XMLHttpRequest();  //Firefox, Opera, Safari itp.
      }
      if (xmlHttp == null){alert("Nie udało się zainicjować obiektu xmlHttpRequest!");return;} //jeśli obiekt nie został utworzony, zwracamy, błąd a skrypt zostaje przerwany
      
      xmlHttp.open("GET", adres); //ustawiamy metodę i adres żądania
      xmlHttp.onreadystatechange = function(){ //funkcja ma za zadanie wyświetlić wyniki zwrócone przez serwer
         if (xmlHttp.readyState == 4 || xmlHttp.status == 200) //sprawdzamy czy udało się pobrać zawartość strony (readyState=4) lub czy serwer nie zwrócił błędu(status=200 oznacza że jest OK)
         document.getElementById("wyniki").innerHTML = xmlHttp.responseText; //zwrócony tekst zapisujemy do warstwy 
      };   
      xmlHttp.send(null); //wysyłamy żądanie
}

function odswiez_podpowiedzi() {
      pozycja=-1;

      // szukamy wszystkich inputow, niestety w calym dokumencie z braku form
      var inputs = document.getElementsByTagName('input');
      var szu_el;
      // szukamy zaznaczonego inputa o nazwie "szu"
      for (var i=0; i < inputs.length; i++) {
        szu_el = inputs[i];
        if (szu_el.name == 'szu' && szu_el.checked) {
          // znaleziony, konczymy szukanie
          break;
        }
      }
      // jesli input zostal znaleziony przypisujemy jego wartosc do szu_val
      var szu_val = szu_el ? szu_el.value : '';

      Zapytanie("elements/szukaj_suggest.php?tek="+document.getElementById("tytul").value+"&szu="+szu_val);
      txt=document.getElementById("tytul").value;
}

function podpowiedz(event){
  
  if(event.keyCode!=40 && event.keyCode!=38 && event.keyCode!=13){
     odswiez_podpowiedzi();
   }
   else{
     ile=document.getElementById("ile_wynikow").value;
      if(event.keyCode==40){
         if(pozycja==ile)pozycja=0; 
         else pozycja++;
      }
      else if(event.keyCode==38){
         if(pozycja==0)pozycja=ile; 
         else pozycja--;
      }
//      else if(event.keyCode==13)ruszaj();

      document.getElementById("tytul").value=(pozycja==ile)?txt:document.getElementById("kl"+pozycja).innerHTML;
      for(var i=0;i<ile;i++)document.getElementById("kl"+i).className=(i==pozycja)?"podswietlone":"zwykle";
   }
}

function swiec(el){
   pozycja=el;
   for(var i=0;i<ile;i++)document.getElementById("kl"+i).className=(i==pozycja)?"podswietlone":"zwykle";
}
function wloz(id){
   document.getElementById("tytul").value=document.getElementById("kl"+id).innerHTML;
//   ruszaj();
}

   function rozwin(co){
with(document.getElementById(co)){className=className=='h'?'v':'h';}
   }
   
   
// popup
function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size() {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
}
function popup(windowname, window_pos_func) {
	blanket_size();
	if (typeof(window_pos_func) != 'undefined') window_pos_func();
	toggle('blanket');
	toggle(windowname);		
}
function popup_m(windowname, e) {
	if (!e) e = window.event;
	doc = document.documentElement,
        body = document.body;
	var x = e.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
        var y = e.clientY + (doc && doc.scrollTop  || body && body.scrollTop  || 0) - (doc && doc.clientTop  || body && body.clientTop  || 0);
	popup(windowname, function() {
  	  var popUpDiv = document.getElementById(windowname);
	  popUpDiv.style.left = x+'px';
	  popUpDiv.style.top = y+'px';
	});
}

