
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 == 'img_l') { var URL = prompt("Please enter image URL","http://"); }
	else if (type == 'img_r') { var URL = prompt("Please enter image URL","http://"); }
	else if (type == 'youtube') { var URL = prompt("Please enter YouTube video ID",""); }
	else if (type == 'myspace') { var URL = prompt("Please enter the MySpace 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 == 'ffvideo') { var URL = prompt("Please enter the FileFront video ID",""); }
	else if (type == 'search') { var URL = prompt("Please enter a seach value",""); }
	else { var URL = prompt("Enter the Email Address",""); }
	if (URL == null) { return null; }
	if (!URL) { return alert("Error : You didn't write the Address"); }
	BBCwrite(form, field, '', "["+type+"]"+URL+"[/"+type+"]", true);
	return null;
}

function BBCimg(form, field, type) {
	var FILE = prompt("Enter the image file URL", "http://");
	if (FILE == null) { return null; }
	if (!FILE) { return alert("Error: You didn't write the image file URL"); }
	var URL = prompt("Enter the URL link", "http://");
	if (URL == null) { return null; }
	if (!URL) { return alert("Error: You didn't write the URL"); }
	BBCwrite(form, field, '', "["+type+" url="+URL+"]"+FILE+"[/"+type+"]", true);
	return null;
}

function BBCode(form, field, code, img, type) {
	if (BBCwrite(form, field, "["+code+"="+type+"]", "[/"+code+"]")) { return; }
	if (bbcode[code+type+form+field] == null) {
		var ToAdd = "["+code+"="+type+"]";
		var re = new RegExp(type+".(\\w+)$");
		img.src = img.src.replace(re, type+"1.$1");
		bbcode[code+type+form+field] = 1;
	} else {
		var ToAdd = "[/"+code+"]";
		var 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, code) {
	if (BBCwrite(form, field, "["+code+"]", "[/"+code+"]")) { return; }
	if (bbcode[form+field+code] == null) {
		var ToAdd = "["+code+"]";
		var re = new RegExp(code+".(\\w+)$");
		img.src = img.src.replace(re, code+"1.$1");
		bbcode[form+field+code] = 1;
	} else {
		var ToAdd = "[/"+code+"]";
		var 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 null; }
	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 null; }
	if (!WS) { WS = 320; }
	var HS = prompt("Enter the "+type+" height", "300");
	if (HS == null) { return null; }
	if (!HS) { HS = 300; }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+"]"+URL+"[/"+type+"]", true);
	return null;
}

function BBCflash(form, field, type) {
	var URL = prompt("Enter the "+type+" file URL", "http://");
	if (URL == null) { return null; }
	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 null; }
	if (!WS) { WS = 400; }
	var HS = prompt("Enter the "+type+" height", "300");
	if (HS == null) { return null; }
	if (!HS) { HS = 300; }
	var BGC = prompt("Enter the "+type+" bgcolor", "000000");
	if (BGC == null) { return null; }
	if (!BGC) { BGC = 000000; }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+" bgcolor="+BGC+"]"+URL+"[/"+type+"]", true);
	return null;
}

function BBCmp3(form, field, type) {
	var URL = prompt("Enter the "+type+" file URL","http://");
	if (URL == null) { return null; }
	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 null; }
	if (!WS) { WS = 400; }
	var HS = prompt("Enter the "+type+" height", "20");
	if (HS == null) { return null; }
	if (!HS) { HS = 20; }
	BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+"]"+URL+"[/"+type+"]", true);
	return null;
}

function BBCmpl(form, field, type, WS, HS, LIC, BAC, FRC) {
	var URL = prompt("Enter the media file URL", "http://");
	if (URL == null) { return null; }
	if (!URL) { return alert("Error: You didn't write the media file URL"); }
	var WS = prompt("Enter the media width", ""+WS+"");
	if (WS == null) { return null; }
	if (!WS) { WS = 320; }
	var HS = prompt("Enter the media height", ""+HS+"");
	if (HS == null) { return null; }
	if (!HS) { HS = 260; }
	var LIC = prompt("Enter the lightcolor", ""+LIC+"");
	if (LIC == null) { return null; }
	if (!LIC) { LIC = CC9900; }
	var BAC = prompt("Enter the backcolor", ""+BAC+"");
	if (BAC == null) { return null; }
	if (!BAC) { BAC = 000000; }
	var FRC = prompt("Enter the frontcolor", ""+FRC+"");
	if (FRC == null) { return null; }
	if (!FRC) { FRC = CCCCCC; }
	var IMG = prompt("Enter the image URL", "http://");
	if (IMG == null) { return null; }
	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);
	return null;
}

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

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

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

function BBCsearch (form, field) {
	var STRING = prompt("Enter a value to search for");
	if (STRING == null) { return null; }
	if (!STRING) { return alert("Error: You didn't write the "+type+" file URL"); }
	BBCwrite(form, field, '', "[search=google]"+STRING+"[/search]", true);
	return null;
}

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);
		var 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;
}

function openclose(bid) {
   var bpe  = document.getElementById('pe'+bid);
   var bph  = document.getElementById('ph'+bid);
   if (bpe && bpe.style.display=="none") {
     if (bph) { bph.style.display="none"; }
     bpe.style.display="";
     hiddenblocks[bid] = null;
   } else {
     if (bph) { bph.style.display=""; }
     if (bpe) { bpe.style.display="none"; }
     hiddenblocks[bid] = bid;
   }
 }