function chkentry(formular)
{
  var tofocus = 0;
  var fehler = "Folgende Felder wurden nicht bzw. nicht richtig ausgefüllt:\n";

  if (formular.name.value == "" || formular.name.value == "name *")
  {
    fehler += "\nName";
    if (tofocus == 0)
      tofocus = 1;
  }

  if (formular.mail.value != "" && formular.mail.value != formular.mail.id)
  {
    if (formular.mail.value.indexOf ('@') == -1 || formular.mail.value.indexOf ('@') < 3)
    {
      fehler += "\nE-Mail";
      if (tofocus == 0)
        tofocus = 2;
    }
    else
      if (formular.mail.value.lastIndexOf ('.') == -1 ||
          formular.mail.value.indexOf ('@') < 3 ||
          (formular.mail.value.length - formular.mail.value.lastIndexOf ('.')) < 2 ||
          (formular.mail.value.lastIndexOf ('.') - formular.mail.value.indexOf ('@')) < 3)
      {
        fehler += "\ne-mail";
        if (tofocus == 0)
      	  tofocus = 2;
      }
      else
      {
        atpos = formular.mail.value.indexOf ('@');
        dotpos = formular.mail.value.lastIndexOf ('.');
        if (dotpos < atpos)
        {
          fehler += "\nE-Mail";
          if (tofocus == 0)
            tofocus = 2;
        }
      }
  }
  
  if (formular.website.value != "" && formular.website.value != formular.website.id &&
      (formular.website.value.length - formular.website.value.lastIndexOf ('.')) < 2)
  {
    fehler += "\nHomepage";
    if (tofocus == 0)
      tofocus = 3;
  }
  
  if (formular.comment.value == "" || formular.comment.value == "kommentar *")
  {
    fehler += "\nKommentar";
    if (tofocus == 0)
      tofocus = 4;
  }

  if (tofocus != 0)
  {
    alert (fehler);
    switch (tofocus)
    {
      case 1:
        formular.name.focus ();
   			break;
      case 2:
        formular.mail.focus ();
   			break;
      case 3:
        formular.website.focus ();
   			break;
      case 4:
        formular.comment.focus ();
    }
    return false;
  }
  
  if (formular.mail.value == formular.mail.id)
    formular.mail.value = "";
  if (formular.website.value == formular.website.id)
    formular.website.value = "";

  return true;
}

function addemoticon(formular, emoticon)
{
  if (formular.comment.value == formular.comment.id)
  {
    formular.comment.value = "";
    formular.comment.style.color = "#000";
  }

  insertAt(formular.comment.id, ":" + emoticon + ":", false);
}

function addfunction(formular, func, text)
{
  if (formular.comment.value == formular.comment.id)
  {
    formular.comment.value = "";
    formular.comment.style.color = "#000";
  }

  if (func == "url")
  {
    insertAt(formular.comment.id, "[url http://www.example.com]Text[/url]", true);

    setSelectionRange(formular.comment, -33, -11);
  }
  else
  {
    var newtext = getCurrentSelection(formular.comment);
    var textselected = false;

    if (newtext != "")
      text = newtext;

    insertAt(formular.comment.id, "[" + func + "]" + text + "[/" + func + "]", true);

    setSelectionRange(formular.comment, -(text + "[/" + func + "]").length, -("[/" + func + "]").length);
  }
}

function showCaption(input)
{
  if (input.value == "")
  {
    if (input.id.substr(input.id.length - 3, 3) == "req")
      input.value = input.id.substr(0, input.id.length - 3) + " *";
    else
      input.value = input.id;
    input.style.color = "#a9a9a9";
  }
}

function hideCaption(input)
{
  var toCompare = "";

  if (input.id.substr(input.id.length - 3, 3) == "req")
    toCompare = input.id.substr(0, input.id.length - 3) + " *"
  else
    toCompare = input.id;

  if (input.value == toCompare)
  {
    input.value = "";
    input.style.color = "#000";
  }
}


var oldComment = "";

function ReloadPreview()
{
  var name = window.document.getElementById("namereq").value;
  var mail = window.document.getElementById("e-mail").value;
  var homepage = window.document.getElementById("homepage").value;
  var comment = window.document.getElementById("kommentarreq").value;

  var previewdate = window.document.getElementById("previewdate");
  var previewcontact = window.document.getElementById("previewcontact");

  var preview = window.document.getElementById("preview");
  var previewArea = window.document.getElementById("previewArea");

  previewArea.style.visibility = "visible";
  previewArea.style.height = "";
  
  if (name == "" && mail == "" && homepage == "" && comment == "")
  {
    previewArea.style.visibility = "hidden";
    previewArea.style.height = "0";
  }

  if (name == "name *")
    name = "";

  if (mail == "e-mail")
    mail = "";

  if (homepage == "homepage")
    homepage = "";

  var protocol = "";

  if (homepage.length > 0 &&
      homepage.substr (0, 7) != "http://" &&
      homepage.substr (0, 8) != "https://")
    protocol = "http://";
  else
  {
    protocol = homepage.substr (0, 7);

    if (protocol.substr (5, 2) != "//")
    {
      protocol = homepage.substr (0, 8);

      if (protocol.substr (6, 2) != "//")
        protocol = "";
    }

    homepage = homepage.substr (protocol.length);
  }

  previewcontact.innerHTML = name;

  if (mail != "")
    previewcontact.innerHTML = '<a href="mailto:' + mail + '">\n' + name + '</a>\n';

  if (homepage != "")
    previewcontact.innerHTML += '<span style="font-size:0.75em;">\n&nbsp;&there4;&nbsp;\n<a href="' + protocol + homepage + '">\n' + homepage + '</a>\n</span>\n';

  var jetzt = new Date();
  previewdate.innerHTML = (jetzt.getDate() < 10 ? "0" : "") + jetzt.getDate() + "." +
   (jetzt.getMonth() < 10 ? "0" : "") + jetzt.getMonth() + "." + jetzt.getFullYear() + " " +
   (jetzt.getHours() < 10 ? "0" : "") + jetzt.getHours() + ":" +
   (jetzt.getMinutes() < 10 ? "0" : "") + jetzt.getMinutes();

  if (comment == "kommentar *")
    comment = "";

  if (oldComment == comment)
    return;

  oldComment = comment;
  
  var index = -1;

  comment = replaceInComment(comment, "&", '&amp;');
  comment = replaceInComment(comment, "<", '&lt;');
  comment = replaceInComment(comment, ">", '&gt;');
  comment = replaceInComment(comment, "  ", '&nbsp;&nbsp;');
  comment = replaceInComment(comment, "&nbsp; ", '&nbsp;&nbsp;');

  while((index = comment.indexOf("\n")) != -1)
    comment = comment.substr(0, index) + "<br />" + comment.substr(index + 1, comment.length);

  comment = replaceInComment(comment, "[b]", '<span style="font-weight:bold;">');
  comment = replaceInComment(comment, "[/b]", '</span>');

  comment = replaceInComment(comment, "[i]", '<span style="font-style:italic;">');
  comment = replaceInComment(comment, "[/i]", '</span>');

  comment = replaceInComment(comment, "[u]", '<span style="text-decoration:underline;">');
  comment = replaceInComment(comment, "[/u]", '</span>');

  comment = replaceInComment(comment, "[img]", '<img src="');
  comment = replaceInComment(comment, "[/img]", '" style="height:5em; padding:0.5em;" />');

  comment = replaceInComment(comment, "[img left]", '<img align="left" src="');
  comment = replaceInComment(comment, "[/img]", '" style="height:5em; padding:0.5em;" />');

  comment = replaceInComment(comment, "[img center]", '<img align="center" src="');
  comment = replaceInComment(comment, "[/img]", '" style="height:5em; padding:0.5em;" />');

  comment = replaceInComment(comment, "[img right]", '<img align="right" src="');
  comment = replaceInComment(comment, "[/img]", '" style="height:5em; padding:0.5em;" />');

  comment = replaceInComment(comment, "[quote]", '<q>');
  comment = replaceInComment(comment, "[/quote]", '</q>');

  comment = replaceInComment(comment, "[url ", '<a href="');
  comment = replaceInComment(comment, "[/url]", '</a>');
  comment = replaceInComment(comment, "]", '">');
  
  comment = replaceInComment(comment, ":smile:", '<img src="/images/smilies/icon_smile.gif" />');
  comment = replaceInComment(comment, ":rolleyes:", '<img src="/images/smilies/icon_rolleyes.gif" />');
  comment = replaceInComment(comment, ":razz:", '<img src="/images/smilies/icon_razz.gif" />');
  comment = replaceInComment(comment, ":frown:", '<img src="/images/smilies/icon_frown.gif" />');
  comment = replaceInComment(comment, ":confused:", '<img src="/images/smilies/icon_confused.gif" />');
  comment = replaceInComment(comment, ":mad:", '<img src="/images/smilies/icon_mad.gif" />');
  comment = replaceInComment(comment, ":question:", '<img src="/images/smilies/icon_question.gif" />');
  comment = replaceInComment(comment, ":twisted:", '<img src="/images/smilies/icon_twisted.gif" />');
  comment = replaceInComment(comment, ":evil:", '<img src="/images/smilies/icon_evil.gif" />');
  comment = replaceInComment(comment, ":arrow:", '<img src="/images/smilies/icon_arrow.gif" />');
  comment = replaceInComment(comment, ":wink:", '<img src="/images/smilies/icon_wink.gif" />');
  comment = replaceInComment(comment, ":mrgreen:", '<img src="/images/smilies/icon_mrgreen.gif" />');
  comment = replaceInComment(comment, ":sad:", '<img src="/images/smilies/icon_sad.gif" />');
  comment = replaceInComment(comment, ":surprised:", '<img src="/images/smilies/icon_surprised.gif" />');
  comment = replaceInComment(comment, ":biggrin:", '<img src="/images/smilies/icon_biggrin.gif" />');
  comment = replaceInComment(comment, ":redface:", '<img src="/images/smilies/icon_redface.gif" />');
  comment = replaceInComment(comment, ":lol:", '<img src="/images/smilies/icon_lol.gif" />');
  comment = replaceInComment(comment, ":eek:", '<img src="/images/smilies/icon_eek.gif" />');
  comment = replaceInComment(comment, ":idea:", '<img src="/images/smilies/icon_idea.gif" />');
  comment = replaceInComment(comment, ":cool:", '<img src="/images/smilies/icon_cool.gif" />');
  comment = replaceInComment(comment, ":exclaim:", '<img src="/images/smilies/icon_exclaim.gif" />');
  comment = replaceInComment(comment, ":neutral:", '<img src="/images/smilies/icon_neutral.gif" />');
  comment = replaceInComment(comment, ":cry:", '<img src="/images/smilies/icon_cry.gif" />');

  comment = replaceInComment(comment, ":)", '<img src="/images/smilies/icon_smile.gif" />');
  comment = replaceInComment(comment, ":-)", '<img src="/images/smilies/icon_smile.gif" />');
  comment = replaceInComment(comment, ":P", '<img src="/images/smilies/icon_razz.gif" />');
  comment = replaceInComment(comment, ":-P", '<img src="/images/smilies/icon_razz.gif" />');
  comment = replaceInComment(comment, ":?", '<img src="/images/smilies/icon_confused.gif" />');
  comment = replaceInComment(comment, ":-?", '<img src="/images/smilies/icon_confused.gif" />');
  comment = replaceInComment(comment, ":x", '<img src="/images/smilies/icon_mad.gif" />');
  comment = replaceInComment(comment, ":-x", '<img src="/images/smilies/icon_mad.gif" />');
  comment = replaceInComment(comment, ";)", '<img src="/images/smilies/icon_wink.gif" />');
  comment = replaceInComment(comment, ";-)", '<img src="/images/smilies/icon_wink.gif" />');
  comment = replaceInComment(comment, ":(", '<img src="/images/smilies/icon_sad.gif" />');
  comment = replaceInComment(comment, ":-(", '<img src="/images/smilies/icon_sad.gif" />');
  comment = replaceInComment(comment, ":o", '<img src="/images/smilies/icon_surprised.gif" />');
  comment = replaceInComment(comment, ":-o", '<img src="/images/smilies/icon_surprised.gif" />');
  comment = replaceInComment(comment, ":D", '<img src="/images/smilies/icon_biggrin.gif" />');
  comment = replaceInComment(comment, ":-D", '<img src="/images/smilies/icon_biggrin.gif" />');
  comment = replaceInComment(comment, "8O", '<img src="/images/smilies/icon_eek.gif" />');
  comment = replaceInComment(comment, "8-O", '<img src="/images/smilies/icon_eek.gif" />');
  comment = replaceInComment(comment, "8)", '<img src="/images/smilies/icon_cool.gif" />');
  comment = replaceInComment(comment, "8-)", '<img src="/images/smilies/icon_cool.gif" />');
  comment = replaceInComment(comment, "B)", '<img src="/images/smilies/icon_cool.gif" />');
  comment = replaceInComment(comment, "B-)", '<img src="/images/smilies/icon_cool.gif" />');
  comment = replaceInComment(comment, ":|", '<img src="/images/smilies/icon_neutral.gif" />');
  comment = replaceInComment(comment, ":-|", '<img src="/images/smilies/icon_neutral.gif" />');

  preview.innerHTML = comment;
}

function replaceInComment(comment, search, replace)
{
  var index = -1;
  var newIndex = 0;
  
  while((newIndex = comment.substr(index + 1).indexOf(search)) != -1)
  {
    index += (newIndex + 1);
    comment = comment.substr(0, index) + replace + comment.substr(index + search.length, comment.length);
  }
  
  return comment;
}

function showImage(imgArray, clicked)
{
  var start = -1;

  clicked.href = "javascript:;";
  window.document.getElementsByTagName("body")[0].style.overflow = "hidden";
  var slideshow = window.document.getElementById("slideshow");
  slideshow.style.visibility = "visible";
  
  for (var i = 0; i < imgArray.length; i++)
  {
    if (clicked.lastChild.src.length - imgArray[i].length < 0)
      continue;
    
    if (clicked.lastChild.src.substring(clicked.lastChild.src.length - imgArray[i].length, clicked.lastChild.src.length) == imgArray[i])
    {
      start = i;
      break;
    }
  }

  if (start == -1)
    start = 0;
  
  slideshow.innerHTML = '';
  
  for (var i = 0; i < imgArray.length; i++)
    slideshow.innerHTML += '<img src="' + imgArray[i] + '" style="padding:0 10px; height:75px;" onmouseover="setNewImage(\'' + imgArray[i] + '\');">';
  
//  slideshow.innerHTML += '<table cellpadding="0" cellspacing="0" style="border:0; width:' + (window.innerWidth - 20) + 'px; height:' + (window.innerHeight - 125) + 'px; margin:20px 0 10px; padding:0;"><tr><td><img id="currentImage" src="" style="border:0; margin:0;" onclick="hideImage();"></td></tr></table>';

  slideshow.innerHTML += '<div style="border:0; width:' + (window.innerWidth - 20) + 'px; height:' + (window.innerHeight - 125) + 'px; margin:20px 0 10px; padding:0;"><img id="currentImage" src="" style="border:0; margin:0; position:fixed;" onclick="hideImage();" /></div>';

  setNewImage(imgArray[start]);
}

function setNewImage(imageSrc)
{
  var imgSlide = new Image();
  imgSlide.src = imageSrc;

  window.document.getElementById('currentImage').src = imageSrc;

  if (imgSlide.height > window.innerHeight - 125)
  {
    window.document.getElementById('currentImage').height = window.innerHeight - 125;
    window.document.getElementById('currentImage').style.top = '125px';
  }
  else
  {
    window.document.getElementById('currentImage').height = imgSlide.height;
    window.document.getElementById('currentImage').style.top = 115 + (window.innerHeight - 125 - imgSlide.height) / 2 + 'px';
  }

  window.document.getElementById('currentImage').style.left = 10 + (window.innerWidth - 20 - imgSlide.width) / 2 + 'px';
}

function hideImage()
{
  window.document.getElementsByTagName("body")[0].style.overflow = "auto";
  var slideshow = window.document.getElementById("slideshow");
  slideshow.style.visibility = "hidden";
  slideshow.innerHTML = "";
}

function openInNewWindow()
{
  var curLink;

  for (i = 0; i < document.links.length; i++)
  {
    curLink = new String(document.links[i]);
    if (curLink.substr(0, 7) != "mailto:" &&
        curLink.substr(0, 19) != "http://synonym24.at" &&
        curLink.substr(0, 23) != "http://www.synonym24.at" &&
        curLink.substr(0, 11) != "javascript:")
      document.links[i].setAttribute("target", "_blank");
  }
}

function faviconize()
{
  var curLink;

  for (var i = 0; i < document.links.length; i++)
  {
    curLink = new String(document.links[i]);
    if (curLink.substr(0, 7) != "mailto:" &&
        curLink.substr(0, 19) != "http://synonym24.at" &&
        curLink.substr(0, 23) != "http://www.synonym24.at" &&
        curLink.substr(0, 20) != "https://synonym24.at" &&
        curLink.substr(0, 24) != "https://www.synonym24.at" &&
        curLink.substr(0, 11) != "javascript:")
    {
      var domain = curLink.substr(0, curLink.indexOf("/", 9));
      var favnames = new Array("favicon.ico", "favicon.gif", "favicon.png",
                               "images/favicon.ico", "images/favicon.gif", "images/favicon.png",
                               "media/favicon.ico", "media/favicon.gif", "media/favicon.png");

      for (var j = 0; j < favnames.length; j++)
      {
        var favicon = new Image();
        favicon.onload = loadFavicon;
        favicon.linkId = i;
        favicon.src = domain + "/" + favnames[j];
      }
    }
  }
}

function loadFavicon(e)
{
  if (!e)
    e = window.event;

  var favicon = e.target;
  var actLink = document.links[favicon.linkId];

  if (actLink.innerHTML.substr(0, 5) != "<img ")
    actLink.innerHTML = "<img src=\"" + favicon.src + "\" alt=\"\" style=\"margin:-0.2em 0 -0.1em 0.1em; width:16px; height:16px;\" /> " + actLink.innerHTML;
}

function getCurrentSelection(textarea)
{
  if (textarea.setSelectionRange)
  {
    return textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
  }
  else if (document.selection)
  {
    return document.selection.createRange().text;
  }

  return "";
}

function insertAt(elementId, replaceString, select)
{
  //inserts given text at selection or cursor position
  textarea = document.getElementById(elementId);
  var toBeReplaced = /text|page|area_name/;//substrings in replaceString to be replaced by the selection if a selection was done
  if (textarea.setSelectionRange)
  {
    //Mozilla UserAgent Gecko-1.4
    var selectionStart = textarea.selectionStart;
    var selectionEnd = textarea.selectionEnd;
    var scrollTop=textarea.scrollTop;
    if (selectionStart != selectionEnd) // has there been a selection
    {
      var newString = replaceString.replace(toBeReplaced, textarea.value.substring(selectionStart, selectionEnd));
      textarea.value = textarea.value.substring(0, selectionStart) + newString + textarea.value.substring(selectionEnd);
      setCaretToPos(textarea, -textarea.value.length);

      setCaretToPos(textarea, selectionStart);
      if (select)
        setSelectionRange(textarea, newString.length, 2 * newString.length);
      else
        setCaretToPos(textarea, newString.length);
    }
    else // set caret
    {
      textarea.value = textarea.value.substring(0, selectionStart) + replaceString + textarea.value.substring(selectionEnd);
      textarea.focus();
      setCaretToPos(textarea, -textarea.value.length);
      setCaretToPos(textarea, selectionStart + replaceString.length);
    }
    textarea.scrollTop=scrollTop;
  }
  else if (document.selection)
  {
    //UserAgent IE-6.0
    textarea.focus();
    var range = document.selection.createRange();
    if (range.parentElement() == textarea)
    {
      var isCollapsed = range.text == '';
      if (! isCollapsed)
      {
        range.text = replaceString.replace(toBeReplaced, range.text);
        range.select();
      }
      else
      {
        range.text = replaceString;
      }
    }
  }
  else //UserAgent Gecko-1.0.1 (NN7.0)
  {
//    setSomeElement(elementId, replaceString)
//    alert("don't know yet how to handle insert" + document);
  }
}

function setSelectionRange(textarea, selectionStart, selectionEnd)
{
  if (textarea.setSelectionRange)
  {
    textarea.focus();

    textarea.setSelectionRange(textarea.selectionStart + selectionStart, textarea.selectionStart + selectionEnd);
  }
  else if (document.selection.createRange)
  {
    var range = document.selection.createRange();
    range.moveStart('character', selectionStart);
    range.moveEnd('character', selectionEnd);
    range.select();
  }
}

function setCaretToPos (textarea, pos)
{
  setSelectionRange(textarea, pos, pos);
}

function processRequest(data)
{
 // taking care of data
 if (data == null)
   return;

 var comments = data.getElementsByTagName("comments")[0];
 var newsID; 
 var comment = comments.getElementsByTagName("comment")[0];
  
 if (comments.attributes.length > 0 && comments.attributes[0].nodeValue != null)
  newsID = comments.attributes[0].nodeValue;
 
 var visibleComments = document.getElementById("comments" + newsID);
 
 if (visibleComments == null)
  return;
 
 while (visibleComments.hasChildNodes())
  visibleComments.removeChild(visibleComments.firstChild);
 
 visibleComments.innerHTML += '<h3 style="text-align:right; margin:1.25em 0 0;"><a href="/?1&amp;news=' + newsID + '&amp;add">Kommentar hinzuf&uuml;gen</a>';

 while (comment != null)
 {
  if (comment.nodeName == "comment")
  {
   var name = comment.getElementsByTagName("name")[0];
   var mail = comment.getElementsByTagName("mail")[0];
   var website = comment.getElementsByTagName("website")[0];
   var date = comment.getElementsByTagName("date")[0];
   var text = comment.getElementsByTagName("text")[0];
   
   var website_name;
   
   if (website.firstChild != null)
   {
    website_name = website.firstChild.nodeValue;
    website_name = website_name.substring(website_name.indexOf("://") + 3);
   }
   
   var entry = '<div class="entry"><h3 class="caption">';
   if (mail.firstChild != null)
    entry += "<a href=\"" + mail.firstChild.nodeValue + "\">";
   if (name.firstChild != null)
    entry += name.firstChild.nodeValue;
   if (mail.firstChild != null)
    entry += "</a>";
   if (website.firstChild != null)
    entry += "<span style=\"font-size:0.75em;\">&nbsp;&there4;&nbsp;<a href=\"" + website.firstChild.nodeValue + "\">" + website_name + "</a></span>";
   entry += '</h3><div class="comment"><div class="filler">';
   if (date.firstChild != null)
    entry += date.firstChild.nodeValue;
   entry +='</div>';
   if (text.firstChild != null)
    entry += text.firstChild.nodeValue;
   entry += '<div class="endcomment"></div></div></div>';
   
   visibleComments.innerHTML += entry;
  }
  comment = comment.nextSibling;
 }
 
 faviconize();
 
 if (document.getElementById("commentslink" + newsID) != null)
  document.getElementById("commentslink" + newsID).href = "javascript:hideComments(" + newsID + ");";
}

function RequestStates()
{
 if(this.readyState == 4 && this.status == 200)
 {
//  alert(this.getAllResponseHeaders());
  // so far so good
  if(this.responseText != null)
  {
     // success!
   processRequest(this.responseXML);
  }
  else
  {
   //test(null);
  }
 }
 else if (this.readyState == 4 && this.status != 200)
 {
  // fetched the wrong page or network error...
  //test(null);
 }
}

function getComments(newsID)
{
 if (document.getElementById("comments" + newsID) != null)
  document.getElementById("comments" + newsID).style.display = 'block';
 
 var client = new XMLHttpRequest();
 client.onreadystatechange = RequestStates;
 client.open("GET", "?comments=" + newsID);
 client.send();
}

function hideComments(newsID)
{
 if (document.getElementById("comments" + newsID) != null)
  document.getElementById("comments" + newsID).style.display = 'none';
 
 if (document.getElementById("commentslink" + newsID) != null)
  document.getElementById("commentslink" + newsID).href = "javascript:getComments(" + newsID + ");";
}