var bbcode = new Array();
var theSelection = false;

function helpline(form, field, help) {
	document.forms[form].elements["help"+field].value = eval(help + "_help");
	document.forms[form].elements["help"+field].readOnly = "true";
}

function emoticon(form, field, text) { BBCwrite(form, field, '', ' '+text+' ', true); }
function BBChr(form, field) { BBCwrite(form, field, '', "[hr]", true); }
function BBCdir(form, field, dirc) { document.forms[form].elements[field].dir=(dirc); }
function BBCft(form, field, box) { BBCfont(form, field, "font", box); }
function BBCfs(form, field, box) { BBCfont(form, field, "size", box); }
function BBCfc(form, field, box) { BBCfont(form, field, "color", box); }
function BBCfont(form, field, code, box) { BBCwrite(form, field, "["+code+"="+box.value+"]", "[/"+code+"]", true); }

function BBCwmi(form, field, type) {
	if (type == 'img') { var URL = prompt("Please enter image URL","http://"); }
	else if (type == 'youtube') { var URL = prompt("Please enter YouTube video ID",""); }
	else if (type == 'google') { var URL = prompt("Please enter the Google video ID",""); }
	else if (type == 'ifilm') { var URL = prompt("Please enter the iFilm video ID",""); }
	else if (type == 'metacafe') { var URL = prompt("Please enter the Metacafe video URL/Title",""); }
	else if (type == 'myspace') { var URL = prompt("Please enter the MySpace video ID",""); }
	else if (type == 'email') { var URL = prompt("Enter the Email Address",""); }
	if (URL == null) { return; }
	if (!URL) { return alert("Error : You didn't write the Address"); }
	BBCwrite(form, field, '', "["+type+"]"+URL+"[/"+type+"]", true);
}

function BBCode(form, field, code, img) {
	var type = img.name;
	if (BBCwrite(form, field, "["+code+"="+type+"]", "[/"+code+"]")) { return; }
	if (bbcode[code+type+form+field] == null) {
		ToAdd = "["+code+"="+type+"]";
		re = new RegExp(type+".(\\w+)$");
		img.src = img.src.replace(re, type+"1.$1");
		bbcode[code+type+form+field] = 1;
	} else {
		ToAdd = "[/"+code+"]";
		re = new RegExp(type+"1.(\\w+)$");
		img.src = img.src.replace(re, type+".$1");
		bbcode[code+type+form+field] = null;
	}
	BBCwrite(form, field, '', ToAdd, true);
}

function BBCcode(form, field, img) {
	var code = img.name;
	if (BBCwrite(form, field, "["+code+"]", "[/"+code+"]")) { return; }
	if (bbcode[form+field+code] == null) {
		ToAdd = "["+code+"]";
		re = new RegExp(code+".(\\w+)$");
		img.src = img.src.replace(re, code+"1.$1");
		bbcode[form+field+code] = 1;
	} else {
		ToAdd = "[/"+code+"]";
		re = new RegExp(code+"1.(\\w+)$");
		img.src = img.src.replace(re, code+".$1");
		bbcode[form+field+code] = null;
	}
	BBCwrite(form, field, '', ToAdd, true);
}

function BBCmm(form, field, type) {
	var URL = prompt("Enter the "+type+" file URL", "http://");
	if (URL == null) { return; }
	if (!URL) { return alert("Error: You didn't write the "+type+" file URL"); }
	var WS = prompt("Enter the "+type+" width", "320");
	if (WS == null) { return; }
	if (!WS) { WS = 320; }
	var HS = prompt("Enter the "+type+" height", "300");
	if (HS == null) { return; }
	if (!HS) { HS = 300; }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+"]"+URL+"[/"+type+"]", true);
}

function BBCflash(form, field, type) {
	var URL = prompt("Enter the "+type+" file URL", "http://");
	if (URL == null) { return; }
	if (!URL) { return alert("Error: You didn't write the "+type+" file URL"); }
	var WS = prompt("Enter the "+type+" width", "400");
	if (WS == null) { return; }
	if (!WS) { WS = 400; }
	var HS = prompt("Enter the "+type+" height", "300");
	if (HS == null) { return; }
	if (!HS) { HS = 300; }
	var BGC = prompt("Enter the "+type+" bgcolor", "000000");
	if (BGC == null) { return; }
	if (!BGC) { HS = 000000; }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+" bgcolor="+BGC+"]"+URL+"[/"+type+"]", true);
}

function BBCmp3(form, field, type) {
	var URL = prompt("Enter the "+type+" file URL","http://");
	if (URL == null) { return; }
	if (!URL) { return alert("Error: You didn't write the "+type+" file URL"); }
	var WS = prompt("Enter the "+type+" width", "400");
	if (WS == null) { return; }
	if (!WS) { WS = 400; }
	var HS = prompt("Enter the "+type+" height", "20");
	if (HS == null) { return; }
	if (!HS) { HS = 20; }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+"]"+URL+"[/"+type+"]", true);
}

function BBCmpl(form, field, type) {
	var URL = prompt("Enter the media file URL", "http://");
	if (URL == null) { return; }
	if (!URL) { return alert("Error: You didn't write the media file URL"); }
	var WS = prompt("Enter the media width", "320");
	if (WS == null) { return; }
	if (!WS) { WS = 320; }
	var HS = prompt("Enter the media height", "240");
	if (HS == null) { return; }
	if (!HS) { HS = 240; }
	var LIC = prompt("Enter the lightcolor", "CC9900");
	if (LIC == null) { return; }
	if (!LIC) { LIC = FFFF00; }
	var BAC = prompt("Enter the backcolor", "000000");
	if (BAC == null) { return; }
	if (!BAC) { BAC = 990000; }
	var FRC = prompt("Enter the frontcolor", "CCCCCC");
	if (FRC == null) { return; }
	if (!FRC) { FRC = FFFF00; }
	var IMG = prompt("Enter the image URL", "http://");
	if (IMG == null) { return; }
	if (!IMG) { return alert("Error: You didn't write the media image URL"); }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+" lightcolor="+LIC+" backcolor="+BAC+" frontcolor="+FRC+" image="+IMG+"]"+URL+"[/"+type+"]", true);
}

function BBCmpt(form, field, type) {
	var URL = prompt("Enter the media playlist file (xml, rss)", "");
	if (URL == null) { return; }
	if (!URL) { return alert("Error: You didn't write the media playlist file"); }
	var WS = prompt("Enter the media width", "320");
	if (WS == null) { return; }
	if (!WS) { WS = 320; }
	var HS = prompt("Enter the media height", "350");
	if (HS == null) { return; }
	if (!HS) { HS = 350; }
	var DHT = prompt("Enter the media display height", "260");
	if (DHT == null) { return; }
	if (!DHT) { DHT = 260; }
	var LIC = prompt("Enter the lightcolor", "CC9900");
	if (LIC == null) { return; }
	if (!LIC) { LIC = FFFF00; }
	var BAC = prompt("Enter the backcolor", "000000");
	if (BAC == null) { return; }
	if (!BAC) { BAC = 990000; }
	var FRC = prompt("Enter the frontcolor", "CCCCCC");
	if (FRC == null) { return; }
	if (!FRC) { FRC = FFFF00; }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+" disphght="+DHT+" lightcolor="+LIC+" backcolor="+BAC+" frontcolor="+FRC+"]"+URL+"[/"+type+"]", true);
}

function BBCflag (form, field, box) {
	BBCwrite(form, field, '', " [flag="+box.value+"] ", true);
}

function BBCapple(form, field, type) {
	var URL = prompt("Enter the "+type+" file URL", "http://");
	if (URL == null) { return; }
	if (!URL) { return alert("Error: You didn't write the "+type+" file URL"); }
	var WS = prompt("Enter the "+type+" width", "488");
	if (WS == null) { return; }
	if (!WS) { WS = 488; }
	var HS = prompt("Enter the "+type+" height", "244");
	if (HS == null) { return; }
	if (!HS) { HS = 244; }
	var WSURL = prompt("Enter the "+type+" file URL", "http://");
	if (WSURL == null) { return; }
	if (!WSURL) { return alert("Error: You didn't write the "+type+" Movie URL"); }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+"]"+URL+"[/"+type+"]<br>[align=center][i]Visit the [url="+WSURL+"]Official Movie Website[/url][/i][/align]", true);
}

function BBCurl(form, field) {
	var URL = prompt("Enter the URL", "http://");
	if (URL == null) { return; }
	if (!URL) { return alert("Error: You didn't write the URL "); }
	if (BBCwrite(form, field, "[url="+URL+"]", "[/url]")) { return; }
	var TITLE = prompt("Enter the page name", "Web Page Name");
	if (TITLE == null) { return; }
	var Add = "]"+URL;
	if (TITLE) { Add = "="+URL+"]"+TITLE; }
	BBCwrite(form, field, '', "[url"+Add+"[/url]", true);
}

function BBCwrite(form, field, start, end, force) {
	var textarea = document.forms[form].elements[field];
	if (textarea.caretPos) {
	  textarea.focus();
		// Attempt to create a text range (IE).
		theSelection = document.selection.createRange().text;
		if (force || theSelection != '') {
			document.selection.createRange().text = start + theSelection + end;
			textarea.focus();
			return true;
		}
	} else if (typeof(textarea.selectionStart) != "undefined") {
		// Mozilla text range replace.
		var text = new Array();
		text[0] = textarea.value.substr(0, textarea.selectionStart);
		text[1] = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd-textarea.selectionStart);
		text[2] = textarea.value.substr(textarea.selectionEnd);
		caretPos = textarea.selectionEnd+start.length+end.length;
		if (force || text[1] != '') {
			textarea.value = text[0]+start+text[1]+end+text[2];
			if (textarea.setSelectionRange) {
				textarea.focus();
				textarea.setSelectionRange(caretPos, caretPos);
			}
			return true;
		}
	} else if (force) {
		// Just put it on the end.
		textarea.value += start+end;
		textarea.focus(textarea.value.length-1);
		return true;
	}
	return false;
}

function storeCaret(text) {
	if (text.createTextRange) text.caretPos = document.selection.createRange().duplicate();
}

function bbstyle(bbnumber) {
	return true;
}
