// request - засорение памяти, их надо вычищать
function AJAX()
{
	var ths=this;
	this.request={};
	this.uniq_id=1;
	this.exec=function(res) {
		this.request[res.ajax_id]=res;
		this.show_status(res.ajax_id,'complete');
		this.js_parse_begin(res);
		if (res.add_html) {
			var t=new RegExp("&"+"#39js;","g");
			res.add_html=res.add_html.replace(t,"'");
			var n=sys.node_add();
			n.innerHTML=res.add_html;
		}
		if (this.request[res.ajax_id+'_script']) {
			if (this.request[res.ajax_id+'_script'].node) {
				sys.node_remove(this.request[res.ajax_id+'_script'].node);
			}
			if (this.request[res.ajax_id+'_script'].func) {
				this.request[res.ajax_id+'_script'].func(res,this.request[res.ajax_id+'_script']);
			}
		}
		this.js_parse_end(res);
		if (this.request[res.ajax_id+'_script'] && this.request[res.ajax_id+'_script'].func_end) {
			this.request[res.ajax_id+'_script'].func_end(res,this.request[res.ajax_id+'_script']);
		}
	}
	this.eval=function(str,id) {
		www='';
		// ебаная кодировка
		for(i=0;i<str.length;i++) {
			var q=str.charCodeAt(i);
			if (q>128) {q+=848;}
			www+=String.fromCharCode(q);
		}
		eval(www);
		return this.request[id];
	}
	
	
	this.get_script=function(url,par) {
		var node=document.body.appendChild(document.createElement("SCRIPT")); 
		var r=new RegExp('&amp;');
	    node.type="text/javascript";
		var url=this.url_set_id(url,'ajaxed');
		if (typeof(par)!="object") {
			par={};
		}
		
		this.request[url[1]+'_script']=par;
		this.request[url[1]+'_script'].url=url[0];
		this.request[url[1]+'_script'].node=node;
		this.show_status(url[1],'start');
	    node.src=url[0];
	}
	
	this.get_post_form=function(form,par) {
		var url=this.url_set_id(form.action,'ajaxed');
		if (typeof(par)!="object") {
			par={};
		}
		
		this.request[url[1]+'_script']=par;
		this.request[url[1]+'_script'].url=url[0];
		this.request[url[1]+'_script'].node=null;
		this.show_status(url[1],'start');
		
		param={};
		for(var i=0;i<form.elements.length;i++) {
			var n=form.elements[i];
			if (n.nodeName=='INPUT' && n.type=='radio' && n.name) {if (n.checked) param[n.name]=n.value;}
			else if (n.nodeName=='INPUT' && n.type=='checkbox' && n.name) {if (n.checked) param[n.name]=n.value;}
			else if (n.nodeName=='INPUT' && n.name) {param[n.name]=n.value;}
			else if (n.nodeName=='TEXTAREA' && n.name) {param[n.name]=n.value;}
		}
		
	    this.post(url[0],param,function(o){if (o.readyState==4) {eval(o.responseText);}});
	}
	
	this.get_post_script=function(url,param,par) {
		
		var url=this.url_set_id(url,'ajaxed');
		if (typeof(par)!="object") {
			par={};
		}
		
		this.request[url[1]+'_script']=par;
		this.request[url[1]+'_script'].url=url[0];
		this.request[url[1]+'_script'].node=null;
		this.show_status(url[1],'start');
	    this.post(url[0],param,function(o){if (o.readyState==4) {eval(o.responseText);}});
	}
	
	this.js_parse_begin=function(obj) {
		if (obj.js_begin) {
			eval(obj.js_begin);
		}
	}
	this.js_parse_end=function(obj) {
		var t=new RegExp("&"+"#39js;","g");
		obj.js_end=obj.js_end.replace(t,"'");
		if (obj.js_end) {
			if (window.execScript) {window.execScript(obj.js_end, 'javascript');}
			else {window.eval(obj.js_end);}
		}
	}
	
	this.show_status=function(id,stat) {
		if (root_ajax_stat) {
			if (stat=='start') {
				root_ajax_stat.add(id);
			} else if (stat=='complete') {
				root_ajax_stat.remove(id);
			}
		}
	}
	
	this.url_set_id=function (url,str) {
		this.uniq_id+=1; 
		var r=new RegExp('&amp;');
		var url=url+(url.indexOf('?')>=0? '&' : '?')+'main&mode=ajax&ajax_id='+this.uniq_id+'&ie_soset='+Math.random(); 
		return [url.replace(r,'&'),this.uniq_id];
	}
	
	this.strip_url=function(a) {
		return encodeURIComponent(a);
	}
	
	// http request
	this.isSupported = function(){
		return !!this.getConnection();
	};
	this.events = ["start", "open", "send", "load", "end"];
	this.filter = encodeURIComponent;
	this.getConnection = function(){
		var i, o = [function(){return new ActiveXObject("Msxml2.XMLHTTP");},
		function(){return new ActiveXObject("Microsoft.XMLHTTP");},
		function(){return new XMLHttpRequest;}];
		for(i = o.length; i--;) try{return o[i]();} catch(e){}
		return null;
	};
	this.formatParams = function(params){
		var i, r = [];
		for(i in params) r[r.length] = i + "=" + (this.filter ? this.filter(params[i]) : params[i]);
		return r.join("&");
	};
	this.get = function(url, params, handler, waitResponse){
		return this.request("GET", url + (url.indexOf("?") + 1 ? "&" : "?") + this.formatParams(params), null, handler, null, waitResponse);
	};
	this.post = function(url, params, handler, waitResponse){
		return this.request("POST", url, params = this.formatParams(params), handler, {
			"Connection": "close",
			"Content-Length": params.length,
			"Method": "POST " + url + " HTTP/1.1",
			"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
		}, waitResponse);
	};
	this.request = function(method, url, params, handler, headers, waitResponse){
		var i, o = this.getConnection(), f = handler instanceof Function;
		try{
			/* Перед вами адский хак против URI access denied возникающий при запросе к /bandera.narprod/ из /narprod/ */
			var wehere=location.href.substring(0,location.href.indexOf("/",7));
			url=wehere+url.substr(url.indexOf("/",7));			
			o.open(method, url, !waitResponse);
			waitResponse || (o.onreadystatechange = function(){
				var s = ths.events[o.readyState];
				f ? handler(o) : s in handler && handler[s](o);
			});
			o.setRequestHeader("HTTP_USER_AGENT", "XMLHttpRequest");
			for(i in headers)
				o.setRequestHeader(i, headers[i]);
			o.send(params);
			waitResponse && (f ? handler(o) : handler["end"] && handler["end"](o));
			return true;
		}
		catch(e){
			alert(e);
			return false;
		}
	};
}
ajax=new AJAX();
/*
with({$: HTTPRequest.prototype}){
	$.isSupported = function(){
		return !!this.getConnection();
	};
	$.events = ["start", "open", "send", "load", "end"];
	$.filter = encodeURIComponent;
	$.getConnection = function(){
		var i, o = [function(){return new ActiveXObject("Msxml2.XMLHTTP");},
		function(){return new ActiveXObject("Microsoft.XMLHTTP");},
		function(){return new XMLHttpRequest;}];
		for(i = o.length; i--;) try{return o[i]();} catch(e){}
		return null;
	};
	$.formatParams = function(params){
		var i, r = [];
		for(i in params) r[r.length] = i + "=" + (this.filter ? this.filter(params[i]) : params[i]);
		return r.join("&");
	};
	$.get = function(url, params, handler, waitResponse){
		return this.request("GET", url + (url.indexOf("?") + 1 ? "&" : "?") + this.formatParams(params), null, handler, null, waitResponse);
	};
	$.post = function(url, params, handler, waitResponse){
		return this.request("POST", url, params = this.formatParams(params), handler, {
			"Connection": "close",
			"Content-Length": params.length,
			"Method": "POST " + url + " HTTP/1.1",
			"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
		}, waitResponse);
	};
	$.request = function(method, url, params, handler, headers, waitResponse){
		var i, o = this.getConnection(), f = handler instanceof Function;
		try{
			o.open(method, url, !waitResponse);
			waitResponse || (o.onreadystatechange = function(){
				var s = $.events[o.readyState];
				f ? handler(o) : s in handler && handler[s](o);
			});
			o.setRequestHeader("HTTP_USER_AGENT", "XMLHttpRequest");
			for(i in headers)
				o.setRequestHeader(i, headers[i]);
			o.send(params);
			waitResponse && (f ? handler(o) : handler["end"] && handler["end"](o));
			return true;
		}
		catch(e){
			return false;
		}
	};
}
*/