        // altalanos funkciok

        // uj elemet fuz a tombhoz
        Array.prototype.push = function (element) {
            this[this.length] = element;
/*          for (var i=0; i<this.length; i++)
                log('after push: '+this[i].url);*/

            return this.length;
        };


        //Legelso elemet torli a tombbol es azzal ter vissza
        Array.prototype.fpop = function () {
            var ret=this[0];
            for (var i=0; i<this.length-1; i++)
              this[i] = this[i+1];
            this.length--;
            return ret;
        };


        //Legelore szur be egy elemet
        Array.prototype.fpush = function (element) {
            for (var i=this.length; i>0; i--)
              this[i] = this[i-1];
            this[0]=element;
        };

        //adott elemet torol
        Array.prototype.remove = function (element) {
/*          for (var i=0; i<this.length; i++)
                log('--- before remove: '+this[i].url);
*/
            for (var i=0; i<this.length; i++)
                if (this[i]===element) {
                        //log('removing '+this[i].url);
                    for (var j=i; j<this.length; j++)
                      this[j] = this[j+1];
                    this.length--;
                    break;
                }
/*          for (var i=0; i<this.length; i++)
                log('after remove: '+this[i].url);*/
        };


        function log(e) {
                window.status=e;                        
                var l=getobjectbyid('log');
                if (l) {l.innerText+="\n"+e;}
        }

        // adott tagnevu gyerek tag objektumat vagy szoveget adja vissza
        function getChildTag(obj, tag, returntype) {
                for (var i=0; i<obj.childNodes.length; i++) {
                        var childnode = obj.childNodes[i];
                        var childtagname = childnode.tagName;
                        if ((childtagname)&&(childtagname.toLowerCase()==tag.toLowerCase())) {
                                //alert(childnode.tagName.toLowerCase+' '+tag.toLowerCase);
                                
                                if (returntype=='text') {
                                        if (childnode.childNodes.length > 1) {                                  
                                            return childnode.childNodes[1].nodeValue;
                                        } else {
                                            if (childnode.firstChild) {
                                                    return childnode.firstChild.nodeValue;              
                                            } else {
                                                   return childnode.nodeValue;
                                            }
                                        }
                                }
                                if (returntype=='object') {
                                        if (childnode.childNodes.length > 1) {
                                            return childnode.childNodes[1];
                                        } else {
                                            return childnode.firstChild;                
                                        }
                                }
                        }
                }
                return false;
        }

        //id szerinti gyerekkeresés
        function findchildbyid(obj, id)
        {
                if (obj) {
                        var childcount=obj.childNodes.length;           
                        for (var i=0; i<childcount; i++) 
                        {
                                var child=obj.childNodes[i];
                                if (child)
                                {                       
                                if ((child.id)&&(child.id.toLowerCase()==id)) { return child; break; }
                                var childs=findchildbyid(child, id);
                                if (childs) { return childs; break; }
                                }
                        }
                }
        }


// ***************
// tserverCallData
// ***************

function tserverCallData() {
        this.url='';
        this._ctl_req=null;
        this.callback=null;
        this.serverCallObj=null;
        this.preload=null;
        this.loaded=null;

        this._ctl_processReqChange=function(){
                //log('(scd '+this.url+' -> rs:'+this._ctl_req.readyState);
            if (this._ctl_req.readyState == 4) {
                //log('(scd '+this.url+' -> rs:'+this._ctl_req.readyState+' st:'+this._ctl_req.status);
                if (this._ctl_req.status == 200) {
                        //log('tserverCallData._ctl_processReqChange -> callback '+this.url);
                        this.callback(this);
                } else {
                        //log('(_ctl_processReqChange) error');
                        this.callback(this);
                }
            }
        }

        this.freeMemory=function(){
                this.url='';
                this._ctl_req=null;
                this.callback=null;
                //this.serverCallObj=null;
                this.preload=null;
        }
}


// ***********
// tserverCall
// ***********

function tserverCall() {
        this._ctl_get_arr=new Array;
        this.dataList=new Array;

        this.parse=function (req) {
                        var items=new Array;
                        //log('Parse '+req.url);
                        var responseXML=req._ctl_req.responseXML;
                        if (responseXML.getElementsByTagName('items').length!=0) {
                                    for (var i=0; i<responseXML.getElementsByTagName('item').length; i++) {
                                            var obj=responseXML.getElementsByTagName('item')[i];                    
                                            if (obj.childNodes.length>=2) {     
                                                var ty=getChildTag(obj, 'type', 'text');
                                                if (ty=='html') {
                                                        var ic=getChildTag(obj, 'html_content', 'text');
                                                        var iid=getChildTag(obj, 'item_id', 'text');
                                                        items.push([ty, iid, ic]);
                                                } else if (ty=="script") {
                                                        var script=getChildTag(obj, 'script', 'text');
                                                        items.push([ty, script]);
                                                } else if (ty=="attribute") {
                                                        var iid=getChildTag(obj, 'item_id', 'text');
                                                        var att=getChildTag(obj, 'attribute', 'text');
                                                        var val=getChildTag(obj, 'value', 'text');
                                                        items.push([ty, iid, att, val]);
                                                } else if (ty=="style") {
                                                        var iid=getChildTag(obj, 'item_id', 'text');
                                                        var att=getChildTag(obj, 'style', 'text');
                                                        var val=getChildTag(obj, 'value', 'text');
                                                        items.push([ty, iid, att, val]);
                                                } else if (ty=="value") {
                                                        var iid=getChildTag(obj, 'item_id', 'text');
                                                        var val=getChildTag(obj, 'value', 'text');
                                                        items.push([ty, iid, val]);
                                                } else if (ty=="delete") {
                                                        var iid=getChildTag(obj, 'item_id', 'text');
                                                        items.push([ty, iid]);
                                                }                               
                                            }
                                    }
                                    this._ctl_load(items);                                      
                        }
                        items=null;
        }

        this._status=function() {
                var loaded=0;
                if (window.global_ctl_status) {
                        for (var i=0; i<this.dataList.length; i++) {
                                if (this.dataList[i].loaded) {loaded++;}
                        }
                        window.global_ctl_status(this.dataList.length, loaded);
                }
        }

        this._ctl_endReq=function(req){
                req.loaded=true;
                /*log('endreq1. datalist count: '+req.serverCallObj.dataList.length);
                log('endreq: req.url=='+req.url);
                log('endreq: req.preload=='+req.preload.toString());*/
                if (!req.preload) {
                        //log('(callback '+req.url+')');
                        req.serverCallObj.parse(req);
                        //log('(callback after parse '+req.url+')');
                        var sco=req.serverCallObj;
                        sco.dataList.remove(req);
                        sco._status();
                        //req.freeMemory();
                        req=null;
                } else {
                        req.serverCallObj._status();
                }
                //log('endreq2. datalist count: '+req.serverCallObj.dataList.length);
        }

        this._ctl_load=function(items) {
                //log('_ctl_load loading items');
                if (items.length>0) {
                        for (var i=0;i<items.length;i++) {
                                var item1=items[i];
                                var ity=item1[0];
                                if (ity=='html') {
                                        var iid=item1[1];
                                        var ic=item1[2];
                                        var obj=getobjectbyid(iid);
                                        if (obj) {
                                                obj.innerHTML=ic;
                                        }
                                } else if (ity=='script') {
                                        var script=item1[1];
                                        eval(script);
                                } else if (ity=='attribute') {
                                        var iid=item1[1];
                                        var a=item1[2];
                                        var v=item1[3];
                                        var obj=getobjectbyid(iid);
                                        if (obj) {
                                                obj.setAttribute(a, v);
                                        }                                       
                                } else if (ity=='style') {
                                        var iid=item1[1];
                                        var a=item1[2];
                                        var v=item1[3];
                                        var obj=getobjectbyid(iid);
                                        if (obj) {
                                                obj.style[a]=v;
                                        }                                       
                                } else if (ity=='value') {
                                        var iid=item1[1];
                                        var v=item1[2];
                                        var obj=getobjectbyid(iid);
                                        if (obj) {
                                                obj.value=v;
                                        }                                       
                                } else if (ity=='delete') {
                                        var iid=item1[1];
                                        var obj=getobjectbyid(iid);
                                        if (obj) {
                                                var par=obj.parentNode;
                                                while(obj.hasChildNodes()) {obj.removeChild(obj.lastChild);}
                                                par.removeChild(obj);
                                        }                                       
                                }
                        }
                }
        }

        this.get=function(url, async, preload, ontime, postdata) {
        if (ontime===undefined) {ontime=0;}
        if (postdata===undefined) {postdata=false;}
        //if (ontime>0) {alert('serverCall.get("'+url+'", '+async.toString()+', '+preload.toString()+')', ontime);}
        //if (ontime>0) {window.setTimeout('serverCall.get("'+url+'", '+async.toString()+', '+preload.toString()+')', ontime);}
        if (!postdata) {
            for (var i=0; i<this.dataList.length; i++) {
                if (url==this.dataList[i].url) {
                        if (this.dataList[i].loaded) {
                                if (preload) {return;}
                                //log('GET: '+url+': betöltés a CACHE-ből');                    
                                this.dataList[i].preload=false;
                                this._ctl_endReq(this.dataList[i]);
                                return;
                        } else {
                                if (this.dataList[i].preload) {
                                        this.dataList[i].preload=false;
                                }
                                return;
                        }
                }
            }
        }
 
        //window.status=url+': GET';                    
            if (async===undefined) {async=true;}
            if (preload===undefined) {preload=false;}
                //log('(_ctl_get> '+url+' getting');

                var data=new tserverCallData;
                data.serverCallObj=this;
                data.url=url;
                data.callback=this._ctl_endReq;
                data.preload=preload;

                    if (window.XMLHttpRequest) {
                        _ctl_req = new XMLHttpRequest();
                        data._ctl_req=_ctl_req;
                        _ctl_req.onreadystatechange = function() {
                                data._ctl_processReqChange();
                        }
                        this.dataList.push(data);
                        this._status();
                        if (postdata) {
                                _ctl_req.open("POST", url, async);
                        } else {
                                _ctl_req.open("GET", url, async);
                        }
                        _ctl_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-2');
                        //log('GET: '+url+': letöltés a szerverről');                   
                        if (postdata) {
                                _ctl_req.send(postdata);
                        } else {
                                _ctl_req.send(null);
                        }
                    } else if (window.ActiveXObject) {
                        _ctl_req = new ActiveXObject("Microsoft.XMLHTTP");
                        data._ctl_req=_ctl_req;
                        if (_ctl_req) {
                                _ctl_req.onreadystatechange = function() {
                                        data._ctl_processReqChange();
                                }
                                this.dataList.push(data);
                                this._status();
                                if (postdata) {
                                        _ctl_req.open("POST", url, async);
                                } else {
                                        _ctl_req.open("GET", url, async);
                                }
                                _ctl_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-2');
                                //log('GET: '+url+': letöltés a szerverről '+postdata);                 
                                if (postdata) {
                                        _ctl_req.send(postdata);
                                } else {
                                        _ctl_req.send("");
                                }
                        }
                    }
                if(!async) {
                        data._ctl_processReqChange();
                }

        }

        this.sendForm=function(sender, url) {
                if (sender) {
                        var form=null;
                        var getstr='';
                        if (sender.tagName=='FORM') { form=sender; }
                        if (!form) {form=findparentbytag(sender, 'form');}
                        if (form) {
                                if (form.method.toLowerCase()=="get") {
                                        var par1=(url.indexOf('?')>=0)?'&':'?';
                                        for (var i=0; i<form.length; i++) {
                                                var elem=form.elements[i];
                                                if ((elem.name)&&(elem.value!==null)&&(elem.type)) {
                                                        sendparam=true;
                                                        if ((elem.type=='checkbox')||(elem.type=='radio')) {
                                                                sendparam=elem.checked;
                                                        }
                                                        if (sendparam) {
                                                                getstr+=(getstr=='')?par1:'&';
                                                                getstr+=elem.name+'='+escape(elem.value);
                                                        }
                                                }
                                        }
                                        this.get(url+getstr);
                                } else if (form.method.toLowerCase()=="post") {
                                        var par1=''; 
                                        var sendparam=true;
                                        for (var i=0; i<form.length; i++) {
                                                var elem=form.elements[i];
                                                if ((elem.name)&&(elem.value!==null)&&(elem.type)) {
                                                        sendparam=true;
                                                        if ((elem.type=='checkbox')||(elem.type=='radio')) {
                                                                sendparam=elem.checked;
                                                        }
                                                        if (sendparam) {
                                                                getstr+=(getstr=='')?par1:'&';
                                                                getstr+=elem.name+'='+escape(elem.value);
                                                        }       
                                                }
                                        }
                                        //alert(getstr);
                                        this.get(url, undefined, undefined, undefined, getstr);
                                }
                        }
                }                       
                return false;
        }
}
serverCall=new tserverCall;

        //tag szerinti szülőkeresés
        function findparentbytag(obj, tag)
        {    
                while (obj) {
                        if ((obj)&&(obj.tagName))
                        {
                                if (obj.tagName.toLowerCase()==tag) {return obj;break;}
                        }
                        obj=obj.parentNode;             
                }
        }

