var sInitColor=null;
var oRange;
var oEditor;
function saveEditor(oCtrl) {
	oEditor = oCtrl;
}
function copyText(oControl) {
	var ancestorID = oControl.parentNode.id;
	var contentareaID = ancestorID.replace('uiRT', 'uiContentrich');
	var contentstoreID = ancestorID.replace('uiRT', 'uiContentstore');
	document.getElementById(contentstoreID).value = document.getElementById(contentareaID).innerHTML;
}
function RTHighlight() {
	var oRange = document.selection.createRange();
	var sHtml = oRange.htmlText;
	sHtml = "<span style='background:#ffff99;color:#000000;'>" + sHtml + "</span>";
	document.selection.createRange().pasteHTML(sHtml);
}
function RTFormat(sCommand, oControl) {
	//var ancestorID = oControl.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	var ancestorID = oControl.parentNode.parentNode.parentNode.id;
	var contentareaID = ancestorID.replace('uiRT', 'uiContentrich');
	document.getElementById(contentareaID).focus();
	document.execCommand(sCommand);
}
function changeForeColour() {
	if (sInitColor == null) { var sColor = document.dlgHelper.ChooseColorDlg(); }
	else { var sColor = document.dlgHelper.ChooseColorDlg(sInitColor); }
	sColor = sColor.toString(16);
	if (sColor.length < 6) {
		var sTempString = '000000'.substring(0,6-sColor.length);
		sColor = sTempString.concat(sColor);
	}
	document.execCommand('ForeColor', false, sColor);
	sInitColor = sColor;
}
function cleanHtml(oControl) {
	var arr_find = [
		String.fromCharCode(8220), //“
		String.fromCharCode(8221), //”
		String.fromCharCode(8216), //‘
		String.fromCharCode(8217), //‘
		String.fromCharCode(8211), //–
		String.fromCharCode(8212), //—
		String.fromCharCode(189), //½
		String.fromCharCode(188), //¼
		String.fromCharCode(190), //¾
		String.fromCharCode(169), //©
		String.fromCharCode(174), //®
		String.fromCharCode(8230) //…
	];
	var arr_replace = ['"','"',"'","'","-","--","1/2","1/4","3/4","(C)","(R)","..."];
	//var ancestorID = oControl.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	var ancestorID = oControl.parentNode.parentNode.parentNode.id;
	var contentareaID = ancestorID.replace('uiRT', 'uiContentrich');
	var sHtml = document.getElementById(contentareaID).innerHTML;
	if ( (sHtml.indexOf('class=Mso') >= 0) || (sHtml.indexOf('mso-') >=0) ) {
		// kill unwanted tags
		sHtml = sHtml.replace(/<\?xml:[^>]*>/g, ''); // Word xml
		sHtml = sHtml.replace(/<\/?st1:[^>]*>/g,''); // Word SmartTags
		sHtml = sHtml.replace(/<\/?[a-z]\:[^>]*>/g,''); // All other funny Word non-HTML stuff
		sHtml = sHtml.replace(/<\/?font[^>]*>/gi,''); // Disable if you want to		
		for ( var i=0; i<arr_find.length; i++ ) {
			var regex = new RegExp(arr_find[i], "gi");
			sHtml = sHtml.replace(regex, arr_replace[i]);
		}
	}
	/*
	sHtml = sHtml.replace(/ <\\/o:[pP]>/g, ''); // Remove all instances of 
	sHtml = sHtml.replace(/o:/g, ''); // delete all o: prefixes
	sHtml = sHtml.replace(//g, '');  // delete all smarttags
	sHtml = sHtml.replace(/<[pP]><\\/[pP]>/g, ''); // delete all empty paragraph tags
	sHtml = sHtml.replace(/<[pP]> <\\/[pP]>/g, '');  // delete all empty paragraph tags
	*/
	sHtml = sHtml.replace(/ class=[^\s|>]*/gi,''); // delete all classes
	sHtml = sHtml.replace(/<style>[^>]*<\/style>/gi, ''); // delete all style tags
	sHtml = sHtml.replace(/ style=\"[^>\"]*\"/gi,''); // delete all inline style
	sHtml = sHtml.replace(/<\/?font[^>]*>/gi, ''); // delete all font tags
	document.getElementById(contentareaID).innerHTML = sHtml;
}
function saveTextRange() {
	try	{oRange = document.selection.createRange().duplicate();} catch (e) {oRange=null;}
}
function pasteHTML(sHtml) {
	try	{oRange.pasteHTML(sHtml);return;} catch (ex) {}
	try	{oRange.item(0).outerHTML=sHtml;return;} catch (ex) {}
}
function openImageDialog() {
	var winImageList;
	winImageList = window.open('cmsadmin/cm_rteditor_listimage.aspx', '', config='height=400,width=370, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, directories=no, status=no');
	winImageList.focus();
}
function openHyperlinkDialog() {
	var winPageList;
	var sLinktext;
	if (document.selection) {
		sLinktext = document.selection.createRange().text;
	}
	winPageList = window.open('cmsadmin/cm_rteditor_listresource.aspx?urltext=' + sLinktext, '', config='height=200,width=570, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, directories=no, status=no');
	winPageList.focus();
}
function openTableDialog() {
	var winPageList;
	var sLinktext;
	if (document.selection) {
		sLinktext = document.selection.createRange().text;
	}
	winPageList = window.open('cmsadmin/cm_rteditor_inserttable.aspx?urltext=' + sLinktext, '', config='height=200,width=570, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, directories=no, status=no');
	winPageList.focus();
}
function toggleStylelist(phStyle) {
	var oPh = document.getElementById(phStyle);
	if (oPh.style.display=="none") {
		oPh.style.display = "inline";
	} else {
		oPh.style.display = "none";
	}
}
function applyStyle(sClass) {
	var sHtml="";
	try {sHtml = oRange.htmlText} catch (ex) {};
	try {sHtml = oRange.item(0).outerHTML} catch (ex) {};
	if (sHtml.length>0)	{
		sHtml = "<span class='" + sClass + "'>" + sHtml + "</span>";
	}
	pasteHTML(sHtml);
}
