// js toggle help notes
// needs to be more unobtrusive, but for now it works.
<!--
function th($id_is){

	var $id_is = document.getElementById($id_is);
	
	if($id_is.style.display == 'none'){
		$id_is.style.display = '';
	}
	else{
		$id_is.style.display = 'none';
	}

}

/************************************************************
Title: addcode.js
Author: Jamie Allison
Date: Aug 22 2004
Description:
	Gives the ability to add bbcode to a
	given text area.

	This code should work in Mozilla (1.3 and greater) and
	Internet Explorer (4.0 and greater)
*************************************************************/

/************************************************************
Function: Selection(idTextArea)
Type: Javascript Object
Description:
	The Selection object is used to create a browser 
	independant selection object. It's purpose is to wrap up
	and hide the different per browser methods of retrieving
	the the current selection of a textarea, giving the end
	user the ability to work with textarea selections in a 
	consistant cross browser manner.

Usage:
	<<<insert usage here>>>
*************************************************************/
function Selection(idTextArea)
{
	this.idTextArea = idTextArea
	this.selectionLength = 0;
	this.selectionStart = 0;
	this.selectionEnd = 0;
	this.getSelectionText = getSelText;
	this.setSelectionText = setSelText;
	this.txtObj = document.getElementById(idTextArea);
	
	
	if(document.selection) // if this is true then we are working with Internet Explorer
	{
		
		this.txtObj.focus();
		var range = document.selection.createRange().duplicate();
		this.selectionLength = range.text.length;
		var i = this.txtObj.value.length + 1;
		
		range.collapse(true);
		while(range.parentElement() == this.txtObj && range.move("character",1)==1 )
		{
			i--;
		}
		
		if(i)
		{
			this.selectionStart = i;
			this.selectionEnd = this.selectionStart + this.selectionLength;
		}
		

		
				
	}
	else // Otherwise assume we are working with Mozilla / Netscape
	{
		this.selectionLength = this.txtObj.selectionEnd - this.txtObj.selectionStart;
		this.selectionStart = this.txtObj.selectionStart;
		this.selectionEnd = this.txtObj.selectionEnd;
	}
}

/************************************************************
Function: setSelText()
Type: Member Function of the Selection object
Description:
	This is a member function used by the Selection object. this function is what gets called
	when a user calls the Selection.setSelectionText() method. 

	This method will set the hilighted (selected) text of a textarea
*************************************************************/

function setSelText(strText)
{
	if(document.selection) 
	{
		this.txtObj.focus();
		var range = document.selection.createRange();
		range.text = strText;
		range.move("character", range.text.length);
		this.txtObj.focus();

	}
	else
	{
		var arrtext = new Array();

		arrtext[arrtext.length] = this.txtObj.value.substr(0, txt.selectionStart);
		arrtext[arrtext.length] = strText;
		arrtext[arrtext.length] = this.txtObj.value.substr(this.txtObj.selectionEnd);

		var rangeStart = this.txtObj.selectionStart + strText.length;
		var rangeEnd = rangeStart;

		this.txtObj.value = arrtext.join('');
		this.txtObj.focus();
		this.txtObj.setSelectionRange(rangeStart, rangeEnd);

	}
}

/************************************************************
Function: getSelText()
Type: Member Function of the Selection object
Description:
	This is a member function used by the Selection object. this function is what gets called
	when a user calls the Selection.getSelectionText() method. 

	This method will return the currently hilighted (selected) text of a text area.
*************************************************************/


function getSelText()
{
	if (document.selection)
	{
		this.txtObj.focus();
		var range = document.selection.createRange();
		return range.text 
	}
	else
	{
		var text = this.txtObj.value.substr(this.txtObj.selectionStart, this.selectionLength);
		return text;
	}
}

/************************************************************
Function: getLink()
Type: Function
Description:
	A helper function for insertCode(). it pops up a window asking the user to enter
	a url. Then it pops up a window asking the user to enter text that will point to the url.
	getLink() will then return an array with the user entered info.

Usage:
	arrData = getLink();

	arrData[0] - this will contain the url the user supplied
	arrData[1] - this will contain the user supplied text that will point to the link url 
*************************************************************/

function getLink()
{
	var url = prompt("Enter your URL", "http://");
	var text = prompt("Enter url text", '');

	var arrData = new Array();

	arrData[arrData.length] = (url == null) ? '' : url;
	arrData[arrData.length] = (text == null) ? '' : text;

	return arrData;
}


/************************************************************
Function: getImg()
Type: Function
Description:
	A helper function for insertCode(). it pops up a window asking the user to enter
	a url pointing to an image. getImg() will then return the url the user entered.
*************************************************************/
function getImg()
{
	var text = prompt("Enter an image url", 'http://');
	return (text == null) ? '' : text;
}


/************************************************************
Function: insertCode(textarea, code, hasCloseTag)
Type: Function
Description: 
	This function does all of the work invloved in adding
	bbcode to a text area. it uses the following parameters:
		param: (string)textarea - this is the id assigned to the textarea you want to add bbcode to
		param: (string)code - this is the bbcode that you want inserted
		param: (boolean)hasCloseTag - determines if the bbcode requires a closing bbcode tag
	
Usage:
	This function requires a textarea in order to work, it may work on text input tags
	but has not been tested on text input tags.

	If the text area has hilighted (selected) text insertCode will surround the hilighted text
	with open and close bbcode tags (if the current bbcode actually has a closing bbcode tag). 
	If no text is hilighted , and the bbcode has a closing bbcode tag, insertCode will first add 
	an open bbcode tag. If called again with an already open bbcode, tag of the same type, insertCode
	will add a closing bbcode tag, of the same type.

	For example if you want to add a url link that when clicked adds the 'b' bbcode to a textarea
	with an id of 'ta1' you would add the following code:
		<form>
			<a href="javascript:insertCode('ta1', 'b', true)">bold</a>
			<textarea id="ta1"></textarea>
		</form>

	Note: the 'b' bbcode has a closing bbcode tag, so the third parameter to insertCode is set to true.

	Another example, adds a 'br' bbcode tag to a textarea, with an id of 'ta1' :
		<form>
			<a href="javascript:insertCode('ta1', 'br', false)">line break</a>
			<textarea id="ta1"></textarea>
		</form>
	
	Note: in this example the 'br' does not have a bbcode closing tag, so the third paramater of insertCode 
	is set to false.
*************************************************************/

var arrTags = new Array();

function insertCode(textarea, code, hasCloseTag)
{
	var bbcode = "[" + code + "]";
	var bbcodeclose = "[/" + code + "]";
	
	txt = document.getElementById(textarea);

	var selection = new Selection(textarea);
	
	
	if(selection.selectionLength == 0) // check to see if the current selection is NON hilighted text
	{
		if(hasCloseTag && code != 'link' && code != 'img')
		{
			re = new RegExp(".*\\[(\\/*)" + code + "\\].*$");
			var text = txt.value.substr(0, selection.selectionStart);
			if( re.test(text) )
			{
				match = re.exec(text);
				if(match[1] == "/")
				{
					selection.setSelectionText(bbcode);
				
					if(!arrTags[""+textarea+""].length)
						arrTags["" + textarea + ""] = new Array();
					
					arrTags["" + textarea + ""]["" + code + ""] = code;

					
				}
				else
				{
					selection.setSelectionText(bbcodeclose);
				}
			}
			else
			{
				selection.setSelectionText(bbcode);
			}
			
		}
		else if(hasCloseTag && (code == 'link' || code == 'img'))
		{
			switch(code)
			{
				case 'link':
					var arrData = getLink();
					if (arrData[0] && arrData[1])
					{
						bbcode = '['+ code +'=' + arrData[0] + ']'+ arrData[1] +'[/'+ code + ']';
						selection.setSelectionText(bbcode);
					}
					break;

				case 'img':
					img = getImg();
					if(img)
					{
						bbcode = '['+ code +']'+ img +'[/'+ code +']';
						selection.setSelectionText(bbcode);
					}
					break;
			}
		}

		else
		{
			selection.setSelectionText(bbcode);
		}
	}
	else // otherwise we are working with hilighted text
	{
		if(hasCloseTag  && code != 'link' && code != 'img')
		{
			selection.setSelectionText(bbcode + selection.getSelectionText() + bbcodeclose);
		}
		
		else if(hasCloseTag && (code == 'link' || code == 'img'))
		{
			switch(code)
			{
				case 'link':
					var arrData = getLink();
					if (arrData[0] && arrData[1])
					{
						bbcode = '['+ code +'=' + arrData[0] + ']'+ arrData[1] +'[/'+ code + ']';
						selection.setSelectionText(selection.getSelectionText() + bbcode);
					}
					break;

				case 'img':
					img = getImg();
					if(img)
					{
						bbcode = '['+ code +']'+ img +'[/'+ code +']';
						selection.setSelectionText(bbcode);
					}
					break;
			}
		}

		else
		{
			selection.setSelectionText(selection.getSelectionText() + bbcode);
		}
	}

}


/*************************************************************
Function: searchText(textarea, strURL)
Type: Function
Description:
	Will perform a search, depending on the URL passed in,
	using the currently Hilighted (Selected) Text, if no
	text is hilighted then an input box pops up asking for 
	text to be searched.
Usage:
	searchText() takes the following arguments:
		(string) textarea  - The id of the textarea to use for search
		(string) strURL    - The search URL you want to pass search test to.

	searchText uses the strURL argument to send your search test to. strURL has a specific
	format that must be used. the strURL uses a place holder "%s" in the URL string that will be 
	replaced with the current selected text, before passing the URL to the browser. 

	for example if you want to create a google search you would use the following format:
		'http://www.google.com/search?q=%s'

	So if your search text was say 'HTML' searchText() will transform the above url to:
		'http://www.google.com/search?q=HTML'

	before sending it to the browser
*************************************************************/
function searchText(textarea, strURL)
{
	var selection = new Selection(textarea);
	var searchText = "";
	
	if (selection.selectionLength == 0) // show inputbox to get text from user
	{
		searchText = prompt("Enter Search Text", ""); // add input box code

		if (searchText == null)
		{
			// if search text is empty then user pressed the cancel button
			// in that case just return.
			return;
		}
	}
	else // otherwise we have a selection so, get text from selection
	{
		searchText = selection.getSelectionText();
	}

	// replace %s string place holder with the search text.
	strURL = strURL.replace('%s', escape(searchText));
	
	window.open(strURL, '_blank');	
	
}
//-->
