var eobj = null;
var Selection = null;

function funcOnEditFocus(obj)
{
	if (obj!=null) eobj = eval(obj.id);
}

function funcGetContentBody()
{
	return eobj.document.body.innerHTML;
}

function funcSetContentBody(html)
{
	eobj.document.body.innerHTML = html;
}

function funcEditorExecFontName(ch)
{
	if (ch.options[ch.selectedIndex].value == '') 
	{
		eobj.document.execCommand("FontName", false, null);
	}else
	{
		eobj.document.execCommand("FontName", false, ch.options[ch.selectedIndex].value);
	}
}

function funcEditorExecFontSize(ch){
			
	if (ch.options[ch.selectedIndex].value == '')
	{
		eobj.document.execCommand("FontSize", false, null);
	}else
	{
		eobj.document.execCommand("FontSize", false, ch.options[ch.selectedIndex].value);
	}
}

function funcSetFontAttr(){

	if(eobj.event.type!="mouseup"&&eobj.event.type!="keyup") return;
	if(eobj.event.type=="keyup"&&(eobj.event.keyCode < 37 || eobj.event.keyCode > 40)) return;

	try
	{
		var sText = eobj.document.selection.createRange();
		if (sText.parentElement().all.eobj == null && sText.parentElement().all.papermain == null)
		{
			var lineval = sText.parentElement().outerHTML;
			lineval = lineval.toLowerCase();
			var idx = lineval.indexOf("<font");
			if (idx > -1)
			{
				try
				{
				
					if (sText.parentElement().face != null&&sText.parentElement().face != "null"&&typeof(sText.parentElement().face)!="undefined")
					{
						if (sText.parentElement().face != "")
						{
							document.all.fontface.value = sText.parentElement().face;
						}else
						{
							document.all.fontface.selectedIndex = 1;
						}
					}else
					{
						document.all.fontface.selectedIndex = 0;
					}

					if (sText.parentElement().size != null)
					{
						if (sText.parentElement().size != "")
						{
							document.all.fontsize.value = sText.parentElement().size;
						}else
						{
							document.all.fontsize.selectedIndex = 0;
						}
					}else
					{
						document.all.fontsize.selectedIndex = 0;
					}
				}catch(e){}

			}else
			{
				try
				{
					document.all.fontface.selectedIndex = 1;
					document.all.fontsize.selectedIndex = 0;
				}catch(e){}
			}
		}
	}catch(e){}
}

function funcEditorAction(order){

	var ordercommand = order.toLowerCase();

	Selection = eobj.document.selection.createRange();

	if (Selection!=null) Selection.select();
	if (Selection.text == '') return;

	switch(ordercommand)
	{
		case "bold" :
		case "italic" :
		case "underline" :
		case "justifyleft" :
		case "justifycenter" :
		case "justifyright" :
		case "strikethrough" :
			eobj.document.execCommand(ordercommand);
			break;
		case "createlink" :
			eobj.focus();
			eobj.document.execCommand(ordercommand,1);
			break;
		case "fontcolor" :
			funcEditorFontColor('1');
			break;
		case "bgcolor" :
			funcEditorFontColor('2');
			break;
	}
}

function funcEditorFontColor(ch){

	if (navigator.userAgent.indexOf("MSIE 5.0") > 0)
	{
		document.all["TableFontColor"].style.top = "65";
		document.all["TableFontBackColor"].style.top = "65";
	}

	if (ch=="1")
	{
		document.all["TableFontColor"].style.display = "";
		document.all["TableLink"].style.display = "none";
		document.all["TableFontBackColor"].style.display = "none";
	}
	if (ch=="2")
	{
		document.all["TableLink"].style.display = "none";
		document.all["TableFontColor"].style.display = "none";
		document.all["TableFontBackColor"].style.display = "";
	}
}

function funcEditorSetFontColor(color){

	if (Selection!=null) Selection.select();
	eobj.document.execCommand("forecolor", null, color);
	document.all["TableFontColor"].style.display = "none";
	eobj.document.selection.empty()
	
}

function funcEditorSetBGColor(color,color2){
    
	if (Selection!=null) Selection.select();
	eobj.document.execCommand("BackColor", null, color );
	
	if (color2 != "")
	{
		eobj.document.execCommand("forecolor", null, color2);
	}

	document.all["TableFontBackColor"].style.display = "none";
	eobj.document.selection.empty()
}

function funcEditorSetLink(mylink)
{
	if (Selection!=null) Selection.select();
	
	eobj.document.execCommand("CreateLink", null, mylink);
	document.all["TableLink"].style.display = "none";
	document.all["linkbox"].value = 'http://';
}


function funcPopMgr(popmode,boardkey){

	var link = "/MBoardLib/Common/mboardmgr" + popmode + ".asp?boardkey=" + boardkey;
	var popname = "tmboardpop" + popmode;
	var popprop = "scrollbars=no,";
	if(eobj == null)
	{
		eobj = FRAMEEDITOR;
		eobj.focus();
	}

	switch(popmode.toLowerCase())
	{
		case "file" :
			popprop = "width=400,height=300";
			break;
		case "image" :
			popprop = "width=400,height=500";
			break;
		case "media" :
			popprop = "width=400,height=510";
			break;
		case "flash" :
			popprop = "width=400,height=490";
			break;
		case "html" :
			popprop = "width=500,height=500";
			break;	
		case "report" :
			link = "/MBoardLib/Common/mboardmgr" + popmode + ".asp?contentkey=" + boardkey;
			popprop = "width=400,height=500";
			break;	
	}
	
	window.open(link,popname,popprop);
	
}
