function goToTheEnd() {
  var ed=tinyMCE.activeEditor;
  var root=ed.dom.getRoot();  // This gets the root node of the editor window
  var lastnode=root.childNodes[root.childNodes.length-1]; // And this gets the last node inside of it, so the last <p>...</p> tag
  if (tinymce.isGecko) {
    // But firefox places the selection outside of that tag, so we need to go one level deeper:
    lastnode=lastnode.childNodes[lastnode.childNodes.length-1];
  }
  // Now, we select the node
  ed.selection.select(lastnode);
  // And collapse the selection to the end to put the caret there:
  ed.selection.collapse(false);
}
function quote(commentId, authorId, authorName)
{  
   var selText = getSel();  
   var commentText = "";
   var id = '#CommentBody_' + commentId;
   //alert($(id).html());
   commentText = $(id).clone();
   commentText.children().filter('.signature').remove();
   commentText = commentText.html();
 
   if(selText != "")
      commentText = selText;

	var oEditor = FCKeditorAPI.GetInstance('Body') ;
	if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG ) {
		var FCKeditorquote = '<blockquote><cite>'+authorName+'</cite> '+ commentText+'</blockquote><p>&nbsp;</p>';
		oEditor.InsertHtml(FCKeditorquote ) ; }
	else
		alert( 'You must be on WYSIWYG mode!' ) ;
	oEditor.Focus();
	location.hash = '#CommentEditor';
	return false;
}
function getSel()
{
   if(window.getSelection)
   {
      var d = document.createElement("p");
      var s = window.getSelection();
   
      for(var i = 0; i < s.rangeCount; ++i)
      {
         var r = s.getRangeAt(i);
         if (!r.collapsed) 
         {
            d.appendChild(r.cloneContents());
         }
      }
      return dumpCodeTree(d);
   }
   else
      return "";
}

function dumpCodeTree(root) 
{
   var children     = root.childNodes;
   var outputText   = "";
   
   //BBCode should use square brackets:
   
   var tagOpenPrefix  = "<";
   var tagOpenSuffix  = ">";
   var tagClosePrefix = "</";
   var tagCloseSuffix = ">";
      
   for (var i = 0; i < children.length; i++) 
   {
      if(children[i].nodeType == 1)
      {
         var tagName = children[i].tagName.toLowerCase();
         
         //If html tags need to be changed into something else,
         //here is the place to do it:
         
         if(tagName == "br")
         {
            outputText += "\r\n";
            tagName = "";
         }
         
         if(tagName != "")
         {
            outputText += tagOpenPrefix; 
            outputText += tagName;

            for (var j = 0; j < children[i].attributes.length; j++) 
            {
               attributeName  = children[i].attributes[j].name;
               attributeValue = children[i].attributes[j].value;
               
               //If html attributes need to be changed into something else,
               //here is the place to do it: 
                             
               if(attributeName == "target"          ||
                  attributeName == "hideFocus"       ||
                  attributeName == "contentEditable" ||
                  attributeName == "disabled"        ||
                  attributeName == "tabIndex")
                  {attributeName = "";}
               
               if(attributeName != "" && attributeValue != "null" && attributeValue != "")
               {
                  outputText += " ";
                  outputText += attributeName;
                  outputText += "=\"";
                  outputText += attributeValue;
                  outputText += "\"";
               }
            }

            outputText += tagOpenSuffix;
            outputText += dumpCodeTree(children[i]);
            outputText += tagClosePrefix;
            outputText += tagName;
            outputText += tagCloseSuffix;
         }
      }
      else if(children[i].nodeType == 3)
      {
         var nodeValue = children[i].nodeValue;
         
         //strip out extra newlines/returns
         nodeValue = nodeValue.replace(/\r\n/g," ");
         nodeValue = nodeValue.replace(/\n/g," ");
         nodeValue = nodeValue.replace(/\r/g," ");
         
         //strip whitespace
         nodeValue = nodeValue.replace(/^\s*|\s*$/g,'');
         
         outputText += nodeValue;
      }
   }
   
   return outputText;
}