// Shopping cart functions

function cartContents()
{ //returns an array containing the item id and quantity
		var cartItems = new Array();
		var cartCookie = getCart();
		if (cartCookie != "") 
		{
			cartItems = cartCookie.split("&");
		}
		else 
		{
			cartItems[0] = "empty";
			cartItems[1] = 0;
		}
		return cartItems;
}

function addItemToCart(itemID) 
{ //put item into cart
		//get existing contents
		var contents = cartContents();
		var cartVal = "";
		if (contents[0] != "empty") 
		{  //cart already exists
		//if not empty then check if item in cart already
		for (var i = 0; i < contents.length; i = i+2)
		    {
			if (contents[i] == itemID) 
				{ alert ("This product is already in your shopping basket. \n You can change the quantity of a product on the Shopping Cart page");
				return false;
				}
			}
		//get existing cookie values
			cartVal = getCart() + "&";
		}
		//add new item to cart
		cartVal += itemID + "&1";
		updateCart(cartVal);
		alert ("This product has been added to your shopping basket.");
		return true;
}

function removeItemFromCart(itemID) 
{ //remove item from cart
		var contents = cartContents();
		var cartVal = "";
		for (var i = 0; i < contents.length; i = i+2)
		    {
			if (contents[i] != itemID) 
			   {	
			   	  if (cartVal == "") cartVal += contents[i] + "&" + contents[i+1]
				  else cartVal += "&" + contents[i] + "&" + contents[i+1];        
			   }
			}
		updateCart(cartVal);
}

function changeItemQuantity(itemID, quantity) 
{ // changes item quantity if zero remove item
	if (quantity == '0') removeItemFromCart(itemID)
	else
	{
		var contents = cartContents();
		var cartVal = "";
		for (var i = 0; i < contents.length; i = i+2)
		    {
			if (contents[i] != itemID) 
			   	{	
			   	  if (cartVal == "") cartVal += contents[i] + "&" + contents[i+1]
				  else cartVal += "&" + contents[i] + "&" + contents[i+1];        
			   	}
			else
				{	
			   	  if (cartVal == "") cartVal += contents[i] + "&" + quantity
				  else cartVal += "&" + contents[i] + "&" + quantity;        
			   	}
			}
		updateCart(cartVal);
	}
}

function getCart()
{ //reads the cart cookie
  var cookieValue = "";
  var search = "cart=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function updateCart(cartContents)
{ // adds or replaces cart cookie, sets cookie to last for one month
  		//expireDate = new Date;
		//expireDate.setMonth(expireDate.getDate()+1);
		document.cookie = "cart=" + cartContents + ";expires=" //+ expireDate.toGMTString();
}

function updateShoppingCart()
{
	var cartVal = ""
	for (var i = 0; i < ((document.shoppingcart.length) - 3); i = i+2)
	{
		if (document.shoppingcart.elements[i+1].value != 0) //remove item if quantity is zero
		 cartVal += document.shoppingcart.elements[i].value + "&" + document.shoppingcart.elements[i+1].value + "&"
	}
	//cartVal += document.shoppingcart.elements[(document.shoppingcart.length)-3].value + "&" + document.shoppingcart.elements[(document.shoppingcart.length)-2].value
	//
	cartVal = cartVal.substring(0, cartVal.length-1)
	//return cartVal
	updateCart(cartVal);
}

function termsWindow()
{
	termsWindow = window.open("terms.htm", "tWin", "width=340,height=250,toolbar=no,location=no,scrollbars=yes")
}

function isBlank(s) {
	for (var i = 0; i< s.length; i++) {
	 var c = s.charAt(i);
	 if ((c != ' ') && (c != '\n') && (c != '')) return false;
	}
	return true;
}

function checkformOK()
{
	var msg;
	var empty_fields = "";
	
	document.AddressForm.country.optional = true;
	document.AddressForm.county.optional = true;
	
   if (document.AddressForm.PaymentMethod.value != "CCSS") 
   {
	document.AddressForm.customerpw.optional = true;

	var pw = document.AddressForm.customerpw

	if ((pw.value != null) && (pw.value != "") && !isBlank(pw.value)) 
	{
		document.AddressForm.address.optional = true;
		document.AddressForm.town.optional = true;
		document.AddressForm.postcode.optional = true;
		document.AddressForm.telephone.optional = true;
	}
	else
	{
		document.AddressForm.address.optional = false;
		document.AddressForm.town.optional = false;
		document.AddressForm.postcode.optional = false;
		document.AddressForm.telephone.optional = false;
	}
  }

	for (var i = 0; i < document.AddressForm.length; i++) 
	 {
	  var e = document.AddressForm.elements[i];
	  if (!e.optional) 
	  {
	   if ((e.value == null) || (e.value == "") || isBlank(e.value)) 
	   {
	 	empty_fields += "\n             " + e.name;
		continue; 
	   }
	  }	 
	 }
	 
	 if (!empty_fields) 
	 {
		if (!document.AddressForm.terms.checked) 
		{
		alert("Please read our Terms & Conditions of Sale \n and tick the box to show your acceptance of them")
		return false
		}
	  return true
	 }
	
/**/ 
	 msg  = "________________________________________________\n\n"
	 msg += "Sorry we couldn't process your order because \n";
	 if (empty_fields) 
	 {
	 msg += "the following required fields are empty:";
	 msg += empty_fields + "\n";
	 msg += "________________________________________________\n\n"
	 }
	 msg += "Please complete these and try again.\n";
	 alert(msg);
	 return false;  
}


