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

  	}

    function getCartSession() {
		$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 (parseInt($qty,10)>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;
	}

function error(element,msg) {
    //element.setFocus();
    document.getElementById("status").innerHTML="<p class='error'>"+msg+"</p>";
}

function validateCard() {
    var ccnumber=document.getElementById('cardNumber');
    var ccv=document.getElementById("cardVerificationNumber");
    var ccmonth=document.getElementById("cardExpiryMonth");
    var ccyear=document.getElementById("cardExpiryYear");

   var ccnum = ccnumber.value;
   var month = 0;
   var year = 0;
   var checksum = 0;
   var factor = 0;
   document.getElementById("status").innerHTML="";

   // do checksum validation on credit card number and
   // reformat so it is easy to read by a human
   ccnum = ccnum.replace(/[^0-9]/gi,"");
   if(ccnum.length < 16)
   {
      error(ccnumber,"Please enter a valid credit card number");
      return false;
   }
   if(ccnum.length %2 )
   {
      factor = 1;
   }
   else
   {
      factor = 2;
   }
   for(x=0; x< ccnum.length; x++)
   {
      digit = ccnum.charAt(x);

      if(digit * factor > 9)
      {
         checksum += (digit * factor) - 9;
      }
      else
      {
         checksum += digit * factor;
      }
      factor = (factor%2)+1;
   }
   // reformat with hyphens every 4 chars
   //ccnumber.value =  ccnum;
   ccnumber.value =  ccnum.replace(/([0-9]{0,4})([0-9]{0,4})([0-9]{0,4})([0-9]{0,4})/,"$1-$2-$3-$4");
   if(checksum % 10)
   {
      error(ccnumber,"The card number you entered is not valid.\n"+"Please try again...");
      return false;
   }
   // VISA is 4xxx xxxx xxxx xxxx
   // MC is 5{0-5}xx xxxx xxxx xxxx
   // BC is 56xx xxxx xxxx xxxx
   if ((ccnum.charAt(0)) == 4 || (ccnum.charAt(0)) == 5) {
    ;
   } else {
      error(ccnumber,"We only accept VISA and Mastercard.\n"+"Please use one of these cards to pay your account.");
      return false;
   }
   if (ccv.value.trim().length!=3) {
      error(ccnumber,"Invalid CCV number, which is the last 3 digits of the code printed on the BACK of your card in the signature panel ");
      return false;
   }
   if (ccmonth.value=="") {
      error(ccmonth,"Invalid Expiry date");
      return false;
   }
   if (ccyear.value=="") {
      error(ccyear,"Invalid Expiry date");
      return false;
   }

    return true;
}



