function confirm_url(msg, url)
{
  if (confirm(msg)) document.location.href = url;
}

//****************************************************************************

/// Get value of the SELECT tag.
function selection_value(sel)
{
  sel = getElement(sel);
  return sel.options[sel.selectedIndex].value;
}

//****************************************************************************

function load_ajax_content(url, target)
{
  d = doSimpleXMLHttpRequest(url);
  d.addCallback(function (req) {
    getElement(target).innerHTML = req.responseText;
  });
}

//****************************************************************************

function toggle_proposals_preview(shell, container, url)
{
  if (hasElementClass(shell, "invisible") && hasElementClass(shell, "no-content"))
  {
    load_ajax_content(url, container)
    removeElementClass(shell, "no-content"); // avoid further loading
  }
  toggleElementClass("invisible", shell);
}

//****************************************************************************

function autoredirect(anchor)
{
  if (!autoredirect_enabled) return;
  href = getElement(anchor);
  addLoadEvent(function(){
    window.setTimeout(function(){
      document.location.href = href;
    }, 800);
  });
}

//****************************************************************************

/// Hide the save button, replace elm with a "wait" image
/// and submit the form.
function submit_form_and_wait(form, elm_to_hide, waiting_img)
{
  swapDOM(elm_to_hide, waiting_img);
  getElement(form).submit();
}

//****************************************************************************

/// @param url: string
/// @param params: dict
function url_add_parameters(url, params)
{
  if (url.indexOf("?") < 0)
    url += "?";
  for (name in params)
  {
    if (url.indexOf("=") >= 0)
      url += "&";
    qstr = queryString([name], [params[name]]);
    url += qstr;
  }
  return url;
}

//****************************************************************************
// grid editor
//****************************************************************************

function GridEditor(name, container)
{
  this.name = name;
  this.container = getElement(container);
  this.rawdata_input = getElement(name);
  this.original_content = this.container.innerHTML;

  /// Retrieve and fill the data.
  /// data = {"html":, "rawdata":}
  this.load_content = function (url) {
    container = this.container;
    rawdata_input = this.rawdata_input;

    params = {};
    params[this.name] = rawdata_input.value;
    url = url_add_parameters(url, params);

    container.innerHTML = this.original_content;

    d = doSimpleXMLHttpRequest(url);
    d.addCallback(function (req) {
      data = evalJSON(req.responseText);
      container.innerHTML = data.html;
      rawdata_input.value = data.rawdata;
    });
  }

  this.add_row = function (url, block_with_new_values) {
    params = {};

    fields = getElementsByTagAndClassName("INPUT", null, block_with_new_values);
    for (i = 0; i < fields.length; i++)
    {
      field = fields[i];
      if (field.type == "checkbox")
        value = field.checked;
      else
        value = field.value;
      params[field.name] = value;
    }

    fields = getElementsByTagAndClassName("SELECT", null, block_with_new_values);
    for (i = 0; i < fields.length; i++)
    {
      field = fields[i];
      params[field.name] = selection_value(field);
    }

    url = url_add_parameters(url, params);
    this.load_content(url);
  }
}

//****************************************************************************
// tabs
//****************************************************************************

tabs = {};

function tabs_register(tabs_name, tab_id, body_id)
{
  if (!(tabs_name in tabs)) tabs[tabs_name] = new Array();
  tabs[tabs_name].push({
      tab_id: tab_id,
      body_id: body_id
    });
}

function tabs_activate(tabs_name, tab_id)
{
  tbs = tabs[tabs_name];
  for(i = 0; i < tbs.length; i++)
  {
    tab = tbs[i];
    div_tab = getElement(tab.tab_id)
    if (tab.tab_id == tab_id)
    {
      div_tab.className = "active";
      showElement(tab.body_id);
    } else {
      div_tab.className = "";
      hideElement(tab.body_id);
    }
  }
}

//****************************************************************************
// pings
//****************************************************************************

g_ping_planned = false;

function plan_ping()
{
  if (g_ping_planned) return;
  g_ping_planned = true;
  window.setTimeout(function() {
      doSimpleXMLHttpRequest(rel_root + "ping");
    }, 1000 * 60 * 10);
}

//****************************************************************************
// TinyMCE
//****************************************************************************

function tinymce_event_handler(e)
{
  plan_ping();
}

function setup_tinymce()
{
  if (!need_wysiwyg_editor) return;
  tinyMCE.init({
    editor_selector : "mceEditor",
    theme: "advanced",
    mode : "textareas",
    plugins: "contextmenu,insertdatetime,layer,paste,searchreplace,table,style,nonbreaking,visualchars,fullscreen,advlink,advimage,autonbsp",
    theme_advanced_buttons1: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,insertlayer,moveforward,movebackward,absolute,|,link,unlink,anchor,image,|,insertdate,inserttime,charmap,hr,nonbreaking,autonbsp,visualchars",
    theme_advanced_buttons2: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,styleselect,|,bullist,numlist,|,sub,sup,|,outdent,indent",
    theme_advanced_buttons3 : "forecolor,backcolor,|,styleprops,code,removeformat,cleanup,|,tablecontrols,|,fullscreen",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "none",
    theme_advanced_resizing: true,
    theme_advanced_resize_horizontal: false,
    theme_advanced_resizing_use_cookie : false,
    theme_advanced_path : true,
    theme_advanced_fonts : "Arial=sans-serif;Times=Times New Roman, Times, serif;Courier=courier new,courier,monospace",
    theme_advanced_blockformats: "p,h1,h2,h3",
    theme_advanced_styles: "",
    content_css : rel_root + "css/main.css",
    plugin_insertdate_dateFormat : "%d.%m.%Y",
    plugin_insertdate_timeFormat : "%H:%M:%S",
    language : "cs",

    accessibility_warnings : false,
    convert_fonts_to_spans: true,
    entities: "160,nbsp,38,amp,34,quot,60,lt,62,gt",
    entity_encoding: "named",
    convert_urls: false,
    verify_html: false,
    apply_source_formatting : true,
    cleanup_on_startup : true,
    invalid_elements : "",

    auto_reset_designmode: true,

    handle_event_callback: "tinymce_event_handler"
  });
}

//****************************************************************************
//****************************************************************************

setup_tinymce();


