function addSmiley(fieldname, code)
{
	$(fieldname).value = $(fieldname).value + ' ' + code;
	
	return false;
}

function addStyle(fieldname, code)
{
	var field = $(fieldname);
	
    /**
     * get selected text
     **/
    if (window.getSelection)
    {
        /* Mozilla */
        selLength   = field.textLength;
        selStart    = field.selectionStart;
        selEnd      = field.selectionEnd;
        
        text_start      = (field.value).substring(0, selStart);
        text_selected   = (field.value).substring(selStart, selEnd);
        text_end        = (field.value).substring(selEnd, selLength);
    }
    else if (document.selection) 
    {
        /* IE */
        text_selected = document.selection.createRange().text;
    }
    
    /**
     * enclose BBcode when text is selected
     **/
    if ('' != text_selected)
    {
        if (window.getSelection)
        {
            /* Mozilla */
            var inserted_text = '[' + code + ']' + text_selected + '[/' + code + ']';
            field.value = text_start + inserted_text + text_end;
            
            var pos = selStart + inserted_text.length;
            field.selectionStart = pos;
            field.selectionEnd = pos;
        }
        else if (document.selection) 
        {
            /* IE */
            field.focus();
            text_selection = document.selection.createRange();
            text_selection.text = '[' + code + ']' + text_selected + '[/' + code + ']';
            
            /* Anpassen der Cursorposition */
            text_selection.moveStart('character', text_selection.text);      
            text_selection.select();

        }
    }
    /**
     * else add the BBcode to cursor position
     **/
    else
    {
        if (window.getSelection)
        {
            /* Mozilla */
            var inserted_text = '[' + code + ']';
            field.value = text_start + inserted_text + '[/' + code + ']' + text_end;
            
            var pos = selStart + inserted_text.length;
            field.selectionStart = pos;
            field.selectionEnd = pos;
            
        }
        else if (document.selection) 
        {
            /* IE */
            field.focus();
            
            var caret_pos = getCaretPosition(field).start;
			var new_pos = caret_pos + code.length + 2;
	
            text_selection = document.selection.createRange();
            text_selection.text = '[' + code + ']' + '[/' + code + ']';
            text_selection.select();

			var range = field.createTextRange(); 
			range.move("character", new_pos); 
			range.select();      
		}

    }
    
    field.focus();
    
    return false;
}

/**
* Get the caret position in an textarea
*/
function getCaretPosition(txtarea)
{
	var caretPos = new Object();
	
	// simple Gecko/Opera way
	if(txtarea.selectionStart || txtarea.selectionStart == 0)
	{
		caretPos.start = txtarea.selectionStart;
		caretPos.end = txtarea.selectionEnd;
	}
	// dirty and slow IE way
	else if(document.selection)
	{
	
		// get current selection
		var range = document.selection.createRange();

		// a new selection of the whole textarea
		var range_all = document.body.createTextRange();
		range_all.moveToElementText(txtarea);
		
		// calculate selection start point by moving beginning of range_all to beginning of range
		var sel_start;
		for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
		{		
			range_all.moveStart('character', 1);
		}
	
		txtarea.sel_start = sel_start;
	
		// we ignore the end value for IE, this is already dirty enough and we don't need it
		caretPos.start = txtarea.sel_start;
		caretPos.end = txtarea.sel_start;			
	}

	return caretPos;
}