var ques =
{
	'img'     	: 'Enter the complete url for the image.',
	'url'     	: 'Enter the complete URL for the hyperlink.'
};
var msg =
{
	'img'     	: 'You must enter the url for the image.'
};
var tags_open =
{
	'b'			: '[b] [/b]',
	'i'			: '[i] [/i]',
	's'			: '[s] [/s]',
	'u'			: '[u] [/u]',
	'quote'		: '[quote] [/quote]',
	'code'		: '[code] [/code]',
	'img'		: '[img]',
	'url'		: '[url='
};
var tags_close =
{
	'img'		: '[/img]',
	'url'		: '[/url]'
};


function insert_tag1(bb,target)
	{
		if(bb=="url") {
			
			str = prompt(ques[bb],'http://');
			if( !str ) {
				alert("You must enter a url for the link."); 
				return;
			}
			str2 = prompt("Enter the title of the link",'My Webpage');
			if( !str2 ) {
				alert("You must enter a title.");
				return;
			}
			str = tags_open[ bb ] + str + "]" + str2 +tags_close[ bb ];

		}
		else {
			str = prompt(ques[bb],'http://');
			if( !str ) {
				alert( msg[bb] ); 
				return;
			}
			str = tags_open[ bb ] + str + tags_close[ bb ];

		}
		
		doInsert(target, str);
		return;
	}


//----------------------------------------------------------------
// Insert normal b,s, i BBCode
//----------------------------------------------------------------

function insert_tag2(bb,target) {
		str = tags_open[ bb ];
		
		doInsert(target, str);
		return;
}


//----------------------------------------------------------------
// Insert font size tags
//----------------------------------------------------------------

function font_size(val,target) {
	if(val > 0) {
		str = "[size=" + val + "] [/size]";
		
		doInsert(target, str);
		return;
	}
	else {
		return;
	}
}

//----------------------------------------------------------------
// Add emoticons
//----------------------------------------------------------------

function emoticon(a,target) {
	
	doInsert(target, a);
	return;
}


//----------------------------------------------------------------
// Do insert on cursor position
//----------------------------------------------------------------
function doInsert(target, str) {
	
	target.focus();
	
	var insPos = getPos(target)
	
	var strStart = target.value.substr( 0, insPos );
	var strEnd = target.value.substr( insPos, target.value.length );
	
	target.value = strStart + str + strEnd;
	
	// If Mozzy, we set the cursor pos
	if( target.selectionStart ) {
		target.selectionStart = str.length + insPos;
		target.selectionEnd = str.length + insPos;
	}
	
	target.focus();
	
	return;
	
}


//----------------------------------------------------------------
// Get the pos of cursor in (works for Mozzy)
//----------------------------------------------------------------

function getPos(target) {
	
	// Mozzy, can do insert
	if( target.selectionStart ) {
		cpos = target.selectionStart;
	}
	// IE or other, crappy insert
	else {
		cpos = target.value.length;
	}
	
	return cpos
}