get = function (id) {return document.getElementById(id) || false;}
blockEvent = function (e) {e = e ? e : window.event;if (e.stopPropagation) e.stopPropagation();else e.cancelBubble = true;if (e.preventDefault) e.preventDefault();else e.returnValue = false;}

createXmlHttp = function ()
{
    if (typeof XMLHttpRequest != "undefined")
    {
		return new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        var aVersions = ["MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP", "Microsoft.XMLHTTP"];
        for(var i = 0; i < aVersions.length; i++)
        {
            try
            {
				var oXmlHttp = new ActiveXObject(aVersions[i]);
                return oXmlHttp;
            }
            catch (oError)
            {
                // Не удалось подключить
            }
        }
    }
    throw new Error("Невозможно создать объект XMLHttp.");
}

getRequestBody = function (oForm)
{
	var aParams = new Array();
	for (var i = 0; i < oForm.elements.length; i++)
	{
		var send = true;
		if (oForm.elements[i].type == 'checkbox')
		{
			send = oForm.elements[i].checked;
		}
		if (send)
		{
			var sParam = encodeURIComponent(oForm.elements[i].name);
			sParam += "=";
			sParam += encodeURIComponent(oForm.elements[i].value);
			aParams.push(sParam);
		}
	}
	return aParams.join("&");
}
eventHandler = function (e)
{
	if (this.obj)
	{
		if (this.obj.blockEvent)
		{
			e = e ? e : window.event;
			if (e.stopPropagation) e.stopPropagation();
			else e.cancelBubble = true;
			if (e.preventDefault) e.preventDefault();
			else e.returnValue = false;
		}
		if (e)
		{
			this.obj.event = e;
			this.handler();
		}
	}
}

timeoutHandler = function (elem)
{
	return function ()
	{
		elem.timeoutHandler();
	}
}

ExtendForm = function (states, timeout)
{
	this.stateReady = states.split('|')[0];
	this.stateWork = states.split('|')[1];
	this.stateResult = states.split('|')[2];

	this.timeout = timeout;

	this.$ = function (id)
	{
		return document.getElementById(id) || false;
	}

	this.getAllForms = function ()
	{
		return document.getElementsByTagName('FORM');
	}

	this.checkStatus = function ()
	{
		if (this.className.search(this.ee.stateReady) > -1)
		{
			return 1;
		}
		else if (this.className.search(this.ee.stateWork) > -1)
		{
			return 2;
		}
		else if (this.className.search(this.ee.stateResult) > -1)
		{
			return 3;
		}
		else
		{
			return 0;
		}
	}

	this.goWork = function ()
	{
		clearTimeout(this.buyTimeout);
		if (!this.buyBlocked)
		{
			this.buyBlocked = true;
			this.timeoutHandler = this.goResult;
			this.className = this.className.replace(this.ee.stateReady, this.ee.stateWork);
			var sBody = getRequestBody(this);

			var oXmlHttp = createXmlHttp();
			oXmlHttp.open("POST", document.location.href.split('?')[0] + '?ajax=31337', true);
			oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			var func = function (param)
			{
				return function ()
				{
					switch(oXmlHttp.readyState)
					{
						case 4 :
							if (oXmlHttp.status == 200)
							{
								if (!param.tmp)
								{
									param.tmp = document.createElement('DIV');
									param.tmp.style.display = 'none';
									param.appendChild(param.tmp);
								}
								param.tmp.innerHTML = oXmlHttp.responseText;
								param.buyTimeout = setTimeout(timeoutHandler(param), 2000);
							}
							else
							{
								// bad...
							}
						break;
					}
				}
			}
			oXmlHttp.onreadystatechange = func(this)

			oXmlHttp.send(sBody);
		}
	}

	this.goResult = function ()
	{
		clearTimeout(this.buyTimeout);
		var elems = this.tmp.getElementsByTagName('*');
		if (elems.length)
		{
			for (var i = 0; i < elems.length; i++)
			{			
				if (elems[i].className.search('updatedBasket') > -1)
				{
					get('basket').innerHTML = elems[i].innerHTML;
				}
				if (elems[i].className.search('bought') > -1)
				{
					var bought = elems[i].innerHTML;
				}
			}
			//this.tmp.innerHTML = '';
			if (bought)
			{
				var all = this.parentNode.parentNode.getElementsByTagName('*');
				for (var i = 0; i < all.length; i++)
				{
					if (all[i].className.search('bought') > -1)
					{
						all[i].innerHTML = bought;
						break;
					}
				}
			}
			this.className = this.className.replace(this.ee.stateWork, this.ee.stateResult);
			this.callback();
			this.timeoutHandler = this.ee.goReady;
		}
		this.buyTimeout = setTimeout(timeoutHandler(this), 5000);
	}

	this.goReady = function ()
	{
		clearTimeout(this.buyTimeout);
		this.className = this.className.replace(this.ee.stateResult, this.ee.stateReady);
		this.buyBlocked = false;
	}

	this.add = function (elem)
	{
		elem.ee = this;

		elem.goWork = this.goWork;
		elem.goResult = this.goResult;
		elem.goReady = this.goReady;

		elem.checkStatus = this.checkStatus;
		
		elem.callback = function ()
		{
			var div = this.parentNode.parentNode;
			this.parentNode.parentNode.className = this.parentNode.parentNode.className.replace('noflagged', 'flagged');
		}
	}
}
