/*####################################################################*/
/*# EDITOR OBJECT #*/ 

/*####################################################################*/
function editorObject()
{
	this.oEditorDiv = null;
	this.paletteIsFor = null;
	this.toggleMode = 0;
	this.havePressedTheButtonBefore = false;	
}
/*####################################################################*/
/*# EDITOR FUNCTIONS #*/
/*####################################################################*/
function makeActive()
{
	lastActiveWindow.lastEditor = this;
}
function getValue()
{
	if(this.toggleMode == 1) this.toggleIt();
	if(this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML == '')
	{
		return this.oEditorDiv.htmlViewer.firstChild.innerText;
	}
	else
	{
		return this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML;
	}
}
function insertValue(theValue)
{
	this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML = theValue;
}

function createEditor(xmlSheetPath,editorWidth,editorHeight,uploadLoc)
{
	this.uploadLoc = uploadLoc;
	
	var fontSizeDropBox = new DropBoxV2("fontSize", "", "", top.app_path+"/pcrunch-api", this);
	fontSizeDropBox.SetLabelWidth(50);
	fontSizeDropBox.SetDimensions(0, 110, 120); // indent width, drop height & drop width
	
	var fontNameDropBox = new DropBoxV2("fontName", "","", top.app_path+"/pcrunch-api", this);
	fontNameDropBox.SetLabelWidth(50);
	fontNameDropBox.SetDimensions(0, 110, 120); // indent width, drop height & drop width
		
	fontSizeDropBox.setOnClick
	fontSizeDropBox.SetOnChange("changeFontSizeThis(top.getDoc(this), \"text_box"+fontSizeDropBox.GetName()+"\" )");
	fontNameDropBox.SetOnChange("changeFontThis(top.getDoc(this), \"text_box"+fontNameDropBox.GetName()+"\" )");

	var droplist1 = fontSizeDropBox.ReturnCreate("3");
	var droplist2 = fontNameDropBox.ReturnCreate("Verdana");
	
	top.DropboxFont = fontNameDropBox;
	top.DropboxSize = fontSizeDropBox;

	// generate editor HTML	
	var editorHTML = '\
		<table style="width: '+editorWidth+'; height: '+editorHeight+';" cellspacing="0" cellpadding="0" border="0" onmouseover="if(!document.all) lastActiveWindow.lastEditor = this.parentNode.oRef;">\
		<tbody>\
			<tr style="height:65px; border-bottom: solid 1px #cccccc;">\
				<td valign="top" style="background: #f0f0f0;">\
					<div id="oEditorToolBar" style="background: #f0f0f0; padding: 3px 3px 0px 3px; ">\
						<!-- Icons will go here after transform -->\
					<\/div>\
					<div style="display: none; position: absolute; width:150px; height:inherit;z-index: 999" onmouseover="this.style.display = \'inline\';" onmouseout="this.style.display = \'none\';">\
						<!-- Pallette will go here after transform -->\
						<div id="fontArea"></div>\
					<\/div>\
					<div style="display: none; position: absolute; width:auto; height:auto; z-index: 999; background-color:#f0f0f0; border:1px solid #cccccc" onmouseover="this.style.display = \'inline\';" onmouseout="this.style.display = \'none\';">\
						<table>\
						<tbody>\
						<tr>\
							<td><label class="link_label">Link<\/label></td><td><input class="link_input_box" type=text name="link_input" id="link_input"></td>\
						</tr>\
						<tr>\
							<td><label class="link_label">New window</label></td><td><input type=checkbox name="new_window" id="new_window">\
							<input type=button id="button_add_link" value="Add Link" onclick="lastActiveWindow.lastEditor.addLinkToText();"><\/td>\
						</tr>\
						</tbody>\
						</table>\
					<\/div>\
					<div id="oEditorToolBar2" style="position:absolute; left:inherit; background: #f0f0f0; padding: 3px 3px 3px 3px;z-index:1">\
					<table style="position:relative; left:inherit; width:150px; height:inherit; ">\
					<tbody>\
					<tr>\
			';
	editorHTML += 				droplist1 + ' ' +droplist2;
	editorHTML += 	' 		<\/tr>\
					<\/tbody>\
					<\/table>\
					<\/div>\
				<\/td>\
			<\/tr>\
			<tr style="height:99%;">\
				<td valign="top" style="height:99%;">\
					<iframe style="height:100%; width:100%; vertical-align:top;" onfocus="lastActiveWindow.lastEditor = this.parentNode.parentNode.parentNode.parentNode.parentNode.oRef;" onblur="/*selection2 = this.contentWindow.document.selection.createRange(); selection2.select();*/" frameborder="0"></iframe>\
				<\/td>\
			<\/tr>\
		<\/tbody>\
		<\/table>\
		<div style="display: none;" id="htmlviewerCI"><textarea style="width: 100%; height: '+editorHeight+';"></textarea></div>\
		<div style="display: none; position: absolute; width: 100%; height: 800px; margin-top: -200px; z-index: 3;"><img src="../pcrunch-api/images/spacer.gif" style="width: 100%; height: 100%;" /></div>\
		<iframe style="display: none;" name="editorupdater"></iframe>\
		<div style="padding: 2px; background: #f0f0f0;" >\
			<div style="text-align: center; width: 50px; padding: 2px; border: solid 1px #cccccc;" onclick="lastActiveWindow.lastEditor.toggleIt(this);">&lt;html&gt;</div>\
		<\/div>\
		';
	
	this.oEditorDiv = document.createElement('div');
	this.oEditorDiv.style.border = 'solid 1px #cccccc';
	
	this.oEditorDiv.innerHTML = editorHTML;
	
	this.oEditorDiv.enableIcons = this.oEditorDiv.getElementsByTagName("DIV")[0];
	this.oEditorDiv.textBBArea = this.oEditorDiv.getElementsByTagName("IFRAME")[0];
	this.oEditorDiv.pallette = this.oEditorDiv.getElementsByTagName("DIV")[1];
	this.oEditorDiv.cover = this.oEditorDiv.getElementsByTagName("DIV")[2];
	this.oEditorDiv.hyperlink = this.oEditorDiv.getElementsByTagName("DIV")[3];
	this.oEditorDiv.enableFont = this.oEditorDiv.getElementsByTagName("DIV")[4];
	this.oEditorDiv.wyswigViewer = this.oEditorDiv.getElementsByTagName("TABLE")[0];
	this.oEditorDiv.htmlViewer = getChildElementById(this.oEditorDiv,'DIV','htmlviewerCI');
	
	
	// transform icon xml sheet with xsl sheet
	// Begin transform
	
	if (window.ActiveXObject)
	{
		// transform icon xml sheet with xsl sheet
		this.xmlPreloadDataEditorIcons = new ActiveXObject("Microsoft.XMLDOM");
		this.xslPreloadDataEditorIcons = new ActiveXObject("Microsoft.XMLDOM");
		
		// Font and size elements
		this.xmlPreloadFontItems =  new ActiveXObject("Microsoft.XMLDOM");
		this.xslPreloadFontItems =  new ActiveXObject("Microsoft.XMLDOM");		
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		this.xmlPreloadDataEditorIcons = document.implementation.createDocument("", "", null);
		this.xslPreloadDataEditorIcons = document.implementation.createDocument("", "", null);
		
		// Font and size elements
		this.xmlPreloadFontItems =  document.implementation.createDocument("", "", null);
		this.xslPreloadFontItems =  document.implementation.createDocument("", "", null);
	}
	
	// Icon elements
	this.xmlPreloadDataEditorIcons.async = false;
	this.xmlPreloadDataEditorIcons.load(xmlSheetPath);

	this.xslPreloadDataEditorIcons.async = false;
	this.xslPreloadDataEditorIcons.load('../pcrunch-api/oEditor/editor.xsl');


	if (window.ActiveXObject)
	{
		// Icon elements
		var newHTML = this.xmlPreloadDataEditorIcons.transformNode(this.xslPreloadDataEditorIcons);
		this.oEditorDiv.enableIcons.innerHTML = newHTML;
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		// Icon elements
		var processor = new XSLTProcessor();
		processor.importStylesheet(this.xslPreloadDataEditorIcons);
		newHTML = processor.transformToFragment(this.xmlPreloadDataEditorIcons, document);
		this.oEditorDiv.enableIcons.appendChild(newHTML);
		
	}
	// end Transform
	
	/* special case icons that cannot be generated */
	var imageIconHTML = '\
		<form action="'+lastActiveWindow.lastEditor.uploadLoc+'" enctype="multipart/form-data" method="post" class="fileUpload" target="editorupdater"><div class="fileinputs" style="overflow: hidden;"><input name="userfile" type="file" class="file hidden" noscript="true" onchange="len(this.parentNode.parentNode); this.parentNode.parentNode.submit();" />\
			<div class="fakefile">\
				<input />\
				<img src="../pcrunch-api/oEditor/images/default/dx_image.gif" alt="Browse" style="position: absolute; left: 0px; z-index:5;" />\
			<\/div>\
		<\/div>\
		<\/form>\
	';
	var imageIcon = document.createElement('span');
	imageIcon.style.position = 'absolute';
	imageIcon.innerHTML = imageIconHTML;
	imageIcon.style.border = 'solid 1px #cccccc';
	imageIcon.style.paddingLeft = '1px';
	imageIcon.style.paddingRight = '4px';
	imageIcon.style.overflow = 'hidden';
	if(document.all) imageIcon.style.height = '24px';
	else imageIcon.style.height = '22px';
	imageIcon.onmouseover = function() { if(lastActiveWindow.lastEditor != null) lastActiveWindow.lastEditor.overIt(imageIcon) }
	imageIcon.onmouseout = function() { if(lastActiveWindow.lastEditor != null) lastActiveWindow.lastEditor.offIt(imageIcon) }
	imageIcon.onmousedown = function() { if(lastActiveWindow.lastEditor != null) lastActiveWindow.lastEditor.offIt(imageIcon) }
	imageIcon.onmouseup = function() { if(lastActiveWindow.lastEditor != null) lastActiveWindow.lastEditor.releasedIt(imageIcon) }
	this.oEditorDiv.enableIcons.appendChild(imageIcon);

	// transform pallette xml sheet with xsl sheet
	if (window.ActiveXObject)
	{
		this.xmlPreloadDataEditorPallette = new ActiveXObject("Microsoft.XMLDOM");
		this.xslPreloadDataEditorPallette = new ActiveXObject("Microsoft.XMLDOM");
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		this.xmlPreloadDataEditorPallette = document.implementation.createDocument("", "", null);
		this.xslPreloadDataEditorPallette = document.implementation.createDocument("", "", null);
	}
	this.xmlPreloadDataEditorPallette.async = false;
	this.xmlPreloadDataEditorPallette.load('../pcrunch-api/oEditor/editor_pallette.xml');

	this.xslPreloadDataEditorPallette.async = false;
	this.xslPreloadDataEditorPallette.load('../pcrunch-api/oEditor/editor_pallette.xsl');

	if (window.ActiveXObject)
	{
		var newHTML = this.xmlPreloadDataEditorPallette.transformNode(this.xslPreloadDataEditorPallette);
		this.oEditorDiv.pallette.innerHTML = newHTML;
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		var processor = new XSLTProcessor();
		processor.importStylesheet(this.xslPreloadDataEditorPallette);
		newHTML = processor.transformToFragment(this.xmlPreloadDataEditorPallette, document);
		this.oEditorDiv.pallette.appendChild(newHTML);
	}
	// end Transform
	this.oEditorDiv.oRef = this;
}

function overrideFonts(xmlSheetPath)
{
	// transform font xml sheet with xsl sheet
	if (window.ActiveXObject)
	{
		this.xmlPreloadDataEditorFont = new ActiveXObject("Microsoft.XMLDOM");
		this.xslPreloadDataEditorFont = new ActiveXObject("Microsoft.XMLDOM");
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		this.xmlPreloadDataEditorFont = document.implementation.createDocument("", "", null);
		this.xslPreloadDataEditorFont = document.implementation.createDocument("", "", null);
	}
	this.xmlPreloadDataEditorFont.async = false;
	this.xmlPreloadDataEditorFont.load(xmlSheetPath);

	this.xslPreloadDataEditorFont.async = false;
	this.xslPreloadDataEditorFont.load('../pcrunch-api/oEditor/fonts.xsl');

	if (window.ActiveXObject)
	{
		var newHTML = this.xmlPreloadDataEditorFont.transformNode(this.xslPreloadDataEditorFont);
		this.oEditorDiv.enableFont.innerHTML = newHTML;
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		var processor = new XSLTProcessor();
		processor.importStylesheet(this.xslPreloadDataEditorIcons);
		newHTML = processor.transformToFragment(this.xmlPreloadDataEditorFont, document);
		this.oEditorDiv.enableFont.appendChild(newHTML);
	}
	// end Transform
}

function editMode() 
{
	this.oEditorDiv.textBBArea.contentWindow.document.designMode = "on";
	this.oEditorDiv.textBBArea.contentWindow.document.onkeydown = imageDialog;
	this.oEditorDiv.textBBArea.contentWindow.document.onmouseup = imageDialog;
}

// button behaviours
function overIt(obj)
{
	obj.style.borderColor = '#000000';
	obj.style.backgroundColor = '#ffdda6';
	obj.style.cursor = 'default';	
}
function offIt(obj)
{
	obj.style.borderColor = '#D6D3CE';  
	obj.style.backgroundColor = '#f0f0f0';
}
function clickedIt(obj)
{
	obj.style.backgroundColor = '#FFFFFF';
}
function releasedIt(obj)
{
    	obj.style.backgroundColor = '';
}

// Button action methods
function boldIt() 
{
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('bold', false, null);
}
function italicIt() 
{ 
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('italic', false, null); 
}
function underlineIt() 
{ 
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('underline', false, null); 
}
function leftIt()
{ 
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('justifyleft', false, null); 
}
function centreIt()
{ 
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('justifycenter', false, null); 
}
function rightIt()
{ 
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('justifyright', false, null); 
}
function numList()
{ 
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('insertorderedlist', false, null); 
}
function bulletList()
{ 
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('insertunorderedlist', false, null); 
}
function fgColour()
{
    	var fCol = prompt('Enter colour coz have to do colour thing', '');
    
    	if(fCol != null)
      	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('forecolor', false, fCol);
}



function getRangeObject(selectionObject) {
	if (selectionObject.getRangeAt)
		return selectionObject.getRangeAt(0);
	else { // Safari!
		var range = document.createRange();
		range.setStart(selectionObject.anchorNode,selectionObject.anchorOffset);
		range.setEnd(selectionObject.focusNode,selectionObject.focusOffset);
		return range;
	}
}

function linkIt()
{
	// Capture selection
	var userSelection;

	if (window.getSelection) {
		userSelection = lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.getSelection();
		top.rangeObject = getRangeObject(userSelection);
		top.currentSelection = rangeObject.commonAncestorContainer.parentNode;
		//check if it is an external link
		
		//if it is an external link - extract the url then set the tick box as ticked
		
		//if it is not an external link set tick box to blank
		document.getElementById("new_window").checked = false;
		if(top.currentSelection.tagName == "A") { document.getElementById('link_input').value = top.currentSelection.href; }
		else { document.getElementById('link_input').value = ""; }
		
		
	}
	else if (document.selection) { // should come last; Opera!
		userSelection = lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.selection.createRange();
		
		//check if it is an external link
		
		//if it is an external link - extract the url then set the tick box as ticked
		
		//if it is not an external link set tick box to blank
		document.getElementById("new_window").checked = false;
		var tempHref = "";
		top.currentSelection = userSelection;
		var tempHtmlString = userSelection.htmlText.toString();
		var tempStartString = tempHtmlString.indexOf('http://');
		var tempEndString = tempHtmlString.indexOf('">');
		if(tempStartString != -1 && tempEndString != -1){
			tempHref = tempHtmlString.substring(tempStartString,tempEndString);
			
		}
		else{
			// there wasnt a previous link
			tempHref = "Http://";
			
		}
		
		//top.LinkValue = tempHref;
		//top.LinkHtml = tempHref;
		document.getElementById('link_input').value = tempHref;

		//if(userSelection.parentElement().tagName == "A") { 
		//	document.getElementById('link_input').value = userSelection.parentElement().href; 
		//    tempHref = userSelection.parentElement().href;
		//}
		//else { document.getElementById('link_input').value = tempHref; }
		//lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.execCommand("CreateLink",false , tempHref);
		
	}

  	//create link
   	//lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.execCommand("CreateLink",false ,"");
   	//top.currentSelection = userSelection.parentElement();
   	//document.getElementById('link_input').value = top.currentSelection.href;



	// Capture selection
//if(document.all)
//{	
//	var sText = lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.selection.createRange();
//	
//    if (sText.text != "")
//    {
    //  create link
//      lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.execCommand("CreateLink",false ,"");
//      top.currentSelection = sText.parentElement();
      //top.currentSelection.href = "test2";
//      document.getElementById('link_input').value = top.currentSelection.href;
//    }
    //else {
    	//no text so remove link
    	
//    }
//}

	// Prepopulate the link box with selected text
	// Clear contents of check box
	

	// Hide pallette divs...
	Editor.oEditorDiv.pallette.style.display = 'none';
	
	// Show our hyperlink div
	Editor.oEditorDiv.hyperlink.style.marginTop = '0px';
	Editor.oEditorDiv.hyperlink.style.marginLeft = '227px';
	Editor.oEditorDiv.hyperlink.style.display = 'inline';

	
}
function addLinkToText()
{
	rawLink = document.getElementById('link_input').value;

	if(rawLink.length > 0)
	{		
		var currentText = "";
		// Check whether user has added http:// or ../ to the url
		if((rawLink.indexOf("://") == "-1") && (rawLink.indexOf("../") == "-1")){
			currentText = "http://" + rawLink;
		}
		else{
			currentText = rawLink;
		}
		// Check whether checkbox is ticked
		newWindow = document.getElementById("new_window").checked;
		if(newWindow) currentText = "javascript: top.OpenURL('"+currentText+"');";

	if(document.all){
		//alert(top.currentSelection.htmlText+"3");
		// top.currentSelection.htmlText = top.currentSelection.htmlText.replace(top.LinkValue,currentText);
		 //alert(top.currentSelection.htmlText+"4");
		 top.currentSelection.execCommand("CreateLink", false, currentText);
		
	}
	else{
		lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.execCommand("CreateLink", false, currentText);
	}	
		Editor.oEditorDiv.hyperlink.style.display = 'none';
	}	
}
function imageIt()
{
    	var imgSrc = prompt('Enter image location', '');
    
    	if(imgSrc != null)    
     	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('insertimage', false, imgSrc);
}
function ruleIt()
{
    	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('inserthorizontalrule', false, null);
}

function unDoIt()
{
    	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('undo', false, null);
}

function reDoIt()
{
    	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('redo', false, null);
}

function maximiseEditor()
{
	grabText = this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML;
	document.getElementById('maximiseWindow').innerHTML = document.getElementById('maxWrapper').innerHTML;
	document.getElementById('fontDialog').style.display = 'none';
	document.getElementById('maxWrapper').innerHTML = '';
	document.getElementById('wrapper').style.height = document.body.clientHeight;
	document.getElementById('textBBArea').style.width = '100%';
	document.getElementById('textBBArea').style.height = document.body.clientHeight - 40;
	setTimeout("document.getElementById('textBBArea').contentWindow.document.body.innerHTML = grabText",0);
	editMode();
	document.getElementById('maximiseWindow').style.display = 'inline';
	overIt(document.getElementById('maxIcon'));
}

function restoreOriginalEditor(originalSize)
{
	grabText = document.getElementById('textBBArea').contentWindow.document.body.innerHTML;
	document.getElementById('maximiseWindow').style.display = 'none';
	document.getElementById('fontDialog').style.display = 'none';
	document.getElementById('maxWrapper').innerHTML = document.getElementById('maximiseWindow').innerHTML;
	document.getElementById('maximiseWindow').innerHTML = '';
	document.getElementById('wrapper').style.height = originalSize;
	document.getElementById('textBBArea').style.height = originalSize;
	setTimeout("document.getElementById('textBBArea').contentWindow.document.body.innerHTML = grabText",0);
	editMode();
	offIt(document.getElementById('maxIcon'));
}

// Pallette Functions
function openPalette(command)
{
	// Hide hyperlink
	this.oEditorDiv.hyperlink.style.display = 'none';
	this.oEditorDiv.pallette.style.display = 'inline';
	this.paletteIsFor = command;
	lastActiveWindow.lastEditor = this.oEditorDiv.oRef;
}

function bgColour(colour)
{
        var bCol = colour;

        if(bCol != null)
	if(this.paletteIsFor == 'backcolor')
	{ 
		if(document.all)
		{
			this.oEditorDiv.textBBArea.contentWindow.document.execCommand('backcolor', false, bCol);
		}
		else 
		{
			this.oEditorDiv.textBBArea.contentWindow.document.execCommand('hilitecolor', false, bCol);
		}
	}
	else 
	{
		this.oEditorDiv.textBBArea.contentWindow.document.execCommand('forecolor', false, bCol);
	}
	this.oEditorDiv.pallette.style.display = 'none';
}

// Font Dialog stuff
function changeFont(fonte)
{
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('FontName', false, fonte);
}

// new versions for new drop box
function changeFontThis(doc, font_id)
{
	var isIE = (document.all)? true : false; 

	// IE requires this selection whereas FF doesn't
	if(isIE) selection1.select(); 
	
	var fontOb = doc.getElementById(font_id);
	currentFont = "";

	for(i=0;i<top.FontArray.length;i++)
	{
		if(top.FontArray[i][top.iData] == fontOb.value)
		{
			currentFont = top.FontArray[i][top.iKey];
			break;
		}
	}	
	
	lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.execCommand('fontname', false, currentFont);
	fontOb.value = '';
}
function changeFontSizeThis(doc, size_id)
{
	var isIE = (document.all)? true : false; 

	// IE requires this selection whereas FF doesn't
	if(isIE) selection1.select(); 
	
	var fontSizeOb = doc.getElementById(size_id);
	currentSize = "";

	for(i=0;i<top.SizeArray.length;i++)
	{
		if(top.SizeArray[i][top.iData] == fontSizeOb.value)
		{
			currentSize = top.SizeArray[i][top.iKey];
			break;
		}
	}
	
	lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.execCommand('fontsize', false, currentSize-0);
	fontSizeOb.value = '';
}
// end of new versions for new drop box

function changeFontSize(sizee)
{
	this.oEditorDiv.textBBArea.contentWindow.document.execCommand('FontSize', false, sizee);
}

function fontDialog()
{
        selection1 = this.oEditorDiv.textBBArea.contentWindow.document.selection.createRange();
}

function showFontDialog()
{
	document.getElementById('fontDialog').style.display = 'inline';
	overIt(document.getElementById('fontIcon'));
        offIt(document.getElementById('imageIcon'));

}

function hideFontDialog(originalHeight)
{
	document.getElementById('fontDialog').style.display = 'none';
	document.getElementById('wrapper').style.height = originalHeight;
	document.getElementById('textBBArea').style.height = originalHeight;
	offIt(document.getElementById('fontIcon'));
}

function imageDialog()
{
	selection1 = lastActiveWindow.lastEditor.oEditorDiv.textBBArea.contentWindow.document.selection.createRange();
	//getSelection();
	// this is to allow custom code to be run when keydown or mouse up. this function is to be specified on the paghe calling the editor.
	try
	{
		//editorCustomCode(this.parentWindow.event);
	}
	catch(e)
	{
	}
}

function disableIcons()
{
	document.getElementById('enableIcons').style.display = 'none';
	document.getElementById('disableIcons').style.display = 'inline';
}

function enableIcons()
{
	document.getElementById('disableIcons').style.display = 'none';
	document.getElementById('enableIcons').style.display = 'inline';
}

function plainText(theHTML)
{
	var returnedHTML = theHTML.replace(/(<([^>]+)>)/ig,'\n');
	return returnedHTML;
}

function convert2HTML(theHTML)
{
	theHTML = theHTML.replace(/\n/ig,'<br>');
	return theHTML;
}

function encodeHref(theHref)
{
	theHref = theHref.replace(/href=\"/ig,'href="http:\/\/@@ci');
	return theHref;
}

function toggleIt(obj)
{
	if(this.toggleMode == 1)
	{
		// IE
		if(document.all)
		{
			this.oEditorDiv.wyswigViewer.style.display = 'inline';
			this.oEditorDiv.htmlViewer.style.display = 'none';
			this.oEditorDiv.textBBArea.contentWindow.document.write('<body />');
			this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML = '';
			this.oEditorDiv.textBBArea.contentWindow.document.write(this.oEditorDiv.htmlViewer.firstChild.innerText);
		}
		else // FF
		{
			this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML = this.oEditorDiv.textBBArea.contentWindow.document.body.textContent;
		}
		this.toggleMode = 0;
		if(obj)
		{
			this.offIt(obj);
			document.getElementById('oEditorToolBar').style.visibility = 'visible';
			document.getElementById('oEditorToolBar2').style.visibility = 'visible';
		}
	}
	else
	{
		// IE
		if(document.all)
		{
			this.oEditorDiv.wyswigViewer.style.display = 'none';
			this.oEditorDiv.htmlViewer.style.display = 'inline';
			if(this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML != '' && this.havePressedTheButtonBefore)
			{
				this.oEditorDiv.htmlViewer.firstChild.innerText = this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML;
			}
			else
			{
				this.oEditorDiv.htmlViewer.firstChild.innerText = document.getElementById('contenthidden').value;
			}

		} 
		else // FF
		{
			this.oEditorDiv.textBBArea.contentWindow.document.body.textContent = this.oEditorDiv.textBBArea.contentWindow.document.body.innerHTML;
		}
		this.havePressedTheButtonBefore = true;	
		this.toggleMode = 1;
		if(obj)
		{
			this.overIt(obj);
			document.getElementById('oEditorToolBar').style.visibility = 'hidden';
			document.getElementById('oEditorToolBar2').style.visibility = 'hidden';
		}
	}
}
function hideDivs()
{
	lastActiveWindow.lastEditor.oEditorDiv.hyperlink.style.display = 'none';
	lastActiveWindow.lastEditor.oEditorDiv.pallette.style.display = 'none';
}
/* Required for extrnal URLS*/
function OpenURL(url)
{
	var junk = window.open(url);
}
top.OpenURL = OpenURL;

/*####################################################################*/
/*# PROTOTYPES #*/
/*####################################################################*/
editorObject.prototype.makeActive = makeActive;
editorObject.prototype.getValue = getValue;
editorObject.prototype.insertValue = insertValue;
editorObject.prototype.createEditor = createEditor;
editorObject.prototype.overrideFonts = overrideFonts;
editorObject.prototype.editMode = editMode;
editorObject.prototype.changeFont = changeFont;
editorObject.prototype.changeFontSize = changeFontSize;
editorObject.prototype.overIt = overIt;
editorObject.prototype.offIt = offIt;
editorObject.prototype.clickedIt = clickedIt;
editorObject.prototype.releasedIt = releasedIt;
editorObject.prototype.boldIt = boldIt;
editorObject.prototype.italicIt = italicIt;
editorObject.prototype.underlineIt = underlineIt;
editorObject.prototype.leftIt = leftIt;
editorObject.prototype.centreIt = centreIt;
editorObject.prototype.rightIt = rightIt;
editorObject.prototype.numList = numList;
editorObject.prototype.bulletList = bulletList;
editorObject.prototype.linkIt = linkIt;
editorObject.prototype.ruleIt = ruleIt;
editorObject.prototype.unDoIt = unDoIt;
editorObject.prototype.reDoIt = reDoIt;
editorObject.prototype.fontDialog = fontDialog;
editorObject.prototype.bgColour = bgColour;
editorObject.prototype.showFontDialog = showFontDialog;
editorObject.prototype.hideFontDialog = hideFontDialog;
editorObject.prototype.imageDialog = imageDialog;
editorObject.prototype.enableIcons = enableIcons;
editorObject.prototype.openPalette = openPalette;
editorObject.prototype.plainText = plainText;
editorObject.prototype.convert2HTML = convert2HTML;
editorObject.prototype.toggleIt = toggleIt;
editorObject.prototype.changeFontSizeThis = changeFontSizeThis;
editorObject.prototype.changeFontThis = changeFontThis;
editorObject.prototype.addLinkToText = addLinkToText;
editorObject.prototype.hideDivs = hideDivs;
