var basket;
var basketwindow;

function ShowBasket()
{
	/* ShowPopUpWindow('basket', basehref+'viewbasket.cfm', 635, 480, 'yes', 'no', 'yes'); */
	if(basketwindow){
		if(!basketwindow.closed) basketwindow.close()
	}

	l = ((screen.width / 2) - (635 / 2))
	t = ((screen.height / 2) - (480 / 2))

	winstring = "width=635, height=480,left=" + l + ",top=" + t +  ",status=yes,resizable=no,scrollbars=yes";

	basketwindow = window.open(basehref+'viewbasket.cfm','basket',winstring);
	basketwindow.focus();

	return;
}

function refreshViewBasket()
{
	if(basketwindow){
	
		basketwindow.document.location.href = basketwindow.document.location.href;
		
		/* if(!basketwindow.closed) basketwindow.close()
		
		l = ((screen.width / 2) - (635 / 2))
		t = ((screen.height / 2) - (480 / 2))

		winstring = "width=635, height=480,left=" + l + ",top=" + t +  ",status=yes,resizable=no,scrollbars=yes";

		basketwindow = window.open(basehref+'viewbasket.cfm','basket',winstring); */
	}

	return;
}

function SewCookie(s)
{
	var ExpDate = new Date();
	ExpDate.setTime(ExpDate.getTime() + 43200000);
	strcookie="BTSheets=" + escape(s) + ";path=/; expires=" + ExpDate.toGMTString();
	
	document.cookie=strcookie;
}

/* function SewCookie(s)
{
	var todaysdate=new Date();
	var ExpDate=new Date(todaysdate.getYear(),todaysdate.getMonth(),todaysdate.getDate(),23,59,59,0);
	ExpDate=ExpDate.toGMTString();
	ExpDate=ExpDate.replace("UTC","GMT");
	strcookie="BTSheets=" + escape(s) + " ; Expires=" + ExpDate + " ; path = /";
	document.cookie=strcookie;
} */

function add2order(id,qty,col,descrip)
{
	if(basket != "") basket += ':';
	basket += id+','+qty+','+col+','+descrip;
	SewCookie(basket);
}

function removeorder(id,qty,colour)
{
	var tmpArray;
	var item;
	var found = false;

	tmpArray = basket.split(':');
	basket = "";
	
	for( var i = 0; i < tmpArray.length; ++i )
	{
		item = tmpArray[i].split(',');
		if( !found && item[0] == id && item[1] == qty && item[2] == colour )
		{
			found = true;
		}
		else {
			if( i != 0 ) basket += ':';
			basket += tmpArray[i];
		}
	}
	SewCookie(basket);
}

function AddToBasket(id,PriceGridID,bloadindex)
{

	var tmpArray;
	var pair;
	
	var qty = 0;
	
	tmpArray = basket.split(':');
	for( var i = 0; i < tmpArray.length; ++i )
	{
		pair = tmpArray[i].split(',');
		
		if( pair[0] == id )
		{
			qty = pair[1];
			break;
		}
	}
	if( qty == 0 ) ShowPopUpWindow('qtyPopup', basketbasehref + 'qtypopup.cfm?id=' + id + '&gid=' + PriceGridID + '&bloadindex=' + bloadindex, 625, 280, 'yes', 'no', 'yes')
	else ShowPopUpWindow('qtyPopup', basketbasehref + 'qtypopup.cfm?id=' + id + '&qty=' + qty + '&gid=' + PriceGridID + '&bloadindex=' + bloadindex, 625, 280, 'yes', 'no', 'yes');
	
}

function clearbasket()
{
	basket = "";
	SewCookie(basket);
}

function OpenQtyPopup(id,PriceGridID,bloadindex)	
{
	AddToBasket(id,PriceGridID,bloadindex);
}