var text_enter_url      = "Enter the complete URL for the hyperlink";
var text_enter_url_name = "Enter the title of the webpage";
var text_enter_image    = "Enter the complete URL for the image";
var text_enter_email    = "Enter the email address";
var text_enter_flash    = "Enter the URL to the Flash movie.";
var text_code           = "Usage: [CODE] Your Code Here.. [/CODE]";
var text_quote          = "Usage: [QUOTE] Your Quote Here.. [/QUOTE]";
var error_no_url        = "You must enter a URL";
var error_no_title      = "You must enter a title";
var error_no_email      = "You must enter an email address";
var error_no_width      = "You must enter a width";
var error_no_height     = "You must enter a height";

bbtags   = new Array();

//--------------------------------------------
// Get stack size
//--------------------------------------------

function stacksize(thearray) {
	for (i = 0 ; i < thearray.length; i++ ) {
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') ) {
			return i;
		}
	}
	return thearray.length;
}

//--------------------------------------------
// Push stack
//--------------------------------------------

function pushstack(thearray, newval) {
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

//--------------------------------------------
// Pop stack
//--------------------------------------------

function popstack(thearray) {
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}

//--------------------------------------------
// Close current open tag
//--------------------------------------------

function closelast() {
	if (bbtags[0]) {
		document.form.bericht.value += "[/" + popstack(bbtags) + "]";
	}
	document.form.bericht.focus();
}

//--------------------------------------------
// Close all tags
//--------------------------------------------

function closeall() {
	if (bbtags[0]) {
		while (bbtags[0]) {
			document.form.bericht.value += "[/" + popstack(bbtags) + "]";
		}
		document.form.bericht.value += " ";
	}
	document.form.bericht.focus();
}

//--------------------------------------------
// EMOTICONS
//--------------------------------------------

function emoticon(theSmilie) {
	if (document.form.bericht.caretPos && document.form.bericht.createTextRange) {
		var caretPos = document.form.bericht.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? theSmilie + ' ' : theSmilie;
	}
	else {
		document.form.bericht.value += ' ' + theSmilie + ' ';
	}
	document.form.bericht.focus();
}

//--------------------------------------------
// ADD CODE
//--------------------------------------------

function add_code(NewCode) {
	document.form.bericht.value += NewCode;
	document.form.bericht.focus();
	return;
}

//--------------------------------------------
// ALTER FONT
//--------------------------------------------

function alterfont(theval, thetag) {
	if (theval == 0) {
	return;
	}
	else {
	document.form.bericht.value += "[" + thetag + "=" + theval + "]";
	pushstack(bbtags, thetag);
	}
	document.form.ffont.selectedIndex  = 0;
	document.form.fsize.selectedIndex  = 0;
	document.form.fcolor.selectedIndex = 0;
	document.form.bericht.focus();
}


//--------------------------------------------
// SIMPLE TAGS (such as B, I U, etc)
//--------------------------------------------

function simpletag(thetag) {
	document.form.bericht.value += "[" + thetag + "]";
	pushstack(bbtags, thetag);
	document.form.bericht.focus();
}

function tag_url() {
	var FoundErrors = '';
	var enterURL   = prompt(text_enter_url, "");
	var enterTITLE = prompt(text_enter_url_name, "Mijn Webpage!");
	if (!enterURL) {
		FoundErrors += " " + error_no_url;
	}
	if (!enterTITLE) {
		FoundErrors += " " + error_no_title;
	}
	if (FoundErrors) {
		alert("Error!"+FoundErrors);
		return;
	}
	var ToAdd = "[URL=http://"+enterURL+"]"+enterTITLE+"[/URL]";
	document.form.bericht.value+=ToAdd;
	document.form.bericht.focus();
}

function tag_image() {
	var FoundErrors = '';
	var enterURL   = prompt(text_enter_image, "Url hierzo!");
	if (!enterURL) {
		FoundErrors += " " + error_no_url;
	}
	if (FoundErrors) {
		alert("Error!"+FoundErrors);
		return;
		}
	var ToAdd = "[IMG]http://"+enterURL+"[/IMG]";
	document.form.bericht.value+=ToAdd;
	document.form.bericht.focus();
}

function tag_email() {
	var emailAddress = prompt(text_enter_email,"");
	if (!emailAddress) { alert(error_no_email); return; }
	var ToAdd = "[EMAIL]"+emailAddress+"[/EMAIL]";
	document.form.bericht.value+=ToAdd;
	document.form.bericht.focus();
}
