﻿//
// BlogEngine Comment Toolbar Extension: Javascript functions
//
// Extension release: 1.0
// BE Compatibility:  1.3 and superior 
// Author:            Cristiano Fino
// WebSite:           http://www.cristianofino.net
//

var txtContentID = "ctl00_cphBody_CommentView1_txtContent";
var sEn = ["[b]","[/b]"];
var sIt = ["[i]","[/i]"];
var sQu = ["[quote]","[/quote]"];

function Quote(sAuthor,commentID) {
        var otext = document.getElementById(commentID);
        var otextCommentArea = document.getElementById(txtContentID);
        if (window.getSelection)
            var sel = window.getSelection();
        else if (document.getSelection)
            var sel = document.getSelection();
        else if (document.selection) {
            var sel = document.selection.createRange().text; }        
        if (otext.innerText){
            if (sel != "") otextCommentArea.value += sEn[0] + sAuthor + sEn[1] + " wrote:\n" + sQu[0] + sel + sQu[1]; 
            else otextCommentArea.value += sEn[0] + sAuthor + sEn[1] + " wrote:\n[quote]" + otext.innerText + sQu[1];
        }
        else { 
            if (sel != "") otextCommentArea.value += sEn[0] + sAuthor + sEn[1] + " wrote:\n" + sQu[0] + sel + sQu[1]; 
            else otextCommentArea.value += sEn[0] + sAuthor + sEn[1] + " wrote:\n" + sQu[0] + otext.textContent + sQu[1];
        }
        otextCommentArea.focus();
        //ToggleCommentMenu('');
        BlogEngine.composeComment();
        //BlogEngine.showCommentPreview('livepreview', otextCommentArea);
}    

function Reply(sAuthor,commentID) {
        var otext = document.getElementById(commentID);
        var otextCommentArea = document.getElementById(txtContentID);
        var commentText = "";
        
        if (otext.innerText){
            commentText = otext.innerText;
        }else{
            commentText = otext.textContent;
        }
        
        if (commentText.length > 100) { commentText = commentText.substring(0,100) + "...";}
        
        otextCommentArea.value += "[reply]";
        otextCommentArea.value += "Reply to comment from " + sEn[0] + sAuthor + sEn[1] + " who wrote:\n";
        otextCommentArea.value += sQu[0] + sIt[0] + commentText + sIt[1] + sQu[1] ;
        otextCommentArea.value += "[/reply]\n";
        otextCommentArea.focus();
        SetEnd(otextCommentArea);
        //ToggleCommentMenu('');
        BlogEngine.composeComment();
        //BlogEngine.showCommentPreview('livepreview', otextCommentArea);
}    

function SetEnd(TB){
    if (TB.createTextRange){
        var FieldRange = TB.createTextRange();
        FieldRange.moveStart('character', TB.value.length);
        FieldRange.collapse();
        FieldRange.select();
    }
}
