<!--

// Class object for validating strings embedded with BB tags.
function BBTags(str)
{
	this.str = str.toString();
}

// Checks the string for proper BB tag syntax.
function BBTags_validate()
{
	var stack = new Array();
	var alltags = new RegExp('\\[(.+?)\\]', 'ig');
	var closetags = new RegExp('\\[/(.+?)\\]', 'i');

	if (1 > this.str.length)
	{
		// Nothing to check = valid.
		return true;
	}

	// Split up all possible BB tags.
	var parts = this.str.match(alltags);

	if (null == parts)
	{
		// No BB tags present = valid.
		return true;
	}

	// Rip out the tag names from tags.
	// We forget about the slash and the square brackets.
	var re_inner = new RegExp('([a-zA-Z0-9]+)', 'ig');

	// Process each tag.
	for (i=0; i<parts.length; i++)
	{
		// Check to see if it is a closing tag.
		if (closetags.test(parts[i].toString()))
		{
			if (stack.length < 1)
			{
				// There are no open tags on stack.
				alert('A closing BB tag ' + parts[i].toString() + ' was found without a corresponding opening tag.');
				return false;
			}
			else
			{
				// Pop the top of the stack and compare.
				var top = stack.pop();

				// Rip out the tag names.
				var top_name = top.toString().match(re_inner);
				var close_name = parts[i].toString().match(re_inner);

				if (top_name[0].toString() != close_name[0].toString())
				{
					alert('A closing BB tag ' + parts[i].toString() + ' was found before closing ' + top.toString() + '.');
					return false;
				}
			}
		}
		else
		{
			// Push this opening BB tag onto the stack.
			stack.push(parts[i]);
		}
	}

	// Final validations.
	if (0 < stack.length)
	{
		// There are left over unclosed tags.
		alert('There is one or more unclosed BB tags');
		return false;
	}

	return true;
}

// Sets the string to validate BB tags with.
function BBTags_set(str)
{
	this.str = str.toString();
}

BBTags.prototype.validate = BBTags_validate;
BBTags.prototype.set = BBTags_set;

//-->
