/*
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.\nIt needs to be installed for spellchecking to work.\nClick Ok to go to ieSpell 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(width,height,includeSave,includeCancel) {
	//write html based on browser type
	if (browser.isIE5up) {
		writeRTE(width,height,includeSave,includeCancel);
	}
	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(width,height,includeSave,includeCancel);
		} else {
			writeDefault();
		}
	}
	else {
		//other browser
		writeDefault()
	}
}

function writeRTE(width,height,includeSave,includeCancel) {
	
	document.writeln('<table border=0 cellspacing=0 cellpadding=0>');
	document.writeln('<input type="hidden" name="xcontent">');
	document.writeln('	<tr>');
	
	// next two lines added to allow save/cancel from toolbar 
	if(includeSave == 'true')  document.writeln('<td><input type=submit id=action name=action value=Save onclick="document.all.xhtml.value=getxhtml(frames.edit.document.body);"></td>');
	if(includeCancel == 'true')document.writeln('<td><input type=submit id=action name=action value=Cancel></td>');
	document.writeln('		<td>');
	document.writeln('			<td><select id="formatblock" onchange="Select(this.id);">');
	document.writeln('				<option value="">Font Type</option>');
	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('			</select>');
	document.writeln('		</td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_bold.gif" width="25" height="24" alt="Bold" title="Bold" onClick="FormatText(\'bold\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_italic.gif" width="25" height="24" alt="Italic" title="Italic" onClick="FormatText(\'italic\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_underline.gif" width="25" height="24" alt="Underline" title="Underline" onClick="FormatText(\'underline\', \'\')"></td>');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_left_just.gif" width="25" height="24" alt="Align Left" title="Align Left" onClick="FormatText(\'justifyleft\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_centre.gif" width="25" height="24" alt="Center" title="Center" onClick="FormatText(\'justifycenter\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_right_just.gif" width="25" height="24" alt="Align Right" title="Align Right" onClick="FormatText(\'justifyright\', \'\')"></td>');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_numbered_list.gif" width="25" height="24" alt="Ordered List" title="Ordered List" onClick="FormatText(\'insertorderedlist\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_list.gif" width="25" height="24" alt="Unordered List" title="Unordered List" onClick="FormatText(\'insertunorderedlist\', \'\')"></td>');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_outdent.gif" width="25" height="24" alt="Outdent" title="Outdent" onClick="FormatText(\'outdent\', \'\')"></td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_indent.gif" width="25" height="24" alt="Indent" title="Indent" onClick="FormatText(\'indent\', \'\')"></td>');
	document.writeln('		<td>&nbsp;</td>');
	document.writeln('		<td><img class="btnImage" src="/images/post_button_hyperlink.gif" width="25" height="24" alt="Insert Link" title="Insert Link" onClick="FormatText(\'createlink\')"></td>');
	if (browser.isIE5up) document.writeln('		<td><img class="btnImage" src="/images/post_button_spellcheck.gif" width="25" height="24" alt="Run Spellcheck" title="Run Spellcheck" onClick="checkspell()"></td>');
	document.writeln('	</tr>');
	document.writeln('	<tr><td colspan=50>');
	document.writeln('<iframe src="/frame.asp" id="edit" width="'+width+'px" height="'+height+'px"></iframe>');
	document.writeln('	</td></tr></table>');
	
	setTimeout("enableDesignMode()", 10);
	
}

function writeDefault() {
	document.writeln('<textarea name="edit" id="edit" style="width:500px; 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"
	}
}
