	function jsCookieValue (cookieName) {
  		var exp = new RegExp (escape(cookieName) + "=([^;]+)");
	    exp.exec (document.cookie + ";");
    	return unescape(RegExp.$1);
  	}

    function getCartSession() {
		//$cart=getCookieValue ("cart");
		$cart=jsCookieValue ("cart");

		if (!$cart) $cart="";
		return ($cart);
 	}

	function putCartSession($cart) {
		// this will actually get called by the php putCartSession()
	   	//if (!writeSessionCookie ("cart", $cart)) alert("Unable to write cookie");
	     document.cookie = escape("cart") + "=" + escape($cart) + "; path=/";
	}

	function UnhideElement($element) {
		if (document.getElementById($element).className=="hide")
			document.getElementById($element).className="show";
		else
			document.getElementById($element).className="hide";
		return false;
	}

 	function deleteCartItem($id) {
		$cart = getCartSession();
        if (!$cart) return; // nothing to delete

		// delete item from cart

		$items = $cart.split(',');
		$newcart = '';
		for ($i=0;$i<$items.length;$i++) {
			$item=$items[$i];

            $x=$item.indexOf("*");
            if ($x>0) {
            	//$qty=$item.substr(0,$x);
                $item=$item.substr($x+1);
			}
			if ($id != $item) {
				if ($newcart != '') $newcart = $newcart + ','
				$newcart = $newcart + $items[$i]; // leave as is
			}

		}
		putCartSession($newcart);
	}

 	function addCartQty($id,$qty) {
		//deleteCartItem($id);	
		$cart = getCartSession();
		
		if ($qty>0) {
        	if ($cart!="") $cart+=",";
            if ($qty==1) $cart+=$id; else $cart+=$qty+"*"+$id;
		}
		putCartSession($cart);
	}

 	function updateCartQty($id,$qty) {
		deleteCartItem($id);		
		$qty=parseInt($qty,10);
		if ($qty>0) addCartQty($id,$qty);
	}

	function confirmClearCart() {
        if (window.confirm("Are you sure you want to clear the cart?")) {
            return true;
        }
 		return false;
	}


