// ========================
// ===== Admin funkciok
// ========================

// By this only one popup can be shown at once and it must be closed before other browser windows can be used again

var popup_window = null;

if (!document.all) window.captureEvents(Event.FOCUS);
window.onfocus = checkForPopups;

function checkForPopups() {
	if (popup_window)
		if (!popup_window.closed) {
			popup_window.focus();
		}

}

// Shows a popup window
function PopupWindow(file,w,h)
{
	popup_window = window.open(file,'cc_popup_a','width='+w+',height='+h+',scrollbars=1,left='+((screen.width/2)-(w/2))+',top='+((screen.height/2)-(h/2)));
	popup_window.focus();
	return false;
}

// Shows a popup for newsletter
function PopupWindowNL(file)
{
	w = 620;
	h = screen.height - 65;
	popup_window = window.open(file,'cc_popup_nl','width='+w+',height='+h+',scrollbars=1,left='+(screen.width - w - 10)+',top=0');
	popup_window.focus();
	return false;
}

// Shows a popup for Lost password
function popupLP(file,w,h) {
	var nyit = window.open(file,"tartalom",'width='+w+',height='+h+',left=20,top=40');
	nyit.focus();
}

// Sort function for admin
function sort(id, order)
{
	var f=document.forms[0];
	f.skip.value=id;
	f.orderby.value=order;
	f.submit();
}

// Jump function for admin
function jump(id)
{
	var f=document.forms[0];
	f.skip.value=id;
	f.submit();
}

// Pager function for admin
function lapozo(newpos)
{
	var f = document.forms[0];
	f.skip.value = newpos;
	f.submit();
}

// HTML format for textarea
function formatStr(tagName)
{
	if (!document.selection) return;
	if (null == document.selection.type || "None" == document.selection.type) return;
	var str = document.selection.createRange().text;
	document.selection.createRange().text = '<' + tagName + '>' + str + '</' + tagName + '>';
}

// Inserts link in textarea
function insertLink(url_str)
{
	if (!document.selection) return;
	if (null == document.selection.type || "None" == document.selection.type) return;
	var str = document.selection.createRange().text;
	var url = prompt('Hivatkozás megadása:', 'http://');
	if (url != null)
	{
		if(url_str == '' || ((url.substr(0, 5) == 'http:') && (url.substr(0, url_str.length) != url_str))) target = 'target="_blank"';
		else target = '';

		document.selection.createRange().text = '<a href="' + url + '" ' + target + '>' + str + '</a>';
	}
}

// Inserts email in textarea
function insertEmail()
{
	if (!document.selection) return;
	if (null == document.selection.type || "None" == document.selection.type) return;
	var str = document.selection.createRange().text;
	var email = prompt('E-mail cím megadása:', '');
	if (email != null)
	{
		document.selection.createRange().text = '<a href="mailto:' + email + '">' + str + '</a>';
	}
}

// Inserts list in textarea
function insertList()
{
	if (!document.selection) return;
	if (null == document.selection.type || "None" == document.selection.type) return;
	var str = document.selection.createRange().text;
	document.selection.createRange().text = '<ul style="list-style-image:url(i/li.gif)" class="lista">\n<li>' + str.replace(/\n/g,'<li>') + '\n</ul>';
}

// Sets highlighing in textarea
function setHighlight()
{
	if (!document.selection) return;
	if (null == document.selection.type || "None" == document.selection.type) return;
	var str = document.selection.createRange().text;
	document.selection.createRange().text = '<span style="background-color: #9E0B0E; color: #FFFFFF;">' + str + '</span>';
}

// By these picture tags are inserted into textarea
function insertAtCaret(textEl, text)
{
	var str = document.selection.createRange().text;
	document.selection.createRange().text = text;
}

function replaceAtCaret(textEl, text)
{
	if (textEl.caretPos) {
		if(textEl.createTextRange)
		{
			var caretPos = textEl.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? text + '' : text;
		} else textEl.value  = text;
	}
}

// Inserts BID tag in textarea
function insertBlock()
{
	if (!document.selection) return;
	if (null == document.selection.type || "None" == document.selection.type) return;

//	var text = prompt('Blokk címe:', '');
//	if (text != null) insertAtCaret(document.forms[0].elements['body'], '\n\n<bid>'+text+'</bid>\n\n');
	var text = document.selection.createRange().text;
	document.selection.createRange().text = '<bid>'+text+'</bid>' ;
}

// Functions for the restricted workflow editor
function SetValue(obj)
{
	if(obj.checked)
	{
		document.getElementById('none').checked = false;
	} else
	{
		document.getElementById('all').checked = false;
	}
}

function SetNone(obj)
{
	if(obj.checked)
	{
		var elemek = document.getElementsByName('userchk');
		for(var i = 0; i < elemek.length; i++)
		{
			elemek[i].checked = false;
		}
	}
}

function SetAll(obj)
{
	if(obj.checked)
	{
		var elemek = document.getElementsByName('userchk');
		for(var i = 0; i < elemek.length; i++)
		{
			elemek[i].checked = true;
		}
	}
}

// Defines body picture alignments in textarea
aalign = new Array();
aalign[0] = "bal";
aalign[1] = "jobb";
aalign[2] = "kozep";

// Value checker
function ert_ell(_val, _type, _length)
{
	switch (_type.toLowerCase())
	{
		case "szam":
			x = parseInt(_val, 10);
			if (!isNaN(x))
			{
				if (x == _val) return true;
			}
			return false;
			break;
		case "egyszam":
			zz = _val;
			for (x = 0;x < zz.length;x++) if (!isNaN(parseInt(zz.charAt(x)))) return true;
			return false;
			break;
		case "egybetu":
			zz = _val;
			for (x = 0;x < zz.length;x++) if (isNaN(parseInt(zz.charAt(x)))) return true;
			return false;
			break;
		case "minhossz":
			x = "_" + _val;
			if (x.length >= _length + 1) return true;
			return false;
			break;
		case "maxhossz":
			x = "_" + _val;
			if (x.length <= _length + 1) return true;
			return false;
			break;
		case "emailcim":
			var kukac = _val.indexOf('@');
			if (kukac < 1) return false;
			if (_val.length < kukac+3) return false;
			var vege = _val.substr(kukac+1);
			var vanbenne = false;
			if (kukac+1 == _val.length) return false;
			for (i=kukac+1;i<_val.length;i++)
				if (_val.charAt(i) == '.')
					vanbenne = i;
			if (!vanbenne) return false;
			if (_val.length-vanbenne < 3) return false;
			return true;
			break;
	}
}
