function ins(name){
if (document.REPLIER) {
var input=document.REPLIER.Post;
input.value=input.value+"[b]"+name+"[/b]"+" \n";
}
}

function Insert(text){
if (text!="") paste("[quote]"+text+"[/quote]\n", 0);
}

function paste(text, flag){
if (document.REPLIER) {
if ((document.selection)&&(flag)) {
document.REPLIER.Post.focus();
document.REPLIER.document.selection.createRange().text = text;
} else document.REPLIER.Post.value += text;
}
}

function old_get_selection() {
   if (document.getSelection){
selection = document.getSelection();
selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
selection = selection.replace(/\r\n/gi, " ");
       while (selection.indexOf("  ") !=-1) selection = selection.replace(/  /gi, "");
selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
  } else
      selection = document.selection.createRange().text;
}


function get_selection() {
  if (window.getSelection){
selection = window.getSelection();
fragment = selection.getRangeAt(0).cloneContents();
replacer(fragment);
selection = fragment.textContent;
//   selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
// selection = selection.replace(/\r\n/gi, " ");
//       while (selection.indexOf("  ") !=-1) selection = selection.replace(/  /gi, "");
// selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
 } else if(document.selection)
 {
   if(document.selection.createRange().htmlText)
  {
     text = document.selection.createRange().htmlText;
    var fragment = document.createDocumentFragment();
  var node = document.createElement("div");
  node.innerHTML = text;
    fragment.appendChild(node);
     replacer(fragment);
  selection = fragment.firstChild.innerText;
  }
  else
  {
  selection = document.selection.createRange().text;
}
 }
 else
 {
   selection = document.getSelection();
 }
}
function replacer(root)
{
if(root.nodeType!=1&&root.nodeType!=11)return;
if(root.hasChildNodes())
{
var len = root.childNodes.length;
for(var i=0;i<len;i++)
{
 var item = root.childNodes.item(i);
  replacer(item);
}
}
switch(root.nodeName)
{
 case "B":
 case "I":
 case "U":
 case "S":
  makeSimpleTags(root,root.nodeName);
break;
 case "MARQUEE":
  makeSimpleTags(root,"MOVE");
break;
 case "BR":
  var br = root.ownerDocument.createElement("div");
br.appendChild(root.ownerDocument.createTextNode("\r\n"));
br.appendChild(root.ownerDocument.createElement("br"));
 root.parentNode.replaceChild(br,root);
break;
 case "IMG":
 var alt = root.attributes.getNamedItem("alt").nodeValue;
if(alt == "user posted image")
{
 var src = root.attributes.getNamedItem("src").nodeValue;
 var newroot = root.ownerDocument.createElement("div");
 root.parentNode.replaceChild(newroot,root);
 newroot.appendChild(root.ownerDocument.createTextNode(src));
 makeSimpleTags(newroot,"IMG");
}
 else
{
 root.parentNode.replaceChild(root.ownerDocument.createTextNode(" "+alt+" "),root);
}
break;
 case "A":
 var href = root.attributes.getNamedItem("href").nodeValue;
makeTags(root,"[URL="+href+"]","[/URL]");
break;

 case "SPAN":
    var style = root.attributes.getNamedItem("style");
 if(style!=null)
 {
  styleData = style.nodeValue.match(/(.*?);/g);
  for(var k=0;k<styleData.length;k++)
  {
   var data = styleData[k].match(/(.*?):\s(.*?);/);
   var name = data[1];
   var value = data[2];
   switch(name)
   {
    case "font-family":
 makeTags(root,"[FONT="+value+"]","[/FONT]");
 break;
    case "color":
 makeTags(root,"[COLOR="+value+"]","[/COLOR]");
 break;
    case "font-size":
 var sizeStr = value.match(/(.*)pt/);
 var size = Number(sizeStr[1])-7;
 makeTags(root,"[SIZE="+size+"]","[/SIZE]");
 break;
   }
  }
 }
 case "DIV":
   var classItem = root.attributes.getNamedItem("class");
if(classItem!=null)
{
 var className = root.attributes.getNamedItem("class").nodeValue;
switch(className)
{
 case "quotetop":
  if(root.previousSibling==null) { quoteHead = "[QUOTE]"; }
  else {
   quoteData = root.previousSibling.nodeValue.match("QuoteBegin-(.*)[\\+](.*)");
   quoteHead = "[QUOTE="+quoteData[1]+","+quoteData[2]+"]";
  }
  root.parentNode.replaceChild(root.ownerDocument.createTextNode(quoteHead),root);
  break;
 case "quotemain":
  makeTags(root,null,"[/QUOTE]");
  break;
}
}
break;

}
return;
}

function makeSimpleTags(root,tag)
{
var startTag = "["+tag+"]";
var endTag = "[/"+tag+"]";
makeTags(root,startTag,endTag);
}

function makeTags(root,startTag,endTag)
{
if(root.firstChild.nodeValue!=startTag && root.lastChild.nodeValue!=endTag)
{
  if(startTag!=null)root.insertBefore(root.ownerDocument.createTextNode(startTag),root.firstChild);
   if(endTag!=null)root.appendChild(root.ownerDocument.createTextNode(endTag));
}
}

function init() {
document.onkeydown = register;
}
function register(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;

if ((code == 13) && ((e.ctrlKey == true) || (e.shiftKey == true))) {get_selection();Insert(selection);}
}
window.onload=init;
