// html editor
//
// Copyright (c)2003 3Probe Technologies Corp.
// http://www.3proe.com.tw

var edtImageSelPage;
var edtTextObj;
var edtSrcEditing=false;


function edtCreateEditor(height)
{


	// toolbar
	document.write('<div id=divEditor><table width=100% style="font-size:12px" cellpadding=0 cellspacing=0>');
	document.write('<tr><td>');
	document.write('<input type="button" onclick="edtSetBold()" value="粗體">');
	document.write('<input type="button" onclick="edtSetItalic()" value="斜體">');
	document.write('<input type="button" onclick="edtSetUnderline()" value="底線">');

	document.write(' 大小:');
	document.write('<SELECT NAME="oTextColorSelect" onchange=edtSetFontSize(this.options(this.selectedIndex).value)>');
	document.write('<OPTION value="1">1</OPTION>');
	document.write('<OPTION value="2" selected>2</OPTION>');
	document.write('<OPTION value="3">3</OPTION>');
	document.write('<OPTION value="4">4</OPTION>');
	document.write('<OPTION value="5">5</OPTION>');
	document.write('<OPTION value="6">6</OPTION>');
	document.write('<OPTION value="7">7</OPTION>');
	document.write('</SELECT>');
	
	document.write(' 字色:');
	document.write('<SELECT NAME="oTextColorSelect" onchange=edtSetColor(this.options(this.selectedIndex).value)>');
	document.write('<OPTION style="background-color:black" value="black" selected>&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:white" value="white">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:red" value="red">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:blue" value="blue">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:green" value="green">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:yellow" value="yellow">&nbsp;&nbsp;</OPTION>');
	document.write('</SELECT>');
	document.write(' 底色:');
	document.write('<SELECT ID="oTextBkColorSelect" NAME="Select1" onchange=edtSetBkColor(this.options(this.selectedIndex).value)>');
	document.write('<OPTION style="background-color:black" value="black">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:white" value="white" selected>&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:red" value="red">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:blue" value="blue">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:green" value="green">&nbsp;&nbsp;</OPTION>');
	document.write('<OPTION style="background-color:yellow" value="yellow">&nbsp;&nbsp;</OPTION>');
	document.write('</SELECT>');

	document.write(' 對齊:');
	document.write('<input type="button" onclick="edtSetAlign(0)" value="左">');
	document.write('<input type="button" onclick="edtSetAlign(1)" value="中">');
	document.write('<input type="button" onclick="edtSetAlign(2)" value="右">');
	document.write(' 項目:');
	document.write('<input type="button" onclick="edtSetList(0)" value="123">');
	document.write('<input type="button" onclick="edtSetList(1)" value=". . .">');
	

	document.write('&nbsp;');
	document.write('<input type="button" onclick="edtOpenImageSelPage()" value="插入圖片">');

	document.write('</td></tr>');
	// editor frame
	document.write('<tr><td><iframe id="oEditor" width="100%" height="'+ height +'"></iframe></td></tr>');
	document.write('</table></div>');
	
	document.write('編輯模式: <input type=radio name=swsrc onclick="edtShowSource(false)" checked>Rich-Text');
	document.write('&nbsp;<input type=radio name=swsrc onclick="edtShowSource(true)">HTML Source');

	
	edtInitEditor();
}

function edtInitEditor()
{
	var html='<body style="font:9pt 新細明體"></body>';
	oEditor.document.open();
	oEditor.document.write(html);
	oEditor.document.close();
	oEditor.document.designMode = "on";
}

function edtSetBold()
{
	oEditor.document.execCommand("bold")
	oEditor.focus();
}

function edtSetItalic()
{
	oEditor.document.execCommand("italic")
	oEditor.focus();
}

function edtSetUnderline()
{
	oEditor.document.execCommand("underline")
	oEditor.focus();
}

function edtSetColor( clr)
{
	oEditor.document.execCommand("forecolor","",clr)
	oEditor.focus();
}

function edtSetBkColor(clr)
{
	oEditor.document.execCommand("backcolor","",clr)
	oEditor.focus();
}

function edtSetFontSize(sz)
{
	oEditor.document.execCommand("fontsize","",sz)
	oEditor.focus();
}

function edtSetAlign(x)
{
	var param;
	if(x==0)
		param='justifyleft';
	else if(x==1)
		param='justifycenter';
	else if(x==2)
		param='justifyright';
	oEditor.document.execCommand(param);
	oEditor.focus();
}

function edtSetList(x)
{
	var param;
	if(x==0)
		param='insertOrderedList';
	else if(x==1)
		param='insertUnorderedList';
	oEditor.document.execCommand(param);
	oEditor.focus();
}

function edtInsertImage(selImageUrl)
{
	oEditor.document.execCommand( "InsertImage", false,selImageUrl);
	oEditor.focus();
}

function edtOpenImageSelPage()
{
	window.open(edtImageSelPage)
}


function edtLoadContent()
{
	oEditor.document.body.innerHTML = edtTextObj.innerText;
}

function edtSaveContent()
{
	if( !edtSrcEditing )
		edtTextObj.innerText = oEditor.document.body.innerHTML;
}

function edtShowSource(bshowsrc)
{
	
	if( bshowsrc )
	{
		edtSaveContent();
		edtSrcEditing=true;
		divEditor.style.display="none";
		edtTextObj.style.display="block";
	}
	else
	{
		edtLoadContent();
		edtSrcEditing = false;
		divEditor.style.display="block";
		edtTextObj.style.display="none";
	}
}

