// Function for a primitive styling-functionionality using deCode
function deboard_addtag(myField, myStartTag, myEndTag, check) {
	var myStartTag = '[' + myStartTag + ']';
	var myEndTag = '[/' + myEndTag + ']';

	if(document.selection) {
		// IE/OPERA support
		myField.focus();
		sel = document.selection.createRange();

		// Check if there are tags to be removed again
		if(check) {
			if(sel.text.substring(0, myStartTag.length) == myStartTag && sel.text.substring(sel.text.length-myEndTag.length, sel.text.length) == myEndTag) {
				sel.text = sel.text.substring(myStartTag.length, sel.text.length - myEndTag.length);
			} else {
				check = false;
			}
		}

		if(!check)
			sel.text = myStartTag + sel.text + myEndTag;

		sel.select();
	} else if(myField.selectionStart || myField.selectionStart == '0') {
		// MOZILLA/NETSCAPE support
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;

		// Check if there are tags to be removed again
		if(check) {
			if(myField.value.substring(startPos, myStartTag.length) == myStartTag && myField.value.substring(endPos - myEndTag.length, endPos) == myEndTag) {
				myField.value = myField.value.substring(0, startPos) + myField.value.substring(startPos+myStartTag.length, endPos-myEndTag.length) + myField.value.substring(endPos, myField.value.length);
				myField.focus();
				myField.selectionStart=startPos;
				myField.selectionEnd=endPos-myStartTag.length-myEndTag.length;
			} else {
				check = false;
			}
		}

		if(!check) {
			myField.value = myField.value.substring(0, startPos) + myStartTag + myField.value.substring(startPos, endPos) + myEndTag + myField.value.substring(endPos, myField.value.length);

			myField.focus();
			myField.selectionStart=startPos;
			myField.selectionEnd=endPos+myStartTag.length+myEndTag.length;
		}
	} else {
		// IF BOTH FAIL, GIVE OUT ERROR
		alert('Sorry, this feature is not supported by your browser =(');
	}
}

function deboard_email(myForm) {
	email = prompt('Please enter email address', '');
	if(email) {
		name = prompt('Please specify name of address owner or leave it blank');

		if(name)
			insertAtCursor(myForm, '[email='+email+']'+name+'[/email]');
		else
			insertAtCursor(myForm, '[email]'+email+'[/email]');
	}
}

function deboard_myImage(myForm) {
	number = prompt('Please specify image number', '');
	if(number)
		insertAtCursor(myForm, '[upl_image]'+number+'[/upl_image]');
}

function deboard_url(myForm) {
	url = prompt('Please enter address', 'http://');
	if(url) {
		name = prompt('Enter a descriptive text or leave it blank');

		if(name)
			insertAtCursor(myForm, '[url='+url+']'+name+'[/url]');
		else
			insertAtCursor(myForm, '[url]'+url+'[/url]');
	}
}

function deboard_youtube(myForm) {
	url = prompt('Please specify url or ID of YouTube-Video. (E.g. http://www.youtube.com/watch?v=abcde12345 or abcde12345', '');
	if(url) {
		insertAtCursor(myForm, '[youtube]'+url+'[/youtube]');
	}
}

function deboard_image(myForm) {
	url = prompt('Please specify url to image', '');
	if(url) {
		insertAtCursor(myForm, '[img]'+url+'[/img]');
	}
}

function deboard_quoteby(myForm) {
	name = prompt('Please specify who you quote', '');
	if(name) {
		deboard_addtag(myForm, 'quote="'+name+'"', 'quote');
	}
}

function deboard_color(myForm) {
	select = document.getElementById('colorselect');
	deboard_addtag(myForm, 'color='+select.options[select.selectedIndex].value, 'color', true);
	select.selectedIndex = 0;
}

function deboard_info(myId, info) {
	document.getElementById(myId).value=info;
}

function deboard_list(myForm) {
	var type = prompt('Please specify type of list. For a numbered list enter "1", enter "a" for an alphabetical list or leave it blank for an dotted one', '');
	switch(type) {
		case "1": myText = "[list=1]"; break;
		case "a": myText = "[list=a]"; break;
		default: myText = "[list]"; break;
	}

	while(text = prompt('Enter list item. To finalize list press chancel or leave it blank'))
		myText = myText + "\n[*]" + text;

	switch(type) {
		case "1": myText += "\n[/list=1]"; break;
		case "a": myText += "\n[/list=a]"; break;
		default: myText += "\n[/list]"; break;
	}

	insertAtCursor(myForm, myText);
}