/* potrebne funkce */

/* zjednodusena funkce getElementById */
function GetE(m_id) {
  return document.getElementById(m_id);
}

/* zajistuje plneni seznamu objektu a zobrazeni/skryti tlacitka
   potrebuje: jquery, objekt s id 'list' */
function checkItem(m_this, m_id, m_field) {
  if (m_this.checked == true) $("#list").val($("#list").val() + 'id=' + m_id + ';');
  else $("#list").val(str_replace('id=' + m_id + ';', '', $("#list").val()));
  
  if (m_field != '') {
    if ($("#list").val() == '') $("#" + m_field).hide();
    else $("#" + m_field).show();
  }
}

/* vytvari dialogova okna ve stylu thickboxu 
   potrebuje: jquery, lang 
   m_type (string):     typ dialogu (napr. 'yesno', 'yesnocancel', 'continue',...
   m_message (string):  text dialogu
   m_---_fn (function): funkce pro urcite tlacitko
   m_width (int):       sirka dialogu
   m_height (int):      vyska dialogu */
function dialog(m_type, m_message, m_yes_fn, m_no_fn, m_cancel_fn, m_continue_fn, m_width, m_height) {
  if (typeof(m_message) != 'string') m_message = m_message.innerHTML;

  var html = '';
  
  if (!m_yes_fn || m_yes_fn == '') m_yes_fn = '$.unblockUI()';
  if (!m_no_fn || m_no_fn == '') m_no_fn = '$.unblockUI()';
  if (!m_cancel_fn || m_cancel_fn == '') m_cancel_fn = '$.unblockUI()';
  if (!m_continue_fn || m_continue_fn == '') m_continue_fn = '$.unblockUI()';
  if (!m_width || m_width == '') m_width = '300px';
  if (!m_height || m_height == '') m_height = '200px';
 
  html += '  <p class="modal_text">' + m_message + '</p>\r\n';
  if (m_type.indexOf('yes') != -1 || m_type.indexOf('no') != -1 || m_type.indexOf('cancel') != -1) {
    html += '  <table class="modal_buttons">\r\n';
    html += '    <tr>\r\n';
    if (m_type.indexOf('yes') != -1) html += '      <td><a class="boldbuttons yes" href="#" onclick="' + m_yes_fn + '"><span>' + java_yes + '</span></a></td>\r\n';
    if (m_type.indexOf('no') != -1) html += '      <td><a class="boldbuttons no" href="#" onclick="' + m_no_fn + '"><span>' + java_no + '</span></a></td>\r\n';
    if (m_type.indexOf('cancel') != -1) html += '      <td><a class="boldbuttons cancel" href="#" onclick="' + m_cancel_fn + '"><span>' + java_cancel + '</span></a></td>\r\n';
    if (m_type.indexOf('continue') != -1) html += '      <td><a class="boldbuttons continue" href="#" onclick="' + m_continue_fn + '"><span>' + java_continue + '</span></a></td>\r\n';
    html += '    </tr>\r\n';
    html += '  </table>\r\n';
  }
  
  $.blockUI({ message: html, css: { padding: '10px', width: m_width, cursor: 'default' } }); 
}

/* rozdeli xml dokument na zpravu, chybu a pole - vhodne pro validaci 
   potrebuje: 
   m_response (xml):  odpoved ze serveru */
function getResponse(m_response) {
  var result = new Array();
  result['string'] = '';
  result['error'] = ''; 
   
  var aktChild = m_response.getElementsByTagName('string')[0].firstChild;
  while(aktChild) {
    result['string'] += aktChild.nodeValue;
    aktChild = aktChild.nextSibling;
  }
  
  var aktChild = m_response.getElementsByTagName('error')[0].firstChild;
  while(aktChild) {
    result['error'] += aktChild.nodeValue;
    aktChild = aktChild.nextSibling;
  }
    
  return result;
}

/* nastavi seznam objektu 
   potrebuje: 
   m_response (xml):  odpoved ze serveru */
function SetList(m_itemsin, m_list) {
  itemsin = GetE(m_itemsin);
  list = GetE(m_list);

  list.value = '';
  for (x=0; x < itemsin.length; x++) {
		list.value = list.value + itemsin.options[x].value + ',';
  }
}

/* zjisti vysku okna prohlizece 
   potrebuje: */
function winH() {
  if (window.innerHeight) return window.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
  else if (document.body && document.body.clientHeight) return document.body.clientHeight;
}

/* zjisti sirku okna prohlizece 
   potrebuje: */
function winW() {
  if (window.innerWidth) return window.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
  else if (document.body && document.body.clientWidth) return document.body.clientWidth;
}

/* ajaxfilemanager pro tinymce
   potrebuje: fckeditor
   automaticke doplneni parametru primo v ediotru */
function ajaxfilemanager(field_name, url, type, win) {
  switch (type) {
    case "image":
      break;
    case "media":
      break;
    case "flash": 
      break;
    case "file":
      break;
    default:3
      return false;
  }
            
  tinyMCE.activeEditor.windowManager.open( {
    url: g_wwwroot + "/tinymce/jscripts/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php",
    width: 782,
    height: 410,
    inline : "yes",
    close_previous : "no"
  }, {
    window : win,
    input : field_name
  });
}

/* nahradi obsah innerHTML objektu
   potrebuje:
   el (objekt nebo string): objekt, ktereho se tyka operace innerHTML
   html (html):             html, ktere ma byt vlozeno */
function replaceHtml(el, html) {
	var oldEl = typeof el === "string" ? document.getElementById(el) : el;
	/*@cc_on // Pure innerHTML is slightly faster in IE
		oldEl.innerHTML = html;
		return oldEl;
	@*/
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	/* Since we just removed the old element from the DOM, return a reference
	to the new element, which can be used to restore variable references. */
	return newEl;
}

/* obdoba str_replace funkce z php
   potrebuje:
   search (string):  hledany retezec
   replace (string): retezec, kterym bude search nahrazen
   subject (string): prohledavany retezec */
function str_replace ( search, replace, subject ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
 
    var result = "";
    var prev_i = 0;
    for (i = subject.indexOf(search); i > -1; i = subject.indexOf(search, i)) {
        result += subject.substring(prev_i, i);
        result += replace;
        i += search.length;
        prev_i = i;
    }
 
    return result + subject.substring(prev_i, subject.length);
}