/**
* Filename................: mel.xhr.js
* Project.................: web pages SDK
* Last Modified...........: $Date: 8/1/2008 16:27:52 $
* CVS Revision............: $Revision: 0.0.5 $
* Idea and Developed by...: Maxim Bulygin (sailormax@gmail.com)
*/

if (typeof MEL == "undefined") var MEL = {};

MEL.xhr = {

	cfg: {
//		STATUS_MSG: "Loading...",
		NOTFOUND_MSG: "URL doesn't exist!"
	},
	__sending: [],

	createXHR: function()
	{
		var res = null;
		if (typeof XMLHttpRequest == "undefined")
		{
			var i, objs = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
			for (i in objs)
				try { if (res = new ActiveXObject(objs[i])) break; } catch (e) {}
		}
		else
			res = new XMLHttpRequest();

		return res;
	},


	sendXHR: function()
	{
		if (this.request)
		{
			var curr = [this.url, this.method, this.vars];
			for (var i in MEL.xhr.__sending)
				if (MEL.xhr.__sending[i]+"" == curr+"")
					return this.request;
			MEL.xhr.__sending.push(curr);

			var self = this;
			this.request.onreadystatechange = function()
			{
				//	self.request.readyState: 0 = uninitialized, 1 = loading, 2 = loaded, 3 = interactive, 4 = complete
				if (self.request.readyState == 4)
				{
					if (self.request.status == 200)
					{
						if (typeof self.resp_func == "function")
							self.resp_func(self.request.responseText, self.resp_param);
						self.request.abort();
					}
					else if (typeof self.error_func == "function")
						self.error_func(self.request.status, self.request.statusText);
					else if (self.request.status == 404)
						alert("Error: " + self.cfg.NOTFOUND_MSG);
					else
						alert("Error: " + self.request.statusText);

					if (MEL.statusBar) MEL.statusBar.hide();

					for (var i in MEL.xhr.__sending)
						if (MEL.xhr.__sending[i]+"" == curr+"")
						{
							MEL.xhr.__sending.splice(i, 1);
							break;
						}
				}
			};

			if (this.method.toLowerCase() == "get")
			{
				if (this.vars.length)
					this.url += (this.url.search(/\?/) > 0 ? "&amp;" : "?") + this.vars;
				this.vars = null;
			}

			this.request.open(this.method, this.url, this.async);

			if (this.method.toLowerCase() == "post")
				this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			this.request.setRequestHeader("Content-length", (typeof this.vars == "string" ? this.vars.length : 0));
//			this.request.setRequestHeader("Connection", "close");	// in IE6 slowdown

			try
			{
				this.request.send(this.vars);
				if (this.cfg.STATUS_MSG && MEL.statusBar)
					MEL.statusBar.show(this.cfg.STATUS_MSG);
			}
			catch (e)
			{
				if (typeof this.error_func == "function")
					this.error_func(e.name, e.message);
				else
					alert(e.name + ": " + e.message);
			}
			return 0;
		}
		return this.request;
	},


	Create: function(url, method, vars, resp_func, error_func)
	{
		if (typeof method == "undefined") method = "GET";
		var obj = {};

		obj.cfg			= this.cfg;

		obj.url			= url;
		obj.resp_func	= resp_func;
		obj.error_func	= error_func;

		obj.async		= true;
		obj.method		= method;
		obj.vars		= vars;
		obj.resp_param	= null;

		obj.request		= this.createXHR();
		obj.send		= this.sendXHR;

		return obj;
	}

}

