function GetE(id) {
	return document.getElementById(id);
}

function GetN(name) {
	return document.getElementsByName(name);
}

function GetT(tag) {
	return document.getElementsByTagName(tag);
}

function CountSymbols(Element, MaxLength) {
	if (Element.value.length>MaxLength) 
		Element.value=Element.value.substring(0, MaxLength);
	GetE('c_'+Element.id).innerHTML=', осталось: '+(MaxLength-Element.value.length);
}

function ReadCookie(name) {
	var Cooks=document.cookie;
	var startIndex=Cooks.indexOf(name);
	if (startIndex==-1) 
		return false;
	startIndex+=name.length+1;
	var endIndex=Cooks.indexOf(';', startIndex);
	if (endIndex==-1) 
		endIndex=Cooks.length;
	var CookValue=Cooks.substring(startIndex, endIndex);
	return unescape(CookValue);
}

function CheckCookie(Cookie) {
	if (!Cookie) {
		alert('Страница использует механизм временных cookie.\nДля продолжения включите cookie в своём обозревателе и обновите страницу.');
		return false;
	}
	return true;
}

function getXmlHttp() {
	var xmlHttp=null;
	try {
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {
			xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	return xmlHttp;
}

function CartAdd(Form, Location) {
	if (!CheckCookie(document.cookie)) 
		return false;

	var req=getXmlHttp();
	if (!req) 
		return true;

	req.open('POST', Location);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.onreadystatechange=function() {
		if (req.readyState==4) {
			try {
				var Response=req.responseText;
			}
			catch (e) {
				alert('Ошибка при обработке запроса.');
			}

			var Response=Response.split('##');
			var CartInfo=GetE('CartInfo');
			var CartNone=GetE('CartNone');
			if (Response.length>1) {
				GetE('CartAmount').innerHTML=Response[0];
				GetE('CartSumm').innerHTML=Response[1];
				if (CartInfo.style.display=='none') {
					CartInfo.style.display='block';
					CartNone.style.display='none';
				}
				alert('Товар добавлен в корзину.');
			}
			else 
				alert(Response);
		}
	};
	req.send('amount='+Form.amount.value+'&ajax=1');

	return false;
}

function TestBuyerInfo(Form, Send, Location) {
	var req=getXmlHttp();
	if (!req) 
		return true;

	req.open('POST', Location);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.onreadystatechange=function() {
		if (req.readyState==4) {
			var Response=req.responseText.split('#');
			if (Response.length>1) {
				alert(Response[1]);
				Form.elements[Response[0]].focus();
			}
			else 
				Form.submit();
		}
	};
	req.send(Send+'&ajax=1');

	return false;
}

function CodeRefresh() {
	var src=GetE('code_image').src;
	var rand=Math.random().toString();
	src=src.slice(0, src.lastIndexOf('/')+1)+rand.slice(2, 7)+src.slice(src.lastIndexOf('.'))
	GetE('code_image').src=src;
	return false;
}

function RightBarStatus() {
	var Expiry=new Date();
	var Cookie;
	Expiry.setFullYear(Expiry.getFullYear()+1);
	if (GetE('RightBar').style.display=='none') {
		GetE('RightBar').style.display='';
		GetE('RightBarButton').style.backgroundPosition='';
		GetE('RightBarButton').alt=GetE('RightBarButton').title='Скрыть';
		Cookie='hiderightbar=0; expires= '+Expiry.toGMTString()+'; path=/';
	}
	else {
		GetE('RightBar').style.display='none';
		GetE('RightBarButton').style.backgroundPosition='right';
		GetE('RightBarButton').alt=GetE('RightBarButton').title='Показать';
		Cookie='hiderightbar=1; expires='+Expiry.toGMTString()+'; path=/';
	}
	document.cookie=Cookie;
	return false;
}

function RightBarStatusLoad() {
	var HideRightBar=ReadCookie('hiderightbar');
	if (HideRightBar==1) 
		RightBarStatus();
}