var selectedTD
var selectedTR
var selectedTBODY
var selectedTable
var selectedImage

function mouseOver(eButton)
	{
	eButton.className = "ButtonMouseOver";	}
function mouseOut(eButton)
	{
	eButton.className = "ButtonNormal";	}
function mouseDown(eButton)
	{
	if (event.button == 1){
	eButton.className = "ButtonPressed";}	}
function mouseUp(eButton)
	{
	if (event.button == 1){
	eButton.className = "ButtonNormal";	eButton = null;} 
	}
function cmdExec(cmd,opt,pathpopup) 
	{
  	switch (cmd)
  	{
  	case "InsertImage":
	  	HTMLEditor.focus();
		var chasm = screen.availWidth;
		var mount = screen.availHeight;
		var w = 400;
		var h = 400;
		if (isImageSelected())
		{
			window.open (pathpopup + 'FileManager.aspx?ToDo=ModifyImage','','width=' + w + ',height=' + h + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5));
		}
		else
		{
			window.open (pathpopup + 'FileManager.aspx?ToDo=InsertImage','','width=' + w + ',height=' + h + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5));
		}		
  		break;
  	case "InsertFile":
	  	HTMLEditor.focus();
		var chasm = screen.availWidth;
		var mount = screen.availHeight;
		var w = 400;
		var h = 200;
		if (isImageSelected())
		{
			window.open (pathpopup + 'RealFileManager.aspx','','width=' + w + ',height=' + h + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5));
		}
		else
		{
			window.open (pathpopup + 'RealFileManager.aspx','','width=' + w + ',height=' + h + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5));
		}		
  		break;    		
  	case "InsertTable":
	  	HTMLEditor.focus();
		var chasm = screen.availWidth;
		var mount = screen.availHeight;
		var w = 400;
		var h = 400;
		if (isTableSelected())
		{
			window.open (pathpopup + 'TableEditor.aspx?ToDo=ModifyTable','','width=' + w + ',height=' + h + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5));
		}
		else
		{
			window.open (pathpopup + 'TableEditor.aspx?ToDo=InsertTable','','width=' + w + ',height=' + h + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5));
		}
  		break;
  	default:
  	if (opt == null) {
   HTMLEditor.document.execCommand(cmd,true);  	
  	} else { 
  	HTMLEditor.document.execCommand(cmd,false,opt); } 
  	HTMLEditor.focus()
  		break;
  	}
  
	}
function setMode(bHTMLMode)
{
	var sHTMLContent;
  	isHTMLMode = bHTMLMode;
  	if (isHTMLMode){sHTMLContent=HTMLEditor.document.body.innerHTML;HTMLEditor.document.body.innerText=sHTMLContent;} 
	else {sHTMLContent=HTMLEditor.document.body.innerText;HTMLEditor.document.body.innerHTML=sHTMLContent;};
}
function InsertNewImage(imageURL,align,src,w,h){
	var curPos = HTMLEditor.document.selection.createRange();
	curPos.type = HTMLEditor.document.selection.type;
	if (curPos.type == 'Control') {
	if (src != null) 
	curPos.item(0).outerHTML = "<table align=" + align + "><tr><td><IMG " + imageURL + " onmouseover=\"this.style.cursor='hand';\" onclick=showDialog('/DisplayImage.aspx?file=" + src + "'," + w + "," + h + ")></td></tr></table>"; 
	else curPos.item(0).outerHTML = "<table align=" + align + "><tr><td><IMG " + imageURL + "></td></tr></table>";
	} else {
	if (src != null) 
	curPos.pasteHTML("<table align=" + align + "><tr><td><IMG " + imageURL + " onmouseover=\"this.style.cursor='hand';\" onclick=showDialog('/DisplayImage.aspx?file=" + src + "'," + w + "," + h + ")></td></tr></table>&nbsp;");
	else curPos.pasteHTML("<table align=" + align + "><tr><td><IMG " + imageURL + "></td></tr></table>&nbsp;");
	}
	HTMLEditor.focus();
	HTMLEditor.document.selection.empty();

}
function InsertNewTable(value){
	var curPos = HTMLEditor.document.selection.createRange();
	curPos.type = HTMLEditor.document.selection.type;
	if (curPos.type == 'Control') {
	curPos.item(0).outerHTML = value;
	} else {
	curPos.pasteHTML(value);
	}
	HTMLEditor.focus();
	HTMLEditor.document.selection.empty();

}
function foreColor(urlpopup){
var arr = showModalDialog("/BackEnd/htmleditor/ColorPicker.aspx","","font-family:arial; font-size:10; dialogWidth:40em; dialogHeight:50em" );
if (arr != null) cmdExec("ForeColor",arr);}

function backColor(){
var arr = showModalDialog("/BackEnd/htmleditor/ColorPicker.aspx","","font-family:arial; font-size:10; dialogWidth:40em; dialogHeight:50em" );
if (arr != null) cmdExec("BackColor",arr);}

function CleanupCode()
{
	if (confirm("Are you sure you want to clean HTML code?")){ 
		HTMLEditor.focus();     
		HTMLEditor.document.body.innerHTML = doCleanCode(HTMLEditor.document.body.innerHTML);
	}
}

function doCleanCode(code) {    
		// removes all empty <p> tags
		code = code.replace(/<p([^>])*>(&nbsp;)*\s*<\/p>/gi,"")
		// removes all empty span tags
		code = code.replace(/<span([^>])*>(&nbsp;)*\s*<\/span>/gi,"")
		// removes all Class attributes on a tag eg. '<p class=asdasd>xxx</p>' returns '<p>xxx</p>'
		code = code.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi, "<$1$3")
		// removes all style attributes eg. '<tag style="asd asdfa aasdfasdf" something else>' returns '<tag something else>'
		code = code.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi, "<$1$3")   
		// gets rid of all xml stuff... <xml>,<\xml>,<?xml> or <\?xml>
		code = code.replace(/<\\?\??xml[^>]>/gi, "")
		// get rid of ugly colon tags <a:b> or </a:b>
		code = code.replace(/<\/?\w+:[^>]*>/gi, "")
		// get rid of silly space tags
		code = code.replace(/&nbsp;/gi, "")
		return code
	}

function isImageSelected() {
	if (HTMLEditor.document.selection.type == "Control") {
		var oControlRange = HTMLEditor.document.selection.createRange();
		if (oControlRange(0).tagName.toUpperCase() == "IMG") {
			selectedImage = HTMLEditor.document.selection.createRange()(0);
			return true;
		}	
	}
}

function isControlSelected() {
	if (HTMLEditor.document.selection.type == "Control") {
		var oControlRange = HTMLEditor.document.selection.createRange();
		if (oControlRange(0).tagName.toUpperCase() != "IMG") {
			selectedImage = HTMLEditor.document.selection.createRange()(0);
			return true;
		}
	}
}

function isTableSelected() {
	if (HTMLEditor.document.selection.type == "Control") {
		var oControlRange = HTMLEditor.document.selection.createRange();
		if (oControlRange(0).tagName.toUpperCase() == "TABLE") {
			selectedTable = HTMLEditor.document.selection.createRange()(0);
			return true;
		}	
	}
}

function isCursorInTableCell() {
	if (document.selection.type != "Control") {
					  var elem = document.selection.createRange().parentElement()
					  while (elem.tagName.toUpperCase() != "TD" && elem.tagName.toUpperCase() != "TH")
					  {
						elem = elem.parentElement
						if (elem == null)
						  break
					  }
			if (elem) {
				selectedTD = elem
				selectedTR = selectedTD.parentElement
				selectedTBODY =  selectedTR.parentElement
				selectedTable = selectedTBODY.parentElement
				return true
			}
	}
} // End function
