/*

J.A.G. (Just Another Guestbook) v1.14

Copyright (C) 2003 crisp - freesoftware@xs4all.nl

See full notice in guestbook.php

*/

var target = null;
function getTarget(el) {

  target = (document.getElementById)? document.getElementById(el):0;

}

function storeCursor(el) {

  if (document.all && el.createTextRange) el.cursorPos = document.selection.createRange().duplicate();

}

function putStr(text) {

  if (target) {

    if (document.all && typeof(target.cursorPos) != 'undefined') {

      target.cursorPos.text = text;

    } else if (typeof(target.selectionStart) != 'undefined') {

      var sStart = target.selectionStart;
      var sEnd = target.selectionEnd;
      target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd, target.value.length);
      target.selectionStart = (sStart == sEnd)? sStart + text.length:sStart;
      target.selectionEnd = sStart + text.length;

    } else {

      target.value += text;

    }

    target.focus();
    storeCursor(target);

  }

}

function apply(style) {

  if (target) {

    var selectedtext = '';
    window.focus();
    if (document.all && typeof(target.cursorPos) != 'undefined') {
      selectedtext = target.cursorPos.text;
    } else if (typeof(target.selectionStart) != 'undefined') {
      selectedtext = target.value.substr(target.selectionStart, target.selectionEnd - target.selectionStart);
    }

    if (!selectedtext) selectedtext = prompt(prompttext[style], '');
    if (!selectedtext) { target.focus(); return; }

    if (style == 'url=') {
      var description = prompt(prompttext['urld'], '');
      if (!description) { target.focus(); return; }
      selectedtext = '[url='+selectedtext+']'+description+'[/url]';
    } else {
      selectedtext = '['+style+']'+selectedtext+'[/'+style+']';
    }

    putStr(selectedtext);

  }

}

var popup = null;
function openwin(URL, winName, height, width) {

  if (!popup) {

    var top = (screen.height - height) / 2;
    var left = (screen.width - width) / 2;
    popup = window.open(URL, winName, 'top='+top+',left='+left+',height='+height+',width='+width+',toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=no');

  }

  try { popup.focus(); }
  catch(e) { popup = null; openwin(URL, winName, height, width); }

}

function sizewin() {

  var curHeight = document.body.scrollHeight + 30;
  var curWidth = window.innerWidth || document.body.offsetWidth;
  if (curHeight < 400) window.resizeTo(curWidth+8, curHeight);

}