var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);
		var form_name = '';
		var text_name = '';		
		// Define the bbCode tags
		bbcode = new Array();
		bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[QUOTE]','[/QUOTE]','[code]','[/code]','[list]','[/list]','[*]','[*]','[img]','[/img]','[url]','[/url]');
		imageTag = false;
		
		// Helpline messages
		b_help = "BOLD: Highlight text and click on B: [B]text[/B]";
		i_help = "ITALIC: Highlight text and click on I: [I]text[/I]";
		u_help = "UNDERLINE: Highlight text and click on U: [U]text[/U]";
		q_help = "Quote text: [QUOTE]text[/QUOTE] (alt+q)";
		c_help = "Code text: [code]text[/code] (alt+c)";
		l_help = "Bulleted list (each list item is to be noted with the [*] icon): [list] [*] Item1 [*] Item2 [/list] (alt+l)";
		o_help = "BULLETS: Click on [*], where you want your bullet to be: [*]B1 [*]B2";
		p_help = "Insert image: [img]<image.url>[/img] (alt+p)";
		w_help = "HYPERLINK: Highlight the text you want as a link and click [URL]";
		a_help = "";
		s_help = "";
		f_help = "";
		e_help = "";
function set_form_name(formName) {
form_name = formName;
}

function set_text_name(textName) {
text_name = textName;
}
function storeCaret(textEl) {
	if (textEl.createTextRange) { textEl.caretPos = document.selection.createRange().duplicate(); }
}

function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function insert_text(text) {
	if (document.forms[form_name].elements[text_name].createTextRange && document.forms[form_name].elements[text_name].caretPos) {
		var caretPos = document.forms[form_name].elements[text_name].caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
	} else {
		var selStart = document.forms[form_name].elements[text_name].selectionStart;
		var selEnd = document.forms[form_name].elements[text_name].selectionEnd;

		mozWrap(document.forms[form_name].elements[text_name], text, '')
		//document.forms[form_name].elements[text_name].selectionStart = selStart + text.length;
		//document.forms[form_name].elements[text_name].selectionEnd = selEnd + text.length;
	}
}
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

		
function bbstyle(bbnumber) {

	donotinsert = false;
	theSelection = false;
	bblast = 0;
	document.forms[form_name].elements[text_name].focus();

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			document.forms[form_name].elements[text_name].value += bbtags[butnumber + 1];
			buttext = eval('document.forms[form_name].addbbcode' + butnumber + '.value');
			if (buttext != "[*]")
			{
				eval('document.forms[form_name].addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
			}
		}
		document.forms[form_name].addbbcode10.value = "List";
		bbtags[10] = "[list]";
		document.forms[form_name].addbbcode12.value = "[*]";
		bbtags[12] = "[*]";
		imageTag = false; // All tags are closed including image tags :D
		document.forms[form_name].elements[text_name].focus();
		return;
	}
	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			storeCaret(document.forms[form_name].elements[text_name]);
			document.forms[form_name].elements[text_name].focus();
			theSelection = '';
			return;
		}
	}
	else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
	{
		mozWrap(document.forms[form_name].elements[text_name], bbtags[bbnumber], bbtags[bbnumber+1]);
		document.forms[form_name].elements[text_name].focus();
		theSelection = '';
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				if (bbtags[butnumber] != "[*]")
				{
					insert_text(bbtags[butnumber + 1]);
				}
				else
				{
					insert_text(bbtags[butnumber]);
				}
				buttext = eval('document.forms[form_name].addbbcode' + butnumber + '.value');
				if (bbtags[butnumber] != "[*]")
				{
					eval('document.forms[form_name].addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				}
				imageTag = false;
			}
			document.forms[form_name].elements[text_name].focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			insert_text(bbtags[15]);

			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.forms[form_name].addbbcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}
		// Open tag
		insert_text(bbtags[bbnumber]);
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		if (bbtags[bbnumber] != "[*]")
		{
			arraypush(bbcode,bbnumber+1);
			eval('document.forms[form_name].addbbcode'+bbnumber+'.value += "*"');
		}
		document.forms[form_name].elements[text_name].focus();
		return;
	}

	storeCaret(document.forms[form_name].elements[text_name]);
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var scrollTop = txtarea.scrollTop;
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) 
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd);
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	txtarea.selectionStart = s1.length + open.length + s2.length + close.length;
	txtarea.selectionEnd = txtarea.selectionStart;
	txtarea.scrollTop = scrollTop;
	return;
}
function helpline(help,helpboxid) {
    myhelpbox = document.getElementById(helpboxid);
	myhelpbox.value = eval(help + "_help");
}


function bbAddLink(theText)
{
	Ba = document.forms[form_name].elements[text_name]
    var Bc = get_text();
	var notitle = 0;
	if (Bc.length < 1)
	{
		Bc = "Link title";
		notitle = 1;
	}

	Bb = prompt("Please enter the url of the link you want to add", "http://");
	if (notitle == 1) {Bc = prompt("Please enter the title of the link you want to add", Bc);}

	if ((Bb != null) && (Bb != ""))
	{
		if ((Bc != null) && (Bc != ""))
		{
			//Ba.value = Ba.value + "[URL=" + Bb + "]" + Bc + "[/URL]";
			replace_text("[URL=" + Bb + "]" + Bc + "[/URL]");
		}
	}
    Ba.focus();

}
function replace_text(text) {
	document.forms[form_name].elements[text_name].focus();
	var theSelection = '';
	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = text;
			storeCaret(document.forms[form_name].elements[text_name]);
			document.forms[form_name].elements[text_name].focus();
			return;
		}
	}
	else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
	{
		txtarea = document.forms[form_name].elements[text_name];
		var scrollTop = txtarea.scrollTop;
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd == 1 || selEnd == 2) 
			selEnd = selLength;

		var s1 = (txtarea.value).substring(0,selStart);
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + text + s3;
		txtarea.selectionStart = s1.length + text.length;
		txtarea.selectionEnd = txtarea.selectionStart;
		txtarea.scrollTop = scrollTop;
		return;
	}
	insert_text(text);
}

function insert_text(text) {
	if (document.forms[form_name].elements[text_name].createTextRange && document.forms[form_name].elements[text_name].caretPos) {
		var caretPos = document.forms[form_name].elements[text_name].caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
	} else {
		var selStart = document.forms[form_name].elements[text_name].selectionStart;
		var selEnd = document.forms[form_name].elements[text_name].selectionEnd;
		mozWrap(document.forms[form_name].elements[text_name], text, '')
		//document.forms[form_name].elements[text_name].selectionStart = selStart + text.length;
		//document.forms[form_name].elements[text_name].selectionEnd = selEnd + text.length;
	}
}
function get_text() {
	document.forms[form_name].elements[text_name].focus();
	var theSelection = '';
	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			return theSelection;
		} else {
			return '';
		}
	}
	else
	{
		txtarea = document.forms[form_name].elements[text_name];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd == 1 || selEnd == 2) 
			selEnd = selLength;

		theSelection = (txtarea.value).substring(selStart, selEnd);
		return theSelection;
	}
	return '';
}
function showHelp(theUrl)
{

	window.open(theUrl,'HELP','toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=0,width=600,height=600,left=100,top=100');

}



