// cart.js

function Cart() {
 var cart = getCookie("cart")
 this.product = new Array()
 this.quantity = new Array()
 if(cart != "") {
  var pairs=cart.split("|")
  for(var i=0;i<pairs.length;++i) {
   var pairSplit=pairs[i].split("-")
   this.product[i] = pairSplit[0]
   this.quantity[i] = pairSplit[1]
  }
 }
 this.save = Cart_save
 this.addProduct = Cart_addProduct
 this.deleteProduct = Cart_deleteProduct
 this.changeQuantity = Cart_changeQuantity
}

function Cart_save() {
 var cart = ""
 for(var i=0; i<this.product.length; ++i) {
  cart += "" + this.product[i] + "-" + this.quantity[i]
  if(i != this.product.length - 1) cart += "|"
 }
 setCookie("cart", cart)
}

function Cart_addProduct(n) {
 for(var i=0; i<this.product.length; ++i) {
  if(this.product[i] == n) {
   this.quantity[i]++
   return
  }
 }
 var last = this.product.length
 this.product[last] = n
 this.quantity[last] = 1
}

function Cart_deleteProduct(n) {
 for(var i=0; i<this.product.length; ++i) {
  if(this.product[i] == n) {
   this.product.splice(i, 1)
   this.quantity.splice(i, 1)
   break
  }
 }
}

function Cart_changeQuantity(n, q) {
 if(q < 0) return
 if(q == 0) {
  this.deleteProduct(n)
  return
 }
 for(var i=0; i<this.product.length; ++i) {
  if(this.product[i] == n) {
   this.quantity[i] = q
   break
  }
 }
}
