﻿// JScript File



    function cartItem(itemID, idStr, stock, price) {
      this.itemID = itemID;
      this.idStr = idStr;
      this.stock = stock;
      this.price = price;
      this.dump = function() {
        return this.itemID + ': ' + this.idStr + ': ' + this.stock + '\n';
      }
      return this;
    }
    
    function cartItems(cartID, productID, btnAddID, txtQtyID, imgDefaultID, ltlPrice1ID, ltlPrice2ID, ddlImgVarID, ddlGroupVarIDArr, imgPath, detailPath, showZeroStock, groupSep, atomSep, msgAdd, msgBusy, msgZeroUnavailable, msgNoZeroOutOfStock, msgNoZeroNotAnItem) {
      
      this.cartID = cartID;
      this.itemID = -1;
      this.item = null;
      this.productID = productID;
      this.imgVarID = -1;
      this.groupVarID = []; //new Array(groups);
      this.items = [];
      this.imgPath = imgPath;
      this.detailPath = detailPath;
      this.imgDefaultObj = document.getElementById(imgDefaultID);
      this.btnAddObj = document.getElementById(btnAddID);
      this.txtQtyObj = document.getElementById(txtQtyID);
      if (this.txtQtyObj)
        this.txtQtyObj.value = 1;
      this.price1Obj = document.getElementById(ltlPrice1ID);
      this.price2Obj = document.getElementById(ltlPrice2ID);
      this.ddlImgVarObj = document.getElementById(ddlImgVarID);
      this.ddlGroupVarIDArr = [];
      for (id = 0; id < ddlGroupVarIDArr.length; id++)
        this.ddlGroupVarIDArr[this.ddlGroupVarIDArr.length] = document.getElementById(ddlGroupVarIDArr[id]);
      this.groupSep = groupSep;
      this.atomSep = atomSep;
      this.showZeroStock = showZeroStock;
      this.msgAdd = msgAdd;
      this.msgBusy = msgBusy;
      this.msgZeroUnavailable = msgZeroUnavailable;
      this.msgNoZeroOutOfStock = msgNoZeroOutOfStock;
      this.msgNoZeroNotAnItem = msgNoZeroNotAnItem;

      this.add = function(itemID, idStr, stock, price)  {
        this.items[this.items.length] = new cartItem(itemID, idStr, stock, price);
      }
      
      this.clear = function() {
        this.items = [];
      }
      
      this.setDetailImg = function(detailID) {
        //this.imgDefaultObj.src = this.detailPath.replace('{0}', this.productID).replace('{1}',detailID);
        setTimeout('document.getElementById("' + this.imgDefaultObj.id + '").src = "' + this.detailPath.replace('{0}', this.productID).replace('{1}',detailID) + '";',1);
      }
      
      this.setImg = function() {
        //this.imgDefaultObj.src = this.imgPath.replace('{0}', this.productID).replace('{1}',this.imgVarID);
        setTimeout('document.getElementById("' + this.imgDefaultObj.id + '").src = "' + this.imgPath.replace('{0}', this.productID).replace('{1}',this.imgVarID) + '";',1);
      }

      this.setImgVar = function(imgVarID) {
        this.imgVarID = imgVarID;
        this.setImg();
        this.checkItem();
      }
      
      this.setImgVarDropDown = function(imgVarID) {
        // finds the value in the dropdownlist that matches the paramter and selects it
        this.setImgVar(imgVarID);
        if (this.ddlImgVarObj) {
          for (i = 0; i < this.ddlImgVarObj.options.length; i++)
            if (this.ddlImgVarObj.options[i].value == this.imgVarID)
              this.ddlImgVarObj.options[i].selected = true;
        }
      }
      
      this.setGroupVarDropDown = function(groupVarID) {
        // finds the value in the correct drop down list (by the first atom of the groupVarID)
        this.setGroupVar(groupVarID);
        for (g = 0; g < this.groupVarID.length; g++) {
          for (i = 0; i < this.ddlGroupVarIDArr.length; i++) {
              if (this.ddlGroupVarIDArr[i].options[0].value.split(this.atomSep)[0] == this.groupVarID[g].split(this.atomSep)[0])
                for (gv = 0; gv < this.ddlGroupVarIDArr[i].options.length; gv++)
                  if (this.ddlGroupVarIDArr[i].options[gv].value.split(this.atomSep)[1] == this.groupVarID[g].split(this.atomSep)[0])
                    this.ddlGroupVarIDArr[i].options[gv].selected = true;
          }
        }
      }
      
      this.setGroupVar = function(groupVarID) {
        // finds the group string with passed group id from the group string, adds it if it is not there,
        // replaces it if it exists with the passed group string
        found = false;
        for (i=0; i < this.groupVarID.length; i++) {
          if (this.groupVarID[i].split(this.atomSep)[0] == groupVarID.split(this.atomSep)[0]) {
            found = true;
            // replace the groupvarid with the matching groupid
            this.groupVarID[i] = groupVarID;
            break;
          }
        }
        if (!found)
          this.groupVarID[this.groupVarID.length] = groupVarID;
          
        // now go through the dropdown for this group
        
        // now check that the item has stock
        this.checkItem();
      }

      this.checkItem = function () {
        // construct the itemstring with the matching imgvar and groupvars
        // not all possible imgVar/group/groupVar combinations have items
        findItem = this.imgVarID;
        for (i = 0; i < this.groupVarID.length; i++)
          findItem += this.groupSep + this.groupVarID[i]
        
        // find the findItem in this.items
        this.item = null;
        for (i = 0; i < this.items.length; i++)
          if (this.items[i].idStr == findItem) {
            this.item = this.items[i];
            break;
          }
        this.itemID = (this.item == null) ? -1 : this.item.itemID;

        //if (this.item)
          //alert('got it : ' + this.item.itemID + ' : ' + this.item.stock + ' in stock');

        this.price1Obj.innerHTML = (this.item == null) ? '&#160;' : this.item.price;
        this.price2Obj.innerHTML = (this.item == null) ? '&#160;' : this.item.price;
        
        if (this.btnAddObj) {
          if (this.showZeroStock) {
            this.btnAddObj.disabled = ((this.item == null) || (this.item.stock == 0));
            if (this.btnAddObj.src)
              this.btnAddObj.src = (this.item == null) ? this.msgNoZeroNotAnItem : (this.item.stock == 0) ? this.msgNoZeroOutOfStock : this.msgAdd;
            else
              this.btnAddObj.value = (this.item == null) ? this.msgNoZeroNotAnItem : (this.item.stock == 0) ? this.msgNoZeroOutOfStock : this.msgAdd;
          }
          else {
            this.btnAddObj.disabled = (this.item == null);
            if (this.btnAddObj.src)
              this.btnAddObj.src = (this.item == null) ? this.msgZeroUnavailable : this.msgAdd;
            else
              this.btnAddObj.value = (this.item == null) ? this.msgZeroUnavailable : this.msgAdd;
          }
        }
      }
      
      this.addToBasket = function() {
        addToBasket(this.productID, this.cartID, this.itemID, this.txtQtyObj.value);
        this.txtQtyObj.value = '1';
        
        this.btnAddObj.disabled = true;
        if (this.btnAddObj.src)
          this.btnAddObj.src = this.msgBusy;
        else
          this.btnAddObj.value = this.msgBusy;
          
        return false;
      }
      
      this.dump = function() {
        str = 'imgVarID: ' + this.imgVarID + '\n';
        str += 'groupVarID: ';
        for (i=0; i < this.groupVarID.length; i++)
          str += this.groupVarID[i] + ',';
        str += '\n';
        for (i= 0 ; i < this.items.length; i++)
          str += this.items[i].dump();
        return str;
      }
      
      return this;

    }
 