function ajs ( url, p, obj, f )
{
	var x;
	try
	{
		x = new XMLHttpRequest();
	}
	catch(e)
	{
		var xv = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0");
		for( var i = 0; i < xv.length && !x; i++ )
		{
			try
			{
				x = new ActiveXObject(xv[i]);
			}
			catch(e)
			{
				alert("ActiveX error!")
			}
		}
	}
	if ( !x )
	{
		return false;
	}
	else
	{
		try
		{
			if ( p.length > 0 ) p = "?" + p;
			x.open("GET", url + p, true);
			x.onreadystatechange = function()
			{
				if ( x.readyState == 4 )
				{
					if ( x.status == 200 || x.status == 404 || x.status == 403 || x.status == 0 )
					{
						if ( obj ) obj.innerHTML = x.responseText;
						if ( f ) f(x);
					}
					/*else
					{
						alert('XMLHttpRequest: '+x.status);
					}*/
				}
			}
			x.send(null);
		}
		catch(e)
		{
			return false;
		}
	}
	return true;
}

function h2s(a)
{
	if(typeof(a)!="object" || a==null)
	{
		if(typeof(a)=="string")
			a='"'+encodeURI(a)+'"';
		return a;
	}
	var res='{',z='"';
	for(var k in a)
	{
		res+=z+k+'":'+h2s(a[k]);z=',"';
	}
	return res+"}";
}
function s2h(a)
{
	return eval('('+a+')');
}

function menuOut(m, s)
{
	var d = - parseInt(m.style.marginTop);
	if (d > 0)
	{
		m.style.marginTop = - d + Math.min(s, d) + 'px';
		m.parentNode.timer = setTimeout(function(){menuOut(m, s)}, 1);
	}
	else
		clearTimeout(m.parentNode.timer);
}

function menuIn(m, s)
{
	var d = - parseInt(m.style.marginTop);
	if (d < m.offsetHeight)
	{
		m.style.marginTop = - d - Math.min(s, m.offsetHeight - d) + 'px';
		m.parentNode.timer = setTimeout(function(){menuIn(m, s)}, 1);
	}
	else
		clearTimeout(m.parentNode.timer);
}

function menuSH(o)
{
	var m = o.nextSibling.childNodes[0];
	if (o.nextSibling.timer)
		clearTimeout(m.parentNode.timer);
	if (m.className == 'open') // если открыто
	{
		m.style.display = 'block';
		m.style.marginTop = '0px';
		m.className = '';
		menuIn(m, 30);
	}
	else
	{
		m.className = 'open';
		m.style.marginTop = - m.offsetHeight + 'px';
		menuOut(m, 30);
	}
	
}

function optionChange(url, param, value)
{
	if (value == 'none')
	{
		if (window.location != url) window.location = url;
	}
	else
	{
		window.location = url+param+value;
	}
}

function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function delElement(obj)
{
	if (typeof(obj) == 'string')
		obj = document.getElementById(obj);
	obj.parentNode.removeChild(obj);
}

function getArray(str)
{
	var d = document.createElement("div");
	d.innerHTML = str;
	var text = trim(d.innerHTML);
	return text.split('|');
}

function ajaxSubmit(form, url, obj, f)
{
	var elCount = form.elements.length;
	var p = '';
	for(var i=0; i<elCount; i++)
	{
		if (form.elements[i].name)
			p += form.elements[i].name+'='+encodeURIComponent(form.elements[i].value)+'&';
	}
	p = p.substring(0, p.length-1);
	ajs(url, p, obj, f);
	return false;
}

function ajaxLoad(url, obj, f)
{
	var elCount = form.elements.length;
	var p = '';
	for(var i=0; i<elCount; i++)
	{
		if (form.elements[i].name)
			p += form.elements[i].name+'='+encodeURIComponent(form.elements[i].value)+'&';
	}
	p = p.substring(0, p.length-1);
	ajs(url, p, obj, f);
	return false;
}

function ajaxCartAdd(obj)
{
	var data = getArray(obj.responseText);
	var out = document.getElementById('basketinfo');
	out.innerHTML = data[0]; 
	showNotify(data[1], 1500);
}

function ajaxChangeHeader(obj)
{
	var list = getArray(obj.responseText);
	var out = document.getElementById('btnBasket');
	out.innerHTML = list[0];
	out.title = list[1];
	var tdTotal = document.getElementById('total');
	tdTotal.innerHTML = list[2];
	showNotify(list[3], 1500);
}

function ajaxDeleteHeader(obj)
{
	if(obj.responseText.indexOf('false') == -1)
	{
		var list = getArray(obj.responseText);
		var out = document.getElementById('basketinfo');
		out.innerHTML = list[1];
		var tdTotal = document.getElementById('total');
		delElement('cartItem'+list[0]);
	}
}

function ajaxLoginHeader(obj)
{
	if (obj.status == 200)
	{
		form = document.getElementById('loginFormH');
		window.location.reload();
	}
}

function ajaxLogoutHeader(obj)
{
	if (obj.status == 200)
	{
		form = document.getElementById('loginFormH');
		window.location.reload();
	}
}

function showNotify(html, timeout)
{
	var hint = document.createElement("div");
	hint.id = 'notify';
	hint.innerHTML = html;
	hint.timeoutId = window.setTimeout("hideNotify()", timeout);
	document.body.appendChild(hint);
}

function hideNotify()
{
	var obj = document.getElementById('notify');
	if (typeof(obj) != 'undefined')
	{
		clearTimeout(obj.timeoutId);
		delElement(obj);
	}
	return false;
}