/*
########################################################################
# File Name : simple_editor.js - SE                                    #
# Author(s) :                                                          #
#   Phil Allen phil@hilands.com                                        #
# Last Edited By :                                                     #
#   phil@hilands.com                                                   #
# Version : 2007122800                                                 #
########################################################################
*/
function se_append (strTarget, strAddition)
{
	document.getElementById(strTarget).value += strAddition;
}


// http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/


function se_append_selected(strTarget, strSAddition, strEAddition)
{
	txtarea = document.getElementById(strTarget);
	// for everything but IE
	if (window.getSelection)
	{
		// we do nothing here. as all browsers but IE work..
	}
	// IE fix? no this is not.. um it selects from anywhere in the document.. sheesh
	else if(document.selection)
	{
		// The current selection
		var range = document.selection.createRange();
		// We'll use this as a 'dummy'
		var stored_range = range.duplicate();
		// Select all text
		stored_range.moveToElementText(txtarea);
		// Now move 'dummy' end point to end point of original range
		stored_range.setEndPoint( 'EndToEnd', range );
		// Now we can calculate start and end points
		txtarea.selectionStart = stored_range.text.length - range.text.length;
		txtarea.selectionEnd = txtarea.selectionStart + range.text.length;
	}
	// determine if values are the same. if true append
	//alert(txtarea.selectionStart+':'+txtarea.selectionEnd);
	if (txtarea.selectionStart == txtarea.selectionEnd)
	{
		txtarea.value += strSAddition+strEAddition
	}
	// if false we should have a selection (should vary with IE as text selection is entire page relavent not textarea.)
	else
	{
		var txtstart = (txtarea.value).substring(0, txtarea.selectionStart);
		var txtmiddle = (txtarea.value).substring(txtarea.selectionStart,txtarea.selectionEnd);  
		var txtend = (txtarea.value).substring(txtarea.selectionEnd,txtarea.value.length);
		//alert(txtstart+strSAddition+txtmiddle+strEAddition+txtend);
		txtarea.value = txtstart+strSAddition+txtmiddle+strEAddition+txtend
	}
}






function insert_url (strTarget)
{
	var thisURL = prompt("Please Enter the URL of the link you are adding", "http://");
	var thisTitle = prompt("Now enter the title of the webpage you wish to reference.", "web page");
	var strInsert = "[url=\"" + thisURL + "\"]" + thisTitle + "[/url]";
	se_append(strTarget, strInsert);
	return;
}





//this is purely for entertainment and should not be used.

function se_preview()
{
	var txtarea = document.getElementById('editor');
	var preview = document.getElementById('preview');
	txtarea.value.replace(/[b]/, '<b>');
	var string = txtarea.value
	// shit wee need to make a loop for this.. as it only replaces the first found?
	//alert(string);


	var arrReplace=new Array(
		'[b]', '<b>',
		'[/b]', '</b>',
		'\n', '<br />',
		'[u]', '<u>',
		'[/u]', '</u>',
		'[i]', '<i>',
		'[/i]', '</i>',
		'[center]', '<div style=\"text-align:center;\">',
		'[/center]', '</div>',
		'[left]', '<div style=\"text-align:left;\">',
		'[/left]', '</div>',
		'[right]', '<div style=\"text-align:right;\">',
		'[/right]', '</div>'
	);
	for (i=0;i<arrReplace.length;i++)
	{
		string = se_replace(string, arrReplace[i], arrReplace[++i]);
	}
	preview.innerHTML = string;
}

function se_replace(strReplace, strOldVal, strNewVal)
{
	while(strReplace.indexOf(strOldVal) != -1)
	{
		strReplace = strReplace.replace(strOldVal, strNewVal);
	}
	return strReplace;
}

