text: ﻿//----------------------------------------------------------------
  // shopping cart
  //
  function shoppingCart(cartName) {
      this.cartName = cartName;
      this.clearCart = false;
      this.checkoutParameters = {};
      this.items = [];
  
      // load items from local storage when initializing
      this.loadItems();
  
      // save items to local storage when unloading
      var self = this;
      $(window).unload(function () {
          if (self.clearCart) {
              self.clearItems();
          }
          self.saveItems();
          self.clearCart = false;
      });
  }
  
  // load items from local storage
  shoppingCart.prototype.loadItems = function () {
      var items = localStorage != null ? localStorage[this.cartName + "_items"] : null;
      if (items != null && JSON != null) {
          try {
              var items = JSON.parse(items);
              for (var i = 0; i < items.length; i++) {
                  var item = items[i];
                  if (item.name != null && item.price != null && item.quantity != null) {
                      item = new cartItem(item.name, item.price, item.quantity,item.measure);
                      this.items.push(item);
                  }
              }
          }
          catch (err) {
              // ignore errors while loading...
          }
      }
  }
  
  // save items to local storage
  shoppingCart.prototype.saveItems = function () {
      if (localStorage != null && JSON != null) {
          localStorage[this.cartName + "_items"] = JSON.stringify(this.items);
      }
  }
  
  // adds an item to the cart
  shoppingCart.prototype.addItem = function (name, price, quantity,measure) {
    console.log('adding item');
    console.log('quantity1'+quantity);
      quantity = this.toNumber(quantity);
  
      if (quantity != 0) {
         console.log('quantity2'+quantity);
          // update quantity for existing item
          var found = false;
          for (var i = 0; i < this.items.length && !found; i++) {
              var item = this.items[i];
              if (item.name == name) {
                  found = true;
                  item.quantity = this.toNumber(item.quantity + quantity);
                  if (item.quantity <= 0) {
                      this.items.splice(i, 1);
                  }
              }
          }
  
          // new item, add now
          if (!found) {
                console.log('found is false ');
              var item = new cartItem(name, price, quantity,measure);
              this.items.push(item);
              console.log('item '+item);
              console.log('items '+this.items);
          }
  
          // save changes
          this.saveItems();
          alert('Item added to cart');
      }
  }
  
  // get the total price for all items currently in the cart
  shoppingCart.prototype.getTotalPrice = function (name) {
      var total = 0;
      for (var i = 0; i < this.items.length; i++) {
          var item = this.items[i];
          if (name == null || item.name == name) {
              total += this.toNumber(item.quantity * item.price);
          }
      }
      return total;
  }
  
  // get the total price for all items currently in the cart
  shoppingCart.prototype.getTotalCount = function (name) {
      var count = 0;
      for (var i = 0; i < this.items.length; i++) {
          var item = this.items[i];
          if (name == null || item.name == name) {
              count += this.toNumber(item.quantity);
          }
      }
      return count;
  }
  
  // clear the cart
  shoppingCart.prototype.clearItems = function () {
      this.items = [];
      this.saveItems();
  }
  
  //delete specifi item
  shoppingCart.prototype.clearspecificItem = function (name) {
    for (var i = 0; i < this.items.length; i++) {
        var item = this.items[i];
        if (item.name == name) {
            this.items.splice(i,1);
            break;
        }
    }
      this.saveItems();
  }
  // define checkout parameters
  shoppingCart.prototype.addCheckoutParameters = function (serviceName, merchantID, options) {
  
      // check parameters
      if (serviceName != "PayPal" && serviceName != "Google" && serviceName != "Stripe") {
          throw "serviceName must be 'PayPal' or 'Google' or 'Stripe'.";
      }
      if (merchantID == null) {
          throw "A merchantID is required in order to checkout.";
      }
  
      // save parameters
      this.checkoutParameters[serviceName] = new checkoutParameters(serviceName, merchantID, options);
  }
  
  // check out
  shoppingCart.prototype.checkout = function (serviceName, clearCart) {
  
      // select serviceName if we have to
      if (serviceName == null) {
          var p = this.checkoutParameters[Object.keys(this.checkoutParameters)[0]];
          serviceName = p.serviceName;
      }
  
      // sanity
      if (serviceName == null) {
          throw "Use the 'addCheckoutParameters' method to define at least one checkout service.";
      }
  
      // go to work
      var parms = this.checkoutParameters[serviceName];
      if (parms == null) {
          throw "Cannot get checkout parameters for '" + serviceName + "'.";
      }
      switch (parms.serviceName) {
          case "PayPal":
              this.checkoutPayPal(parms, clearCart);
              break;
          case "Google":
              this.checkoutGoogle(parms, clearCart);
              break;
          case "Stripe":
              this.checkoutStripe(parms, clearCart);
              break;
          default:
              throw "Unknown checkout service: " + parms.serviceName;
      }
  }
  
  
  // utility methods
  shoppingCart.prototype.addFormFields = function (form, data) {
      if (data != null) {
          $.each(data, function (name, value) {
              if (value != null) {
                  var input = $("<input></input>").attr("type", "hidden").attr("name", name).val(value);
                  form.append(input);
              }
          });
      }
  }
  shoppingCart.prototype.toNumber = function (value) {
      value = value * 1;
      return isNaN(value) ? 0 : value;
  }
  
  //----------------------------------------------------------------
  // checkout parameters (one per supported payment service)
  //
  function checkoutParameters(serviceName, merchantID, options) {
      this.serviceName = serviceName;
      this.merchantID = merchantID;
      this.options = options;
  }
  
  //----------------------------------------------------------------
  // items in the cart
  //
  function cartItem(name, price, quantity,measure) {
      this.name = name;
      this.price = price ;
      this.quantity = quantity;
      this.measure = measure;
  }
  