/*
Cross-Browser Rich Text Editor
Copyright (C) 2003 Kevin Roth

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Contact Information: http://www.kevinroth.com/content/contact.asp

For more info, please visit: http://www.gnu.org/copyleft/gpl.html
*/

var rng;

//Function to format text in the text box
function FormatText(command, option) {
	if ((command == "forecolor") || (command == "hilitecolor")) {
		parent.command = command;
		buttonElement = document.getElementById(command);
		document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement) + "px";
		document.getElementById("colorpalette").style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + "px";
		if (document.getElementById("colorpalette").style.visibility == "hidden")
			document.getElementById("colorpalette").style.visibility="visible";
		else {
			document.getElementById("colorpalette").style.visibility="hidden";
		}
		
		//get current selected range
		var sel = document.getElementById('edit').contentWindow.document.selection; 
		if (sel!=null) {
			rng = sel.createRange();
		}
	}
	else if (command == "createlink" && browser.isIE5up == false) {
		var szURL = prompt("Enter a URL:", "");
		document.getElementById('edit').contentWindow.document.execCommand("CreateLink",false,szURL)
	}
	else {
		document.getElementById("edit").contentWindow.focus();
	  	document.getElementById("edit").contentWindow.document.execCommand(command, false, option);
		document.getElementById("edit").contentWindow.focus();
	}
}

//Function to set color
function setColor(color) {
	if (browser.isIE5up) {
		//retrieve selected range
		var sel = document.getElementById('edit').contentWindow.document.selection; 
		if (sel!=null) {
			var newRng = sel.createRange();
			newRng = rng;
			newRng.select();
		}
	}
	else {
		document.getElementById("edit").contentWindow.focus();
	}
	document.getElementById("edit").contentWindow.document.execCommand(parent.command, false, color);
	document.getElementById("edit").contentWindow.focus();
	document.getElementById("colorpalette").style.visibility="hidden";
}

//Function to add image
function AddImage() {
	imagePath = prompt('Enter Image URL:', 'http://');				
	if ((imagePath != null) && (imagePath != "")) {
		document.getElementById("edit").contentWindow.focus()
		document.getElementById("edit").contentWindow.document.execCommand('InsertImage', false, imagePath);
	}
	document.getElementById("edit").contentWindow.focus()
}

//Function to clear form
function ResetForm() {
	if (window.confirm('<%=strResetFormConfirm%>')) {
		document.getElementById("edit").contentWindow.focus()
	 	document.getElementById("edit").contentWindow.document.body.innerHTML = ''; 
	 	return true;
	 } 
	 return false;		
}

//Function to open pop up window
function openWin(theURL,winName,features) {
  	window.open(theURL,winName,features);
}

//function to perform spell check
function checkspell() {
	try {
		var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
		tmpis.CheckAllLinkedDocuments(document);
	}
	catch(exception) {
		if(exception.number==-2146827859) {
			if (confirm("ieSpell not detected.  Click Ok to go to download page."))
				window.open("http://www.iespell.com/download.php","DownLoad");
		}
		else
			alert("Error Loading ieSpell: Exception " + exception.number);
	}
}

function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	
	return mOffsetTop;
}

function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	
	while(mOffsetParent) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	
	return mOffsetLeft;
}

function Select(selectname)
{
	var cursel = document.getElementById(selectname).selectedIndex;
	// First one is always a label
	if (cursel != 0) {
		var selected = document.getElementById(selectname).options[cursel].value;
		document.getElementById('edit').contentWindow.document.execCommand(selectname, false, selected);
		document.getElementById(selectname).selectedIndex = 0;
	}
	document.getElementById("edit").contentWindow.focus();
}

function Start() {
	//write html based on browser type
	if (browser.isIE5up) {
		writeRTE();
	}
	else if (browser.isGecko) {
		//check to see if midas is enabled
		var isMidasEnabled = false;
		document.getElementById('testFrame').contentDocument.designMode = "on";
		try {
			document.getElementById('testFrame').contentWindow.document.execCommand("undo", false, null);
			isMidasEnabled = true;
		}  catch (e) {
			isMidasEnabled = false;
		}
		
		if (isMidasEnabled) {
			writeRTE();
		} else {
			writeDefault();
		}
	}
	else {
		//other browser
		writeDefault()
	}
}

function ctb() 
{
	
}

function writeRTE() {
	document.writeln('<div id="fbEditorBox">');
	document.writeln('<table id="fbEditorOptions" width="504" border="0" cellspacing="0" cellpadding="2" align="center" summary="Blog Editor">');
	document.writeln('	<tr>');
	document.writeln('		<td width="80">');
	document.writeln('			<select id="formatblock" onchange="Select(this.id);">');
	document.writeln('				<option value="<p>">Normal</option>');
	document.writeln('				<option value="<p>">Paragraph</option>');
	document.writeln('				<option value="<h1>">Heading 1 <h1></option>');
	document.writeln('				<option value="<h2>">Heading 2 <h2></option>');
	document.writeln('				<option value="<h3>">Heading 3 <h3></option>');
	document.writeln('				<option value="<h4>">Heading 4 <h4></option>');
	document.writeln('				<option value="<h5>">Heading 5 <h5></option>');
	document.writeln('				<option value="<h6>">Heading 6 <h6></option>');
	document.writeln('				<option value="<address>">Address <ADDR></option>');
	document.writeln('				<option value="<pre>">Formatted <pre></option>');
	document.writeln('			</select>');
	document.writeln('		</td>');
	document.writeln('		<td width="120">');
	document.writeln('			<select id="fontname" name="selectFont" onchange="Select(this.id)">');
	document.writeln('				<option value="Font" selected>Font</option>');
	document.writeln('				<option value="Arial, Helvetica, sans-serif">Arial</option>');
	document.writeln('				<option value="Courier New, Courier, mono">Courier New</option>');
	document.writeln('				<option value="Times New Roman, Times, serif">Times New Roman</option>');
	document.writeln('				<option value="Verdana, Arial, Helvetica, sans-serif">Verdana</option>');
	document.writeln('			</select>');
	document.writeln('		</td>');
	document.writeln('		<td width="304">');
	document.writeln('			<select unselectable="on" id="fontsize" onchange="Select(this.id);">');
	document.writeln('				<option value="Size">Size</option>');
	document.writeln('				<option value="1">1</option>');
	document.writeln('				<option value="2">2</option>');
	document.writeln('				<option value="3">3</option>');
	document.writeln('				<option value="4">4</option>');
	document.writeln('				<option value="5">5</option>');
	document.writeln('				<option value="6">6</option>');
	document.writeln('				<option value="7">7</option>');
	document.writeln('			</select>');
	document.writeln('		</td>');
	document.writeln('	</tr>');
	document.writeln('  <tr><td colspan="3" nowrap>');	
	document.writeln('  <table border="0" cellpadding="3" cellspacing="0">');
	document.writeln('	<tr>');
	//document.writeln('<!--');
	document.writeln('		<td class="ctb-off" onclick="clickCtb()"><img class="btnImage" src="/images/format_cut.gif" width="18" height="18" alt="Cut" title="Cut" onClick="FormatText(\'cut\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_copy.gif" width="18" height="18" alt="Copy" title="Copy" onClick="FormatText(\'copy\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_paste.gif" width="18" height="18" alt="Paste" title="Paste" onClick="FormatText(\'paste\')"></td>');
	//document.writeln('		<td>&nbsp;</td>');
	//document.writeln('		<td><img class="btnImage" src="/images/post_button_undo.gif" width="25" height="24" alt="Undo" title="Undo" onClick="FormatText(\'undo\')"></td>');
	//document.writeln('		<td><img class="btnImage" src="/images/post_button_redo.gif" width="25" height="24" alt="Redo" title="Redo" onClick="FormatText(\'redo\')"></td>');
	//document.writeln('//-->');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_bold.gif" width="18" height="18" alt="Bold" title="Bold" onClick="FormatText(\'bold\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_italic.gif" width="18" height="18" alt="Italic" title="Italic" onClick="FormatText(\'italic\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_underline.gif" width="18" height="18" alt="Underline" title="Underline" onClick="FormatText(\'underline\', \'\')"></td>');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_align_left.gif" width="18" height="18" alt="Align Left" title="Align Left" onClick="FormatText(\'justifyleft\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_align_center.gif" width="18" height="18" alt="Center" title="Center" onClick="FormatText(\'justifycenter\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_align_right.gif" width="18" height="18" alt="Align Right" title="Align Right" onClick="FormatText(\'justifyright\', \'\')"></td>');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_list_num.gif" width="18" height="18" alt="Ordered List" title="Ordered List" onClick="FormatText(\'insertorderedlist\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_list_bullet.gif" width="18" height="18" alt="Unordered List" title="Unordered List" onClick="FormatText(\'insertunorderedlist\', \'\')"></td>');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_indent_more.gif" width="18" height="18" alt="Outdent" title="Outdent" onClick="FormatText(\'outdent\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_indent_less.gif" width="18" height="18" alt="Indent" title="Indent" onClick="FormatText(\'indent\', \'\')"></td>');
	document.writeln('		<td><div id="forecolor"><img class="btnImage" src="/images/format_color_fg.gif" width="18" height="18" alt="Text Color" title="Text Color" onClick="FormatText(\'forecolor\', \'\')"></div></td>');
	document.writeln('<!--');
	document.writeln('		<td><div id="hilitecolor"><img class="btnImage" src="/images/post_button_bgcolor.gif" width="25" height="24" alt="Background Color" title="Background Color" onClick="FormatText(\'hilitecolor\', \'\')"></div></td>');
	document.writeln('//-->');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_link.gif" width="18" height="18" alt="Insert Link" title="Insert Link" onClick="FormatText(\'createlink\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/format_image.gif" width="18" height="18" alt="Add Image" title="Add Image" onClick="AddImage()"></td>');
	//document.writeln('		<td><a href="javascript:openWin(\'smileys.asp\',\'smilies\',\'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1,width=400,height=400\')"><img src="/images/post_button_smiley.gif" width="25" height="24" align="absmiddle" alt="Insert Smiley" border="0"></a></td>');	
	if (browser.isIE5up) document.writeln('		<td><img class="btnImage" src="/images/format_spellcheck.gif" width="18" height="18" alt="Add Image" title="Add Image" onClick="checkspell()"></td>');
	document.writeln('	</tr>');
	document.writeln('  </table>');
	document.writeln('  </td></tr>');
	document.writeln('</table>');
	document.writeln('<iframe id="edit" src="' + iframeSRC + '" style="height:260px" width="100%" height="260"></iframe>');
	document.writeln('</div>');
	document.writeln('<iframe width="200" height="150" id="colorpalette" border="0" frameborder="0" src="palette.htm" style="visibility:hidden; position: absolute; left: 0px; top: 0px;"></iframe>');
	
	setTimeout("enableDesignMode()", 10);
}

function writeDefault() {
	document.writeln('<textarea name="edit" id="edit" style="height: 200px;"></textarea>');
}

function enableDesignMode() {
	if (browser.isIE5up) {
		frames.edit.document.designMode = "On";
		//frames.edit.document.onmouseup = document.selection.clear;
	}
	else {
		document.getElementById('edit').contentDocument.designMode = "on"
	}
}

