// b2 quick tags
// - authorized adaptation of the 'bbCode control code' by subBlue design ( www.subBlue.com )

// Define the quick tags
bbcode = new Array();
bbtags = new Array('<strong>','</strong>','<em>','</em>','<span style="text-decoration:underline;">','</span>','<span style="text-decoration:line-through;">','</span>','<blockquote>','</blockquote>','<p>','</p>','<li>','</li>','<img src="" alt="" style="border:0;"/>','','<a href="">','</a>');
imageTag = false;

// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}


function checkForm(formObj) {

	formErrors = false;

	if (formObj.comment.value.length < 2) {
		formErrors = "You must enter a message!";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		bbstyle(formObj, -1);
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		return true;
	}
}


function emoticon(theSmilie) {
	if ((parseInt(navigator.appVersion) >= 4) && (navigator.appName == "Microsoft Internet Explorer"))
		theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) {
		// Add tags around selection
		document.selection.createRange().text = theSelection + theSmilie + ' ';
		formObj.comment.focus();
		theSelection = '';
		return;
	}


	document.post.comment.value += ' ' + theSmilie + ' ';
	document.post.comment.focus();
}


function bbfontstyle(formObj, bbopen, bbclose) {
	if ((parseInt(navigator.appVersion) >= 4) && (navigator.appName == "Microsoft Internet Explorer")) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			formObj.comment.value += bbopen + bbclose;
			formObj.comment.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		formObj.comment.focus();
		return;
	} else {
		formObj.comment.value += bbopen + bbclose;
		formObj.comment.focus();
		return;
	}
}


function bbstyle(formObj, bbnumber) {

	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			formObj.comment.value += bbtags[butnumber + 1];
			buttext = eval('formObj.addbbcode' + butnumber + '.value');
			eval('formObj.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		formObj.comment.focus();
		return;
	}

	if ((parseInt(navigator.appVersion) >= 4) && (navigator.appName == "Microsoft Internet Explorer"))
		theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) {
		// Add tags around selection
		document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
		formObj.comment.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;
				formObj.comment.value += bbtags[butnumber + 1];
				buttext = eval('formObj.addbbcode' + butnumber + '.value');
				eval('formObj.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			formObj.comment.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			formObj.comment.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			formObj.addbbcode14.value = "image";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		formObj.comment.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		eval('formObj.addbbcode'+bbnumber+'.value += "*"');
		formObj.comment.focus();
		return;
	}

}
