var ie = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var ie7 = ie?(parseInt(navigator.userAgent.toLowerCase().match(/msie (\d+)/)[1], 10) >= 7):false;
var webkit = navigator.userAgent.toLowerCase().indexOf('applewebkit') > -1;
var ie4 = document.all;
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var opera = !!window.opera;

function OpenDialogBrowser(url, width, height, fck_c, fck_w)
{
	var box = new DialogBox();
	box.box_url = url;
	box.FCKConfig = fck_c;
	box.callerWindow = fck_w;
	box.show('OpenDialogBrowser', width, height);
	return box;
}
function GetImage(id, type, typeid)
{
	var extra = "";
	if (typeid > 0) extra = "&editortypeid="+typeid;
	
	var box = OpenDialogBrowser(URL_DIRECT+'piclist.php?id='+id+'&type=1&editortype='+type+extra, screen.width, screen.height);
	box.layer_box.contentWindow.ImageDestination = document.getElementById(id);
}
function GetDocument(id)
{
	var box = OpenDialogBrowser(URL_DIRECT+'linklist.php?id='+id+'&type=5', screen.width, screen.height);
	box.layer_box.contentWindow.HrefDestination = document.getElementById(id);
}
function GetChapta(imgid)
{
	d = document.getElementById(imgid);
	if (!d) return;
	
	d.src = '';
	d.src = URL_DIRECT + 'chapta.php?rand=' + parseInt(Math.random()*99999999);
}
function SetPreview(pID, tID)
{
	p=document.getElementById(pID);
	t=document.getElementById(tID);
	if (!t) return;
	if (!p) return;
	
	src = t.value;
	
	if (src.length>0) {
		src = src.replace(new RegExp("^"+URL_IMAGE_BIG,"gi"), "");
		src = src.replace(new RegExp("^"+URL_IMAGE_SMALL,"gi"), "");
		src = src.replace(new RegExp("^"+URL_IMAGE,"gi"), "");
		src = src.replace(new RegExp("^"+URL_ICON,"gi"), "");
		t.value = src;
		
		p.innerHTML='<img src="'+URL_IMAGE_SMALL+src+'>';
	}
	else p.innerHTML='';
}

function GetX(obj)
{
	if (ie) return obj.offsetLeft;
	var x = 0;
	do {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	} while (obj);
	return x;
}
function GetY(obj)
{
	if (ie) return obj.offsetTop;
	var y = 0;
	do {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	} while (obj);
	return y;
}

var array_select_depend_field = new Array();

// A fuggo select-ek inicializalasa
function init_select_depend_field(select_id)
{
	var select = document.getElementById(select_id);
	if (!select) return;
	var depend_id = select.getAttribute("depend");
	if (!depend_id) return;
	var depend = document.getElementById(depend_id);
	if (!depend) return;
	
	array_select_depend_field[select_id] = new Array();
	var selected_value = "";
	
	for (i=0; i<select.length; i++) {
		dv = select.item(i).getAttribute("depend_value");
		if (!dv) continue;
		
		if (typeof(array_select_depend_field[select_id][dv]) == "undefined") array_select_depend_field[select_id][dv] = new Array();
		array_select_depend_field[select_id][dv].push(new Array(select.item(i).value, select.item(i).innerHTML));
		
		if (select.item(i).selected) selected_value = select.item(i).value;
		select.remove(i);
		i--;
	}
	
	i=0;
	do {
		i++;
		depended_id = depend.getAttribute("depended"+i);
	} while (depended_id != null);
	
	depend.setAttribute("depended"+i, select_id);
	
	depend.onchange = change_select_depend_field;
	
	depend.onchange();
	
	if (selected_value.length > 0) for (i=0; i<select.length; i++) if (selected_value == select.item(i).value) select.item(i).selected = true;
}
function change_select_depend_field()
{
	var i=0;
	do {
		i++;
		depended_id = this.getAttribute("depended"+i);
		if (depended_id) change_select_depended_field(depended_id);
	} while (depended_id != null);
}
function change_select_depended_field(select_id)
{
	select = document.getElementById(select_id);
	if (!select) return;
	depend_id = select.getAttribute("depend");
	if (!depend_id) return;
	depend = document.getElementById(depend_id);
	if (!depend) return;
	
	depend_value = depend.value;
	
	var i;
	
	for (i=0; i<select.length; i++) {
		dv = select.item(i).getAttribute("depend_value");
		if (!dv) continue;
		select.remove(i);
		i--;
	}
	
	if (typeof(array_select_depend_field[select_id][depend_value]) != "undefined") for (i=0; i<array_select_depend_field[select_id][depend_value].length; i++) {
		opt = add_option(select, array_select_depend_field[select_id][depend_value][i][0], array_select_depend_field[select_id][depend_value][i][1]);
		opt.setAttribute("depend_value", depend_value);
	}
}

// Add optiongroup item to a select input tag
function add_optiongroup(select, label)
{
	opt = document.createElement("OPTGROUP");
	opt.label = label;
	
	try {
		select.add(opt, null);
	}
	catch (e) {
		try {
			select.add(opt);
		}
		catch (e) {
			select.appendChild(opt);
		}
	}
	
	return opt;
}

// Add option item to a select input tag
function add_option(select, value, text)
{
	opt = document.createElement("OPTION");
	opt.value = value;
	opt.innerHTML = text;
	
	try {
		select.add(opt, null);
	}
	catch (e) {
		try {
			select.add(opt);
		}
		catch (e) {
			select.appendChild(opt);
		}
	}
	
	return opt;
}

/*
	You can choose elements from a select tag and move to an other based on an array as valuelist and a hidden tag as the selected items
	srcArray: this is a list
		[]['id'] : item id
		[]['path'] : path of ids in the tree
		[]['title'] : item title
	lstChoosen: this is the destination select tag
	lstChooseable: this is the source select tag
	dstHidden: this is the return hidden text tag
*/
function InitCategory(lstChoosen,lstChooseable,dstHidden,srcArray)
{
	lst1 = document.getElementById(lstChoosen);
	lst2 = document.getElementById(lstChooseable);
	txt = document.getElementById(dstHidden);
	
	if (lst1 == null) return;
	if (lst2 == null) return;
	if (txt == null) return;
	
	while (lst1.length) lst1.remove(0);
	while (lst2.length) lst2.remove(0);
	
	ids = txt.value.split(",");
	
	prevpath1 = new Array();
	prevpath2 = new Array();
	for (i in srcArray) {
		if (typeof(srcArray[i]) == 'function') continue;
		e = false;
		for (j in ids) {
			if (typeof(ids[j]) == 'function') continue;
			if (srcArray[i]["id"] == ids[j]) {
				e = true;
				break;
			}
		}
		
		path = srcArray[i]["path"].split("/");
		title = srcArray[i]["title"].split(" / ");
		t = "";
		deep = 0;
		
		if (e) {
			for (j in prevpath1) {
				if (typeof(prevpath1[j]) == 'function') continue;
				if (path.length <= j) break;
				if (prevpath1[j] == path[j]) {
					t = t + "&nbsp;&nbsp;&nbsp;&nbsp;";
					deep++;
				}
				else break;
			}
		}
		else {
			for (j in prevpath2) {
				if (typeof(prevpath2[j]) == 'function') continue;
				if (path.length <= j) break;
				if (prevpath2[j] == path[j]) {
					t = t + "&nbsp;&nbsp;&nbsp;&nbsp;";
					deep++;
				}
				else break;
			}
		}
		
		k = 0;
		for (j in title) {
			if (typeof(title[j]) == 'function') continue;
			if (j >= deep) {
				if (k) t = t + " / ";
				t = t + title[j];
				k++;
			}
		}
		
		if (e) {
			add_option(lst1, srcArray[i]["id"], t);
			prevpath1 = path;
		}
		else {
			add_option(lst2, srcArray[i]["id"], t);
			prevpath2 = path;
		}
	}
}
function AddCategory(lstChoosen,lstChooseable,dstHidden,srcArray)
{
	lst2 = document.getElementById(lstChooseable);
	txt = document.getElementById(dstHidden);
	
	if (lst2 == null) return;
	if (txt == null) return;
	
	if (txt.value.length) ids = txt.value.split(",");
	else ids = new Array();
	
	for (i=0; i<lst2.length; i++) if (lst2.item(i).selected) {
		e = false;
		for (j in ids) {
			if (typeof(ids[j]) == 'function') continue;
			if (ids[j] == lst2.item(i).value) {
				e = true;
				break;
			}
		}
		if (!e) ids.push(lst2.item(i).value);
	}
	
	txt.value = ids.join(",");
	
	InitCategory(lstChoosen,lstChooseable,dstHidden,srcArray);
}
function RemoveCategory(lstChoosen,lstChooseable,dstHidden,srcArray)
{
	lst1 = document.getElementById(lstChoosen);
	txt = document.getElementById(dstHidden);
	
	if (lst1 == null) return;
	if (txt == null) return;
	
	if (txt.value.length) ids = txt.value.split(",");
	else ids = new Array();
	
	for (i=0; i<lst1.length; i++) if (lst1.item(i).selected) {
		for (j in ids) {
			if (typeof(ids[j]) == 'function') continue;
			if (ids[j] == lst1.item(i).value) ids.splice(j,1);
		}
	}
	
	txt.value = ids.join(",");
	
	InitCategory(lstChoosen,lstChooseable,dstHidden,srcArray);
}

function InitCategoryWide(lstChoosen,lstChooseable,dstHidden,srcArray)
{
	var lst1 = document.getElementById(lstChoosen);
	var lst2 = document.getElementById(lstChooseable);
	var txt = document.getElementById(dstHidden);
	
	if (lst1 == null) return;
	if (lst2 == null) return;
	if (txt == null) return;
	
	while (lst1.length) lst1.remove(0);
	while (lst2.length) lst2.remove(0);
	
	var i, t = ','+txt.value+',';
	
	for (i in srcArray) {
		if (typeof(srcArray[i]) == 'function') continue;
		
		if (t.search(','+i+',') < 0) add_option(lst2, i, srcArray[i]);
		else add_option(lst1, i, srcArray[i]);
	}
}
function AddCategoryWide(lstChoosen,lstChooseable,dstHidden,srcArray)
{
	var lst2 = document.getElementById(lstChooseable);
	var txt = document.getElementById(dstHidden);
	
	if (lst2 == null) return;
	if (txt == null) return;
	
	if (txt.value.length) ids = txt.value.split(",");
	else ids = new Array();
	
	var i, t = ','+txt.value+',';
	
	for (i=0; i<lst2.length; i++) if (lst2.item(i).selected) {
		if (t.search(','+lst2.item(i).value+',') < 0) ids.push(lst2.item(i).value);
	}
	
	txt.value = ids.join(",");
	
	InitCategoryWide(lstChoosen,lstChooseable,dstHidden,srcArray);
}
function RemoveCategoryWide(lstChoosen,lstChooseable,dstHidden,srcArray)
{
	var lst1 = document.getElementById(lstChoosen);
	var txt = document.getElementById(dstHidden);
	
	if (lst1 == null) return;
	if (txt == null) return;
	
	var i, ids;
	
	if (txt.value.length) ids = txt.value.split(",");
	else ids = new Array();
	
	for (i=0; i<lst1.length; i++) if (lst1.item(i).selected) {
		for (j in ids) {
			if (typeof(ids[j]) == 'function') continue;
			if (ids[j] == lst1.item(i).value) ids.splice(j,1);
		}
	}
	
	txt.value = ids.join(",");
	
	InitCategoryWide(lstChoosen,lstChooseable,dstHidden,srcArray);
} 

function GenerateURL(fromName, toName)
{
	var cim = document.getElementById(fromName);
	if (!cim) return;
	GenerateObjURL(cim, toName);
}
function GenerateObjURL(fromObj, toName)
{
	var url = document.getElementById(toName);
	if (!url) return;
	if (url.value.length > 0) return;
	if (fromObj.value.length == 0) return;
	src = fromObj.value;
	var swap = new Array("áéíóöõúüûÁÉÍÓÖÕÚÜÛ", "aeiooouuuaeiooouuu");
	for (var i=0; i<swap[0].length; i++) src = src.replace(new RegExp(swap[0].substr(i,1), "g"), swap[1].substr(i,1));
	var dst = src.replace(new RegExp("([^[a-zA-Z0-9\-]]*)", "gi"), "-");
	dst = dst.replace(new RegExp("[\-]+", "g"), "-");
	url.value = dst.toLowerCase();
}
function GenerateField(f)
{
	src = f.value;
	var swap = new Array("áéíóöõúüûÁÉÍÓÖÕÚÜÛ", "aeiooouuuaeiooouuu");
	for (var i=0; i<swap[0].length; i++) src = src.replace(new RegExp(swap[0].substr(i,1), "g"), swap[1].substr(i,1));
	var dst = src.replace(new RegExp("([^[a-zA-Z0-9\-\_]]*)", "gi"), "-");
	dst = dst.replace(new RegExp("\-\-", "g"), "-");
	f.value = dst.toLowerCase();
}

function CapitalizeWords(string)
{
	var tmpStr, tmpChar, preString, postString, strlen;
	tmpStr = string;
	stringLen = tmpStr.length;
	if (stringLen == 0) return string;
	
	for (i = 0; i < stringLen; i++) {
		if (i == 0)
		{
			tmpChar = tmpStr.substring(0,1).toUpperCase();
			postString = tmpStr.substring(1,stringLen);
			tmpStr = tmpChar + postString;
		}
		else
		{
			tmpChar = tmpStr.substring(i,i+1);
			if (tmpChar == " " && i < (stringLen-1))
			{
				tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
				preString = tmpStr.substring(0,i+1);
				postString = tmpStr.substring(i+2,stringLen);
				tmpStr = preString + tmpChar + postString;
			}
		}
	}
	return tmpStr;
}

function FCKeditor_OnComplete(editorInstance)
{
	editorInstance.Events.AttachEvent('OnAfterLinkedFieldUpdate', FCKeditor_AfterLinkedFieldUpdate);
}
function FCKeditor_AfterLinkedFieldUpdate(editorInstance)
{
	var obj = document.getElementById(editorInstance.Name);
	
	var re, html = obj.value;
	
	re = new RegExp("\\[\\?tpl(([^\\?]|\\?[^\\]])*)\\?\\]","gi");
	html = html.replace(re, "<?tpl$1?>");
	re = new RegExp("<\\?tpl(([^\\?]|\\?[^>])*)&lt;(([^\\?]|\\?[^>])*)\\?>","gi");
	html = html.replace(re, "<?tpl$1<$3?>");
	re = new RegExp("<\\?tpl(([^\\?]|\\?[^>])*)&gt;(([^\\?]|\\?[^>])*)\\?>","gi");
	html = html.replace(re, "<?tpl$1>$3?>");
	re = new RegExp("<\\?tpl(([^\\?]|\\?[^>])*)&amp;(([^\\?]|\\?[^>])*)\\?>","gi");
	html = html.replace(re, "<?tpl$1&$3?>");
	re = new RegExp("<\\?tpl(([^\\?]|\\?[^>])*)&quot;(([^\\?]|\\?[^>])*)\\?>","gi");
	html = html.replace(re, "<?tpl$1\"$3?>");
	re = new RegExp("<\\?tpl(([^\\?]|\\?[^>])*)&nbsp;(([^\\?]|\\?[^>])*)\\?>","gi");
	html = html.replace(re, "<?tpl$1 $3?>");
	
	re = new RegExp("(<IMG[^>]*src=\")("+URL_MEDIA_DOCUMENT+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_DOCUMENT}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_MEDIA_MOVIE+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_MOVIE}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_MEDIA_AUDIO+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_AUDIO}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_MEDIA_BANNER+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_BANNER}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_MEDIA_FLASH+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_FLASH}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_MEDIA+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_IMAGE_BIG+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_IMAGE_BIG}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_IMAGE_SMALL+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_IMAGE_SMALL}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_IMAGE+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_IMAGE}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_ICON+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_ICON}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+URL_PREFIX+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_PREFIX}$3");
	re = new RegExp("(<IMG[^>]*src=\")("+PATH_ABSOLUTE+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_PREFIX}$3");
	
	re = new RegExp("(<A [^>]*href=\")("+URL_MEDIA_DOCUMENT+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_DOCUMENT}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_MEDIA_MOVIE+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_MOVIE}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_MEDIA_AUDIO+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_AUDIO}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_MEDIA_BANNER+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_BANNER}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_MEDIA_FLASH+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA_FLASH}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_MEDIA+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_MEDIA}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_IMAGE_BIG+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_IMAGE_BIG}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_IMAGE_SMALL+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_IMAGE_SMALL}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_IMAGE+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_IMAGE}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_ICON+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_ICON}$3");
	re = new RegExp("(<A [^>]*href=\")("+URL_PREFIX+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_PREFIX}$3");
	re = new RegExp("(<A [^>]*href=\")("+PATH_ABSOLUTE+")([^\"]*\"[^>]*>)","gi");
	html = html.replace(re, "$1{URL_PREFIX}$3");
	
	var frm = obj.form;
	if (frm == null) return true;
	
	if (typeof(frm[editorInstance.Name+"_converted"]) == "undefined") {
		tmp = document.createElement('input');
		re = new RegExp("^([^\\[]*)(\\[[^\\]]*\\])?$","gi");
		tmp.name = editorInstance.Name.replace(re, "$1_converted$2");
		tmp.type = "hidden";
		tmp.value = "";
		frm.appendChild(tmp);
		frm[editorInstance.Name+"_converted"] = tmp;
	}
	convobj = frm[editorInstance.Name+"_converted"];
	convobj.value = html;
	
	return true;
}

function putDebug(obj)
{
	var i, e, x;
	var s = '<table>';
	
	for (i in obj) {
		s += '<tr><td>'+i+'</td><td>';
		try { s += obj[i]; } catch (e) {};
		s += '</td></tr>';
	}
	
	s += '</table>';
	
	x = window.open();
	x.document.open();
	x.document.write(s);
	x.document.close();
}
