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 (!get('tmp'))
								{
									var tmp = document.createElement('DIV');
									tmp.style.display = 'none';
									tmp.id = 'tmp';
									document.body.appendChild(tmp);
								}
								get('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 divs = get('tmp').getElementsByTagName('DIV');
		var forms = get('tmp').getElementsByTagName('FORM');
		
		if (forms.length)
		{
			this.innerHTML = forms[0].innerHTML;
			get('tmp').innerHTML = '';
			this.buyBlocked = false;
			this.className = this.className.replace(this.ee.stateWork, this.ee.stateReady);
		}
		else
		{
			var parent = this.parentNode;
			var error = false;
			while (parent.className !== 'popup-content')
			{
				if (parent.tagName == 'body')
				{
					var error = true;
					break;
				}
				parent = parent.parentNode;
			}
			if (!error)
			{
				for (var i = 0; i < divs.length; i++)
				{
					if (divs[i].className == 'popup-content')
					{
						parent.innerHTML = divs[i].innerHTML;
					}
				}
				get('tmp').innerHTML = '';
				this.buyBlocked = false;
				this.className = this.className.replace(this.ee.stateWork, this.ee.stateReady);
			}
		}
	}

	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;
	}
}