/* BBcode engine */

function addtag(tag,single) {
	var txt = document.getElementById('mta');
	if(document.selection) {
		txt.focus();
		sel = document.selection.createRange();
		if(single == true) {
			sel.text = '[' + tag + ']';
		} else {
		    sel.text = '[' + tag + ']' + sel.text + '[/' + tag + ']';
		}
	} else if(txt.selectionStart || txt.selectionStart == '0') {
		if(single == true) {
			txt.value = (txt.value).substring(0, txt.selectionStart) + "["+tag+"]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
		} else {
			txt.value = (txt.value).substring(0, txt.selectionStart) + "["+tag+"]" + (txt.value).substring(txt.selectionStart, txt.selectionEnd) + "[/"+tag+"]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
		}
	} else {
		if(single) {
			txt.value = '[' + tag + ']';
		} else {
			txt.value = '[' + tag + '][/' + tag + ']';
		}
	}
	return;
}

function addsmile(tag) {
	var txt = document.getElementById('mta');
	txt.value = (txt.value).substring(0, txt.selectionStart) + tag + (txt.value).substring(txt.selectionEnd, txt.textLength);
	return;
}

function addurltag() {
	var txt = document.getElementById('mta');
	var link = prompt("Type the address:", "http://");
	if(link.length == 0 || link == "http://") {
		return;
	} else {
		var link = "=" + link;
		var text;
		var sel2 = "";
		if(document.selection) {
			txt.focus();
			sel = document.selection.createRange();
			sel2 = sel.text;
		} else if(txt.selectionStart || txt.selectionStart == '0') {
			sel2 = (txt.value).substring(txt.selectionStart, txt.selectionEnd);
		}
		if(sel2.length > 0) {
			text = sel2;
		} else {
			text = prompt("Enter the link text:", "");
		}
	}
	if(document.selection) {
		txt.focus();
		sel = document.selection.createRange();
		sel.text = "[url" + link + "]" + text + "[/url]";
	} else {
		txt.value = (txt.value).substring(0, txt.selectionStart) + "[url" + link + "]" + text + "[/url]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	}
	return;
}

function addthumbtag() {
	var txt = document.getElementById('mta');
	var link = prompt("A kép urlje:", "http://");
	if(link.length == 0 || link == "http://") {
		return;
	} else {
		var link = link;
		var text;
		var sel2 = "";
		if(document.selection) {
			txt.focus();
			sel = document.selection.createRange();
			sel2 = sel.text;
		} else if(txt.selectionStart || txt.selectionStart == '0') {
			sel2 = (txt.value).substring(txt.selectionStart, txt.selectionEnd);
		}
		if(sel2.length > 0) {
			text = sel2;
		} else {
			text = prompt("A thumbnail urlje:", "");
		}
	}
	if(document.selection) {
		txt.focus();
		sel = document.selection.createRange();
		sel.text = "[thumb:" + text + ";" + link + "]";
	} else {
		txt.value = (txt.value).substring(0, txt.selectionStart) + "[thumb:" + text + ";" + link + "]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	}
	return;
}

function addcurltag() {
	var txt = document.getElementById('mta');
	var link = prompt("A link:", "http://");
	if(link.length == 0 || link == "http://") {
		return;
	} else {
		var link = link;
		var text;
		var sel2 = "";
		if(document.selection) {
			txt.focus();
			sel = document.selection.createRange();
			sel2 = sel.text;
		} else if(txt.selectionStart || txt.selectionStart == '0') {
			sel2 = (txt.value).substring(txt.selectionStart, txt.selectionEnd);
		}
		if(sel2.length > 0) {
			text = sel2;
		} else {
			text = prompt("Szöveg:", "");
		}
	}
	if(document.selection) {
		txt.focus();
		sel = document.selection.createRange();
		sel.text = "[url:" + link + ";" + text + "]";
	} else {
		txt.value = (txt.value).substring(0, txt.selectionStart) + "[url:" + link + ";" + text + "]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	}
	return;
}


function addmaptag() {
	var txt = document.getElementById('mta');
	var zoneid = prompt("Zone ID:", "");
	if(zoneid.length == 0) {
		return;
	} else {
		var zoneid;
		var xcoord;
		var ycoord;
		xcoord = prompt("Az npc helyének x koordinátája:", "");
		ycoord = prompt("Az npc helyének y koordinátája", "");
	
	}
	txt.value = (txt.value).substring(0, txt.selectionStart) + "[npcmap:" + zoneid + ";" + xcoord + "," + ycoord + "]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	return;
}

function addzonetag() {
	var txt = document.getElementById('mta');
	var zoneid = prompt("Zone ID:", "");
	if(zoneid.length == 0) {
		return;
	}
	txt.value = (txt.value).substring(0, txt.selectionStart) + "[zonemap:" + zoneid + "]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	return;
}

function addnpctag() {
	var txt = document.getElementById('mta');
	var npcid = prompt("NPC ID:", "");
	if(npcid.length == 0) {
		return;
	}
	txt.value = (txt.value).substring(0, txt.selectionStart) + "[npc:" + npcid + "]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	return;
}

function additemtag() {
	var txt = document.getElementById('mta');
	var itemid = prompt("Item ID:", "");
	if(itemid.length == 0) {
		return;
	}
	txt.value = (txt.value).substring(0, txt.selectionStart) + "[item:" + itemid + "]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	return;
}


function addvaluetag(sValue,tag) {
	if(sValue=="") {
		return;
	}
	var txt = document.getElementById('mta');
	if(document.selection) {
		txt.focus();
		sel = document.selection.createRange();
		sel.text = '["+tag+"=' + sValue + ']' + sel.text + '[/"+tag+"]';
	} else if(txt.selectionStart || txt.selectionStart == '0') {	
		txt.value = (txt.value).substring(0, txt.selectionStart) + "["+tag+"="+sValue+"]" + (txt.value).substring(txt.selectionStart, txt.selectionEnd) + "[/"+tag+"]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
	} else {
		txt.value = '["+tag+"=' + sValue + '][/"+tag+"]';
	}
	return;
}
