/* HTML nástrojová lišta

Programoval:
   Radovan Moser - PATROKOLOS
   internetové služby
   http://www.patrokolos.cz
   moser@patrokolos.cz
   
Všechna práva vyhrazena!
Jakékoliv kopírování částí tohoto a všech souvisejících souboru
není dovoleno bez vědomí a souhlasu jejich tvůrce!
*/

function storeCaret(editEl) {
   if (editEl.createTextRange)
      editEl.currRange = document.selection.createRange().duplicate();
}

function replace_selection(action) {
   if (!document.activeElement.name) {
      alert("Nejprve klikněte do vtupního pole,\n\rkteré chcete editovat.");
      return false;
   }
   eval("var el = document.all."+ document.activeElement.name +";");
   el.focus();
   var selection = document.selection.createRange().text;
   if (!selection && ((action == "b") || (action == "i") || (action == "u") || (action == "center") || (action == "right") || (action == "justify"))) {
      var selection = prompt("Vložte text, který bude uzavřen do zvoleného tagu", "");
      if (selection == null)
         return;
   }
   switch(action) {
      case 'br': var txt = '<br>'; break;
      case 'b': var txt = '<b>'+ selection +'</b>'; break;
      case 'i': var txt = '<i>'+ selection +'</i>'; break;
      case 'u': var txt = '<u>'+ selection +'</u>'; break;
      case 'center': var txt = '<p align="center">'+ selection +'</p>'; break;
      case 'right': var txt = '<p align="right">'+ selection +'</p>'; break;
      case 'justify': var txt = '<p align="justify">'+ selection +'</p>'; break;
      case 'ul': txt = get_list('ul'); break;
      case 'ol': txt = get_list('ol'); break;
      case 'a_url': txt = get_href('url', selection); break;
      case 'a_mail': txt = get_href('mail', selection); break;
      default: var txt = action; break;
   }
/* pridavani mezery pred tag
   if (txt)
      if (el.value && (action != "br"))
         txt = ' ' + txt;
*/
   document.selection.createRange().text = txt;
   return true;
}

function get_list(typ) {
   if (!typ)
      var typ = "ul";
   var pocet = prompt("Kolik položek bude seznam obahovat? (max 10)", 2);
   if (pocet > 0) {
      if (pocet > 10)
         var pocet = 10;
      var polozky = new Array();
      for (i=1; i <= pocet; i++) {
         polozky[i] = prompt("Zadejte "+ i +". položku (z "+ pocet +") seznamu", "");
      }
   } else
      var polozky = new Array();
   var txt = "";
   for (i=1; i < polozky.length; i++)
      if (polozky[i] != null)
         var txt = txt + ' <li>'+ polozky[i] +'</li>\n';
   if (txt)
      var txt = '<'+ typ +'>\n'+ txt + '</'+ typ +'>';
   return txt;
}

function get_href(typ, content) {
   if (!typ)
      var typ = "url";
   switch(typ) {
      case "url":
         if (!content)
            var content = prompt('Zadejte text odkazu (např.: "Seznam - katalog odkazů").', '');
         if (content == null)
            var content = "{odkaz}";
         var href = prompt('Kam bude odkaz směřovat? (např.: http://www.seznam.cz).', 'http://');
         if (!href || (href == "http://"))
            var href = "#";
//         return '<a href="'+ href +'" target="_blank">'+ content +'</a>';
         return '<a href="'+ href +'" target="_self">'+ content +'</a>';
      break;
      case "mail":
         var href = prompt('Zadejte E-mailovou adresu (např.: muj@email.cz).', '@');
         if (!content)
            var content = prompt('Zadejte text odkazu (např.: "Aleš Novák").', '');
         if (content == null)
            var content = "{odkaz}";
         if (!href || (href == "@"))
            var href = "#";
         else
            var href = "mailto:" + href;
         return '<a href="'+ href +'">'+ content +'</a>';
      break;
   }
}
