var enableEditor = false;

function htmlEditor(tooltipText, tooltipData)
{
	var html = "";
	
	if(enableEditor)
	{

	html += "<div style = \"text-align: center;\"><table cellpadding = \"0\" cellspacing = \"0\" align = \"center\" class = \"editor_buttons\">";
	html += "<tr>" + //separator
		"<td><img src = \"images/editor/bold.gif\" onClick = \"htmlCommand('bold','')\"></td>" + //italic
		"<td><img src = \"images/editor/italic.gif\" onClick = \"htmlCommand('italic','')\"></td>" + //underline
		"<td><img src = \"images/editor/underline.gif\"  onClick = \"htmlCommand('underline','')\"></td>" + //strikethrough
		//"<td><img src = \"images/editor/strikethrough.gif\" onClick = \"htmlCommand('strikethrough','')\"></td>" +	//separator
		//"<td><img src = \"images/editor/subscript.gif\" onClick = \"htmlCommand('subscript','')\"></td>" + //superscript
		//"<td><img src = \"images/editor/superscript.gif\" onClick = \"htmlCommand('superscript','')\"></td>" +
		"<td><img src = \"images/editor/justifyleft.gif\" onClick = \"htmlCommand('justifyleft','')\"></td>" + //justify center
		"<td><img src = \"images/editor/justifycenter.gif\" onClick = \"htmlCommand('justifycenter','')\"></td>" + //justify right
		"<td><img src = \"images/editor/justifyright.gif\" onClick = \"htmlCommand('justifyright','')\"></td>" + //justify full
		"<td><img src = \"images/editor/justifyfull.gif\" onClick = \"htmlCommand('justifyfull','')\"></td>" +
		//"<td><img src = \"images/editor/horizontalrule.gif\" onClick = \"htmlCommand('inserthorizontalrule','')\"></td>" +
		"<td><img src = \"images/editor/orderedlist.gif\" onClick = \"htmlCommand('insertorderedlist','')\"></td>" + //unordered list
		"<td><img src = \"images/editor/unorderedlist.gif\" onClick = \"htmlCommand('insertunorderedlist','')\"></td>" +
		//"<td><img src = \"images/editor/outdent.gif\" onClick = \"htmlCommand('outdent','')\"></td>" + //indent
		//"<td><img src = \"images/editor/indent.gif\" onClick = \"htmlCommand('indent','')\"></td>" + //remove format
		//"<td><img src = \"images/editor/removeformat.gif\" onClick = \"htmlCommand('removeformat','')\"></td>" +
		//"</tr></table>";

	////////line 2

	//html += "<table cellpadding = \"0\" cellspacing = \"0\" align = \"center\" class = \"editor_buttons\"><tr>"+
		//"<td><select onChange = \"setFont()\" id = \"htmledfontface\">" +
		//	"<option value = \"\" selected>Normal</option>" +
		//	"<option value = \"Verdana\" style = \"font-family:Verdana\">Verdana</option>" +
		//	"<option value = \"Arial\" style = \"font-family:Arial\">Arial</option>" +
		//	"<option value = \"Times\" style = \"font-family:Times\">Times</option>" +
		//	"<option value = \"sans-serif\" style = \"font-family:sans-serif\">Sans serif</option>" +
		//	"<option value = \"Comic,Comic Sans Ms\" style = \"font-family:Comic,Comic Sans Ms\">Comic</option>" +
		//"</select></td>" + //fontsize
		//"<td>&nbsp;<select onChange = \"setFontSize()\" id = \"htmledfontsize\">" +
		//	"<option value = \"1\" selected>1pt</option>" +
		//	"<option value = \"2\">2pt</option>" +
		//	"<option value = \"3\">3pt</option>" +
		//	"<option value = \"4\">4pt</option>" +
		//	"<option value = \"5\">5pt</option>" +
		//	"<option value = \"6\">6pt</option>" +
		//	"<option value = \"7\">7pt</option>" +
		//"</select></td>" + //separator
		"<td><img src = \"images/editor/fontcolor.gif\" onClick = \"showHidePal('palfore')\"><iframe frameborder = \"0\" class = \"ehpal\" id = \"palfore\" scrolling = \"no\"></iframe></td>" +
		"<td><img src = \"images/editor/bgcolor.gif\" onClick = \"showHidePal('palback')\"><iframe frameborder = \"0\" class = \"ehpal\" id = \"palback\" scrolling = \"no\"></iframe></td>" +
		//separator
		"<td><img src = \"images/editor/undo.gif\" onClick = \"htmlCommand('undo','')\"></td>" + //redo
		"<td><img src = \"images/editor/redo.gif\" onClick = \"htmlCommand('redo','')\"></td>" +
		//separator
		//"<td><img src = \"images/editor/url.gif\" onClick = \"urlWindow()\"></td>" +
		//"<td><img src = \"images/editor/nourl.gif\" onClick = \"htmlCommand('unlink','')\"></td>" + //image
		//"<td><img src = \"images/editor/image.gif\" onClick = \"imgWindow()\"></td>" + //separator
		//"<td><img src = \"images/editor/table.gif\" onClick = \"tableWindow()\"></td>" + //separator
		//"<td><img src = \"images/editor/emotions.gif\" onClick = \"smileWindow()\"></td>" + //separator
		"</tr></table>";

	//frame

	html += "<iframe name = \"htmled\" id = \"htmled\" frameborder = \"0\" style = \"border: 1px solid #D9D9D9; width: 400px; height: 120px; margin-top: 3px;\"></iframe></div>";

	//dummy field
	html += "<input type = \"hidden\" id = \"editor_hidden\" name = \"htmlcontent\">";
	}
	else
	{
		//for disabled editor
		html += "<textarea name = \"htmlcontent\" id = \"htmlcontent\" style = \"border: 1px solid #D9D9D9; width: 400px; height: 120px; margin-top: 3px;\" onKeyUp = \"gE('letterCount').value = this.value.length;\" onFocus = \"gE('letterCount').value = this.value.length;\" onChange = \"gE('letterCount').value = this.value.length;\" onMouseUp = \"gE('letterCount').value = this.value.length;\"></textarea>";
		html += "<div style = \"padding: 1px 55px 1px 1px;; text-align: right;\">";
		if(tooltipText)
		{
			html += "<div style=\"padding-right: 15px; float: left; text-align: center; text-indent: 15px; width: 280px; padding-top: 5px;\"><span style=\"color: #336699; font-size: 11px; cursor: pointer;\" onmouseover=\"Tip('"+tooltipData+"')\">" + tooltipText + "</span></div>";
		}
		html += "<input type = \"text\" id = \"letterCount\" readonly style = \"width: 35px; font-size: 9px; height: 16px;\"></div>";
	}


	document.open();
	document.write(html);
	document.close();
}

function doEditorSubmit()
{
	if(enableEditor)
	{
		gE('editor_hidden').value = gE('htmled').contentWindow.document.body.innerHTML;
	}
}

function editorStartup(starttext)
{
	if(enableEditor)
	{
	//set inner html
	gE('htmled').contentWindow.document.open();
	gE('htmled').contentWindow.document.write("<html>\n\
					<head>");
	//ie/opera inline style hack
	if(browser == "ie" || browser == "opera")
	{
		gE('htmled').contentWindow.document.write("<link type = \"text/css\" rel = \"stylesheet\" href = \"/css/editor.css\">");
	}

	gE('htmled').contentWindow.document.write("</head>\n\
					<body>" + starttext + "</body>\n\
				</html>");
	gE('htmled').contentWindow.document.close();

	//ffox/gecko inline style hack

	if(browser == "gecko")
	{
		var ssh = gE('htmled').contentWindow.document.createElement('link');
		ssh.rel = 'stylesheet';
		ssh.type = 'text/css';
		ssh.href = '/css/editor.css';
		gE('htmled').contentWindow.document.getElementsByTagName('head')[0].appendChild(ssh);
	}

	//set design mode
	setTimeout("gE('htmled').contentWindow.document.designMode = 'On';gE('htmled').contentWindow.focus();",200);

	//set palettes

	//perform gecko/ie rendering engine (designMode) fix
	var bgcommand;
	switch(browser)
	{
		case 'ie':
			bgcommand = 'backcolor';
			break;
		case 'gecko':
		case 'opera':
			bgcommand = 'hilitecolor';
			break;
	}

	var pal;

	//open palette 1 forecolor
	pal = gE('palfore');
	pal.contentWindow.document.open();
	pal.contentWindow.document.write("<html><body style = \"padding: 1px; margin-top: 3px; margin-left: 3px; margin-bottom: 3px; margin-right: 3px;\">");
	pal.contentWindow.document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\" style = \"border-collapse: separate;\" width = \"*\">");
		//write palette
		var pi,pj;
		for(pi = 0; pi < colors.length; pi ++)
		{
			pal.contentWindow.document.write("<tr>");
			for(pj = 0; pj < colors[pi].length; pj ++)
			{
				pal.contentWindow.document.write("<td id=\"" + colors[pi][pj] + "\" bgcolor=\"" + colors[pi][pj] + "\" width=\"14\" height=\"14\" onMouseOver = \"this.style.border = '1px solid black'\" onMouseOut = \"this.style.border = '1px solid white'\" onMouseDown = \"parent.gE('htmled').contentWindow.document.execCommand('forecolor',false,this.id);parent.showHidePal('palfore');this.style.border = '1px solid white'\" style = \"border: 1px solid white;\"><img width=\"1\" height=\"1\">");
			}
			pal.contentWindow.document.write("</tr>");
		}
	pal.contentWindow.document.write("</table>");
	pal.contentWindow.document.write("</body></html>");
	pal.contentWindow.document.close();

	pal = gE('palback');
	pal.contentWindow.document.open();
	pal.contentWindow.document.write("<html><body style = \"padding: 1px; margin-top: 3px; margin-left: 3px; margin-bottom: 3px; margin-right: 3px;\">");
	pal.contentWindow.document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\" style = \"border-collapse: separate;\" width = \"*\">");
		//write palette
		var pi,pj;
		for(pi = 0; pi < colors.length; pi ++)
		{
			pal.contentWindow.document.write("<tr>");
			for(pj = 0; pj < colors[pi].length; pj ++)
			{
				pal.contentWindow.document.write("<td id=\"" + colors[pi][pj] + "\" bgcolor=\"" + colors[pi][pj] + "\" width=\"14\" height=\"14\" onMouseOver = \"this.style.border = '1px solid black'\" onMouseOut = \"this.style.border = '1px solid white'\" onMouseDown = \"parent.gE('htmled').contentWindow.document.execCommand('" + bgcommand + "',false,this.id);parent.showHidePal('palback');this.style.border = '1px solid white'\" style = \"border: 1px solid white;\"><img width=\"1\" height=\"1\">");
			}
			pal.contentWindow.document.write("</tr>");
		}
	pal.contentWindow.document.write("</table>");
	pal.contentWindow.document.write("</body></html>");
	pal.contentWindow.document.close();
	}
	else
	{
		gE('htmlcontent').value = starttext;
		gE('letterCount').value = starttext.length;
	}
}

function htmlCommand(command,option)
{
	var hed = gE('htmled').contentWindow;
	hed.document.execCommand(command,false,option);
	hed.focus();
}

function setFont()
{
	htmlCommand('fontname',gE('htmledfontface').value);
}

function setFontSize()
{
	htmlCommand('fontsize',gE('htmledfontsize').value);
}

function insertHtml(code)
{
	var iframe = document.getElementById('htmled').contentWindow;

	if(document.all)
	{
		var range = iframe.document.selection.createRange();
		if(range.text == "")
		{
			iframe.document.body.innerHTML +=code;
		}
		else
		{
			range.pasteHTML(html);
			range.collapse(false);
			range.select();
		}
	}
	else
	{
		iframe.document.execCommand('insertHTML', false, code);
	}
	iframe.focus();
}

function showHidePal(id)
{
	var p = gE(id);
	if(id == 'palfore')gE('palback').style.display = 'none';
	if(id == 'palback')gE('palfore').style.display = 'none';

	if(p.style.display == 'none' || p.style.display == '')
	{
		p.style.display = 'block';
	}
	else
	{
		p.style.display = 'none';
	}
}

function getSelText()
{
	if(document.selection)
	{
		return gE('htmled').contentWindow.document.selection.createRange().text;
	}
	else
	{
		return gE('htmled').contentWindow.getSelection();
	}
}

function urlWindow()
{
	window.open('system.php?todo=urldlg','_new','location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=no,width=420,height=480,top=' + (screen.height/2 - 240).toString() + ',left=' + (screen.width/2  - 210).toString());
}

function imgWindow()
{
	window.open('system.php?todo=imgdlg','_new','location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=no,width=420,height=480,top=' + (screen.height/2 - 240).toString() + ',left=' + (screen.width/2  - 210).toString());
}

function tableWindow()
{
	window.open('system.php?todo=tabledlg','_new','location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=no,width=420,height=190,top=' + (screen.height/2 - 95).toString() + ',left=' + (screen.width/2  - 210).toString());
}

function smileWindow()
{
	window.open('system.php?todo=smiles','_new','location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=no,width=250,height=100,scrollbars=yes,top=' + (screen.height/2 - 50).toString() + ',left=' + (screen.width/2  - 125).toString());
}
