function checkout(url)
{
	form=document.cart;
	form.action=url;
	form.target = "_self";
	form.submit();
}

function itemInCart()
{
	productId = document.fItems.productId.value;
	if (document.cart)  // if cart is not empty
	{
	elements=document.cart.elements;
		for (i=0; i < elements.length; i++)
		{
			var name=elements[i].name.substring(0, 9);
			if (name == "productId")
			{
				if (elements[i].value == productId)
				{
					return true;   // item already in shopping cart
				}
			}
		}
		return false;
	}
	else return false;
}

function empty_cart(form)
{
	if (!confirm("Are you sure you want to empty all items in the shopping cart?")) return;
	form.act.value="empty_cart";
	form.action = "";
	form.target = "_self";
	form.submit();
}

function before_update(form)
{
	var elements;
	elements=form.elements;
	for (i=0; i < elements.length; i++)
	{
		var name=elements[i].name.substring(0, 3);
		if (name == "qty" && elements[i].name.length != 3) // qty is the array of quantity, qty0, qty1 are the quantity of the shopping cart
		{
			if (elements[i].value == "0")
			{
				if (!confirm("Are you sure you want to remove this item from your shopping cart?")) return;
			}
			else if (!verify_qty(form.elements[i].value)) return;
			else if (parseInt(elements[i].value) < parseInt(elements[i+1].value))
			{
				alert("The quantity you have specified is less than the minimum quantity. Please try again.");
				return;
			}
		}
	}
	form.act.value="update_qty";
	form.action = "";
	form.target = "_self";
	form.submit();
}

function verify_qty(quantity)
{
   var message="The quantity you specified is invalid. Please re-enter a positive whole number in the Qty. field";
   if (isNaN(quantity)) {
      alert(message);
      return false;
      }
   else if (quantity < 0) {
      alert(message);
      return false;
      }
   else if (quantity.indexOf(".") != -1) {
      alert(message);
      return false;
      }
   else {
      return true;
      }
}

function verify_zip(zip)
{
   var message="The zip code you specified is invalid.  Please re-enter a 5 digit zip code";
   if (isNaN(zip)) {
      alert(message);
      return false;
      }
   else if (zip.length != 5) {
      alert(message);
      return false;
      }
   else {
      return true;
      }
}

function shipping_cost(form)
{
	if (!verify_zip(form.zipcode.value)) return;
	form.action = "shipping.php";
	form.target = "_blank";
	form.submit();
}
