function chatReakce(cislo) {
  if(cislo==0) {
    document.getElementById('chatReakceDiv').style.display='none';
    document.getElementById('chatReakceDiv').getElementsByTagName('p')[0].innerHTML='';
    document.getElementById('chatReakce').value='0';
  } else {
    document.getElementById('chatReakce').value=cislo;
    document.getElementById('chatReakceDiv').style.display='block';
    document.getElementById('chatReakceDiv').getElementsByTagName('p')[0].innerHTML=document.getElementById('chat'+cislo).getElementsByTagName('p')[0].innerHTML;
  }
  document.getElementById('chatZprava').focus();
  return false;
}
function chatOnkeyup(event) {
  if(window.event) {
    event=window.event;
  }
  if(document.getElementById('chatZprava').value.length>256) {
    document.getElementById('chatZprava').value=document.getElementById('chatZprava').value.substring(0,256);
  }
  if(event.keyCode==13) {
    chatPosli();
  }
}
var posilam=false;
function chatPosli() {
  if(posilam) {
    return;
  }
  if(document.getElementById('chatJmeno').value.length<2) {
    document.getElementById('chatJmeno').focus();
    return false;
  } else {
    document.getElementById('chatJmeno').readOnly=true;
  }
  document.getElementById('chatZprava').value=document.getElementById('chatZprava').value.replace(/[\n\r\t]/g,'');
  if(document.getElementById('chatZprava').value.length<2) {
    document.getElementById('chatZprava').focus();
    return false;
  }
  ajax_sub("akce=posli-zpravu&dotaznik="+document.getElementById('chatDotaznik').value+"&posledni="+document.getElementById('chatPosledni').value+"&reakce="+document.getElementById('chatReakce').value+"&jmeno="+ajaxrepla(document.getElementById('chatJmeno').value)+"&zprava="+ajaxrepla(document.getElementById('chatZprava').value), function(h) {
    chatNoveZpravy(h.responseXML);
  });
  chatReakce(0);
  document.getElementById('chatZprava').value='';
  document.getElementById('chatOdeslat').disabled=true;
  posilam=true;
  window.setTimeout(function() {
    document.getElementById('chatZprava').focus();
    document.getElementById('chatOdeslat').disabled=false;
    posilam=false;
    },5000);
}
function chatNoveZpravy(h) {
  var zpravy=h.getElementsByTagName('zprava');
  if(zpravy.length>0) {
    if(document.getElementById('chatNic')) document.getElementById('chatNic').style.display='none';
    for(i=0;i<zpravy.length;i++) {
      document.getElementById('chatZpravy').innerHTML=zpravy[i].firstChild.nodeValue+document.getElementById('chatZpravy').innerHTML;
      document.getElementById('chatPosledni').value=zpravy[i].getAttribute('id');
    }
  }
}

function chatSmaz(cislo) {
  if(document.getElementById('chat'+cislo)) {
    document.getElementById('chat'+cislo).parentNode.removeChild(document.getElementById('chat'+cislo));
  }
  ajax_sub("akce=smaz-zpravu&zprava="+cislo,function() {});
  return false;
}

function chatStahni() {
  ajax_sub("akce=stahni-zpravy&dotaznik=", function(h) {
    chatNoveZpravy(h.responseXML);
    setTimeout("chatStahni()",5000)
  });
}

function chatZvyrazni(cislo, klik) {
  if(!document.getElementById('chat'+cislo)) {
    if(klik) {
      alert('Tato zpráva byla již smazána.');
    }
    return false;
  }
  var divy=document.getElementById('chatZpravy').getElementsByTagName('div');
  for(var i=0;i<divy.length;i++) {
    if(divy[i].className=='active') {
      divy[i].className='';
    }
  }
  document.getElementById('chat'+cislo).className='active';
  if(!klik) {
    return false;
  }
  var adr=window.location.href;
  if(adr.indexOf('#')>0) {
    adr=adr.substring(0,adr.indexOf('#'));
  }
  window.location.href=adr+'#chat'+cislo;
  return false;
}
setTimeout("chatStahni()",60000);