//url - adres url strony
//func - funkcja do wykonania pozaładowaniu
//method - true = POST | false = GET
//param - parametry z formularza

//wyslanie formularza
//  button.onclick = function ()
//   {
//    return sendForm(this,function () {
//       alert(this.httpRequest.responseText);
//     },true,'tmp/test.php');
//   }

//----odwołanie do wartości httpRequest w funkcji wysyłanej jako paramert
//odwołanie w funkcji func do httpRequest przez -> this.httpRequest
//stopsend - true -> zatrzymuje wyslanie

FSite2._ajaxSendRequest = function ()
{
	this.httpRequest.open(((this.method)?'POST':'GET'), this.url, true);
	if (this.param && (this.param.length > 0))
	{
		this.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.httpRequest.setRequestHeader("Content-length", this.param.length);
	}
	this.httpRequest.setRequestHeader("X-Requested-With", 'XMLHttpRequest');
	this.httpRequest.send((this.param)?this.param:null);
}

FSite2.getFormElements = function (node)
{
	el = node.getElementsByTagName('*');
	els = [];
	for (i=0;i<el.length;i++)
	{
		if (((el[i].tagName == 'INPUT') && (el[i].disabled != true) && el[i].name) ||
			((el[i].tagName == 'SELECT') && (el[i].disabled != true) && el[i].name) ||
			((el[i].tagName == 'TEXTAREA') && (el[i].disabled != true) && el[i].name))
		{
			els.push(el[i]);
		}
	}
	return els;
}

FSite2.elementsToString = function (fields)
{
	str = '';
	for (i = 0; i < fields.length; i++)
	if ((fields[i].type=='checkbox') || (fields[i].type=='radio'))
	{
		if (fields[i].checked)
			str += '&' + fields[i].name + '=' + encodeURIComponent(fields[i].value);
	}
	else
		str += '&' + fields[i].name + '=' + encodeURIComponent(fields[i].value);
	return str.substr(1, str.length - 1);
}

FSite2.sendForm = function (form, func, method, action)
{
	requestParam = this.elementsToString(this.getFormElements(form));
	requestMethod = (method)?method:((form.method)?form.method:'get');
	url = (action)?action:((form.action)?form.action:document.location.toString());
	return new this.HTTPRequest(url, func, requestMethod, requestParam);
}

FSite2._ajaxReady = function (el)
{
	if (el.httpRequest.readyState == 4)
	{
		if (el.httpRequest.status == 200)
		{
			if (el.loaded)
				el.loaded();
		}
	}
}

FSite2.HTTPRequest = function (url, func, method, param, stopsend)
{
	this.ajaxSendRequest = FSite2._ajaxSendRequest;
	this.ajaxReady = FSite2._ajaxReady;

	this.method=method;
	this.param=param;

	url = url.toString();
	xTest = url.split('?');
	if (xTest.length>1)
		url += '&';
	else
		url+='?';
	url += 'load=' + ((Math.random() * 1000000000000000000).toString());
	this.url=url;
	if (window.XMLHttpRequest)
	{
		this.httpRequest = new XMLHttpRequest();
		if (this.httpRequest.overrideMimeType)
			this.httpRequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject)
	{
		try
		{
			this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
			this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	
	if (!this.httpRequest) return false;
		this.loaded=false;
	if (func) this.loaded = func;
	
	this.httpRequest.onreadystatechange = FSite2._callRef(this.ajaxReady,this);
	
	if (stopsend)
		return this;
	else
		this.ajaxSendRequest(); 
}
