function tartalmaz(adat,minta)
{
	for (var i=0; i<adat.length; i++)
		if (minta.indexOf(adat.charAt(i)) != -1) return true;
	return false;
}

function emailcheck(mezo)
{
	var szoveg = mezo.value;
	if (!(tartalmaz(szoveg,'@') && szoveg.indexOf('@')<szoveg.length-5 && szoveg.indexOf('@')>=2) || !(tartalmaz(szoveg,'.') && szoveg.indexOf('.')<szoveg.length-2))
	{
		return false;
	}
	else return true;
}

function tag_url(input, url) {
	url = url.value;
	if (url.length == 0) {
		alert('A link mező üres');
		return;
	}

	if (url.indexOf("http://") == 0) {
		url = url.substring (7);
		form.url.value = url;
	}

	doInsert(input, "[URL=http://" + url + "]", "[/URL]");
}


function tag_email(input, url) {
	if (url.value.length == 0) {
		alert('Nem töltötte ki az e-mail cím mezőti!');
		return;
	} else if (!emailcheck(url)) {
		alert('Hibás e-mail cím!');
		return;
	}
	doInsert(input, "[EMAIL=" + url.value + "]", "[/EMAIL]");
}

function tag_icon(input) {
	icon = document.forms[0].icon;
	if (icon.length) {
		for (var i=0; i < icon.length; i++)
		if (icon[i].checked) {
			doInsert(input, "[" + icon[i].value + "]", "");
		}
	}
}

function simpletag(input, thetag) {
	doInsert(input, "[" + thetag + "]", "[/" + thetag + "]");
}

function alterfont(input, theval, thetag) {
	doInsert(input, "[" + thetag + "=" + theval + "]", "[/" + thetag + "]");
	
	form = document.forms[0];
	form.fsize.selectedIndex  = 0;
	form.fcolor.selectedIndex = 0;
}


function setSelectionRange(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	} else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}
function setCaretToPos (input, pos) {
	setSelectionRange(input, pos, pos);
}
function doInsert(input, ibTag, ibClsTag) {
	if (input.setSelectionRange) {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		var originalString = input.value.substring(selectionStart, selectionEnd);
		input.value = input.value.substring(0, selectionStart) + ibTag + originalString + ibClsTag + input.value.substring(selectionEnd);
		if (selectionStart != selectionEnd) { // has there been a selection
			setSelectionRange(input, selectionStart, selectionStart + ibTag.length + originalString.length + ibClsTag.length);
		} else { // set caret
			setCaretToPos(input, selectionStart + ibTag.length);
		}
	} else if (document.selection) {
		input.focus();
		var range = document.selection.createRange();
		if (range.parentElement() == input) {
			var originalString = range.text;
			var isCollapsed = range.text == '';
			range.text = ibTag + originalString + ibClsTag;
			if (!isCollapsed)  { // there has been a selection
				//it appears range.select() should select the newly 
				//inserted text but that fails with IE
				range.moveStart('character', -ibTag.length-originalString.length-ibClsTag.length);
			} else {
				range.moveEnd('character', -ibClsTag.length);
			}
			range.select();
		}
	}
}

