// JavaScript Document

//start of shopping basket stuff............

var basketWin;
var offersWin;
var itemID;
var shoppingListIDs = new Array(50);
var shoppingListQty = new Array(50);
//var shoppingListMat = new Array(50);
var maxItems = 49;
var itemsInBasket = 0;
var currentItem = 1;

function newBasketWindow(basketlink)
{

	var sWidth = screen.width;
	var sHeight = screen.height;	
	var offsetX;
	var offsetY;

		offsetX = (sWidth-420)/2;
		offsetY = (sHeight-350)/2;
		sWidth = 500;
		sHeight = 350;

	 if ((basketWin) && (!(basketWin.closed)))	
		{
		basketWin.close();
		}

        basketWin = window.open(basketlink,"basket",config = 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=no,copyhistory=no');
	//basketWin.resizeTo(sWidth,sHeight);
	basketWin.moveTo(offsetX,offsetY);
        basketWin.focus(); 
}

function addToBasket(itemID)
{

	if (itemsInBasket == maxItems) {alert('Cant fit any more into your basket !');return;}

	itemLocationSlot = 0;
	for (rummage = 1; rummage <= itemsInBasket; rummage++)
		{
		if ((shoppingListIDs[rummage] == itemID) ) { itemLocationSlot = rummage;}
		}

	if (itemLocationSlot > 0)
		{
		shoppingListQty[itemLocationSlot]++;
		currentItem = itemLocationSlot;
		}
	else
		{
		itemsInBasket++;
		shoppingListQty[itemsInBasket]=1;
		shoppingListIDs[itemsInBasket]=itemID;
		//shoppingListMat[itemsInBasket]=material;
		currentItem = itemsInBasket;
		}

	refreshBasketView();
	alert("The item was added to your basket");
	
}

function removeFromBasket(itemID)
{
	itemLocationSlot = 0;
	for (rummage = 1; rummage <= itemsInBasket; rummage++)
		{
		if ((shoppingListIDs[rummage] == itemID) ) { itemLocationSlot = rummage;}
		}

	if (itemLocationSlot > 0)
		{
		setQty(itemID,shoppingListQty[itemLocationSlot]-1);
		}
}


function setQty(itemID,itemQty)
	{
	itemLocationSlot = 0;
	for (rummage = 1; rummage <= itemsInBasket; rummage++)
		{
		if ((shoppingListIDs[rummage] == itemID) ) { itemLocationSlot = rummage;}
		}

	if (itemLocationSlot > 0)
		{
		if (itemQty < 1)
			{
			shoppingListIDs[itemLocationSlot] = null;
			shoppingListQty[itemLocationSlot] = null;
			//shoppingListMat[itemLocationSlot] = null;
			for (rummage = itemLocationSlot;rummage <= itemsInBasket;rummage++)
				{
				shoppingListIDs[rummage]=shoppingListIDs[rummage+1];
				shoppingListQty[rummage]=shoppingListQty[rummage+1];
				//shoppingListMat[rummage]=shoppingListMat[rummage+1];
				}
			shoppingListIDs[itemsInBasket] = null;
			shoppingListQty[itemsInBasket] = null;
			//shoppingListMat[itemsInBasket] = null;
			itemsInBasket--;
			}
			else
			{
			shoppingListQty[itemLocationSlot] = itemQty;
			}
		}
	
	refreshBasketView();
	
}
	
				
function newBasket()
{
	shoppingListIDs = new Array(50);
	shoppingListQty = new Array(50);
	//shoppingListMat = new Array(50);
	itemsInBasket = 0;
	currentItem=1;
	
	openBasketView();
	//quickView();
}
	

function generateBasketCall()
{
	var basketlink;
	// basketlink='../cgi-bin/refreshbasket.pl?'; // Webworlds version
	basketlink='http://www.nsnewmedia.co.uk/cgi-bin/otb_refreshbasket.pl?'; // A1 version
	for (rummage = 1;rummage <= itemsInBasket;rummage++)
		{
		basketlink = basketlink + '-' + shoppingListIDs[rummage] +','+shoppingListQty[rummage];
		}
	return basketlink;
}


function openBasketView()
{
	if ((basketWin==null) || (basketWin.closed)) {newBasketWindow(generateBasketCall());}

	basketWin.focus();	
}
	
			
function refreshBasketView()
{
	if ((basketWin==null) || (basketWin.closed)) {return;}
	basketWin.close();
	newBasketWindow(generateBasketCall());
}

//............end of shopping basket stuff

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

