function Init()
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Ваш браузер не поддерживает технологию, на которой работает корзина.")
		return
	} 
}


function GetXmlHttpObject(handler)
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function CartBoxMove()
{
var x,y;
if(document.scrollHeight)
{
	x = document.scrollWidth;
	y = document.scrollHeight;
}
else if (self.innerHeight) {
    x = self.innerWidth;
    y = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
} else if (document.body) {
    x = document.body.clientWidth;
    y = document.body.clientHeight;
}

  documentHeight = document.body.scrollTop + y/2
  documentWidth = document.body.scrollLeft + x/2

  // проверяем чтобы окно не залезло на флешку сверху
	if(documentHeight - 75 < 251) documentHeight = 251 + 75
		
	boxLeft = documentWidth-175
  boxTop = documentHeight-75
	
	xdiff = boxLeft-window.oldleft
	ydiff = boxTop-window.oldtop
	
	if ((ydiff < (-1)) || (ydiff > (1))) ydiff = Math.round(ydiff / 10);
	if ((xdiff < (-1)) || (xdiff > (1))) xdiff = Math.round(xdiff / 10);

	document.getElementById("cartbox").style.left = window.oldleft + (xdiff)
	document.getElementById("cartbox").style.top  = window.oldtop + (ydiff)
	window.oldtop = window.oldtop + (ydiff)
	window.oldleft = window.oldleft + (xdiff)
	
	
	if(window.cartbox_show) setTimeout('CartBoxMove()', 10);
}

function CartBoxHide()
{
	window.cartbox_show=false;
	document.getElementById("cartbox").style.display='none';
	clearTimeout(window.dmtimeout);
	window.dmtimeout=null
}

function CartBoxShow()
{
var x,y;
if(document.scrollHeight)
{
	x = document.scrollWidth;
	y = document.scrollHeight;
}
else if (self.innerHeight) {
    x = self.innerWidth;
    y = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
} else if (document.body) {
    x = document.body.clientWidth;
    y = document.body.clientHeight;
}

  documentHeight = document.body.scrollTop + y/2
  documentWidth = document.body.scrollLeft + x/2

  // проверяем чтобы окно не залезло на флешку сверху
	if(documentHeight - 75 < 251) documentHeight = 251 + 75
		
	boxLeft = documentWidth-175
  boxTop = documentHeight-75
	
	window.oldtop = boxTop
	window.oldleft = boxLeft
	
	window.cartbox_show=true
	CartBoxMove();
	
	document.getElementById("cartbox").style.display=''
}

function CartBoxOK()
{
	qtty = document.getElementById("box_qty_input").value
	send2cart(window.tovar_id,qtty)
}

function add2cart(tovar_id,qtty,nname,pprice)
{
	if(window.cartbox_show) return
	
	window.tovar_id=tovar_id
	if(nname.length>70) nname=nname.substring(0,70)+'...'
	document.getElementById("box_name").innerHTML=nname
	document.getElementById("box_price").innerHTML=pprice
	
	if(qtty>0) send2cart(tovar_id,qtty)
	else
	{
		document.getElementById("box_message_done").style.display='none'
		document.getElementById("box_message").style.display='none'
		document.getElementById("box_qty_span").style.display='none'
		document.getElementById("box_qty_form").style.display=''

		CartBoxShow();
	}
}


function send2cart(tovar_id,qtty)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) 	return;
	
	document.getElementById("box_message_done").style.display='none'
	document.getElementById("box_qty_form").style.display='none'
	document.getElementById("box_qty_span").style.display=''
	
	document.getElementById("box_qty").innerHTML=qtty
	document.getElementById("box_message").style.display=''
	
	
	CartBoxShow();
	
	var url="/mycart/backend.php?act=send"
	url+="&tovar_id="+tovar_id
	url+="&qty="+qtty
	url+="&rnd="+Math.random()
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}





function getcart()
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) 	return;
	
	var url="/mycart/backend.php?act=get"
	url+="&rnd="+Math.random()
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		str = xmlHttp.responseText
		
		document.getElementById("box_qty_input").value=1
		document.getElementById("box_message_done").style.display=''
		document.getElementById("box_message").style.display='none'

		if(str.length>1)
		{
			document.getElementById("cart").style.display=''
			document.getElementById("cart").innerHTML=str
		}
		else
			document.getElementById("cart").style.display='none'

	} 
} 