var cmd = "";
var rng;
function start()
{
  try
  {
  	document.getElementById('edit').contentWindow.document.designMode = "on";
  	try 
	{
    	document.getElementById('edit').contentWindow.document.execCommand("undo", false, null);
  	}  
	catch (e) 
	{
    alert("This demo is not supported on your level of Mozilla.");
  	}
  }
  catch (e)
  {
  }
}
function ToolbarButton_Click(command,option)
{
   document.getElementById('edit').contentWindow.focus();
   document.getElementById('edit').contentWindow.document.execCommand(command, false,option);
}
function ApplyFont()
{
   var fontName = "";

   if(document.getElementById('selectFont').selectedIndex == 1)
      fontName = "Arial";
   else if(document.getElementById('selectFont').selectedIndex == 2)
      fontName = "Arial Black";
   else if(document.getElementById('selectFont').selectedIndex == 3)
      fontName = "Courier New";
   else if(document.getElementById('selectFont').selectedIndex == 4)
      fontName = "Garamond";
   else if(document.getElementById('selectFont').selectedIndex == 5)
      fontName = "Tahoma";
   else if(document.getElementById('selectFont').selectedIndex == 6)
      fontName = "Times New Roman";
   else if(document.getElementById('selectFont').selectedIndex == 7)
      fontName = "Verdana";
   else if(document.getElementById('selectFont').selectedIndex == 8)
      fontName = "Webdings";
   else if(document.getElementById('selectFont').selectedIndex == 9)
      fontName = "Wingdings";

   document.getElementById('edit').contentWindow.focus();
   document.getElementById('edit').contentWindow.document.execCommand("fontname", false, fontName);
   document.getElementById('selectFont').selectedIndex = 0;
}
function ApplySize()
{
   document.getElementById('edit').contentWindow.document.focus();
   document.getElementById('edit').contentWindow.document.execCommand("fontsize", false, document.getElementById('selectSize').selectedIndex);
   document.getElementById('selectSize').selectedIndex = 0;
}

//InsertImage 	e.g. execCommand('InsertImage', false, 'http://www.mydomain.com/myimage.jpg';
//InsertHyperlink 	e.g. execCommand('InsertHyperLink', false, 'http://www.mydomain.com';
//ForeColor 	e.g. execCommand('ForeColor', false, '#666666';
function showData()
{
	alert(document.getElementById('edit').contentWindow.document.body.innerHTML);
}
function setRange(rte) {
	//function to store range of current selection
	var oRTE;
	if (document.all) {
		oRTE = frames[rte];
		var selection = oRTE.document.selection; 
		if (selection != null) rng = selection.createRange();
	} else {
		oRTE = document.getElementById(rte).contentWindow;
		var selection = oRTE.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
}
function showHideElement(element, showHide) {
	//function to show or hide elements
	//element variable can be string or object
	if (document.getElementById(element)) {
		element = document.getElementById(element);
	}
	
	if (showHide == "show") {
		element.style.visibility = "visible";
	} else if (showHide == "hide") {
		element.style.visibility = "hidden";
	}
}

function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	var parents_up = 2;
	
	while(parents_up > 0) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}
	
	return mOffsetLeft;
}

function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	
	while(mOffsetParent != null) {
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	
	return mOffsetTop;
}



function dlgColorPalette(rte,command) {
	setRange('edit');
	var oDialog = document.getElementById('cp');
	var buttonElement = document.getElementById(command);
	var iLeftPos = getOffsetLeft(buttonElement);
	var iTopPos = getOffsetTop(buttonElement) + (buttonElement.offsetHeight + 4);
	oDialog.style.left = (iLeftPos) + "px";
	oDialog.style.top = (iTopPos) + "px";
	
	if (command == parent.command){
		if (oDialog.style.visibility == "hidden") {
			showHideElement(oDialog, 'show');
		} else {
			showHideElement(oDialog, 'hide');
		}
	} else {
		showHideElement('cp', 'hide');
		showHideElement(oDialog, 'show');
	}
	
	cmd = command;
	
}
function setRange(rte) {
	//function to store range of current selection
	var oRTE;
	if (document.all) {
		oRTE = frames[rte];
		var selection = oRTE.document.selection; 
		if (selection != null) rng = selection.createRange();
	} else {
		oRTE = document.getElementById(rte).contentWindow;
		var selection = oRTE.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
}

function setColor(color) {
	showHideElement('cp', 'hide');
	var oRTE;
	if (document.all) {
		//retrieve selected range
		var sel = frames['edit'].document.selection; 
		if (sel != null) {
			var newRng = sel.createRange();
			newRng = rng;
			newRng.select();
		}
	}
	

	ToolbarButton_Click(cmd, color);
	
}

function insertLink(rte) {
	//function to insert link
	var szURL = prompt("Enter a URL:", "");
	try {
		//ignore error for blank urls
//		ToolbarButton_Click("Unlink", null);
		ToolbarButton_Click("createlink", szURL);
		alert('test');
	} catch (e) {
		alert(e.message);
	}
}

function updateRT()
{
	document.getElementById('hdRT').value = document.getElementById('edit').contentWindow.document.body.innerHTML;
}
function initRT()
{
	document.writeln('<div style="width:700px">');
	document.writeln('<SELECT id="selectFont" name="selectFont" onChange="ApplyFont();">');
	document.writeln('<OPTION>(Font)</OPTION>');
	document.writeln('<OPTION>Arial</OPTION>');
	document.writeln('<OPTION>Arial Black</OPTION>');
	document.writeln('<OPTION>Courier New</OPTION>');
	document.writeln('<OPTION>Garamond</OPTION>');
	document.writeln('<OPTION>Tahoma</OPTION>');
	document.writeln('<OPTION>Times New Roman</OPTION>');
	document.writeln('<OPTION>Verdana</OPTION>');
	document.writeln('<OPTION>Webdings</OPTION>');
	document.writeln('<OPTION>Wingdings</OPTION>');
	document.writeln('</SELECT>');
	document.writeln('<SELECT id="selectSize" name="selectSize" onChange="ApplySize();">');
	document.writeln('<OPTION>(size)</OPTION>');
	document.writeln('<OPTION>1</OPTION>');
	document.writeln('<OPTION>2</OPTION>');
	document.writeln('<OPTION>3</OPTION>');
	document.writeln('<OPTION>4</OPTION>');
	document.writeln('<OPTION>5</OPTION>');
	document.writeln('<OPTION>6</OPTION>');
	document.writeln('<OPTION>7</OPTION>');
	document.writeln('</SELECT>');
	document.writeln('<table width="700px" cellpadding="0" cellspacing="0" id="Buttons2_" style="border:1px solid #A8C6E0">');
	document.writeln('	<tr>');
	document.writeln('		<td><img id="bold" src="js/images/bold.gif" width="25" height="24" alt="Bold" title="Bold" onClick="ToolbarButton_Click(\'bold\')"></td>');
	document.writeln('		<td><img src="js/images/italic.gif" width="25" height="24" alt="Italic" title="Italic" onClick="ToolbarButton_Click(\'italic\')"></td>');
	document.writeln('		<td><img src="js/images/underline.gif" width="25" height="24" alt="Underline" title="Underline" onClick="ToolbarButton_Click(\'underline\')"></td>');
	document.writeln('		<td><img src="js/images/blackdot.gif" width="1" height="20" border="0" alt=""></td>');
	document.writeln('		<td><img src="js/images/left_just.gif" width="25" height="24" alt="Align Left" title="Align Left" onClick="ToolbarButton_Click(\'justifyleft\')"></td>');
	document.writeln('		<td><img src="js/images/centre.gif" width="25" height="24" alt="Center" title="Center" onClick="ToolbarButton_Click(\'justifycenter\')"></td>');
	document.writeln('		<td><img src="js/images/right_just.gif" width="25" height="24" alt="Align Right" title="Align Right" onClick="ToolbarButton_Click(\'justifyright\')"></td>');
	document.writeln('		<td><img src="js/images/justifyfull.gif" width="25" height="24" alt="Justify Full" title="Justify Full" onclick="ToolbarButton_Click(\'justifyfull\')"></td>');
	document.writeln('		<td><img src="js/images/blackdot.gif" width="1" height="20" border="0" alt=""></td>');
	document.writeln('		<td><img src="js/images/hr.gif" width="25" height="24" alt="Horizontal Rule" title="Horizontal Rule" onClick="ToolbarButton_Click(\'inserthorizontalrule\')"></td>');
	document.writeln('		<td><img src="js/images/blackdot.gif" width="1" height="20" border="0" alt=""></td>');
	document.writeln('		<td><img src="js/images/numbered_list.gif" width="25" height="24" alt="Ordered List" title="Ordered List" onClick="ToolbarButton_Click(\'insertorderedlist\')"></td>');
	document.writeln('		<td><img src="js/images/list.gif" width="25" height="24" alt="Unordered List" title="Unordered List" onClick="ToolbarButton_Click(\'insertunorderedlist\')"></td>');
	document.writeln('		<td><img src="js/images/blackdot.gif" width="1" height="20" border="0" alt=""></td>');
	document.writeln('		<td><img src="js/images/outdent.gif" width="25" height="24" alt="Outdent" title="Outdent" onClick="ToolbarButton_Click(\'outdent\')"></td>');
	document.writeln('		<td><img src="js/images/indent.gif" width="25" height="24" alt="Indent" title="Indent" onClick="ToolbarButton_Click(\'indent\')"></td>');
	document.writeln('		<td><div id="forecolor"><img src="js/images/textcolor.gif" width="25" height="24" alt="Text Color" title="Text Color" onClick="dlgColorPalette(\'edit\', \'forecolor\', \'\')"></div></td>');
	document.writeln('		<td><div id="backcolor"><img src="js/images/bgcolor.gif" width="25" height="24" alt="Background Color" title="Background Color" onClick="dlgColorPalette(\'edit\', \'backcolor\', \'\')"></div></td>');
	document.writeln('		<td><img src="js/images/blackdot.gif" width="1" height="20" border="0" alt=""></td>');
	document.writeln('		<td width=100%>&nbsp;</td>');
	document.writeln('	</tr>');
	document.writeln('</table>');
	document.writeln('<iframe id="edit" width="698px" height="200px" frameborder="0" style="border:1px solid #A8C6E0"></iframe>');
	document.writeln('<iframe width="154" height="104" id="cp" src="js/palette.htm" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"></iframe><input type="hidden" name="hdRT" id="hdRT"></div>');
}
