var win=null;

function popup(url,titel,w,h,pos,infocus){
	if(pos=="random"){
		myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
		myleft=(screen.width)?(screen.width-w)/2:100;
		mytop=(screen.height)?(screen.height-h)/2:100;
	}else if((pos!='center' && pos!="random") || pos==null){
		myleft=0;
		mytop=20
	}
	settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + (myleft+250) + ",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
	win=window.open(url,titel,settings);
	win.focus();
}

// CODEBUTTONS!!

function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == 0) {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
	
}
function add_smiley(code,form,textje)
{
	if(code=='stop'){
		code='<stop>';
	}
	insertAtCursor(textje, code);
}
function dosmiley(code,textje)
{
	el = textje;
	el.value += ' '+code+' ';
	el.focus();
	/*window.close();*/
}
var myTags = new Array();
var tagstring = new Array();
tagstring['url'] = 'de url';
tagstring['iurl'] = 'de inline url';
tagstring['img'] = 'de link van de afbeelding';
tagstring['email'] = 'het emailadres';
tagstring['youtube'] = 'de link naar het Youtube fimpje';
function add_bbcode(element, tag, form, textje)
{
	el = textje; //Aantonen aan FF dat het over deze tekst gaat.
	noselection = false; //standaard is er tekst geselecteerd
	if(el.setSelectionRange)
	{ //FF
		selectedText = el.value.substring(el.selectionStart,el.selectionEnd);
		if(selectedText != "") //indien geen tekst geselecteerd
		{	var newText;
			switch(tag)
			{
				case "url":
					var the_url = window.prompt('Geef de URL:', 'http://');
					newText = "[url="+the_url+"]"+selectedText+"[/url]";
					break;
				case "youtube":
					var the_url = window.prompt('Geef de URL:', 'http://youtube.com/watch?v=XXXXXXXX');
					if(the_url!=null)
						newText = "[youtube]"+the_url+"[/youtube]";
					break;
				case "iurl":
					var the_iurl = window.prompt('Geef de inline URL:', 'http://');
					newText = "[iurl="+the_iurl+"]"+selectedText+"[/iurl]";
					break;
				case "color":
					var the_color = element;
					if(the_color == "kleur") the_color = window.prompt('Geef een kleur in hexadecimale code (bv: #A26BF3):', '#');
					newText = "[color="+the_color+"]"+selectedText+"[/color]";
					break;
				default:
					newText =  "[" + tag + "]" + selectedText + "[/" + tag + "]";
					break;
			}
			el.value = el.value.substring(0,el.selectionStart) + newText + el.value.substring(el.selectionEnd,el.value.length);
		}
		else
		{
			noselection = true;
		}
	}
	else
	{//IE
		var selectedText = document.selection.createRange().textje; //Aantonen aan IE dat het over deze tekst gaat

		if (selectedText != "")
		{ //indien tekst geselecteerd
			var newText;
			switch(tag)
			{
				case "url":
					var the_url = window.prompt('Geef de URL:', 'http://');
					newText = "[url="+the_url+"]"+selectedText+"[/url]";
					break;
				case "youtube":
					var the_url = window.prompt('Geef de URL:', 'http://youtube.com/watch?v=XXXXXXXX');
					if(the_url!=null)
						newText = "[youtube]"+the_url+"[/youtube]";
					break;
				case "iurl":
					var the_iurl = window.prompt('Geef de inline URL:', 'http://');
					newText = "[iurl="+the_iurl+"]"+selectedText+"[/iurl]";
					break;
				case "color":
					var the_color = element;
					if(the_color == "#") the_color = window.prompt('Geef een kleur in hexadecimale code (bv: #A26BF3):', '#');
					newText = "[color="+the_color+"]"+selectedText+"[/color]";
					break;
				default:
					newText =  "[" + tag + "]" + selectedText + "[/" + tag + "]";
					break;
			}
			
			document.selection.createRange().text = newText;
		}
		else
		{
			noselection = true;
		}
	}
	if(noselection)	//indien geen tekst geselecteerd
	{
		if(!inArray(tag, myTags))
		{
			if(tag=='url'||tag=='img'||tag=='email'||tag=='iurl'||tag=='youtube'){
				tagprompt = prompt('Vul hier '+tagstring[tag]+' in:');
				if(tagprompt!='' && tagprompt!=null){
					insertAtCursor(el, '['+tag+']'+tagprompt+'[/'+tag+']');		//el.value += '['+tag+']'+tagprompt+'[/'+tag+']';
				}
			}else{
				if(tag == "color") insertAtCursor(el, '[color='+element+'][/color]'); //el.value += '[color='+element+'][/color]';
				else insertAtCursor(el, '['+tag+']'); // el.value += '['+tag+']';
				if(tag != "color") arrayPush(myTags, tag);
			}
		}else{
			insertAtCursor(el, '[/'+tag+']'); //el.value += '[/'+tag+']';
			removeItems(myTags, tag);
		}
	}
	//}
	el.focus();
}

function newSelectedText(tag, selectedText)
{
	var newText;
	switch(tag)
	{
		case "url":
			var the_url = window.prompt('Geef de URL:', 'http://');
			newText = "[url="+the_url+"]"+selectedText+"[/url]";
			break;
		case "iurl":
			var the_iurl = window.prompt('Geef de inline URL:', 'http://');
			newText = "[iurl="+the_iurl+"]"+selectedText+"[/iurl]";
			break;
		case "color":
			var the_color = document.newreply.color.value;
			if(the_color == "#") the_color = window.prompt('Geef een kleur in hexadecimale code (bv: #A26BF3):', '#');
			newText = "[color="+the_color+"]"+selectedText+"[/color]";
			break;
		default:
			newText =  "[" + tag + "]" + selectedText + "[/" + tag + "]";
			break;
	}
	return newText;
}

function garraySize(arrayName) {
	for (i=0;i<arrayName.length;i++) {
		if((arrayName[i] == "undefined") || (arrayName[i] == "") || (arrayName[i] == null)) {
			return i;
		}
	}
	return arrayName.length;
}

function arrayPush(arrayName, arrayValue) {
	arraySize = garraySize(arrayName);
	arrayName[arraySize] = arrayValue;
}

function inArray(item, arrayName) {
	for(i=0;i<arrayName.length;i++) {
		if(arrayName[i] == item) {
			return true;
		}
	}
	return false;
}

function removeItems(array, item)
{
	var i = 0;
	while (i < array.length)
	{
		if (array[i] == item)
		{
			array.splice(i, 1);
		}
		else
		{
			i++;
		}
	}
	return array;
}

function closeBBTags(textje)
{
	for(i=myTags.length-1;i>=0;i--) //ZODAT DE TAGS IN DE JUISTE VOLGORDE GESLOTEN WORDEN
	{
		textje.value += '[/'+myTags[i]+']';
		document.getElementById('BB_'+myTags[i]).style.border = null;
		document.getElementById('BB_'+myTags[i]).style.margin = null;
	}
	myTags = Array();
}