//Adds tags to selected text
//see http://www.vladdy.net/demos/textareainsertion.html 
function getSel(ta)
{
	//see http://www.quirksmode.org/js/selected.html for browser compatibility
	var bits = [ta.value,'','','']; 

	if(window.getSelection) //firefox
	{
		if(ta.selectionStart == ta.selectionEnd) return null;
		bits=(new RegExp('([\u0000-\uffff]{'+ta.selectionStart+'})([\u0000-\uffff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\u0000-\uffff]*)')).exec(ta.value);	
	}
	else if (document.getSelection) //opera
	{
		if(ta.selectionStart == ta.selectionEnd) return null;
		bits=(new RegExp('([\u0000-\uffff]{'+ta.selectionStart+'})([\u0000-\uffff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\u0000-\uffff]*)')).exec(ta.value);	
	}
	else //if(document.selection) //IE
	{
		var vs = '#$%^%$#';
		var tr=document.selection.createRange()
		if(tr.parentElement()!=ta) return null;
		bits[2] = tr.text;
        tr.text = vs;
        fb = ta.value.split(vs);
        tr.moveStart('character',-vs.length);
        tr.text = bits[2];
        bits[1] = fb[0];
        bits[3] = fb[1];
	}

	return bits;	
}

function matchTags(str)
{
	str = ' ' + str + ' ';	
	ot = str.split(/\[[b|u|i|url].*?\]/i);
	ct = str.split(/\[\/[b|u|i|url].*?\]/i);
	return ot.length==ct.length;
}

function getOpenTag(type)
{
	switch(type)
	{
	case 'bold':
		return '[b]';
	case 'underline':
		return '[u]';
	case 'italic':
		return '[i]';
	case 'url':
		var address = prompt("Please enter the address that you want the link to point to.\nLink will open in a new browser window.");
		if(address)
			return '[url=' + address.replace(" ", "%20") + ']';
		else
			return '[url]';
	case 'file':
		var address = prompt("Please enter the name of the file that you want the link to point to.\nIf you haven't already done so, upload the file using the File Manager.");
		if(address)
			return '[file=' + address + ']';
			//return '[file=' + address.replace(" ", "%20") + ']';
		else
			return '[file]';
	case 'ilink':
		var address = prompt("Please enter the filename of the page on this site that you want the link to point to:");
		if(address)
			return '[ilink=' + address + ']';
			//return '[file=' + address.replace(" ", "%20") + ']';
		else
			return '[ilink]';
	}
}

function getCloseTag(type)
{
	switch(type)
	{
	case 'bold':
		return '[/b]';
	case 'underline':
		return '[/u]';
	case 'italic':
		return '[/i]';
	case 'url':
		return '[/url]';
	case 'file':
		return '[/file]';
	case 'ilink':
		return '[/ilink]';
	}
}

function addTag(ta, type)
{
	bits = getSel(ta);
    if(bits)
	{
		if(!matchTags(bits[2]))
		{
			alert('\t\tInvalid Selection: The selected text contains unmatched opening or closing tags.');
			return;
		}
		ta.value = bits[1] + getOpenTag(type) + bits[2] + getCloseTag(type) + bits[3];
	}
	else
		alert('\t\tFirst highlight the section of text that you want to format.');
}

function help()
{
	var message = "Highlight the text that you would like to format, then click the appropriate button. ";
	message = message + "If you click the Link button you will be asked to enter the URL that you would like the link to point to.\n\n";
	message = message + "The formatting is represented as [tags] surrounding the selected text. ";
	message = message + "These are rendered into the appropriate style when the text is displayed on the web page.\n\n";
	message = message + "To remove the formatting from a section of text, simply delete the tags at the beginning and end of the section.\n\n";
	message = message + "Unfortunately the insertion of tags does not work on certain browsers eg IE and Safari on the Mac.";
	alert(message);
}
