function ersetzen(links, rechts, textarea)
{
   if (textarea.setSelectionRange)
   {
      var selLength = textarea.textLength;
      var selStart = textarea.selectionStart;
      var selEnd = textarea.selectionEnd;
      if (selEnd==1 || selEnd==2) selEnd=selLength;
      var s1 = (textarea.value).substring(0,selStart);
      var s2 = (textarea.value).substring(selStart, selEnd)
      var s3 = (textarea.value).substring(selEnd, selLength);
      textarea.value = s1 + links + s2 + rechts + s3;
   }
   else if (document.selection)
   {
      auswahl = document.selection.createRange().text;
      textarea.focus();
      ersetzt = links + auswahl + rechts;
      document.selection.createRange().text = ersetzt;
   }
   else
   {
      textarea.value += links + rechts;
   }
   textarea.focus();
   func_vorschau();
}

function inserttag(tag)
{
    var textarea = window.document.getElementById('text');
    var links = '';
    var rechts = '';

    if( tag == 'img' )
    {
       var imageurl = prompt( 'Geben sie bitte die URL des Bildes ein.','http://' );
       rechts = '[img]' + imageurl + '[/img]';
    }
    else if( tag == 'url' )
    {
       var urlurl = prompt( 'Geben sie bitte die URL des Links ein.','http://' );
       var urlname = prompt( 'Geben sie bitte den Namen des Links ein.','' );
       rechts = '[url="' + urlurl +'"]' + urlname + '[/url]';
    }
    else if( tag == 'mail' )
    {
       var mail = prompt( 'Geben sie bitte die Mailadresse ein.','' );
       rechts = '[mail="' + mail +'"][/mail]';
    }
    else
    {
       links = '[' + tag + ']';
       rechts = '[/' + tag + ']'
    }

    ersetzen(links, rechts, textarea);
}

function insertcolor(color)
{
   var textarea = window.document.getElementById('text');
   links = '[color="' + color + '"]';
   rechts = '[/color]';
   ersetzen(links, rechts, textarea);
}


function insertsmilie (smilie)
{
  var textarea = window.document.getElementById('text');

   if (textarea.setSelectionRange)
   {
      var selLength = textarea.textLength;
      var selStart = textarea.selectionStart;
      var selEnd = textarea.selectionEnd;
      var s1 = (textarea.value).substring(0,selStart);
      var s3 = (textarea.value).substring(selEnd, selLength);
      textarea.value = s1 + smilie + s3;
      pos = smilie.length + selStart;
      textarea.selectionStart = pos;
      textarea.selectionEnd = pos;
   }
   else if (document.selection)
   {
      auswahl = document.selection.createRange().text;
      textarea.focus();
      document.selection.createRange().text = smilie;
   }
   else
   {
      textarea.value += smilie;
   }
   textarea.focus();
   func_vorschau();
}
function func_vorschau()
{
   var text = document.getElementById('text').value;
   //window.document.getElementById('vorschau').innerHTML = text;

   var antwort = escape(text);

   dojo.xhrGet(
   {
      url: '/guestbook/vorschau.php?antwort=' + antwort,
      handleAs: "text",
      handle: function(data, evt)
      {
         document.getElementById('con-vorschau').innerHTML = data;
      },
      mimetype: "text/html"
   });
}
function func_hoehe(hoehe)
{
   document.getElementById('ivorschau').style.height = hoehe + 'px';
}
function speichernGaestebuch()
{
   var name = document.getElementById('name').value;
   if (name == '')
   {
       alert("Bitte einen Namen eintragen");
       return;
   }
   var ort = document.getElementById('ort').value;
   var email = document.getElementById('email').value;
   var home = document.getElementById('home').value;
   var text = document.getElementById('text').value;
   if (text == '')
   {
       alert("Bitte einen Text eingeben");
       return;
   }
   dojo.xhrPost(
   {
      url: 'guestbook/speichern.php',
      handleAs: "text",
      content: {
         name: name,
         ort: ort,
         email: email,
         home: home,
         text: text
      },
      handle: function(data, evt)
      {
         gaestebuchOeffnen(0,'none',1)
      },
      contentType: "application/x-www-form-urlencoded; charset=utf-8",
      mimetype: "text/html"
   });


}
function smilie(thesmilie) {
// inserts smilie text
        document.getElementById('text').value += thesmilie+" ";
        document.getElementById('text').focus();
}