search = new Array(
          /\[img\](.*?)\[\/img\]/g,
          /\[url=([\w]+?:\/\/[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/g,
          /\[url\]((www|ftp|)\.[^ \\"\n\r\t<]*?)\[\/url\]/g,
          /\[url=((www|ftp|)\.[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/g,
          /\[email\](([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+))\[\/email\]/g,
          /\[b\](.*?)\[\/b\]/g,
		  /\[i\](.*?)\[\/i\]/g,
		  /\[u\](.*?)\[\/u\]/g,
		  /\[size=([\d]+?)\](.*?)\[\/size\]/g,
          /\[url\](http:\/\/[^ \\"\n\r\t<]*?)\[\/url\]/g,
		  /<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>/g);

replace = new Array(
          "<img src=\"$1\" alt=\"An image\">",
          "<a href=\"$1\" target=\"blank\">$2</a>",
          "<a href=\"http://$1\" target=\"blank\">$1</a>",
          "<a href=\"$1\" target=\"blank\">$1</a>",
          "<a href=\"mailto:$1\">$1</a>",
          "<b>$1</b>",
		  "<span style=\"font-style:italic;\">$1</span>",
		  "<span style=\"text-decoration:underline;\">$1</span>",
		  "<span style=\"font-size:$1px\">$2</span>",
          "<a href=\"$1\" target=\"blank\">$1</a>",
		  "");

function parseBBCode(text) {
	var t = text;
	t = t.replace(/\n/g,'<br>');
	for(i = 0; i < search.length; i++) {
		t = t.replace(search[i],replace[i]);
	}
	return t;
}

function createPreview() {
	if(typeof ($ = window.jQuery) != "undefined") {
		var title = $("input[name=article_name]").val();
		$("div.articlecontent h2.padded a").text(title);
	
		var text = $("textarea[name=article_body]").val()
		$("p.preview").html(parseBBCode(text));
		scroll(0,0);
	}
}



// START EREPUBLIK PLUS CODE

function getCurrentSelection(obj) {
	return (obj.value.substring(obj.selectionStart, obj.selectionEnd));
}

function updateTextarea(object, wrapped) {
	var len = object.value.length;
	var scrollTop = object.scrollTop;
	var scrollLeft = object.scrollLeft;
	object.value = object.value.substring(0, object.selectionStart) + wrapped + object.value.substring(object.selectionEnd, len);
	object.scrollTop = scrollTop;
	object.scrollLeft = scrollLeft;
}
	
function addAttr(attr) {
	var object = document.getElementById('body');
	var attrValue = null;
	do 	{
		attrValue = prompt('Please enter ' + attr + ': ');
	} while (attrValue == '');
	
	if (attrValue != null)
	{
		if (attr == 'url')
			attrValue = attrValue.replace('?','%3F');
		var wrapped = "[" + attr + "=" + attrValue + "]" + getCurrentSelection(object)  + "[/" + attr + "]";
		updateTextarea(object, wrapped);
	}
}

function addStyle(styleTag) {
	var object = document.getElementById('body');
	var wrapped = "["+ styleTag + "]" + getCurrentSelection(object) + "[/" + styleTag + "]";
	updateTextarea(object, wrapped);
}

function addImage(attr) {
	var object = document.getElementById('body');
	var attrValue = null;
	do 	{
		attrValue = prompt('Please enter the image url: ');
	} while (attrValue == '');
	
	if (attrValue != null)
	{
		var wrapped = "[" + attr + "]" + attrValue + "[/" + attr + "]";
		updateTextarea(object, wrapped);
	}
}
	
// END EREPUBLIK PLUS CODE

	