function submitform(productnumber)
{
	var	list = document.forms[productnumber].selSize;
	if (list !=	null)
		appendCartCookie(list.options[list.selectedIndex].value);
	else
		appendCartCookie(document.forms[productnumber].hidBarcode.value);
		
	showItems();
	window.location = "basket.aspx";
}

function URLencode(sStr) 
{
	return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function appendCartCookie(sBarcode)
{
  var currCart = ReadCartCookie();
 
	if (currCart != "")
	{
		var barcodePos = currCart.indexOf(sBarcode)
		if (barcodePos != -1)
		{
	  		// This item already exists - increase the quantity
			var sizeStart = currCart.indexOf('[',barcodePos);
			var sizeEnd = currCart.indexOf(']', sizeStart);
			
			document.cookie = "cartData=" + currCart.substring(0,sizeStart+1) + 
												(parseInt(currCart.substring(sizeStart+1, sizeEnd)) + 1) +
												currCart.substring(sizeEnd, currCart.length);
	  }
	  else		// This is a new item, just add it.
			document.cookie = "cartData=" + currCart + "|" + escape(sBarcode) + "[1]";
	}
	else
		document.cookie = "cartData=" +"|" + sBarcode + "[1]";
}


function updateCartCookie(sBarcode, formNo)
{
  	var currCart = ReadCartCookie();
   
	if (currCart != "")
	{
		var barcodePos = currCart.indexOf(sBarcode)
		if (barcodePos != -1)
		{
	  		// This item already exists - increase the quantity
			var sizeStart = currCart.indexOf('[',barcodePos);
			var sizeEnd = currCart.indexOf(']', sizeStart);
					
			document.cookie = "cartData=" + currCart.substring(0,sizeStart+1) + 
												document.forms[formNo].txtQty.value +
												currCart.substring(sizeEnd, currCart.length);
		}
		else
		   alert ("error updating product");
	}
	else
		alert ("error updating product");
	
	// reload the page
	location.reload();
}

function deleteCartItem(sBarcode)
{
	var currCart = ReadCartCookie();  
	if (currCart != "")
	{
		
		var barcodePos = currCart.indexOf(sBarcode)
		if (barcodePos != -1)
		{
	  		// The item exists
			var sizeEnd = currCart.indexOf(']', barcodePos);
			
			var sCookie = "cartData=" + currCart.substring(0,barcodePos-1) +
												currCart.substring(sizeEnd+1, currCart.length);
			document.cookie = sCookie;
		}
		else
			alert ("Error deleting item");
	}
	else
		alert ("Error deleting item");
	
	// reload the page
	location.reload();
	return;
}

function CountCartItems()
{
	var currCart = ReadCartCookie();
	var cartArray = currCart.split("|");
	var numItems = parseInt(0);
	
	if (currCart != "")
	{
		for (i = 0; i < cartArray.length; i++)
		{
			var sizeStart = cartArray[i].indexOf('[',0);
			var sizeEnd = cartArray[i].indexOf(']', sizeStart);

			if (sizeStart != -1 && 
				sizeEnd != -1)
			{		
				numItems += parseInt(cartArray[i].substring(sizeStart+1, sizeEnd));
			}
		}
	}
	return numItems;
}

function showItems()
{
	var cartDisp = document.getElementById('cartDisp');
	var itemCount = CountCartItems();
	if (itemCount <= 0)
		cartDisp.innerHTML = "view basket (empty)";
	if (itemCount == 1)
		cartDisp.innerHTML = "view basket (" + itemCount + " item)";
	if (itemCount > 1)
		cartDisp.innerHTML = "view basket (" + itemCount + " items)";

		
}

function ReadCartCookie() 
{
	var cookieName = "cartData";
	var theCookie = document.cookie;
	var ind = theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") 
		return ""; 
	
	var ind1 = theCookie.indexOf(';',ind);
	if (ind1==-1) 
		ind1=theCookie.length; 
		
	return theCookie.substring(ind+cookieName.length+1,ind1);
}

function killCartCookie()
{
	var kill_date = new Date("January 1, 1970");
	document.cookie = "cartData=stub;expires=" + kill_date;
}

function submitLocation()
{
	document.frmLocation.submit();
}

function submitCart()
{
	var f = document.frmLocation;
	var selLoc = -1;
	var i;	
	
	for (i=0 ; i < f.radLoc.length; i++) 
	{
		if (f.radLoc[i].checked) 
			selLoc = i;
	}

	if (selLoc == -1) 
	{
		alert("You must select your shipping location before you can proceed to the checkout.");
		return;
	}
		
	document.frmPPCart.submit();
}

///////////////////////////////////////////////////
// Not cart related..
///////////////////////////////////////////////////

// The small image viewer (list is shown as thumbnails)
function selectImage(imageFilename)
{
	var imageloc;
	
	imageloc = document.getElementById("main_image");
	imageloc.src = imageFilename;
}

// The large image viewer (list is contained in a dropdown)
function changeImage()
{
	var list = document.getElementById('selOptionList');
	var imageloc;
	if (list != null)
	{
		imageloc = document.getElementById("main_image");
		imageloc.src = list.options[list.selectedIndex].value;
	}
}

function PrevImage()
{
	var list = document.getElementById('selOptionList');
	if(list.selectedIndex == 0)
		list.selectedIndex = list.options.length-1;
	else
		list.selectedIndex-=1;

	changeImage();
}

function NextImage()
{
	var list = document.getElementById('selOptionList');
	if(list.selectedIndex == list.options.length-1)
		list.selectedIndex = 0;
	else
		list.selectedIndex+=1;

	changeImage();
}

// Search functions
function musicsearch()
{
	if (document.forms[0].search.value != "")
		document.forms[0].submit();
}

function clothessearch()
{
	if (document.forms[1].search.value != "")
		document.forms[1].submit();
}

function productsearch()
{
	if (document.forms[2].search.value != "")
		document.forms[2].submit();
}