var currentfocus; /* Used to keep focus within a particular text area. */
var nofocus = true;

var agt = navigator.userAgent.toLowerCase();
var is_ie	   = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_opera  = (agt.indexOf("opera") != -1);
var is_mac	   = (agt.indexOf("mac") != -1);
var is_mac_ie = (is_ie && is_mac);
var is_win_ie = (is_ie && !is_mac);
var is_gecko  = (navigator.product == "Gecko");


function init_editor(section,key)
{
	if( section!='' ) section='_'+section;
	if( key!='' ) key='_'+key;

	var iframe = document.getElementById('iframe'+section+key);
	var friend = document.getElementById(iframe.getAttribute('friend'));

	if( iframe.contentDocument!=null )
	{
		var doc = iframe.contentDocument;
	} else {
		var doc = iframe.contentWindow.document;
	}

	// Copy the friend content into the frame...
	doc.body.innerHTML = friend.value;

	// Get the blur function.
	eval("editor_blur = blur"+section+key+";");
	eval("editor_focus = focus"+section+key+";");

	
	// Mozilla...

	if( doc.addEventListener!=null )
	{
		eval("editor_click = click"+section+key+";");

		// Setup the edit mode.
		doc.designMode = 'on';

		doc.execCommand('useCSS',false,false);
		doc.execCommand('readonly',false,true); // backward but correct!

		// Add events.
		doc.addEventListener( 'blur', editor_blur, true );
		doc.addEventListener( 'focus', editor_focus, true );
		doc.addEventListener( 'click', editor_click, true );
	}

	// IE...

	if( iframe.attachEvent!=null )
	{
		eval("editor_click = selectionchange"+section+key+";");

		// Setup the edit mode.
		doc.body.contentEditable = true;

		// Add events.
		iframe.attachEvent( 'onblur', editor_blur );
		iframe.attachEvent( 'onfocus', editor_focus );
		doc.attachEvent( 'onselectionchange', editor_click );
	}

	return;
}


function openImageWindow()
{
	if( currentfocus!=null  )
	{
		win = window.open("?tool=image", "linktoolbox", "height=350, width=400, resizable=0, left=200, screenX=200, top=200, screenY=200, status=1, toolbar=0, scrollbars=0, menubar=0");
		win.focus();
	} else {
		alert ("To place an image, first select a point to place it, then click the image button.");
	}

	return;
}

function openFileWindow()
{
	if( currentfocus!=null ) // && currentfocus.hasSelectedText() )
	{
		win = window.open("?tool=file", "linktoolbox", "height=250, width=500, resizable=0, left=200, screenX=200, top=200, screenY=200, status=1, toolbar=0, scrollbars=0, menubar=0");
		win.focus();
	} else {
		alert ("To create a file link, first select some text, then click the link button.");
	}

	return;
}

function toolbox1(cmd)
{
	if( currentfocus )
	{
		iframe.focus();
		currentfocus.execCommand(cmd);
	}

	return;
}

function toolbox2(cmd,param)
{
	if( currentfocus )
	{
		iframe.focus();
		currentfocus.execCommand(cmd, param);
	}

	return;
}

function toolbox3(cmd,param,param2)
{
	if( currentfocus )
	{
		iframe.focus();
		currentfocus.execCommand(cmd, param, param2);
	}

	return;
}


function getSelection(theFrame, theFocus)
{
	//SC - Firefox keeps returning errors as this function is being called with blank parameters from somewhere
	if(!theFrame && !theFocus) return "";
	
	
	if (theFrame != null  && theFrame.getSelection != null)
	{
		//think this is MAC IE
		txt = theFrame.getSelection();
	}
	else if (theFrame.contentWindow.getSelection)
	{
		// NEW WAY ON FIREFOX
		txt = theFrame.contentWindow.getSelection();
	}
	else if (theFocus.getSelection)
	{
		//older mozilla
		txt = theFocus.getSelection();
	}
	else if( theFocus.selection )
	{
		txt = theFocus.selection.createRange().text;
	} else {
		alert("sorry, your browser does not support this feature.");
		txt = ""
	}

	return( txt );
}

// In progress!! Onlt returns HTML for IE... 
// All other browsers still returns the text.
function getSelectionHTML(theFrame, theFocus)
{
	if (theFrame != null  && theFrame.getSelection != null)
	{
		//think this is MAC IE
		txt = theFrame.getSelection();
	}
	else if (theFrame.contentWindow.getSelection)
	{
		// NEW WAY ON FIREFOX
		txt = theFrame.contentWindow.getSelection();
	}
	else if (theFocus.getSelection)
	{
		//older mozilla
		txt = theFocus.getSelection();
	}
	else if( theFocus.selection )
	{
		txt = theFocus.selection.createRange().htmlText;
	} else {
		alert("sorry, your browser does not support this feature.");
		txt = "";
	}
	
	return( txt );
}

function pasteHTML(theFrame,theFocus,theField,text)
{
	if( theFrame != null  && theFrame.getSelection != null )
	{
		theFrame.getSelection();
	}
	else if (theFrame.contentWindow.getSelection)
	{
		var doc = theFrame.contentWindow.document;
		var fragment = doc.createDocumentFragment();
		var div = doc.createElement("div");

		div.innerHTML = text;

		while (div.firstChild)
		{
			// the following call also removes the node from div
			fragment.appendChild(div.firstChild);
		}

		insertNodeAtSelection(theFrame, fragment);
		// focus does not not work to force a save so it is being done by hand.
		saveHTML(theFrame,theField);
	}
	else if (theFocus.getSelection)
	{
		alert("javascript: pasteHTML - not coded this browser" );
	}
	else if( theFocus.selection )
	{
		theFocus.selection.createRange().pasteHTML( text );
		saveHTML(theFrame,theField);
	} else {
		alert("sorry, your browser does not support this feature.");
	}

	return( "" );
}


function createRange(theFrame,sel)
{
	doc = theFrame.contentWindow.document;

	theFrame.focus();

	if( typeof sel != "undefined" )
	{
		try
		{
			return sel.getRangeAt(0);
		} catch(e) {
			return doc.createRange();
		}
	} else {
		return doc.createRange();
	}
}

function insertNodeAtSelection(theFrame, toBeInserted)
{
	var	sel = theFrame.contentWindow.getSelection();
	var	range = createRange(theFrame,sel);

	sel.removeAllRanges();
	range.deleteContents();
	var node = range.startContainer;
	var pos = range.startOffset;

	switch( node.nodeType )
	{
		case 3: // Node.TEXT_NODE
			// we have to split it at the caret position.
			if( toBeInserted.nodeType == 3 )
			{
				// do optimized insertion
				node.insertData(pos, toBeInserted.data);
				range = createRange(theFrame);
				range.setEnd(node, pos + toBeInserted.length);
				range.setStart(node, pos + toBeInserted.length);
				sel.addRange(range);
			} else {
				node = node.splitText(pos);
				var selnode = toBeInserted;

				if (toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */)
				{
					selnode = selnode.firstChild;
				}

				node.parentNode.insertBefore(toBeInserted, node);
			}
			break;

		case 1: // Node.ELEMENT_NODE
			var selnode = toBeInserted;

			if( toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */ )
			{
				selnode = selnode.firstChild;
			}

			node.insertBefore(toBeInserted, node.childNodes[pos]);
			break;
	}

	return;
}
