 function VratNasviceni(prvek) {
   if (typeof prvek.selectionStart != 'undefined' && typeof prvek.selectionEnd != 'undefined')
    return { zacatek: prvek.selectionStart, konec: prvek.selectionEnd };

   if (document.selection && document.selection.createRange) {
     prvek.focus();

     var rozsah = document.selection.createRange().duplicate();

     if (rozsah.parentElement () != prvek) //test, jestli vubec existuje nasviceny text/kurzor
      return { zacatek: 'n/a', konec: 'n/a' };

     var pozice;
     for (pozice=0; rozsah.moveStart('character',-1); pozice++) {  //odpocitani znaku
       if (rozsah.htmlText.charAt(0) == '<') break;
      }

     if (rozsah.htmlText.length == 0)  //pokud vubec neexistuje
      return { zacatek: 'n/a', konec: 'n/a' };

     return { zacatek: pozice, konec: rozsah.text.length } ;
    }

   return { zacatek: null, konec: null };
  }

 function NastavNasviceni(prvek,zacatek,konec) {
   if (konec == 0) konec = zacatek;
   if (typeof prvek.selectionStart != 'undefined' && typeof prvek.selectionEnd != 'undefined') {
     prvek.setSelectionRange(zacatek,konec);
     prvek.focus();
    }
   else if (document.selection && document.selection.createRange) {
     var rozsah = prvek.createTextRange();
     rozsah.move('character',zacatek);
     rozsah.moveEnd('character',konec-zacatek);
     rozsah.select();
    }
  }
 function VlozSmajlika(smajlikprvek,textid) {
   var poz,p=document.getElementById(textid),smajlik=smajlikprvek.getAttribute('alt');
   poz = VratNasviceni(p);
   p.value = p.value.substring(0,poz.zacatek) + smajlik + p.value.substring(poz.konec);
   NastavNasviceni(p,poz.zacatek+smajlik.length,0);
   p.focus();
   return false;
  }

 var bold=0,ital=0;

 function VlozTag(tagprvek,tag,textid) {
   var poz,p=document.getElementById(textid);
   p.focus();
   poz = VratNasviceni(p);
   if (poz.zacatek != poz.konec) {   //"obaleni" textu tagy
     p.value = p.value.substring(0,poz.zacatek) + '[' + tag + ']' + p.value.substring(poz.zacatek,poz.konec) + '[/' + tag + ']' + p.value.substring(poz.konec);
     NastavNasviceni(p,poz.konec+(tag.length*2)+5,0);
    }
   else {
     switch(eval(tag)) {
       case 0:                       //otevreni tagu
         tagprvek.style.backgroundColor = '#B0E0F0'; eval(tag+'=1;');
         p.value = p.value.substring(0,poz.zacatek) + '[' + tag + ']' + p.value.substring(poz.zacatek);
         NastavNasviceni(p,poz.zacatek+tag.length+2,0);
         break;
       default:                                    //zavreni tagu
         tagprvek.style.backgroundColor = '#DDD'; eval(tag+'=0;');
         p.value = p.value.substring(0,poz.zacatek) + '[/' + tag + ']' + p.value.substring(poz.zacatek);
         NastavNasviceni(p,poz.zacatek+tag.length+3,0);
         break;
      }
    }
   p.focus();
   return false;
  }
 function VlozTagURL(textid) {
   var poz,p=document.getElementById(textid);
   poz = VratNasviceni(p);
   p.value = p.value.substring(0,poz.zacatek) + '[url=http://]' + p.value.substring(poz.zacatek,poz.konec) + '[/url]' + p.value.substring(poz.konec);
   NastavNasviceni(p,poz.zacatek+12,0);
   p.focus();
   return false;
  }


 var diakritikazkontrolovana=0;
 var velikostpismenzkontrolovana=0;
 function DiakritikaBlika(kolikrat) {
   document.getElementById('diakritika').style.color=((kolikrat%2) == 0 ? '#B0FFFF' : 'black');
   document.getElementById('diakritika').style.backgroundColor=((kolikrat%2) == 0 ? '#C00000' : 'transparent');
   if (kolikrat > 1) setTimeout("DiakritikaBlika('"+(kolikrat-1)+"');",500);
  }
 function KontrolaDiakritiky(prvekid) {
 var prispevek;
   if (diakritikazkontrolovana == 1) return;
   prispevek=document.getElementById(prvekid).value;
   if (prispevek.length > 30) {
     prispevek=prispevek.substring(0,30);
     if (prispevek.search('^[a-zA-Z0-9 \-\(\)\,\?\!]*$') != -1) DiakritikaBlika(6);
     diakritikazkontrolovana=1;
    }
  }
 function KontrolaVelikostiPismen(prvekid) {
 var prispevek;
   if (velikostpismenzkontrolovana == 1) return;
   prispevek=document.getElementById(prvekid).value;
   if (prispevek.length > 20) {
     prispevek=prispevek.substring(0,20);
     if (prispevek.search('([A-Z].*){5}') != -1 && prispevek.search('[a-z]') == -1) alert('Omlouváme se za vyrušení, ale zřejmě máte omylem zapnutý CapsLock. Příspěvky psané velkými písmeny jsou v rozporu s netiketou a budou smazány.\r\n\r\nProsím dodržujte toto pravidlo.');
     velikostpismenzkontrolovana=1;
    }
  }

 function ZvetsiOkenko(prvekid,vyska) {
   vyska *= 1.58;      //roztec radku
   document.getElementById(prvekid).style.height=vyska+'em';
  }

 function ExterniStranka(prvek) {
 var pozadavek;
   if (window.ActiveXObject) pozadavek = new ActiveXObject('Microsoft.XMLHTTP');
   else pozadavek = new XMLHttpRequest();
   pozadavek.open('POST','http://www.svice.cz/skripty/externiodkaz',false);
   pozadavek.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
   pozadavek.send('url='+prvek.getAttribute('href'));
  }

 function PrictiVlakno(vlakno) {
   var znaky,cislo,novevlakno;
   znaky = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

   cislo = 0;
   cislo += znaky.indexOf(vlakno.substring(vlakno.length-2,vlakno.length-1)); cislo *= znaky.length;
   cislo += znaky.indexOf(vlakno.substring(vlakno.length-1,vlakno.length));

   cislo++;             //pricteni jednicky
   if (cislo >= (znaky.length * znaky.length)) return false;  //maximum

                        //prevedeni cisla na znaky
   novevlakno = znaky.substring(cislo % znaky.length,(cislo%znaky.length)+1); cislo = Math.floor(cislo/znaky.length);
   novevlakno = znaky.substring(cislo % znaky.length,(cislo%znaky.length)+1) + novevlakno;

   return(vlakno.substring(0,vlakno.length-2) + novevlakno);
  }

 function Otevri(id) {
   if (document.getElementById('zavrit'+id).innerHTML == 'X') return;

   document.getElementById('komentarhlava'+id).style.color = '#F00';

   document.getElementById('komentartelo'+id).style.display = 'block';
   document.getElementById('komentartelo'+id).style.position = 'absolute';
   document.getElementById('komentartelo'+id).style.marginLeft = '100px';
   if (document.all && !window.opera)
    document.getElementById('komentartelo'+id).style.width = '350px';
   document.getElementById('komentartelo'+id).style.minWidth = '300px';
   document.getElementById('komentartelo'+id).style.maxWidth = '600px';
   document.getElementById('komentartelo'+id).style.border = '2px solid white';
  }
 function Zavri(id) {
   if (document.getElementById('komentartelo'+id).style.position != 'absolute') return;

   document.getElementById('komentarhlava'+id).style.color = '#000';

   document.getElementById('komentartelo'+id).style.display = 'none';
   document.getElementById('komentartelo'+id).style.position = 'static';
   document.getElementById('komentartelo'+id).style.marginLeft = '0px';
   document.getElementById('komentartelo'+id).style.width = 'auto';
   document.getElementById('komentartelo'+id).style.maxWidth = '1000px';
   document.getElementById('komentartelo'+id).style.minWidth = '0px';
   document.getElementById('komentartelo'+id).style.border = '0px solid white';
  }

 function NapisTextOtevreniVlakna(id) {
   if (document.getElementById('zavrit'+id).innerHTML == 'X') {
     document.getElementById('zavrit'+id).innerHTML = '=&gt;';
     document.getElementById('zavrit'+id).setAttribute('title','Otevřít toto vlákno');
    }
   else {
     document.getElementById('zavrit'+id).innerHTML = 'X';
     document.getElementById('zavrit'+id).setAttribute('title','Zavřít toto vlákno');
    }
  }

 function OtevriZavriVlakno(id,co) {
   while (true) {
     if (!document.getElementById('komentar'+id)) id = PrictiVlakno(id);
     if (!document.getElementById('komentar'+id)) id = PrictiVlakno(id);
     if (!document.getElementById('komentar'+id)) break;

     OtevriZavriVlakno(id+'01',co);
     NapisTextOtevreniVlakna(id);

     document.getElementById('komentartelo'+id).style.display = co;

     id = PrictiVlakno(id);
    }
  }

 function KlikVlakno(id) {
   if (document.getElementById('komentartelo'+id).style.position == 'absolute')
    Zavri(id);             //zavreni nahledu

   if (document.getElementById('zavrit'+id).innerHTML == 'X') {
     OtevriZavriVlakno(id+'01','none');
     document.getElementById('komentartelo'+id).style.display = 'none';
    }
   else {
     OtevriZavriVlakno(id+'01','block');
     document.getElementById('komentartelo'+id).style.display = 'block';
    }
   NapisTextOtevreniVlakna(id);
   return false;     //zabraneni odkazu
  }

 function ZavriVsechnaVlakna(prvektext) {
   if (prvektext.innerHTML == 'Zavřít všechna vlákna') {
     prvektext.innerHTML = 'Otevřít všechna vlákna';
     OtevriZavriVlakno('01','none');
    }
   else {
     prvektext.innerHTML = 'Zavřít všechna vlákna';
     OtevriZavriVlakno('01','block');
    }
   return false;
  }

 function getElementsByClassName(classname, node)  {
   if(!node) node = document.getElementsByTagName("body")[0];
   var a = [];
   var re = new RegExp('\\b' + classname + '\\b');
   var els = node.getElementsByTagName("*");
   for(var i=0,j=els.length; i<j; i++)
     if(re.test(els[i].className))a.push(els[i]);
   return a;
  }
