function newEvent(obj, type, funct) {
	if (obj != null) {
		if (obj.addEventListener)
			obj.addEventListener(type, funct, false);
		else
			obj.attachEvent("on" + type, funct);
	}
}

function eventHandled() {
	if (window.addEventListener) {
		event.preventDefault();
		event.stopPropagation();
	}
	else {
		event.returnValue = false;
		event.preventBubble();
	}
}

function insertAtCursor( myField, myValue )
{
	if ( document.selection ) // IE
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	else if ( myField.selectionStart || myField.selectionStart == 0 ) // W3C
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring( 0, startPos ) + myValue + myField.value.substring( endPos, myField.value.length );
	}
	else
	{
		myField.value += myValue;
	}
}

newEvent( window, 'load',
	function( event )
	{
		if ( document.getElementById( 'editBold' ) == null )
		{
			return;
		}
		
		newEvent( document.getElementById( 'editBold' ), 'click',
			function( event )
			{
				var text = prompt( 'Enter some text to make bold:', '' );
				if ( text != null && text != '' )
				{
					insertAtCursor( document.getElementById( 'content2' ), '[b]' + text + '[/b]' );
				}
			}
		);
		
		newEvent( document.getElementById( 'editItalic' ), 'click',
			function( event )
			{
				var text = prompt( 'Enter some text to make italic:', '' );
				if ( text != null && text != '' )
				{
					insertAtCursor( document.getElementById( 'content2' ), '[i]' + text + '[/i]' );
				}
			}
		);
		
		newEvent( document.getElementById( 'editLink' ), 'click',
			function( event )
			{
				var url = prompt( 'Enter the URL for your link:', '' );
				if ( url == null || url == '' )
				{
					return;
				}
				
				var description = prompt( 'Enter a description for your link (optional, leave empty to use the URL as the description):' );
				if ( description == null )
				{
					return;
				}
				
				if ( !url.match( /\w+:/ ) )
				{
					url = 'http://' + url;
				}
				
				if ( description != '' )
				{
					description = '=' + description;
				}
				insertAtCursor( document.getElementById( 'content2' ), '[link' + description + ']' + url + '[/link]' );
			}
		);
	}
);