// Define if we are using ie and/or windows

var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_win = ((clientPC.indexOf("win") != -1) || (clientPC.indexOf("16bit") != -1));

// Define our bbtags and bbcode variables

var bbtags = new Array("[b]", "[i]", "[u]", "[s]", "[url]", "[email]", "[/b]", "[/i]", "[/u]", "[/s]", "[/url]", "[/email]");
var bbcode = new Array();

//--------------------------------------------------
// Check which textarea is available to us
//--------------------------------------------------

function GetTextareaName() {
	if (document.forms[0].elements['body']) {
		return "body";
	} else if (document.forms[0].elements['description']) {
		return "description";
	}
}

//--------------------------------------------------
// Adds the emoticon code to the comment textbox
//--------------------------------------------------

function AddEmoticon(code) {
	InsertAtCaret(" " + code + " ");
}

//--------------------------------------------------
// Checks for valid url or e-mail
//--------------------------------------------------

function IsValid(type, str) {
	if (type == "url") {
		var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	} else if (type == "email") {
		var regexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	}
	
	return regexp.test(str);
}

//--------------------------------------------------
// Adds a text formatting tag to the comment textbox
//--------------------------------------------------

function AddTextTag(tag) {

	var body = document.forms[0].elements[GetTextareaName()];

	theSelection = false;

	// Are we using IE and Windows? Or do we have any text selected?

	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;

		// Do we have any text selected?

		if (theSelection) {
			document.selection.createRange().text = bbtags[tag] + theSelection + bbtags[tag+6];
			theSelection = "";

			return;
		}
	} else if (body.selectionEnd && (body.selectionEnd - body.selectionStart > 0)) {
		// Code obtained from:
		// http://www.massless.org/mozedit/

		var selLength = body.textLength;
		var selStart = body.selectionStart;
		var selEnd = body.selectionEnd;

		if (selEnd == 1 || selEnd == 2) {
			selEnd = selLength;
		}

		var s1 = (body.value).substring(0, selStart);
		var s2 = (body.value).substring(selStart, selEnd)
		var s3 = (body.value).substring(selEnd, selLength);

		body.value = s1 + bbtags[tag] + s2 + bbtags[tag+6] + s3;

		return;
	}

	// No text was selected, continue...

	// Trying to close open tags?

	for (i=bbcode.length-1; i>=0; i--) {
		if (tag == bbcode[i]) {
			var tag_found = true;
			var tag_id = i;
			break;
		}
	}

	// Do we have any tags to close or open a new one?

	if (tag_found) {
		for (i=bbcode.length-1; i>=tag_id; i--) {
			body.value += bbtags[bbcode[i]+6];

			button = document.forms[0].elements['bbcode' + bbcode[i]];
			button.style.fontWeight = "normal";

			bbcode.length--;
		}
	} else {
		// Open a new tag and change button name

		body.value += bbtags[tag];
		bbcode[bbcode.length] = tag;

		button = document.forms[0].elements['bbcode' + tag];
		button.style.fontWeight = "bold";
	}
}

//--------------------------------------------------
// The following code was obtained from:
// The PHPMyAdmin project
//--------------------------------------------------

function InsertAtCaret(text) {
	var body = document.forms[0].elements[GetTextareaName()];

	// Insert text at caret position or at the end?

	if (document.selection) {
		body.focus();
		document.selection.createRange().text = text;
	} else if (body.selectionStart || body.selectionStart == '0') {
		var s_pos = body.selectionStart;
		var e_pos = body.selectionEnd;

		var s_body = body.value.substring(0, s_pos);
		var e_body = body.value.substring(e_pos, body.value.length);

		body.value = s_body + text + e_body;
		body.focus();
	} else {
		body.value += tagged;
		body.focus();
	}
}