function compressCart($cart) {
		if ($cart) {
			$items = $cart.split(',');
			$newcart = ''; $lastitem="~"; $q=0;
			for ($i=0;$i<$items.length;$i++) {
				$item=$items[$i];
                if ($item==$lastitem) {
                    $q++;
                } else {
                    if ($q>0) {
                        if ($newcart!="") $newcart+=",";
                        if ($q==1) $newcart+=$lastitem; else $newcart+=$q+"*"+$lastitem;
                    }
                    $q=1; $lastitem=$item;
                }
			}
            if ($q>0) {
                if ($newcart!="") $newcart+=",";
                if ($q==1) $newcart+=$lastitem; else $newcart+=$q+"*"+$lastitem;
                $q=0;
            }
            $cart=$newcart; 
		}
        return $cart;

}

function uncompressCart($cart) {
		if ($cart) {
			$items = $cart.split(',');
			$newcart = '';
			for ($i=0;$i<$items.length;$i++) {
                if ($newcart!="") $newcart+=",";
				$item=$items[$i];
                $x=$item.indexOf("*");
                if ($x>0) {
                    $qty=$item.substr(0,$x);
                    $item=$item.substr($x+1);
                    if ($qty>0) {
                        $newcart+=$item; $qty--;
                        while ($qty--) $newcart+=","+$item;
                    }
                } else {
                    $newcart+=$item;

                }
			}
            //alert($cart+"=>"+$newcart)
            $cart=$newcart;
		}
        return $cart;
}


    function getCartSession() {
		$cart=(getCookieValue ("cart"));
		if (!$cart) $cart="";
		return (uncompressCart($cart));
 	}
	function putCartSession($cart) {
    		if (!writeSessionCookie ("cart", compressCart($cart))) alert("Unable to write cookie");
 	}

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

    var addBtn=false;

	function addCartDelay(btn,$id,$price) {
        addBtn=btn;
        btn.className="b_add2cart adding2cart";
		document.getElementById("qty_"+$id).className="hide";
        setTimeout('addCart("'+$id+'","'+$price+'")',750);
        return false;
    }

    function addCartDelayCheck(btn,$id,$price,$qtyInStock) {
		i=document.getElementById("qty_"+$id).innerHTML;
		if (!isNaN(parseInt(i)))
            i=parseInt(i);
        else
            i=0;
		if (i+1>$qtyInStock) {
			alert("Sorry, but there is no more stock remaining");
			return false;
		}		

     	addCartDelay(btn,$id,$price);
       return false;
    }

	function addCart($id,$price) {
		// Ajax
       	//x_addCart($id, wait_cartSummary);
		$cart=getCartSession();
		if ($id) {
			if ($cart) {
				$cart = $cart + "," + $id;
			} else {
				$cart = $id;
			}
			putCartSession($cart);
		}
		document.getElementById("ajaxcart").className="show";
		i=parseInt(document.getElementById("numInCart").innerHTML)+1;
		document.getElementById("numInCart").innerHTML=i+" items";
		opacity('cart', 30, 100, 800);

		i=parseFloat(document.getElementById("priceInCart").innerHTML)+parseFloat($price);
		document.getElementById("priceInCart").innerHTML=(Math.round(i*100)/100).toFixed(2);
		
		$id="qty_"+$id;
		$qty=document.getElementById($id).innerHTML;
		//if ($qty.substr(0,1)=='x') $qty=$qty.substr(1);

		if (!isNaN(parseInt($qty)))
			i=parseInt($qty)+1;
		else
			i=1;
		document.getElementById($id).innerHTML=i;
		//document.getElementById($id).innerHTML+="<span> in cart</span>";
		document.getElementById($id).className="showQuantity";
		//opacity($id, 0, 100, 800);
        if (addBtn) addBtn.className="b_add2cart";
        addBtn=false;
		return false;
	}

    function addCartQtyDelay(btn,$id,$price,$qty) {
        addBtn=btn;
        btn.className="b_add2cart adding2cart";
		document.getElementById("qty_"+$id).className="hide";
        setTimeout('addCartQty("'+$id+'","'+$price+'","'+$qty+'")',750);
        return false;
    }

    function addCartQtyDelayCheck(btn,$id,$price,$qty,$qtyInStock) {
		i=document.getElementById("qty_"+$id).innerHTML;
		if (!isNaN(parseInt(i)))
            i=parseInt(i);
        else
            i=0;
		if (i+$qty>$qtyInStock) {
			$qty=$qtyInStock-i;
			if ($qty<=0) {
				alert("Sorry, but we are now out of stock");
				return false;
			} else {
				if (!confirm("Sorry, but we only have "+$qtyInStock+" in stock\n\nWould you like the last "+$qty+"?")) return false;
			}
		}		

     	addCartQtyDelay(btn,$id,$price,$qty);
       return false;
    }

	function addCartQty($id,$price,$qty) {
		$qty=parseInt($qty);
		$cart=getCartSession();
		if ($id) {
			i=$qty;
			while (i-- > 0) {
				if ($cart) {
					$cart = $cart + "," + $id;
				} else {
					$cart = $id;
				}
			}
			putCartSession($cart);
		}
		document.getElementById("ajaxcart").className="show";
		i=parseInt(document.getElementById("numInCart").innerHTML)+$qty;
		document.getElementById("numInCart").innerHTML=i+" items";
		i=parseFloat(document.getElementById("priceInCart").innerHTML)+parseFloat($price*$qty);
		document.getElementById("priceInCart").innerHTML=(Math.round(i*100)/100).toFixed(2);

		$id="qty_"+$id;
		i=document.getElementById($id).innerHTML;
		if (!isNaN(parseInt(i)))
            i=parseInt(i)+$qty;
        else
            i=$qty;
		document.getElementById($id).innerHTML=i;
		document.getElementById($id).className="showQuantity";

		opacity('cart', 30, 100, 800);
        if (addBtn) addBtn.className="b_add2cart";
        addBtn=false;
		
		return false;
	}

	function deleteCart($id,r) {
 		document.getElementById("qty"+$id).value="0";
		updateCart($id);
  		var i=r.parentNode.parentNode.rowIndex
  		document.getElementById('scart').deleteRow(i)

 		return false;
	}

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

	function updateCartQty($id,$qty) {
		$cart = getCartSession();
		//alert($cart);
		$q=0;
		if ($cart) {
			$items = $cart.split(',');
			$newcart = '';
			for ($i=0;$i<$items.length;$i++) {
				$item=$items[$i];
				if ($id != $item) {
					if ($newcart != '') {
						$newcart = $newcart + ',' + $item;
					} else {
						$newcart = $item;
					}
				} else {
					$q++;
					if ($q <= $qty) {
						if ($newcart != '') {
							$newcart = $newcart + ',' + $item;
						} else {
							$newcart = $item;
						}
					}
				}
			}
			$cart = $newcart;
			while ($q++<$qty) {
				if ($cart) {
					$cart = $cart + ',' + $id;
				} else {
					$cart = $id;
				}
			}
		}
		//alert($cart);
		putCartSession($cart);

	}

	function getCartQty($id) {
		// Function not used ... yet...

		$cart = getCartSession();
		$q=0;
		if ($cart) {
			$items = $cart.split(',');
			$newcart = '';
			for ($i=0;$i<$items.length;$i++) {
				$item=$items[$i];
				if ($id != $item) {
					$q++;
				}
			}
		}
	}

	function updateCart($id) {
		//Ajax
		//x_updateCart($id,document.getElementById('qty'+$id).value,wait_cartFull);

		$wasTotal=parseFloat(document.getElementById("total"+$id).innerHTML);
		$price=parseFloat(document.getElementById("price"+$id).innerHTML);
		$qty=parseInt(document.getElementById("qty"+$id).value);
		if (isNaN($qty) || isNaN($price) || $qty==0 || $price==0 ) {
			document.getElementById("total"+$id).innerHTML="0.00"; 
			$total=0;
		} else {
			$total=$price * $qty;
			document.getElementById("total"+$id).innerHTML=(Math.round($total*100)/100).toFixed(2);
		}
		$i=parseFloat(document.getElementById("total").innerHTML);
		$i=$i+$total-$wasTotal;
		document.getElementById("total").innerHTML=(Math.round($i*100)/100).toFixed(2);

		$i=parseFloat(document.getElementById("subTotal").innerHTML);
		$i=$i+$total-$wasTotal;
		document.getElementById("subTotal").innerHTML=(Math.round($i*100)/100).toFixed(2);
		updateCartQty($id,$qty);
		return false;	
	}



function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 0, 100, millisec);
    }
} 

	function addOption(selectbox,text,value )
	{
		var optn = document.createElement("OPTION");
		optn.text = text;
		optn.value = value;
		selectbox.options.add(optn);
	}


function checkEnter(e){ 
	var characterCode 

	if(e && e.which){ 
		e = e
		characterCode = e.which 
	} else{
		e = event
		characterCode = e.keyCode 
	}

	if(characterCode == 13){ 
		return true
	} else{
		return false
	}
}

