
/*
 * type item type
 */
function addPurchaseItem(name) {

  var cookie = getCookie(name);
  
  if (cookie == null || cookie == "NaN") {
    setCookie(name, "1");
  }
  else {
    setCookie(name, "" + (Number(cookie) + 1));
  }
}



/*
 * type item type
 */
function removePurchaseItem(name) {

  var cookie = getCookie(name);
  
  if (cookie == null || cookie == "NaN") {
    return;
  }
  
  var cookieNum = Number(cookie);
  
  if (cookieNum == 1) {
    deleteCookie(name);
  }
  else if (cookieNum > 1) {
    cookieNum = cookieNum - 1;
    setCookie(name, "" + cookieNum);
  }
  else {
    deleteCookie(name);
  }
}



/*
 * name      name of the cookie
 * value     value of the cookie
 * [expires] expiration date of the cookie (defaults to end of current session)
 * [path]    path for which the cookies is valid (defaults to path of calling document)
 * [domain]  domain for which the cookie is valid (defaults to domain of calling document)
 * [secure]  boolean value indicating if the cookie transmission requires a secure transmission
 *
 * A default argument will be used if the trailing argument is omitted or if the argument is null.
 */
 
function setCookie(name, value, expires, path, domain, secure) {

  var curCookie = name + "=" + escape(value) +
                  ((expires) ? "; expires=" + expires.toGMTString() : "") +
                  ((path)    ? "; path="    + path                  : "") +
                  ((domain)  ? "; domain="  + domain                : "") +
                  ((secure)  ? "; secure"   : "");
                   
  document.cookie = curCookie;
}
 
 
 
/*
 * name name of the desired cookie
 *
 * return string containing value of specified cookie, or null if cookie does not exist
 */
  
function getCookie(name) {

  if (document.cookie.length > 0) {
  
    var c_start = document.cookie.indexOf(name + "=");
    
    if (c_start != -1) {
    
      c_start = c_start + name.length + 1;
      var c_end = document.cookie.indexOf(";", c_start);
      
      if (c_end == -1) {
        c_end = document.cookie.length;
      }
      
      return unescape(document.cookie.substring(c_start, c_end));
    }
  }
  
  return "";
}
 
 
 
/*
 * name     name of the cookie
 * [path]   path of the cookie (must be same as path used to create cookie)
 * [domain] domain of the cookie (must be same as domain used to create cookie)
 *
 * A default argument will be used if the trailing argument is omitted or if the argument is null.
 */
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
                      ((path)   ? "; path="   + path   : "") +
                      ((domain) ? "; domain=" + domain : "") +
                      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}