﻿var command = "";
var stylesheeturl = "http://bsa.3form.intranet/templates/nonprintable/stylesheet.css";
var editorFrame;

function Start(fieldName, startText)
{
editorFrame = document.getElementById(fieldName);
editorFrame.contentWindow.document.body.innerHTML = startText;
editorFrame.contentWindow.document.body.className = "threeRTEdit";


var elm = document.createElement("link");
elm.rel = "Stylesheet";
elm.type = "text/css";
elm.href = stylesheeturl;
editorFrame.contentWindow.document.getElementsByTagName("head")[0].appendChild(elm);

editorFrame.contentWindow.document.designMode = "on";
try
{
editorFrame.contentWindow.document.execCommand("undo", false, null);
}
catch (e)
{
alert("This editor is not supported on your level of Mozilla.");
}

InitToolbarButtons(fieldName);
//document.addEventListener("mousedown", dismisscolorpalette, true);
//editorFrame.contentWindow.document.addEventListener("mousedown", dismisscolorpalette, true);
//document.addEventListener("keypress", dismisscolorpalette, true);
//editorFrame.contentWindow.document.addEventListener("keypress", dismisscolorpalette, true);
return (true);
}

function InitToolbarButtons(fieldName) {
kids = document.getElementsByTagName('DIV');

for (var i=0; i < kids.length; i++) {
if (kids[i].className == "TEimagebutton") {
kids[i].onclick = tbclick;
kids[i].editorName = fieldName;
}
}
}

function insertNodeAtSelection(win, insertNode)
{
// get current selection
var sel = win.getSelection();

// get the first range of the selection
// (there's almost always only one range)
var range = sel.getRangeAt(0);

// deselect everything
sel.removeAllRanges();

// remove content of current selection from document
range.deleteContents();

// get location of current selection
var container = range.startContainer;
var pos = range.startOffset;

// make a new range for the new selection
range=document.createRange();

if (container.nodeType==3 && insertNode.nodeType==3) {

// if we insert text in a textnode, do optimized insertion
container.insertData(pos, insertNode.nodeValue);

// put cursor after inserted text
range.setEnd(container, pos+insertNode.length);
range.setStart(container, pos+insertNode.length);

} else {


var afterNode;
if (container.nodeType==3) {

// when inserting into a textnode
// we create 2 new textnodes
// and put the insertNode in between

var textNode = container;
container = textNode.parentNode;
var text = textNode.nodeValue;

// text before the split
var textBefore = text.substr(0,pos);
// text after the split
var textAfter = text.substr(pos);

var beforeNode = document.createTextNode(textBefore);
var afterNode = document.createTextNode(textAfter);

// insert the 3 new nodes before the old one
container.insertBefore(afterNode, textNode);
container.insertBefore(insertNode, afterNode);
container.insertBefore(beforeNode, insertNode);

// remove the old node
container.removeChild(textNode);

} else {

// else simply insert the node
afterNode = container.childNodes[pos];
container.insertBefore(insertNode, afterNode);
}

range.setEnd(afterNode, 0);
range.setStart(afterNode, 0);
}

sel.addRange(range);
};

function getOffsetTop(elm) {

var mOffsetTop = elm.offsetTop;
var mOffsetParent = elm.offsetParent;

while(mOffsetParent){
mOffsetTop += mOffsetParent.offsetTop;
mOffsetParent = mOffsetParent.offsetParent;
}

return mOffsetTop;
}

function getOffsetLeft(elm) {

var mOffsetLeft = elm.offsetLeft;
var mOffsetParent = elm.offsetParent;

while(mOffsetParent){
mOffsetLeft += mOffsetParent.offsetLeft;
mOffsetParent = mOffsetParent.offsetParent;
}

return mOffsetLeft;
}

function tbclick()
{
var extractedId = this.id.substring(this.id.lastIndexOf('_') + 1);
if ((extractedId == "forecolor") || (extractedId == "hilitecolor")) {
parent.command = extractedId;
buttonElement = document.getElementById(extractedId);
//document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement);
//document.getElementById("colorpalette").style.top = getOffsetTop(buttonElement) + buttonElement.offsetHeight;
//document.getElementById("colorpalette").style.visibility="visible";
} else if (extractedId == "link") {
var szURL = prompt("Enter a URL:", "http://");
if ((szURL != null) && (szURL != "")) {
document.getElementById(this.editorName).contentWindow.document.execCommand("CreateLink",false,szURL);
}
} else if (extractedId == "image") {
imagePath = prompt('Enter Image URL:', 'http://');
if ((imagePath != null) && (imagePath != "")) {
document.getElementById(this.editorName).contentWindow.document.execCommand('InsertImage', false, imagePath);
}
} else if (extractedId == "table") {
e = document.getElementById(this.editorName);
rowstext = prompt("enter rows");
colstext = prompt("enter cols");
rows = parseInt(rowstext);
cols = parseInt(colstext);
if ((rows > 0) && (cols > 0)) {
table = e.contentWindow.document.createElement("table");
table.setAttribute("border", "1");
table.setAttribute("cellpadding", "2");
table.setAttribute("cellspacing", "2");
tbody = e.contentWindow.document.createElement("tbody");
for (var i=0; i < rows; i++) {
tr =e.contentWindow.document.createElement("tr");
for (var j=0; j < cols; j++) {
td =e.contentWindow.document.createElement("td");
br =e.contentWindow.document.createElement("br");
td.appendChild(br);
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
insertNodeAtSelection(e.contentWindow, table);
}
} else {
document.getElementById(this.editorName).contentWindow.document.execCommand(extractedId, false, null);
}
}

function Select(selectname, editorName)
{
var cursel = document.getElementById(selectname).selectedIndex;
/* First one is always a label */
if (cursel != 0) {
var selected = document.getElementById(selectname).options[cursel].value;
document.getElementById(editorName).contentWindow.document.execCommand('formatblock', false, selected);
document.getElementById(selectname).selectedIndex = 0;
}
document.getElementById(editorName).contentWindow.focus();
}

//function dismisscolorpalette()
//{
//  document.getElementById("colorpalette").style.visibility="hidden";
//}

