//elkuldi az adott id-t
function submit_id(url,id,idnev){
	if(idnev==undefined) idnev="id";
	var submitForm = document.createElement("FORM");
	document.body.appendChild(submitForm);
	submitForm.method = "POST";
	var newElement = document.createElement("input");
	newElement.setAttribute("type", "hidden");
	newElement.setAttribute("name", idnev);
	newElement.setAttribute("value", id);
	submitForm.appendChild(newElement);
	submitForm.action= url;
	submitForm.submit();
}

//hozzaad a formhoz (1. paraméter) hidden mezoket
function add_hidden(/*form, mezo1, ertek1, stb.*/){
	if(arguments.length<1) return false;
	form=arguments[0];
	for(i=1; i+1<arguments.length; i+=2) {
		var newElement = document.createElement("input");
		newElement.setAttribute("type", "hidden");
		newElement.setAttribute("name", arguments[i]);
		newElement.setAttribute("value", arguments[i+1]);
		form.appendChild(newElement);
   }
}

function add_form(id){
	cucc=document.getElementById(id);
	myform=document.createElement("form");
	cucc.appendChild(myform); 
	return myform;
}

function add_textarea(form,text,name,cols,rows){
	var ta = document.createElement('textarea');
	ta.setAttribute("name",name);
	ta.setAttribute("id",name);
    ta.setAttribute("cols",cols);
    ta.setAttribute("rows",rows);
	form.appendChild(ta);
}

//hozzaad a formhoz (1. paraméter) hidden mezoket és elkuldi
function submit_form(){
	if(arguments.length<1) return false;
	form=arguments[0];
	add_hidden.apply(this, arguments);
	form.submit();
}

//checkboxokat kapcsol, form neve, es checkbox nevenek az eleje
function check_cb(form,nev){
	st=eval("window.checkbox"+nev);	//szamlalo, hogy check vagy uncheck jon, true az uncheck
	allcheck=false;
	alluncheck=false;
	if(st==true) {alluncheck=uncheckAll(form,nev); st=false;}
	else {allcheck=checkAll(form,nev); st=true;}
	if(allcheck) {uncheckAll(form,nev); st=false;}
	if(alluncheck) {checkAll(form,nev); st=true;}
	eval("window.checkbox"+nev+"="+st);
}

//eslo text input mezore allitja a kurzort/focust
function focusfirsttext(){
	arr = document.getElementsByTagName('input');
	for(var i=0; i < arr.length; i++)
	{  if(arr[i].type=='text') {arr[i].focus(); return;}
	}
}

//ajaxos oldal betolto cucc, url: pl index.php query: pl m=5&f=2
function loadContent(urlcim,query,elem,loading,succfn){
	if(window.hostroot===undefined) hostroot='';
	if(elem==undefined){
		var i=0;
		while ($('#loadasdfgh'+i).length>0) i++;
		$('body').append("<div id='loadasdfgh"+i+"' style='display: block;'></div>");
		elem='loadasdfgh'+i;
		contentLoaded=function(){$('#loadasdfgh'+i).remove();};
	} else if($('#'+elem).length==0) $('body').append("<div id='"+elem+"'></div>");
	$.ajax({type: "POST",url: hostroot+urlcim,cache: false,data: query, dataType: "html",
		beforeSend: function(){
			if(typeof contentUnloaded == 'function') contentUnloaded();
			contentUnloaded=function(){};
			if(loading==undefined) loading="loading";
			$("#"+elem).append("<div id='"+elem+loading+"' class='"+loading+"'>&nbsp;</div>");
		},
		complete: function(){
			$("#"+elem+loading).fadeOut(100);
			setTimeout("$('#"+elem+loading+"').remove();",200);
		},
		success: function(html){
			$("#"+elem).fadeIn(100);
			$("#"+elem).html(html);
			if(typeof contentLoaded == 'function') contentLoaded();
			if(typeof succfn == 'function') succfn();
			contentLoaded=function(){};
		}
	});
}

//eltavolit egy adott idju elemet
function item_remove(id){
	el = document.getElementById(id);
	el.parentNode.removeChild(el);
}
//php compatible urlencode
function urlencode(s) {
  s = encodeURIComponent(s);
  return s.replace(/~/g,'%7E').replace(/%20/g,'+');
 }
 
//oldal frissitese uj menupontnal, ha nincs id, akkor marad az oldal es a kotelezoen frissulok frissulnek. ret akkor false, ha nem false
function refreshPage(id,query,ret,all){
	if(id==false||id==undefined||id=='') id=menu;
	if(query==undefined) query='';
	if(all!==true) all=false;
	regionev=0;
	for(var r in regiok){
		rid=regiok[r].id;
		var modul1=[]; //elozo oldal moduljai, id=>id
		var modul2a=[]; //uj oldal moduljai, N=>id
		var modul2b=[]; //uj oldal moduljai, id=>modul obj
		//szukseges tombok felepitese
		for(var m in modulok[rid]){
			if((modulok[rid][m].menu==menu||modulok[rid][m].menu==0)&&rid==modulok[rid][m].regio&&tartalmak[rid][menu]==1) modul1[modulok[rid][m].id]=modulok[rid][m].id;	//elozo oldal
			if((modulok[rid][m].menu==id||modulok[rid][m].menu==0)&&rid==modulok[rid][m].regio&&tartalmak[rid][id]==1) {modul2a[modul2a.length]=modulok[rid][m].id; modul2b[modulok[rid][m].id]=modulok[rid][m];}	//uj oldal
		}
		if($('#'+regiok[r].nev).length==0&&modul2a.length>0) {	//letre kell hozni a regiot
			if(regionev==0)	$('#all').prepend('<div id="'+regiok[r].nev+'">&nbsp;</div>');//elso regio
			else	$('#'+regionev).after('<div id="'+regiok[r].nev+'">&nbsp;</div>');//regio letrehoz
		}
		if($('#'+regiok[r].nev).length>0&&modul2a.length==0) $('#'+regiok[r].nev).remove();//torolni a regiot
		for(var m in modul1){
			if(modul2b[m]==undefined||(modul2b[m]!=undefined&&(modul2b[m].frissit==1||all))) $('#modul'+m).remove();//modul torles
		}
		modulid=0;
		for(var m in modul2a){
			if(modul1[modul2a[m]]==undefined||(modul2b[modul2a[m]]!=undefined&&(modul2b[modul2a[m]].frissit==1||all))) {//modul letrehozas
				if(modulid==0) $('#'+regiok[r].nev).prepend('<div class="'+modul2b[modul2a[m]].modulclass+'" id="modul'+modul2a[m]+'"></div>');//elso modul
				else 	$('#modul'+modulid).after('<div class="'+modul2b[modul2a[m]].modulclass+'" id="modul'+modul2a[m]+'"></div>');//mar vannak modulok
				loadContent('modul.php','modulid='+modul2a[m]+'&menuid='+id+'&langid='+langid+'&userid='+userid+'&usernev='+usernev+query,'modul'+modul2a[m],'loading');//tartalom update
			}
			modulid=modul2a[m];
		}
		if($('#'+regiok[r].nev).length!=0) regionev=regiok[r].nev;
	}
	menu=id;
	if(ret!==false) return false;
}

//egy modul frissitese
function refreshModule(id,query){
		loadContent('modul.php','modulid='+id+'&menuid='+menu+'&langid='+langid+'&userid='+userid+'&usernev='+usernev+query,"modul"+id,'loading');
}

//validalas, mezok felvetele, tipus: =, <,>,!=,<=,>=, mezo id-je, ertek, mezo neve
function addValidate(){
	if(arguments.length<4) return false;
	if(window.validate==undefined) window.validate=[];
	var tipus=arguments[0];
	for(i=1; i+2<arguments.length; i+=3) {
		window.validate.push({"tipus":tipus, "mezo":arguments[i], "ertek":arguments[i+1], "nev":arguments[i+2]});
   }
}
//validalas, csak a != és email van meg
function doValidate(){
	if(window.validate==undefined||typeof szotar=='undefined') return true;
	var ok=true;
	msg="";
	for (i = 0; i < window.validate.length; i++){
		if(window.validate[i].tipus=="=") {ok=($("[name='"+window.validate[i].mezo+"']").val()==$("[name='"+window.validate[i].ertek+"']").val()); msg=szotar.jsnemegyenlo.replace("%n",window.validate[i].nev);}
		else if(window.validate[i].tipus=="!=") {ok=($("[name='"+window.validate[i].mezo+"']").val()!=window.validate[i].ertek); msg=szotar.jsuresmezo.replace("%n",window.validate[i].nev);}
		else if(window.validate[i].tipus=="<") ok=($("[name='"+window.validate[i].mezo+"']").val()<window.validate[i].ertek);
		else if(window.validate[i].tipus==">") ok=($("[name='"+window.validate[i].mezo+"']").val()>window.validate[i].ertek);
		else if(window.validate[i].tipus=="<=") ok=($("[name='"+window.validate[i].mezo+"']").val()<=window.validate[i].ertek);
		else if(window.validate[i].tipus==">=") ok=($("[name='"+window.validate[i].mezo+"']").val()>=window.validate[i].ertek);
		else if(window.validate[i].tipus=="email") {ok=($("[name='"+window.validate[i].mezo+"']").val().search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!=-1); msg=szotar.jsrosszemail.replace("%n",window.validate[i].nev);}
		else if(window.validate[i].tipus=="checked"){ok=$("[name='"+window.validate[i].mezo+"']").is(':checked'); msg=szotar.jsnincspipa.replace("%n",window.validate[i].nev);}
		if(!ok) {alert(msg); return false;}
	}
	window.validate=undefined;
	return ok;
}

//a body onload esemenye
function betoltes(){
}

function addBetoltes(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//felugro ablak
function imagePopUp(img) {
html = "<html><head><title>" + img + "</title>" +  '</head><body style="margin: 0px; text-align:center; cursor: pointer;">' +  "<img src='" + img + "'     name='image' " +  "onload='window.resizeTo(document.image.width,(document.image.height*1.3)); window.moveTo(screen.width/2-document.image.width/2,screen.height/2-document.image.height*0.65);' onclick='window.close();'>" +  "</body></html>";
popup=window.open('','image','toolbar=0,location=0,  directories=0,menuBar=0, scrollbars=0,resizable=1');
popup.document.open();
popup.document.write(html);
popup.document.focus();
popup.document.close();
};  

//betoltes
$(document).ready(function(){
	betoltes();
});


function addToFunc(funcname,code){
	if(eval("typeof "+funcname+" == 'undefined'")) eval(funcname+" = function(){};");
	eval(funcname+"=function(){"+eval(funcname+".toString();").substr(13).substr(0,eval(funcname+".toString();").length-15)+code+"}");
}

function moveoptions(name1,name2,hidden,add){
	var multiplevalues=$('[name='+name1+']').val()||[];
	for(var i in multiplevalues)
	{
		if(add) {mezoadd=hidden+multiplevalues[i]; mezodel=hidden+multiplevalues[i]+"_del";}
		else {mezoadd=hidden+multiplevalues[i]+"_del"; mezodel=hidden+multiplevalues[i];}
		$('[name=\''+name1+'\']').after('<input type=\'hidden\' name=\''+mezoadd+'\' class="hidden" value=\''+multiplevalues[i]+'\' />');
		$('[name=\''+mezodel+'\']').remove();
	}
	return !$('[name=\''+name1+'\'] option:selected').appendTo('[name=\''+name2+'\']');
}

jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i > 0; i--) {
		jQuery("<img>").attr("src", a[i]);
	}
}

$.maxZIndex = $.fn.maxZIndex = function(opt) {
    var def = { inc: 10, group: "*" };
    $.extend(def, opt);
    var zmax = 0;
    $(def.group).each(function() {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    if (!this.jquery)
        return zmax;

    return this.each(function() {
        zmax += def.inc;
        $(this).css("z-index", zmax);
    });
}
