XHR = {};

XHR.getSync = function (path)
{
	return getSyncXHR (path);
}


XHR.__runningForm = false;
XHR.__runningOptions = false;
XHR.sendForm = function (form, options)
{
	if (form.sending != undefined && form.sending)
		return;

	var XHR = this;
	XHR.__runningForm = form;
	XHR.__runningOptions = options;
	form.sending = true;

	try
	{
		/*
		if (options.disableInputs != undefined && options.disableInputs)
			this.setInputsDisable (form,true);
		*/

		var arguments = getFormElementsUrlEncode (form);

		var xhr = getXHR ();
		xhr.open ("post", "/catalog/xhrListEvent.php", true);
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.setRequestHeader("Content-length", arguments.length);
		xhr.setRequestHeader("Connection", "close");
		xhr.onreadystatechange = function ()
		{
			if (this.readyState == 4)
				XHR.gotResult (this.responseText);
		}
		xhr.send (arguments + "&xhr=1");
	}
	catch (e)
	{
		debugger;
		form.sending = false;
	}
}
XHR.sendLink = function (link, path, options)
{
	// debugger;
	this.__runningOptions = options;

	var xhr = getXHR ();
	xhr.open ("post", "/catalog/xhrListEvent.php", true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", arguments.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.onreadystatechange = function ()
	{
		if (this.readyState == 4)
			XHR.gotResult (this.responseText);
	}
	xhr.send (path + "&xhr=1");
}
XHR.gotResult = function (text)
{
	if (typeof this.__runningOptions.resultElem == "string")
		this.__runningOptions.resultElem = $ (this.__runningOptions.resultElem);

	var resultElem = this.__runningOptions.resultElem;

	var style = resultElem.style;
	if (style.display == "none")
		style.display = "block";

	if (typeof resultElem.value != undefined)
		resultElem.value = text;

	try
	{
		resultElem.innerHTML = text;
	}
	catch (e) {}
	XHR.__runningForm.sending = false;
}


XHR.setInputsDisable = function (form, disabled)
{
	for (var i = 0; i < form.elements.length; i++)
	{
		var input = form.elements[i];

		input.disabled = disabled;
	}
}



/*
function AutoLaunchXHR ()
{
}
AutoLaunchXHR.waitSrc = "autoLaunchImgLoading";
AutoLaunchXHR.launchMillisec = 2000;

AutoLaunchXHR.inputs = [];
AutoLaunchXHR.setupForm = function (form)
{
	var inputs = AutoLaunchXHR.__getInputs (form);
}
AutoLaunchXHR.launch = function ()
{
	var post = "";
	for (var index = 0; index < AutoLaunchXHR.inputs.length; index++)
		post += inputGetUrlencode (input);


	var xhr = getXHR ();
	xhr.open ("post", "/catalog/xhrListEvent.php", true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", post.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.onreadystatechange = function ()
	{
		if (this.readyState == 4)
			AutoLaunchXHR.setResult (this.responseText);
	}
	xhr.send (post);
}


// Events
AutoLaunchXHR.__inputKeyUp = function (event)
{
	$cT (AutoLaunchXHR.launchTimeout);

	AutoLaunchXHR.launchTimeout = window.setTimeout ("AutoLaunchXHR.launch ()", AutoLaunchXHR.launchMillisec);
}


// Service Methods
AutoLaunchXHR.__getInputs = function (form)
{
	var result = [];

	for (var i in form)
	{
		var input = form[i];

		if (input.type != undefined && input.type == "text")
		{
			input.onkeyup = AutoLaunchXHR.__inputKeyUp;
			AutoLaunchXHR.inputs.push (input);
			result.push (input);
		}
	}

	return result;
}
*/



function getXHR ()
{
	var xhr = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xhr = new XMLHttpRequest ();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xhr = new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xhr = new ActiveXObject ("Microsoft.XMLHTTP");
		}
	}

	return xhr;
}
function getSyncXHR (path)
{
	var xhr = getXHR ();

	xhr.open ("get", path, false);
	xhr.send (null);

	return xhr.responseText;
}


function XHRBox (path, box)
{
	this.xhr = getXHR ();
	this.path = path;
	this.box = box;

	this.xhr.open ("get", path, true);
	window.callingXHRBox = this;
	this.xhr.onreadystatechange = function()
	{
		if (this.readyState == 4)
			window.callingXHRBox.ok ();
	};
	this.xhr.send (null);

	this.ok = function ()
	{
		var res = this.xhr.responseText;

		var d = $ ("boxBuz");
		if (!d)
		{
			d = $C ("div");
			d.onclick = function ()
			{
				this.style.display = "none";
			}
			d.id = "boxBuz";
			d.className = "boxBuz";
			d.$ = this;


			document.body.appendChild (d);
		}
		else
			d.style.display = "block";

		d.innerHTML = "<img style=\"cursor: pointer; width: 15px; float: right\" src=\"/common/images/x2.png\" />" + res;

		$sT ("$('boxBuz').$.setBox ()", 1);
		this.xhr = null;
	}


	this.setBox = function ()
	{
		var d = $ ("boxBuz");
		var s = d.style;

		s.top = this.box.y + "px";

		switch (this.box.x)
		{
			case "center":
				s.left = (Math.max (500, window.getSize ()[0] / 2 - d.offsetWidth / 2)) + "px";
				break;

			default:
				s.left = this.box.x + "px";
		}

		s.width = this.box.w + "px";

		switch (this.box.h)
		{
			case 0:
				break;

			default:
				s.height = this.box.h + "px";
		}
	}
}


