// ajax codebase
function $(id){
    return document.getElementById(id);
}

function JSON_(txt){
    return eval('('+txt+')');
}

Function.prototype.bind = function(object) { 
    var __method = this;
    return function() { return __method.apply(object, arguments); }
}

var net=new Object();
net.RSTATE_COMPLETED=4;

function is_null(v){
    return (typeof(v)==undefined || v==null);
};

net.Request=function(url,props){//handler,data,fdata,method,needxml){
        this.url=url;
        this.handler = props.handler;
        this.data    = is_null(props.data)?'':props.data;
        this.fdata   = is_null(props.fdata)?null:props.fdata;
    	this.method  = is_null(props.method)?'GET':props.method;
    	this.needxml = is_null(props.needxml)?false:props.needxml;
};

net.RequestsQueue=new Array();
net.Cache=new Array();
net.rmtServer    = null;

net.XMLRequester=function(r){
	this.fullinfo=r;
        this.onload=r.handler;
        this.req=null;
        this.proceed=false;
        this.error_happen=false;
        this.fdata=r.fdata;
        this.method=r.method;
	this.needxml=r.needxml;
        this.PrepareReq();
        this.url=r.url;
        this.data=r.data;
        this.Exec(r.url,r.data);
};

function proceed_next(){
        if (net.rmtServer) return;
        if (net.RequestsQueue.length>0){
                request=net.RequestsQueue.pop();
                net.rmtServer = new net.XMLRequester(request);
        };
};

function add_request(url,handler,data,fdata,method){
        net.RequestsQueue.push(new net.Request(url,{handler: handler,
    						    data:    data,
    						    fdata:   fdata,
    						    method:  method}));
        proceed_next();
};

function ajax_req(url,props){
    net.RequestsQueue.push(new net.Request(url,props));
    proceed_next();
};

function ajax_cached_req(url,props){
    if (typeof(net.Cache[url])=='undefined'){
	props.fdata={fdata: (is_null(props.fdata)?null:props.fdata), url: url, handler: props.handler};
	props.handler=function(txt,par){
	    net.Cache[par.url]=txt;
	    par.handler(txt,par.fdata);
	};
	ajax_req(url,props);
    }else{
	props.handler(net.Cache[url],props.fdata);
    };
};

function add_request_xml(url,handler,data,fdata,method){
        net.RequestsQueue.push(new net.Request(url,{handler: handler,
    						    data:    data,
    						    fdata:   fdata,
    						    method:  method,
    						    needxml: true }));
        proceed_next();
};

net.XMLRequester.prototype={
        PrepareReq:function(){
            try {
                // Mozilla / Safari
                this.req = new XMLHttpRequest();
            } catch (e) {
                // Explorer
                var ms_variants = new Array(
                'Microsoft.XMLHTTP',
                'MSXML2.XMLHTTP',
                'Msxml2.SERVERXMLHTTP',
                'MSXML2.XMLHTTP.5.0',
                'MSXML2.XMLHTTP.4.0',
                'MSXML2.XMLHTTP.3.0'
                );
                var success = false;
                for (var i=0;i < ms_variants.length && !success; i++) {
                        try {
                                this.req = new ActiveXObject(ms_variants[i]);
                                success = true;
                        } catch (e) {
                        }
                }
                if ( !success ) {
                        throw 'error';
                }
            }
//          this.req.onerror=this.OnError.bind(this);
            this.req.onreadystatechange=this.OnloadCall.bind(this);
        },
        Exec:function(url,data){
            this.proceed=true;
            this.req.open(this.method,url,true);
            if (this.method=='POST')
        	this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            else
        	this.req.setRequestHeader("Content-Type","text/plain");
            this.req.setRequestHeader("Pragma","no-cache");
            this.req.setRequestHeader("Cache-Control", "no-cache");
            this.req.setRequestHeader('Expires','Sat, 1 Jan 2005 05:00:00 GMT');
            this.req.setRequestHeader("If-Modified-Since","Tue, 11 Jul 2000        18:23:51 GMT");
            this.req.send(data);
        },
        OnloadCall:function(){
                if (this.req.readyState==net.RSTATE_COMPLETED && !this.error_happen){
            	    try{
                        if (this.req.status && this.req.status==200){
//                              this.onload(this.req.responseXML);
                                if (is_null(this.fdata))
                                        this.onload(this.needxml?this.req.responseXML:this.req.responseText);
                                else
                                        this.onload(this.needxml?this.req.responseXML:this.req.responseText,this.fdata);
                        };
                        this.proceed=false;
                        net.rmtServer=null;
                        proceed_next();
                    }catch(e){
                	this.OnError(e);
                    };
                };
        },
        OnError:function(event){
                this.proceed=false;
//                this.error_happen=true;
    		net.RequestsQueue.push(this.fullinfo);
                proceed_next();
        },
        Proceeding:function(){
                return this.proceed;
        }
};


