/* NASTAVENI FOCUSU */
function set_focus(id){
  if(document.getElementById(id)){
    document.getElementById(id).focus();
  }else{
    //alert('Prvek s "'+id+'" neexistuje!');
  }
}

function fake_mouse_fce(){
	document.getElementById("fake_mouse").value="54";
	//document.getElementById("fake_mouse").style.display = "none";	
}

/* Vlozi html kod do prvku id */
function write_html(id, text){
  document.getElementById(id).innerHTML=text;
}
/* Zobrazi prvek */
function display_show(id){
  document.getElementById(id).style.display="inline";
}
/* Skryje prvek */
function display_none(id){
  document.getElementById(id).style.display="none";
}
/* Oznaci checkbox */
function checkbox_check(id){
  document.getElementById(id).checked=true;
}
/* Zrusi oznaceni checkboxu */
function checkbox_uncheck(id){
  document.getElementById(id).checked=false;
}
function input_disabled(id){
  document.getElementById(id).disabled=true;
}
function input_enabled(id){
  document.getElementById(id).disabled=false;
}

function del()
{
	return confirm("Opravdu smazat?")
}
function zmenit_prava()
{
	return confirm("Opravdu chcete změnit práva uživatele?")
}
  
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    inputs[i].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
  
  var inputs = document.getElementsByTagName("textarea");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    inputs[i].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
  var inputs = document.getElementsByTagName("select");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    inputs[i].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
}
//addLoadEvent(prepareInputsForHints);

//////////////////////////////////////////////////////////////////////////////////////////
// diskuse
////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Vytvoreni AJAX objektu
 * @return ajax objekt
 */
function createXHR() 
{
  var request = false;
  try {
    request = new ActiveXObject('Msxml2.XMLHTTP');
  }
  catch (err2) {
    try {
        request = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch (err3) {
      try {
        request = new XMLHttpRequest();
      }
      catch (err1) 
      {
        request = false;
      }
    }
  }
  return request;
}

///////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Forum category
 * 1 - vytvorit sekci
 * 2 - smazat sekci
 * @param friend_id ID kamarada
 * @return
 */
function forum_category_show()
{
  var xhr = createXHR();  
  xhr.onreadystatechange  = function()
  {
    if(xhr.readyState == 4)
    {
      if(xhr.status == 200){   
        // skryjeme progress
        display_none('loading_tree');
        // vlozime nove vygenerovany strom
        write_html('tree', xhr.responseText);
        document.getElementById('tree').style.color="black";
      }else{
        alert('Chyba: category_tree_show');
      }
    }
  };

  var parameters="action=6";
  xhr.open("POST", "/diskuse_new/ajax_forum_admin.php", true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.send(parameters);
}

/**
 * Forum category 

 * @param friend_id ID kamarada
 * @return
 */
function forum_category(action, id, parent, level, name)
{
  if(action==2 && del()===false){
    return 0;
  }

  display_show('loading_tree');
  document.getElementById('tree').style.color="silver";

  var xhr = createXHR();
  xhr.onreadystatechange = function()
  {
    if(xhr.readyState == 4)
    {
      if(xhr.status == 200){
        forum_category_show();
        if(xhr.responseText!=""){
          alert(xhr.responseText);
        }
      }else{
        alert('Chyba: tree');
      }
    }
  };

  var parameters="action="+action+"&id="+id+"&parent="+parent+"&level="+level+"&name="+name;
  xhr.open("POST", "/diskuse_new/ajax_forum_admin.php", true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.send(parameters);
}




///////////////////////////////////////////////////////////////////////////////////////////////
// DYNAMICKE INPUTY FILE PRO UPLOAD FOTEK
///////////////////////////////////////////////////////////////////////////////////////////////

var input_file_num_of_lines = 0;

function input_file_pridat(element, smazat){
  input_file_num_of_lines++;

  // kam budeme inputy vkladat
  var x = document.getElementById( element );

  // input pro vyber souboru
  var input = document.createElement('input');
  input.setAttribute('type', 'file' );
  input.setAttribute('name', 'soubory[]' );
  input.setAttribute('id', 'file_' + input_file_num_of_lines );

  // odkaz pro smazani inputu
  if(smazat==1){
    var text_smazat = document.createElement('span');
    text_smazat.innerHTML = ' <span onclick="input_file_smazat(\''+element+'\',\''+input_file_num_of_lines+'\');"><u>Zrušit</u></span>';
  }

  var p = document.createElement('span');
  p.setAttribute('id', 'span_' + input_file_num_of_lines );

  var br = document.createElement('br');

  // vkladani prvku
  p.appendChild(input);
  if(smazat==1){
    p.appendChild(text_smazat);
  }
  p.appendChild(br);
  x.appendChild(p);
}

function input_file_smazat(element, id){
  var d = document.getElementById( element );
  var olddiv = document.getElementById( 'span_' + id );
  d.removeChild(olddiv);
}

function input_file_otevrit_dialog(){
  if ("file_".input_file_num_of_lines){
    document.getElementById("file_".input_file_num_of_lines).click();
  }
}

//////////////////////////////////////////

