var canFormat = 0;
if (document.selection)
  canFormat = 1;
var ua = navigator.userAgent;
if (ua.indexOf('Gecko') >= 0 && ua.indexOf('Safari') < 0)
  canFormat = 1;

//function to display comment tools such as bold, 
//italicize, quote, etc.
function commentTools() {
  var commentTxBox = "document.commentform.text";
  
  if (canFormat) {
    with (document) {
        write('<img src="/sepia/images/left_endcap.gif" width="8" ' + 
	  'height="24" border=0 />');
        write('<a title="Quote" href="#" onclick="return ' + 
	  'formatStr(' + commentTxBox + ', \'blockquote\')">' +
	  '<img src="/sepia/images/blockquote_button.gif" alt="Quote" ' + 
	  'width="22" height="24" border=0 /></a>');
        write('<a title="Insert Link" href="#" ' + 
	  'onclick="return insertLink(' + commentTxBox + ')">' +
	  '<img src="/sepia/images/hyperlink_button.gif" alt="Insert Link" ' +
	  'width="22" height="24" border=0 /></a>');
        write('<a title="Bold" href="#" ' + 
	  'onclick="return formatStr(' + commentTxBox + ', \'b\')">' +
	  '<img src="/sepia/images/bold_button.gif" alt="Bold" width="22" ' +
	  'height="24" border=0 border=0/></a>');
        write('<a title="Italic" href="#" ' + 
	  'onclick="return formatStr(' + commentTxBox + ', \'i\')">' +
	  '<img src="/sepia/images/italic_button.gif" alt="Italic" ' + 
	  'width="22" height="24" border=0 /></a>');
        write('<a title="Underline" href="#" ' +
	  'onclick="return formatStr(' + commentTxBox + ', \'u\')">' + 
	  '<img src="/sepia/images/underline_button.gif" alt="Underline" ' +
	  'width="22" height="24" border=0 /></a>');
        write('<a title="Strikeout" href="#" ' +
	  'onclick="return formatStr(' + commentTxBox + ', \'strike\')">' +
	  '<img src="/sepia/images/strikeout_button.gif" alt="Strikeout" ' +
	  'width="22" height="24" border=0 /></a>');
        write('<img src="/sepia/images/right_endcap.gif" width="8" ' +
	  'height="24" border=0 /></a>');
    }
  }
}

//fill Blog comment form
//copied over from MT template.
function fillBlogCommentForm() {
    if (document.commentform) { //if comments are still open
      if (document.commentform.email != undefined)
        document.commentform.email.value = getCookie("mtcmtmail");
	
      if (document.commentform.author != undefined) 
        document.commentform.author.value = getCookie("mtcmtauth");
	
      if (document.commentform.url != undefined)
        document.commentform.url.value = getCookie("mtcmthome");
	
      if (document.commentform.bakecookie != undefined) {
        if (getCookie("mtcmtauth") || getCookie("mtcmthome")) {
          document.commentform.bakecookie[0].checked = true;
        } else {
          document.commentform.bakecookie[1].checked = true;
        }
      }
      
    }
}

$(document).ready(function() {fillBlogCommentForm()});

//used in Comment tools. gets selected text.
//processes per tool. replaces selected text with
//processed text.
function formatStr (e, v) {
    if (!canFormat) return;
    var str = getSelection(e);
    if (!str) return;
    setSelection(e, '<' + v + '>' + str + '</' + v + '>');
    return false;
}

//used in Comment tools. gets selected text for
//processing by a comment tool.
function getSelection (e) {
    if (document.selection)
        return document.selection.createRange().text;
    else {
        var length = e.textLength;
        var start = e.selectionStart;
        var end = e.selectionEnd;
        if (end == 1 || end == 2) end = length;
        return e.value.substring(start, end);
    }
}

//comment tool to insert a link, i.e., create a hyperlink.
function insertLink (e, isMail) {
    if (!canFormat) return;
    var str = getSelection(e);
    if (!str) return;
    var my_link = isMail ? prompt('Enter email address:') : prompt('Enter URL:', '');
    if (isMail) my_link = 'mailto:' + my_link;
    if (my_link != null)
        setSelection(e, '<a href="' + my_link + '">' + str + '</a>');
    return false;
}   

//comment tools use this function to replace selected text
//after processed by tool.
function setSelection (e, v) {
    if (document.selection)
        document.selection.createRange().text = v;
    else {
        var length = e.textLength;
        var start = e.selectionStart;
        var end = e.selectionEnd;
        if (end == 1 || end == 2) end = length;
        e.value = e.value.substring(0, start) + v + e.value.substr(end, length);
    }
}

