// Created with MAX's HTML Beauty++ 2004
//alert("AJAX lib is connected!!!");
//VERSION 1.0.1

function AJAXRequest(debug) {
	this.debug	= debug;
	try {
		this.request	= new XMLHttpRequest();
	} catch (e) {
		try {  
			this.request	= new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {    
			try {  
				this.request	= new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Не возможно создать AJAX-запрос:\n" + e);
				return null;
			}
		}
	}
	return this.InitEvents(this);
}

AJAXRequest.prototype	= {
	debug			: false,
	request			: null,
	responseText	: null,
	response		: null,
	InitEvents		: function (pObject) {
		this.request.onreadystatechange = function () {
			var r	= pObject.request;
			if (r.readyState == 4) {
				if (r.status == 200) {
					if (pObject.debug)
						alert(r.responseText);
					pObject.responseText	= r.responseText;
					eval("pObject.response = " + r.responseText);
					return pObject.onResponse(r.responseText);
				} else
					return pObject.onError(r.status, r.statusText, r.responseText);
			}
		};
		delete pObject;
		return this;
	},
	Send	: function (sAction, sMethod, pParams) {
		if (typeof(sMethod) == "undefined")
			sMethod	= "GET";
		var query	= this.ParamsEncode(pParams);
		if (sMethod	== "GET" && sAction.indexOf("?") != -1 && query)
			sAction += query;
		this.request.open(sMethod, sAction, true);
		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1251");
		return this.request.send(query);
	},
	SendEx	: function (sAction, sMethod, pParams) {
		if (typeof(sMethod) == "undefined")
			sMethod	= "GET";
		var query	= this.ParamsEncode(pParams);
		if (sMethod	== "GET" && sAction.indexOf("?") != -1 && query)
			sAction += query;
		this.request.open(sMethod, sAction, true);
		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1251");
		var res	= this.request.send(query);
		//this.checkState(this.request);
		return res;
	},
	SendForm	: function (pForm) {
		var e		= pForm.elements;
		var params	= {};
		for(var i=0; i<e.length; i++) {
			if (e[i].type == "button" || e[i].type == "submit" || e[i].type == "reset" || e[i].disabled || ((e[i].type	== "checkbox" || e[i].type	== "radio") && !e[i].checked))
				continue;
			params[e[i].name]	=  e[i].value;
		}
		return this.Send(pForm.action + "", pForm.method + "", params);
	},
	ParamsEncode	: function (pParams) {
		if (!pParams)
			return null;
		var query	= "";
		for (var p in pParams) {
			if (typeof(pParams[p]) == "array") {
				for(var i in pParams[p])
					query	+= (query != "" ? "&" : "") + p + "[" + i + "]=" + encodeURIComponent(pParams[p][i]);
			} else
				query	+= (query != "" ? "&" : "") + p + "=" + encodeURIComponent(pParams[p]);
		}
		return query;
	},
	checkState			: function () {
		alert('r=' + this.request);
		if (this.request.readyState == 4) {
			if (this.request.status == 200) {
				if (this.debug)
					alert(this.request.responseText);
				this.responseText	= this.request.responseText;
				eval("this.response = " + this.request.responseText);
				this.onResponse(this.request.responseText);
				return;
			} else {
				this.onError(this.request.status, this.request.statusText, this.request.responseText);
				return;
			}
		}
		var obj	= this;
		setTimeout("obj.checkState();", 10);	
	},
	onResponse			: function (sResponseText) { return true; },
	onError				: function (nStatus, sStatusText, sResponseText) { 
		alert("AJAX. Ошибка запроса\n" + nStatus + ". " + sStatusText + ".\nОтвет: " + sResponseText);
		return false;
	},
	Destroy		: function () {
		this.DestroyProperty(this.request);
		this.DestroyProperty(this.response);
	},
	DestroyProperty		: function (pProp) {
		if (pProp)
			return;
		if (typeof(pProp) == 'object') {
			for (var n in pProp)
				this.DestroyProperty(pProp[n]);
		} 
		pProp = null;
	}
};
