var modalbox;
var shade;
var scrollWidth;
var scrollHeight;
var price;
var gid;
var byRequest = false;

window.onload = function() 
{
	modalbox = $("basketBox");
	shade = $("basketShade");

	scrollWidth = document.body.scrollWidth;
	scrollHeight = document.body.scrollHeight;
	
	$("main").style.width = scrollWidth;
	$("main").style.height = scrollHeight;
}

function addToBasket(goodId, byReq) 
{
	$("main").style.filter = "alpha(opacity=40)";
	$("main").style.opacity = 0.4;
	$("main").style.display = "block";
	
	gid = goodId;
	byRequest = byReq;
	
	// fill good name
	$('basketName').innerHTML = $('row' + goodId + 'Name').innerHTML;
	
	// fill price
	$('basketPrice').innerHTML = $('row' + goodId + 'Price').innerHTML;
	
	var tmpPrice = $('row' + goodId + 'Price').innerHTML;
	
	var posCODE = tmpPrice.indexOf(' RUB');
	var isRUB = true;
	if (posCODE == -1) {
		posCODE = tmpPrice.indexOf(' EUR');
		isRUB = false;
	}
	
	tmpPrice = tmpPrice.substr(0, posCODE).replace(' ', '');
	
	price = parseFloat(tmpPrice);
	
	$('totalPrice').innerHTML = priceParse(price) + ' ' + (isRUB ? 'RUB' : 'EUR');
	
	modalbox.style.display = "block";
	shade.style.display = "block";
	
	posleft = Math.round(scrollWidth / 2) - 150;
	postop = document.body.scrollHeight - 600;
	
	shade.style.left = posleft + 7;
	shade.style.top = document.body.scrollTop + 307;
	
	modalbox.style.left = posleft;
	modalbox.style.top = document.body.scrollTop + 300;
}

function HideBasketBox() 
{
	$("main").style.display = "none";
	modalbox.style.display = "none";
	shade.style.display = "none";
}

function onBasketHandlerB(e)
{
	var posCODE = $('totalPrice').innerHTML.indexOf(' RUB');
	var isRUB = true;
	if (posCODE == -1) {
		posCODE = $('totalPrice').innerHTML.indexOf(' EUR');
		isRUB = false;
	}
	if (parseInt($('qty').value) > 0) 
		$('totalPrice').innerHTML = priceParse(price * parseInt($('qty').value)) + ' ' + (isRUB ? 'RUB' : 'EUR');
	else if ($('qty').value.length > 0) {
		$('totalPrice').innerHTML = priceParse(price) + ' ' + (isRUB ? 'RUB' : 'EUR');
		$('qty').value = 1;
	} else 
		$('totalPrice').innerHTML = '';
}

function addToBasketSubmit() 
{
	if ($('qty').value == '' || $('qty').value == '0') {
		alert('Для продолжения, введите количество товара');
		return;
	}
	
	with(document.basket) {
		GCNT.value = $('qty').value;
		GID.value = gid;
		BYR.value = (byRequest ? 1 : 0);
		submit();
	}
}