﻿function ShoppingCart()
{
  this.items = new Array();
  this.loaded = false;
  this.orderPage = ((document.location.toString().indexOf('/homepages/')==-1)&&(typeof category != "undefined"));
  this.handlingCharge = 10.00;
  this.hasRequestedBooks = null;
  
  if(typeof ShoppingCart._initialized == "undefined")
  {
    ShoppingCart.prototype.printMiniCart = function()
    {
      document.write('<div id="cart">');
        if(this.orderPage)
        {
          document.write('<a href="#" class="addToCart" onclick="addItemsToCart(event)">Add to order</a>');
        } else {
          document.write('<span class="addToCart">Add to order</span>');
        }
        if(this.items.length > 0)
        {
          document.write('<a href="libraries/Shopping+cart+summary?open&template=domPrimary">View order</a>');
          document.write('<a href="libraries/CheckoutNew?open&template=domPrimary">Checkout</a>');
        } else {
          document.write('<span class="viewOrder">View order</span>');
          document.write('<span class="checkout">Checkout</span>');
        }       
        document.write('<h3>Shopping Summary</h3>');
        document.write('<p id="items">No. of items: ',this.getItemsQuantity(),'</p>');
        document.write('<p id="total">Current total: $',this.getTotal(),'</p>');        
        if((typeof checkTC != "undefined")&&(checkTC == true))
        {
            document.write('<p id="etotal">e-Club total: $',this.getEClubTotal(),'</p>');
        }        
      document.write('</div>');
    };
    
    ShoppingCart.prototype.load = function()
    {   
      if(!getCookie('MacPrimary') || getCookie('MacPrimary')=="") return false;      
      //this.items = new Array();
      if(this.items.length == 0)
      {      
          var oCookie = getCookie('MacPrimary').split(';');      
          var booksRemoved = "";      
          for(var i=0; i < oCookie.length; i++)
          {         
            var itemSplit = oCookie[i].split('|');  
           
            var priceAvailability = new PriceAvailability(itemSplit[0],itemSplit[2]);  
        
            if(priceAvailability.price)
            {
                var price = priceAvailability.price;
            } else {
                        //var price = unescape(searchOnixResultsCartBook[1].viewCol7);
                        var price = null;
                   }
            
            var oCartItem = new CartItem((this.loadOnixBook(oCookie[i],price)).split('|')); 
                   
            if(priceAvailability.available) 
            {
                oCartItem.available = true;
            } else {
                        oCartItem.shipDate = priceAvailability.getShipDateStringForSummary();          
                   }
                
            this.items.push(oCartItem);
          }
          /*
          if(booksRemoved != "")
          {
            alert("The following items will be removed from the shopping cart due to not being available at the moment:" + booksRemoved);
          }
          */
      }
      this.loaded = true;
     /*
      var oStr = "cart items:" + this.items.length + "\n\n";
      for(var i=0; i<this.items.length;i++)
      {
        
        oStr += "\n" + this.items[i].isbn + "   " + this.items[i].title + "   " +   this.items[i].price + "   " +   this.items[i].buyInspect + "   " +   this.items[i].quantity + "   " + this.items[i].divCatStr;
      }
      alert(getCookie('MacPrimary') + "\n\n\n\n" + oStr);
      */
    }; 
    
    ShoppingCart.prototype.loadFromCookie = function(str)
    {
        var splitStr = str.split('|');
        var item = new Array();
        item.push(splitStr[0]);
        item.push('t');
        item.push(splitStr[1]);
        item.push(splitStr[2]);
        item.push(splitStr[3]);
        item.push(splitStr[4]);
        return item.join('|');
    };
    
    ShoppingCart.prototype.getPriceAvailability = function(isbn)
    {
        var priceAvailability = new PriceAvailability(isbn);  
        
        if(!priceAvailability.available) return null;
        
        if(priceAvailability.price)
        {
            return priceAvailability.price;
        } else {
                    return unescape(searchOnixResultsCartBook[1].viewCol7);
               }
    };
    
    ShoppingCart.prototype.loadOnixBook = function(str,sPrice)
    {
        //str = str.split('|');
        //var sStr = str[0] + '|' + str[3] + '|' + str[4] + '|' + str[5];
        //var splitStr = sStr.split('|');
        var splitStr = str.split('|');
        
        var sUrl = '/primary/newonix/onixsearch.js?open&query=([ISBN]=' + splitStr[0] + ')+and+[FORM]=Product&start=1&count=2&label=CartBook';
        var oXHR = createXHR();
        oXHR.open('GET',sUrl,false);
        oXHR.send(null);  
        eval(oXHR.responseText);
        
        if(!sPrice) sPrice = unescape(searchOnixResultsCartBook[1].viewCol7);
        
        var item = new Array();
        if(typeof searchOnixResultsCartBook[1] != 'undefined')
        {             
            if(sPrice.indexOf('$') != -1) sPrice = sPrice.substring(1);
           
            item.push(splitStr[0]);
            item.push(unescape(searchOnixResultsCartBook[1].viewCol2));
            item.push(sPrice);
            item.push(splitStr[1]);
            item.push(splitStr[2]);
            if(typeof searchOnixResultsCartBook[1].viewCol15 != 'undefined')
            {
                item.push(splitStr[3] + '>' + unescape(searchOnixResultsCartBook[1].viewCol13).toLowerCase().replace(/[\s\W]/gi,"") + '>' + unescape(searchOnixResultsCartBook[1].viewCol15).toLowerCase().replace(/[\s\W]/gi,""));
            } else {
                         item.push(splitStr[3] + '>' + unescape(searchOnixResultsCartBook[1].viewCol13).toLowerCase().replace(/[\s\W]/gi,""));
                   }            
        }
        //alert(item.join('|'));
        return item.join('|');
    };
    
    ShoppingCart.prototype.getBookAuthor = function(isbn)
    {        
        var sUrl = '/primary/newonix/onixsearch.js?open&query=([ISBN]=' + isbn + ')+and+[FORM]=Product&start=1&count=2';
        var oXHR = createXHR();
        oXHR.open('GET',sUrl,false);
        oXHR.send(null);  
        eval(oXHR.responseText);
        
        if(typeof searchOnixResults[1] != 'undefined')
        {
            return unescape(searchOnixResults[1].viewCol4);
        }
        return "";
    };
    
    ShoppingCart.prototype.getOnixBookAuthor = function(isbn)
    { 
        var sUrl = '/primary/newonix/onixsearch.js?open&query=([ISBN]=' + isbn + ')+and+[FORM]=Product&start=1&count=2';
        var oXHR = createXHR();
        oXHR.open('GET',sUrl,false);
        oXHR.send(null);  
        eval(oXHR.responseText);
        
        var item = new Array();
        if(typeof searchOnixResults[1] != 'undefined')
        {
                       
        }
        return "";
    };
    
    ShoppingCart.prototype.save = function()
    {
      //if(this.items.length == 0) return false;      
      var cookieExpires = new Date();
      cookieExpires.setTime(cookieExpires.getTime() + (1 * 60 * 60 * 1000 ));
      var tmpCookie = new Array();
      //for(var i=0; i < this.items.length; i++) tmpCookie.push(this.items[i].cookieString());
      for(var i=0; i < this.items.length; i++) tmpCookie.push(this.items[i].newCookieString());
      //alert(tmpCookie.join(';'));
      setCookie('MacPrimary', tmpCookie.join(';'), cookieExpires, '/');      
      this.loaded = false;
      this.load();            
    }
    
    ShoppingCart.prototype.isCartPage = function ()
    {
        var section = (document.forms[0].Section != 'undefined' && document.forms[0].Section.value.length > 0) ? unescape(document.forms[0].Section.value) : '';
        var subsection = (document.forms[0].Subsection != 'undefined' && document.forms[0].Subsection.value.length > 0) ? unescape(document.forms[0].Subsection.value) : '';
        if(section && subsection && section == 'Library' && subsection == 'Shopping cart')
        {
            return true;
        }
        return false;
    };
    
    ShoppingCart.prototype.isIsbnInCart = function (isbn)
    {
      if(!this.loaded) return false;      
      for(var i=0; i < this.items.length; i++)
      {
        if(this.items[i].isbn.toLowerCase() == isbn.toLowerCase()) return true;
      }
      return false;
    };
    
    ShoppingCart.prototype.addToCart = function (isbn,title,price,buyInspect,quantity,divCatStr,availability)
    {
      if(this.isIsbnInCart(isbn)) return false;            
      var tempArray = new Array(isbn,title,price,buyInspect,quantity,divCatStr);
      var tempItem = new CartItem(tempArray);
      if(availability)tempItem.available = true;
      this.items.push(tempItem); 
      this.save();
    };
    
    ShoppingCart.prototype.deleteFromCart = function(isbn)
    {
      if(!this.isIsbnInCart(isbn)) return false;      
      var tempArray = new Array();
      for(var i=0; i < this.items.length; i++)
      {
        if(isbn.toLowerCase() != this.items[i].isbn.toLowerCase()) tempArray.push(this.items[i]);
      }
      this.items = new Array();
      for(var i=0; i < tempArray.length; i++) this.items.push(tempArray[i]);
      this.save();
    };
    
    ShoppingCart.prototype.updateShoppingSummary = function ()
    {
      var cart = document.getElementById('cart');
      cart.innerHTML = "";      
      if(this.orderPage)
      {
          var addToCart = document.createElement('a');
          addToCart.setAttribute('href','#');
          if(document.attachEvent)
          {
            addToCart.onclick = function(oEvent){addItemsToCart(event);}; 
          } else {
            addToCart.setAttribute('onclick','addItemsToCart(event)'); 
          }
      } else {
          var addToCart = document.createElement('span');
      }
      addToCart.className = "addToCart";
      addToCart.appendChild(document.createTextNode('Add to order'));
      cart.appendChild(addToCart);
    
      if(this.items.length != 0)
      {
          var viewOrder = document.createElement('a');
          viewOrder.setAttribute('href','libraries/Shopping+cart+summary');
      } else {
          var viewOrder = document.createElement('span');
          viewOrder.className = "viewOrder";
      }
      viewOrder.appendChild(document.createTextNode('View order'));
      cart.appendChild(viewOrder);
      
      if(this.items.length != 0)
      {
          var checkout = document.createElement('a');
          checkout.setAttribute('href','libraries/CheckoutNew');
      } else {
          var checkout = document.createElement('span');
          checkout.className = "checkout";
      }
      checkout.appendChild(document.createTextNode('Checkout'));
      cart.appendChild(checkout);      
      var oHeading = document.createElement('h3');
      oHeading.appendChild(document.createTextNode('Shopping Summary'));
      cart.appendChild(oHeading);      
      var oItems = document.createElement('p');
      oItems.setAttribute('id','items');
      oItems.appendChild(document.createTextNode('No. of items: ' + this.getItemsQuantity()));
      cart.appendChild(oItems);       
      var oTotal = document.createElement('p');
      oTotal.setAttribute('id','total');
      oTotal.appendChild(document.createTextNode('Current total: $' + this.getTotal()));
      cart.appendChild(oTotal);      
      if((typeof checkTC != "undefined")&&(checkTC == true))
      {
          var oETotal = document.createElement('p');
          oETotal.setAttribute('id','etotal');
          oETotal.appendChild(document.createTextNode('e-Club total: $' + this.getEClubTotal()));
          cart.appendChild(oETotal);   
      }
    };
    
    ShoppingCart.prototype.isItemInspect = function(isbn)
    {
      if(!this.isIsbnInCart(isbn)) return false;      
      for(var i=0; i < this.items.length; i++)
      {
        if((this.items[i].isbn.toLowerCase() == isbn.toLowerCase())&&(this.items[i].buyInspect.substring(1) == "I")) return true;
      }
      return false;
    };
    
    ShoppingCart.prototype.getItemQuantity = function(isbn)
    {
      if(!this.isIsbnInCart(isbn)) return false;
      for(var i=0; i < this.items.length; i++)
      {
        if(this.items[i].isbn.toLowerCase() == isbn.toLowerCase()) return this.items[i].quantity;
      }
      return false;
    }
    
    ShoppingCart.prototype.getItemsQuantity = function()
    {
      if(!this.loaded) return 0;
      var quantity = 0;
      for(var i=0; i < this.items.length; i++) quantity += parseInt(this.items[i].quantity);
      return quantity;
    };
    
    ShoppingCart.prototype.getTotal = function()
    {
      if(!this.loaded) return "0.0";
      var total = 0;
      for(var i=0; i < this.items.length; i++) 
      {
        //if(this.items[i].buyInspect.substring(1) != "I" && this.items[i].available)
        if(this.items[i].buyInspect.substring(1) != "I")
        {
          if(this.items[i].price.indexOf('$') == -1)
          {    
            var tempPrice = Number(this.items[i].price);
          } else {
            var tempPrice = Number(this.items[i].price.substring(1));            
          }
          
          total += (tempPrice) * (Number(this.items[i].quantity));
        }
      }
      if((typeof oCartForm == "object")&&((oCartForm.step == "2")||(oCartForm.step == "3"))&&(oCartForm.payType == "2")&&(checkTC == false))
      {
        if(total > 0)total += this.handlingCharge;
      }
      
      return total.toFixed(2);
    };
    
    ShoppingCart.prototype.getEClubTotal = function()
    {
      if(!this.loaded) return "0.0";
      var total = 0;
      for(var i=0; i < this.items.length; i++) 
      {
        //if(this.items[i].buyInspect.substring(1) != "I" && this.items[i].available)
        if(this.items[i].buyInspect.substring(1) != "I")
        {
          if(this.items[i].price.indexOf('$') == -1)
          {    
            var tempPrice = Number(this.items[i].price);
          } else {
            var tempPrice = Number(this.items[i].price.substring(1));            
          }
          if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
          {
            tempPrice = tempPrice * 0.9;
          }
          total += (tempPrice) * (Number(this.items[i].quantity));
        }
      }
      if((typeof oCartForm == "object")&&((oCartForm.step == "2")||(oCartForm.step == "3"))&&(oCartForm.payType == "2")&&(checkTC == false))
      {
        total += this.handlingCharge;
      }      
      return total.toFixed(2);
    };
    
    ShoppingCart.prototype.eClubPrice = function(i)
    {
        if(this.items[i].price.indexOf('$') == -1)
        {
            var ePrice = Number(this.items[i].price);
                    
        } else {
            var ePrice = Number(this.items[i].price.substring(1));
        }
        return (ePrice * 0.9).toFixed(2);
    };
    
    ShoppingCart.prototype.setAddToOrder = function()
    {      
    };
    
    ShoppingCart.prototype.printShoppingCartOverviewHeader = function()
    {
      document.write('<thead>');
      document.write('<tr>');
      document.write('<th>ISBN</th>');
      document.write('<th>Title</th>');
      document.write('<th>RRP</th>');
      document.write('<th>e-Club price</th>');
      document.write('<th>Quantity</th>');
      document.write('<th>Inspect</th>');
      document.write('<th>Buy</th>');
      document.write('<th class="last"></th>');
      document.write('</tr>');
      document.write('</thead>'); 
    };
    
ShoppingCart.prototype.attachShoppingCartOverviewHeaderAvailable = function(oHolder)
{
    var tr = document.createElement('tr');
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('ISBN'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Title'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('RRP'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('e-Club price'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Quantity'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Inspect'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Buy'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Total'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.className = 'last';
    tr.appendChild(th);   
            
    var oHead = document.createElement('thead');
    oHead.appendChild(tr);
    oHolder.appendChild(oHead);
};

ShoppingCart.prototype.attachShoppingCartOverviewHeaderAvailableAndUnavailable = function(oHolder)
{
    var tr = document.createElement('tr');
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('ISBN'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Title'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('RRP'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('e-Club price'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Quantity'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Inspect'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Buy'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Total'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Available'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.className = 'last';
    tr.appendChild(th);   
            
    var oHead = document.createElement('thead');
    oHead.appendChild(tr);
    oHolder.appendChild(oHead);
};

ShoppingCart.prototype.attachShoppingCartOverviewHeaderUnavailable = function(oHolder)
{
    var tr = document.createElement('tr');
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('ISBN'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Title'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('RRP'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('e-Club price'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Quantity'));
    tr.appendChild(th); 
    /*
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Inspect'));
    tr.appendChild(th); 
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Buy'));
    tr.appendChild(th); 
    */
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Total'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.appendChild(document.createTextNode('Available'));
    tr.appendChild(th);
    
    var th = document.createElement('th');
    th.className = 'last';
    tr.appendChild(th);   
            
    var oHead = document.createElement('thead');
    oHead.appendChild(tr);
    oHolder.appendChild(oHead);
};
    
    ShoppingCart.prototype.printInspectBuy = function(ib,i)
    {
      if(ib.toUpperCase() == (this.items[i].buyInspect.substring(1)).toUpperCase())return 'checked';
      return "";
    };
    
    ShoppingCart.prototype.inspectAvailable = function(i)
    {
      if((this.items[i].buyInspect.substring(0,1)).toUpperCase() == "I")return '';      
      return 'disabled';
    };
    
    ShoppingCart.prototype.isInspected = function(i)
    {
      if(this.items[i].buyInspect == "II")return 'disabled="disabled"';      
      return '';
    };
/*    
ShoppingCart.prototype.attachShoppingCartOverview = function()
{
    //get reference to shopping summary container on page
    var oHolder = document.getElementById('checkoutCart');
    oHolder.innerHTML = '';    
    
    //attach table with unavailable books
    if(this.hasUnavailableBooks() == true)
    {
        this.attachShoppingCartOverviewUnavailable(oHolder);
    }
    
    //attach table with available books
    if(this.hasAvailableBooks() == true)
    {
        this.attachShoppingCartOverviewAvailable(oHolder);
    }
    
    if((this.hasAvailableBooks() == false) && (this.hasUnavailableBooks() == false))
    {
        var oP = document.createElement('p');
        oP.appendChild(document.createTextNode('There are no items in the shopping cart.'));
        oHolder.appendChild(oP);
    } 
    
    //if there are no available books print full price as $0.00
    if(this.hasAvailableBooks() == false)
    {
        if((typeof checkTC != "undefined")&&(checkTC == true))
        {
            var price = this.getEClubTotal();
        } else {
                    var price = this.getTotal();
               }
             
        //attach total price
        var oSpan = document.createElement('span');
        oSpan.innerHTML = 'Total purchase<br />$' + price;
        var oP = document.createElement('p');
        oP.appendChild(oSpan);
        oP.setAttribute('id','totalPrice');
        oHolder.appendChild(oP);
    }
    
    if((this.hasAvailableBooks() != false) || (this.hasUnavailableBooks() != false))
    {
        //attach button to update order
        var oInput = document.createElement('input');
        oInput.setAttribute('type','button');
        oInput.className = 'updateOrder';
        oInput.setAttribute('value','Update order');
        oInput.onclick = updateOverviewCart;    
       
        var oDiv = document.createElement('div');
        oDiv.className = 'updateOrderHolder';
        oDiv.appendChild(oInput);
        oHolder.appendChild(oDiv);
        
        var winLoc = window.location.toString();
    
        if(winLoc.indexOf('libraries/CheckoutNew') == -1)
        {
            //attach 'Continue shopping' button
            var oInput = document.createElement('input');
            oInput.className = 'continueShopping';
            oInput.setAttribute('value','Continue Shopping');
            oInput.setAttribute('type','button');
            oInput.onclick = continueShopping;
            oHolder.appendChild(oInput); 
            
            //attach 'Checkout now' button
            var oInput = document.createElement('input');
            oInput.className = 'checkoutNow';
            oInput.setAttribute('value','Checkout now');
            oInput.setAttribute('type','button');
            oInput.onclick = function()
            {
                window.location = '/primary31/site/libraries/CheckoutNew';
            };
            oHolder.appendChild(oInput);
        }
    }
    
    //update radio buttons
    //setTimeout('oCart.checkRadioButtons()',500);
      
}; 
*/


ShoppingCart.prototype.attachShoppingCartOverview = function()
{
    //get reference to shopping summary container on page
    var oHolder = document.getElementById('checkoutCart');
    oHolder.innerHTML = '';    
    
    var hasBooks = (this.items.length > 0) ? true : null;
    
    //attach table with books
    if(hasBooks)
    {
        this.attachShoppingCartOverviewAvailableAndUnavailable(oHolder);
    } else {
        var oP = document.createElement('p');
        oP.appendChild(document.createTextNode('There are no items in the shopping cart.'));
        oHolder.appendChild(oP);
    } 
    
    /*
    //if there are no available books print full price as $0.00
    if(this.hasAvailableBooks() == false)
    {
        if((typeof checkTC != "undefined")&&(checkTC == true))
        {
            var price = this.getEClubTotal();
        } else {
                    var price = this.getTotal();
               }
             
        //attach total price
        var oSpan = document.createElement('span');
        oSpan.innerHTML = 'Total purchase<br />$' + price;
        var oP = document.createElement('p');
        oP.appendChild(oSpan);
        oP.setAttribute('id','totalPrice');
        oHolder.appendChild(oP);
    }
    */
    if(hasBooks)
    {
        //attach button to update order
        var oInput = document.createElement('input');
        oInput.setAttribute('type','button');
        oInput.className = 'updateOrder';
        oInput.setAttribute('value','Update order');
        oInput.onclick = updateOverviewCart;    
       
        var oDiv = document.createElement('div');
        oDiv.className = 'updateOrderHolder';
        oDiv.appendChild(oInput);
        oHolder.appendChild(oDiv);
        
        var winLoc = window.location.toString();
    
        if(winLoc.indexOf('libraries/CheckoutNew') == -1)
        {
            //attach 'Continue shopping' button
            var oInput = document.createElement('input');
            oInput.className = 'continueShopping';
            oInput.setAttribute('value','Continue Shopping');
            oInput.setAttribute('type','button');
            oInput.onclick = continueShopping;
            oHolder.appendChild(oInput); 
            
            //attach 'Checkout now' button
            var oInput = document.createElement('input');
            oInput.className = 'checkoutNow';
            oInput.setAttribute('value','Checkout now');
            oInput.setAttribute('type','button');
            oInput.onclick = function()
            {
                window.location = '/primary31/site/libraries/CheckoutNew';
            };
            oHolder.appendChild(oInput);
        }
    }
    
    //update radio buttons
    //setTimeout('oCart.checkRadioButtons()',500);
      
}; 

ShoppingCart.prototype.attachShoppingCartOverviewUnavailable = function(oHolder)
{
    var oTable = document.createElement('table');
    
    var oCaption = document.createElement('caption');
    oCaption.appendChild(document.createTextNode('Requests'));
    oTable.appendChild(oCaption);
    
    this.attachShoppingCartOverviewHeaderUnavailable(oTable);
    
    var tBody = document.createElement('tbody');
    
    for(var i = 0; i < this.items.length; i++)
    {
        if(!this.items[i].available)
        {
            tBody.appendChild(this.attachItem(i));
        }
    }
    
    oTable.appendChild(tBody);
    oHolder.appendChild(oTable);
};   

ShoppingCart.prototype.attachShoppingCartOverviewAvailable = function(oHolder)
{
    var oTable = document.createElement('table');
    
    var oCaption = document.createElement('caption');
    oCaption.appendChild(document.createTextNode('Purchases'));
    oTable.appendChild(oCaption);
    
    //attach table head
    this.attachShoppingCartOverviewHeaderAvailable(oTable);
    
    var tBody = document.createElement('tbody');
    
    //attach items
    for(var i = 0; i < this.items.length; i++)
    {
        if(this.items[i].available)
        {
            tBody.appendChild(this.attachItem(i));
        }
    }
    
    oTable.appendChild(tBody);
    oHolder.appendChild(oTable);
    
    if((typeof checkTC != "undefined")&&(checkTC == true))
    {
        var price = this.getEClubTotal();
    } else {
                var price = this.getTotal();
           }
    
    
    //attach total price
    var oSpan = document.createElement('span');
    oSpan.innerHTML = 'Total purchase<br />$' + price;
    var oP = document.createElement('p');
    oP.appendChild(oSpan);
    oP.setAttribute('id','totalPrice');
    oHolder.appendChild(oP);
    
};

ShoppingCart.prototype.attachShoppingCartOverviewAvailableAndUnavailable = function(oHolder)
{
    var oTable = document.createElement('table');
    /*
    var oCaption = document.createElement('caption');
    oCaption.appendChild(document.createTextNode('Purchases'));
    oTable.appendChild(oCaption);
    */
    //attach table head
    this.attachShoppingCartOverviewHeaderAvailableAndUnavailable(oTable);
    
    var tBody = document.createElement('tbody');
    
    //attach items
    for(var i = 0; i < this.items.length; i++)
    {
        //if(this.items[i].available)
        //{
            tBody.appendChild(this.attachItem(i));
        //}
    }
    
    oTable.appendChild(tBody);
    oHolder.appendChild(oTable);
    
    if((typeof checkTC != "undefined")&&(checkTC == true))
    {
        var price = this.getEClubTotal();
    } else {
                var price = this.getTotal();
           }
    
    
    //attach total price
    var oSpan = document.createElement('span');
    oSpan.innerHTML = 'Total purchase<br />$' + price;
    var oP = document.createElement('p');
    oP.appendChild(oSpan);
    oP.setAttribute('id','totalPrice');
    oHolder.appendChild(oP);
    
};
    
    
    
    
    
ShoppingCart.prototype.printShoppingCartOverviewUnavailable = function()
{
    if(this.items.length == 0)return false;
    
    document.write('<table>');
    document.write('<caption>Requests</caption>');
    this.printShoppingCartOverviewHeader();
    document.write('<tbody>');
    for(var i=0; i < this.items.length; i++)
    {  
        if(!this.items[i].available)
        {      
            document.write('<tr>');
            document.write('<td>',isbn10to13(this.items[i].isbn),'</td>');
            var bookLink = this.items[i].getBookLink();
            document.write('<td class="title"><a href="',bookLink,'" title="',this.items[i].title,'" >',this.items[i].title,'</a></td>');
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                document.write('<td>-</td>');
                document.write('<td>$',this.eClubPrice(i),'</td>');
            } else {
                        document.write('<td>$',this.items[i].price,'</td>');
                        document.write('<td>-</td>');
                   }
            document.write('<td><input type="text" class="units" value="',this.items[i].quantity,'" ',this.isInspected(i),'/></td>');
            document.write('<td><input type="radio" name="ib',i,'" id="ib',i,'" value="I" ',this.printInspectBuy('I',i),' ',this.inspectAvailable(i),' onclick="updateInspectBuy(event)" /></td>');
            document.write('<td><input type="radio" name="ib',i,'" id="ib',i,'" value="B" ',this.printInspectBuy('B',i),' onclick="updateInspectBuy(event)" /></td>');
            document.write('<td class="last"><input type="button" class="remove" name="',i,'" onclick="removefromOverviewCart(event)" value="remove" /></td>');
            document.write('</tr>');
        }
    }       
    document.write('</tbody>');      
    document.write('</table>'); 
    /*
    if((typeof checkTC != "undefined")&&(checkTC == true))
    {
        document.write('<p id="totalPrice"><span>Total purchase<br />$',this.getEClubTotal(),'</span></p>');
    } else {
                document.write('<p id="totalPrice"><span>Total purchase<br />$',this.getTotal(),'</span></p>');  
    }
    document.write('<div class="updateOrderHolder"><input type="button" class="updateOrder" onclick="updateOverviewCart(event)" value="Update order" /></div>');
    */
};

ShoppingCart.prototype.hasUnavailableBooks = function()
{
    for(var i = 0; i < this.items.length; i++)
    {
        if(!this.items[i].available)return true;
    }
    return false;
};

ShoppingCart.prototype.hasAvailableBooks = function()
{
    for(var i = 0; i < this.items.length; i++)
    {
        if(this.items[i].available)return true;
    }
    return false;
};
    
ShoppingCart.prototype.printShoppingCartOverview = function()
{
    if(this.items.length == 0)return false;
    
    document.write('<table>');
    document.write('<caption>Book information</caption>');
    this.printShoppingCartOverviewHeader();
    document.write('<tbody>');
    for(var i=0; i < this.items.length; i++)
    {        
        if(this.items[i].available)
        {
            document.write('<tr>');
            document.write('<td>',isbn10to13(this.items[i].isbn),'</td>');
            var bookLink = this.items[i].getBookLink();
            document.write('<td class="title"><a href="',bookLink,'" title="',this.items[i].title,'" >',this.items[i].title,'</a></td>');
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                document.write('<td>-</td>');
                document.write('<td>$',this.eClubPrice(i),'</td>');
            } else {
                        document.write('<td>$',this.items[i].price,'</td>');
                        document.write('<td>-</td>');
                   }
            document.write('<td><input type="text" class="units" value="',this.items[i].quantity,'" ',this.isInspected(i),'/></td>');
            document.write('<td><input type="radio" name="ib',i,'" id="ib',i,'" value="I" ',this.printInspectBuy('I',i),' ',this.inspectAvailable(i),' onclick="updateInspectBuy(event)" /></td>');
            document.write('<td><input type="radio" name="ib',i,'" id="ib',i,'" value="B" ',this.printInspectBuy('B',i),' onclick="updateInspectBuy(event)" /></td>');
            document.write('<td class="last"><input type="button" class="remove" name="',i,'" onclick="removefromOverviewCart(event)" value="remove" /></td>');
            document.write('</tr>');
        }
    }       
    document.write('</tbody>');      
    document.write('</table>'); 
    if((typeof checkTC != "undefined")&&(checkTC == true))
    {
        document.write('<p id="totalPrice"><span>Total purchase<br />$',this.getEClubTotal(),'</span></p>');
    } else {
                document.write('<p id="totalPrice"><span>Total purchase<br />$',this.getTotal(),'</span></p>'); 
           }
    document.write('<div class="updateOrderHolder"><input type="button" class="updateOrder" onclick="updateOverviewCart(event)" value="Update order" /></div>');
};
    
    
 /*   
ShoppingCart.prototype.updateShoppingCartOverview = function()
{
    //get reference to table records    
    var oBody = document.getElementById('bodyMiddle');
    var tBody = oBody.getElementsByTagName('tbody');
    
    var trItems = tBody[0].getElementsByTagName('tr');
    
    //update unavailable books
    if(oCart.hasUnavailableBooks() == true)
    {      
        var trItems = tBody[0].getElementsByTagName('tr');
        for (var x = (trItems.length - 1); x > -1; x--)//loop through items in cart overview and remove them [reverse order]
        {
            tBody[0].removeChild(trItems[x]);
        }      
            
        for(var x=0; i < this.items.length; x++)//loop through cart items and attach them to cart overview
        {
            tBody[0].appendChild(this.attachItem(x));        
        }
    }
    
    //update available books
    if(oCart.hasAvailableBooks() == true)
    {  
        var trItems = tBody[1].getElementsByTagName('tr');    
        for (var x = (trItems.length - 1); x > -1; x--)//loop through items in cart overview and remove them [reverse order]
        {
            tBody[1].removeChild(trItems[x]);
        }      
            
        for(var x=0; i < this.items.length; x++)//loop through cart items and attach them to cart overview
        {
            tBody[1].appendChild(this.attachItem(x));        
        }
    }
    
    
    //update cart overviw total
    var oTotal = document.getElementById('totalPrice');
    if((typeof checkTC != "undefined")&&(checkTC == true))
    {
        oTotal.innerHTML = "<span>Total purchase<br />$" + this.getEClubTotal() + "</span>";
    } else {
                oTotal.innerHTML = "<span>Total purchase<br />$" + this.getTotal() + "</span>";
           }      
    
    //update radio buttons
    this.checkRadioButtons();
};
 */   
ShoppingCart.prototype.checkRadioButtons = function()
{    
    //get reference to table body
    //get reference to table records    
    var oBody = document.getElementById('bodyMiddle');
    var oTBody = oBody.getElementsByTagName('tbody');
    
    //get reference to table records
    for(var x = 0; x < oTBody.length; x++)
    {
        var itemHolders = oTBody[x].getElementsByTagName('tr');
        
        for(var i=0; i < itemHolders.length; i++)//loop through table records in order to update radio buttons
        {
            var oTempInputs = itemHolders[i].getElementsByTagName('input');
            var oInputs = new Array();
            for(var n=0; n < oTempInputs.length; n++)
            {
                if(oTempInputs[n].type == "radio") oInputs.push(oTempInputs[n]);
            }
            oInputs[0].checked = false;
            oInputs[1].checked = false;
            if(this.items[i].buyInspect == "II")
            {
                oInputs[0].checked = true;                
            } else {
                        oInputs[1].checked = true;
                   }
        }
    }
};
    
ShoppingCart.prototype.checkInspectBuy = function(ib,i)
{    
    if((this.items[i].buyInspect.substring(1)).toUpperCase() == ib)return true;
    return false;
};
    
    
/*----------------------------------------------------------------------------------------------------------------------------------------*/



ShoppingCart.prototype.attachItem = function(i)
{
    //create table record holder
    var oTR = document.createElement('tr');      
    
    //create isbn column
    var tdIsbn = document.createElement('td');
    tdIsbn.appendChild(document.createTextNode(isbn10to13(this.items[i].isbn)));
    oTR.appendChild(tdIsbn);      
      
    //create title column  
    var tdTitle = document.createElement('td');
    tdTitle.className = "title";
    var titleLink = document.createElement('a');
    var bookLink = this.items[i].getBookLink();
    titleLink.setAttribute('href',bookLink);
    titleLink.title = this.items[i].title;
    titleLink.appendChild(document.createTextNode(this.items[i].title));
    tdTitle.appendChild(titleLink);
    oTR.appendChild(tdTitle);      
      
    //create price column  
    var tdPrice = document.createElement('td');      
    if((typeof checkTC == "undefined")||((typeof checkTC == "boolean")&&(checkTC == false)) ||!this.items[i].isEClub())
    {
        if(this.items[i].price.indexOf('$') == -1)
        {
            var ePrice = "$" + this.items[i].price;
        } else {
                    var ePrice = this.items[i].price;
               }
    } else {
                var ePrice = "-";
           }
    tdPrice.appendChild(document.createTextNode(ePrice));
    oTR.appendChild(tdPrice);      
      
    //create eClub price column  
    var tdClubPrice = document.createElement('td');
    if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
    {
        if(this.items[i].price.indexOf('$') == -1)
        {
            var ePrice = "$" + this.eClubPrice(i);
        } else {
                    var ePrice = this.eClubPrice(i);
               }
    } else {
                var ePrice = "-";
           }
           
    tdClubPrice.appendChild(document.createTextNode(ePrice));
    oTR.appendChild(tdClubPrice);      
      
    //create quantity column  
    var tdQuantity = document.createElement('td');
    var iQuantity = document.createElement('input');
    iQuantity.setAttribute('type','text');
    iQuantity.className = "units";
    iQuantity.setAttribute('value',this.items[i].quantity);
    if(this.items[i].buyInspect == "II")
    {
        iQuantity.disabled = true;
    }
    tdQuantity.appendChild(iQuantity);
    oTR.appendChild(tdQuantity);      
      
    //create inspect column  

    var tdInspect = document.createElement('td');
    var iInspect = document.createElement('input');
    try
    {
        var oChecked = (this.items[i].buyInspect.substring(1) == "I") ? 'checked' : '';
        var iInspect = document.createElement('<input type="radio" name="ib' + i + '" ' + oChecked + ' />');
    }catch(err){
        var iInspect = document.createElement('input');
        if(this.items[i].buyInspect.substring(1) == "I")
        {
            iInspect.checked;
            iInspect.setAttribute('checked','true');
        }
    }
    iInspect.setAttribute('type','radio');
    iInspect.setAttribute('name',('ib' + i));
    iInspect.setAttribute('value','I');    
    if(this.items[i].buyInspect.substring(0,1) == "I")
    {
        if(document.attachEvent)
        {
            iInspect.onclick = function(oEvent){updateInspectBuy();}; 
        } else {
            iInspect.setAttribute('onclick','updateInspectBuy(event)'); 
        }
    } else {
        iInspect.disabled = true;
    }
    tdInspect.appendChild(iInspect);
    oTR.appendChild(tdInspect);      
          
    //create buy column  
    var tdBuy = document.createElement('td');   
    try
    {
        var oChecked = (this.items[i].buyInspect.substring(1) == "B") ? 'checked' : '';
        var iBuy = document.createElement('<input type="radio" name="ib' + i + '" ' + oChecked + ' />');
    }catch(err){
        var iBuy = document.createElement('input');
        if(this.items[i].buyInspect.substring(1) == "B")
        {
            iBuy.checked;
            iBuy.setAttribute('checked','true');
        }
    }
    iBuy.setAttribute('type','radio');
    iBuy.setAttribute('name',('ib' + i));
    iBuy.setAttribute('value','B');
    if(document.attachEvent)
    {
        iBuy.onclick = function(oEvent){updateInspectBuy();}; 
    } else {
        iBuy.setAttribute('onclick','updateInspectBuy(event)'); 
    }
        
    tdBuy.appendChild(iBuy);
    oTR.appendChild(tdBuy);      
    
    //If item is up for inspection do not add price to total column
    if(this.items[i].buyInspect.substring(1) == "I")
    {
        var itemTotal = 0;
    } else {
        if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
        {
            var itemTotal = (this.eClubPrice(i).indexOf('$') != -1) ? this.eClubPrice(i).substring(1) : this.eClubPrice(i);
        } else {
            var itemTotal = (this.items[i].price.indexOf('$') != -1) ? this.items[i].price.substring(1) : this.items[i].price;
        }
        
        itemTotal = Number(itemTotal) * this.items[i].quantity;
    }
    
    var td = document.createElement('td');
    td.appendChild(document.createTextNode('$' + itemTotal.toFixed(2)));
    oTR.appendChild(td);
    
    if(!this.items[i].available)
    {
        var shipDate = this.items[i].shipDate;    
    } else {
        var shipDate = 'available';
    }
    
    var td = document.createElement('td');
    td.appendChild(document.createTextNode(shipDate));
    oTR.appendChild(td);
      
    //create remove button column  
    var tdRemove = document.createElement('td');
    tdRemove.className = "last";
    var aRemove = document.createElement('input');
    aRemove.setAttribute('name',i);
    aRemove.setAttribute('type','button');
    aRemove.value = 'remove';
    aRemove.className = "remove";
    if(document.attachEvent)
    {
        aRemove.onclick = function(oEvent){removefromOverviewCart();}; 
    } else {
                aRemove.setAttribute('onclick','removefromOverviewCart(event)'); 
           }      
    tdRemove.appendChild(aRemove);
    oTR.appendChild(tdRemove);      
    
    
    //return table record element
    return oTR;
};





/*
ShoppingCart.prototype.attachItem = function(i)
{
    //create table record holder
    var oTR = document.createElement('tr');      
    
    //create isbn column
    var tdIsbn = document.createElement('td');
    tdIsbn.appendChild(document.createTextNode(isbn10to13(this.items[i].isbn)));
    oTR.appendChild(tdIsbn);      
      
    //create title column  
    var tdTitle = document.createElement('td');
    tdTitle.className = "title";
    var titleLink = document.createElement('a');
    var bookLink = this.items[i].getBookLink();
    titleLink.setAttribute('href',bookLink);
    titleLink.title = this.items[i].title;
    titleLink.appendChild(document.createTextNode(this.items[i].title));
    tdTitle.appendChild(titleLink);
    oTR.appendChild(tdTitle);      
      
    //create price column  
    var tdPrice = document.createElement('td');      
    if((typeof checkTC == "undefined")||((typeof checkTC == "boolean")&&(checkTC == false)) ||!this.items[i].isEClub())
    {
        if(this.items[i].price.indexOf('$') == -1)
        {
            var ePrice = "$" + this.items[i].price;
        } else {
                    var ePrice = this.items[i].price;
               }
    } else {
                var ePrice = "-";
           }
    tdPrice.appendChild(document.createTextNode(ePrice));
    oTR.appendChild(tdPrice);      
      
    //create eClub price column  
    var tdClubPrice = document.createElement('td');
    if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
    {
        if(this.items[i].price.indexOf('$') == -1)
        {
            var ePrice = "$" + this.eClubPrice(i);
        } else {
                    var ePrice = this.eClubPrice(i);
               }
    } else {
                var ePrice = "-";
           }
           
    tdClubPrice.appendChild(document.createTextNode(ePrice));
    oTR.appendChild(tdClubPrice);      
      
    //create quantity column  
    var tdQuantity = document.createElement('td');
    var iQuantity = document.createElement('input');
    iQuantity.setAttribute('type','text');
    iQuantity.className = "units";
    iQuantity.setAttribute('value',this.items[i].quantity);
    if(this.items[i].buyInspect == "II")
    {
        iQuantity.disabled = true;
    }
    tdQuantity.appendChild(iQuantity);
    oTR.appendChild(tdQuantity);      
      
    if(this.items[i].available)
    {
         
        //create inspect column  

        
        var tdInspect = document.createElement('td');
        var iInspect = document.createElement('input');
        try
        {
            var oChecked = (this.items[i].buyInspect.substring(1) == "I") ? 'checked' : '';
            var iInspect = document.createElement('<input type="radio" name="ib' + i + '" ' + oChecked + ' />');
        }catch(err){
                        var iInspect = document.createElement('input');
                        if(this.items[i].buyInspect.substring(1) == "I")
                        {
                            iInspect.checked;
                            iInspect.setAttribute('checked','true');
                        }
                   }
        iInspect.setAttribute('type','radio');
        iInspect.setAttribute('name',('ib' + i));
        iInspect.setAttribute('value','I');    
        if(this.items[i].buyInspect.substring(0,1) == "I")
        {
            if(document.attachEvent)
            {
                iInspect.onclick = function(oEvent){updateInspectBuy();}; 
            } else {
                        iInspect.setAttribute('onclick','updateInspectBuy(event)'); 
                   }
        } else {
                    iInspect.disabled = true;
               }
        tdInspect.appendChild(iInspect);
        oTR.appendChild(tdInspect);      
          
        //create buy column  
        var tdBuy = document.createElement('td');   
        try
        {
            var oChecked = (this.items[i].buyInspect.substring(1) == "B") ? 'checked' : '';
            var iBuy = document.createElement('<input type="radio" name="ib' + i + '" ' + oChecked + ' />');
        }catch(err){
                        var iBuy = document.createElement('input');
                        if(this.items[i].buyInspect.substring(1) == "B")
                        {
                            iBuy.checked;
                            iBuy.setAttribute('checked','true');
                        }
                   }
        iBuy.setAttribute('type','radio');
        iBuy.setAttribute('name',('ib' + i));
        iBuy.setAttribute('value','B');
        if(document.attachEvent)
        {
            iBuy.onclick = function(oEvent){updateInspectBuy();}; 
        } else {
                    iBuy.setAttribute('onclick','updateInspectBuy(event)'); 
               }
        
        tdBuy.appendChild(iBuy);
        oTR.appendChild(tdBuy);      
    }
    
    if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
    {
        var itemTotal = (this.eClubPrice(i).indexOf('$') != -1) ? this.eClubPrice(i).substring(1) : this.eClubPrice(i);
    } else {
        var itemTotal = (this.items[i].price.indexOf('$') != -1) ? this.items[i].price.substring(1) : this.items[i].price;
    }
    
    itemTotal = Number(itemTotal) * this.items[i].quantity;
    
    var td = document.createElement('td');
    td.appendChild(document.createTextNode('$' + itemTotal.toFixed(2)));
    oTR.appendChild(td);
    
    if(!this.items[i].available)
    {
        var td = document.createElement('td');
        td.appendChild(document.createTextNode(this.items[i].shipDate));
        oTR.appendChild(td);
    }
      
    //create remove button column  
    var tdRemove = document.createElement('td');
    tdRemove.className = "last";
    var aRemove = document.createElement('input');
    aRemove.setAttribute('name',i);
    aRemove.setAttribute('type','button');
    aRemove.value = 'remove';
    aRemove.className = "remove";
    if(document.attachEvent)
    {
        aRemove.onclick = function(oEvent){removefromOverviewCart();}; 
    } else {
                aRemove.setAttribute('onclick','removefromOverviewCart(event)'); 
           }      
    tdRemove.appendChild(aRemove);
    oTR.appendChild(tdRemove);      
    
    
    //return table record element
    return oTR;
};
*/       
ShoppingCart.prototype.sortAvailable = function()
{
    var sortAvai = function(obj)
    {
        if(obj.available)return 1;
        return -1;
    };
    this.items.sort(sortAvai);
};   

ShoppingCart.prototype.display = function()
{
    var oStr = "cart items:" + this.items.length + "\n\n";
    for(var i=0; i<this.items.length;i++)
    {   
        oStr += "\n" + this.items[i].isbn + "   " + this.items[i].title + "   " +   this.items[i].price + "   " +   this.items[i].buyInspect + "   " +   this.items[i].quantity + "   " + this.items[i].divCatStr;
    }
    //alert(getCookie('MacPrimary') + "\n\n\n\n" + oStr);
    alert(oStr);
};
      
      
      
      
       
       
    ShoppingCart.prototype.removeFromCartOverview = function(n)
    {      
      this.deleteFromCart(this.items[n].isbn);      
      //this.updateShoppingCartOverview();
      this.attachShoppingCartOverview();
      this.updateShoppingSummary();
    };
    
    
ShoppingCart.prototype.updateCartOverview = function(iQuantity,requested)
{
    var tempItems = new Array();//array to store new items
    
    //loop through and update items with new quantities as well as deleting items.  
    
    //var str2 = "";
    
    for(var i=0; i < iQuantity.length; i++)
    {               
        if(iQuantity[i] != 0)//if item is not to be deleted  
        {
            if(iQuantity[i] > 0)
            {
                this.items[i].quantity = iQuantity[i];  
            }
            var tempArray = new Array();
            tempArray.push(this.items[i].isbn);
            tempArray.push(this.items[i].title);
            tempArray.push(this.items[i].price);
            tempArray.push(this.items[i].buyInspect);
            tempArray.push(this.items[i].quantity);
            tempArray.push(this.items[i].divCatStr);        
            tempItems.push(new CartItem(tempArray));
            
            //str2 += '\n[' + i + ']=' + this.items[i].quantity;
        }     
    }
    
    //alert(str2);   
   
    //reset items                 
    this.items = new Array();
    
    //populate items  
    for(var i=0; i < tempItems.length; i++)
    {
        var tempArray = new Array();          
        tempArray.push(tempItems[i].isbn);
        tempArray.push(tempItems[i].title);
        tempArray.push(tempItems[i].price);
        tempArray.push(tempItems[i].buyInspect);
        tempArray.push(tempItems[i].quantity);
        tempArray.push(tempItems[i].divCatStr);          
        this.items.push(new CartItem(tempArray));
    }  
         
    this.save();//save cart into cookie
      
    //var bookError = ""; //variable to store books flagged as requested
    
    //loop through items in order to identify which are not avaialable
    for(var i = 0; i < this.items.length; i++)
    {
        var priceAvailability = new PriceAvailability(this.items[i].isbn,this.items[i].quantity);
        if(priceAvailability.available)
        {
            this.items[i].available = true;
        } else {
                    this.items[i].shipDate = priceAvailability.getShipDateStringForSummary();  
                    /*
                    //alert(requested);
                    //check if book is requested already
                    var alreadyRequested = false;
                    for(var x = 0; x < requested.length; x++)
                    {                        
                        if(requested[x] == this.items[i].isbn)
                        {
                            alreadyRequested = true;
                        }
                    }
                    
                    if(alreadyRequested == false)
                    {           
                        bookError += '\n\n- ' + this.items[i].title + ' (' + this.items[i].isbn + ')';
                    }
                    */
               }    
    } 
    
    //update shopping cart overview
    //this.updateShoppingCartOverview();
    this.attachShoppingCartOverview();
    
    //update shopping cart summary
    this.updateShoppingSummary();
    /* 
    if(bookError != '')
    {
        alert('The following books are not available at the quantity requested:' + bookError + '\n\nThey have been added as requests.');
    }      
    */
};
    
    /*
    ShoppingCart.prototype.updateCartOverview = function(iQuantity)
    {
      var bookError = "";
      var tempItems = new Array();
      for(var i=0; i < iQuantity.length; i++)
      { 
              
        if(iQuantity[i] != -1 && iQuantity[i] != 0)  
        {
        
            var priceAvailability = new PriceAvailability(this.items[i].isbn,iQuantity[i]);
            if(priceAvailability.available || (!priceAvailability.available && this.items[i].price == 0))
            {
                this.items[i].quantity = iQuantity[i];
                this.items[i].available = true;
            } else {            
                        bookError += '\n\n-' + this.items[i].title + '\n' + this.items[i].isbn;
                   }
                   
        }       
        var tempArray = new Array();
        tempArray.push(this.items[i].isbn);
        tempArray.push(this.items[i].title);
        tempArray.push(this.items[i].price);
        tempArray.push(this.items[i].buyInspect);
        tempArray.push(this.items[i].quantity);
        tempArray.push(this.items[i].divCatStr);        
        tempItems.push(new CartItem(tempArray));
      }                  
      this.items = new Array();
      for(var i=0; i < tempItems.length; i++)
      {
        if(tempItems[i].quantity != 0)
        {
          var tempArray = new Array();          
          tempArray.push(tempItems[i].isbn);
          tempArray.push(tempItems[i].title);
          tempArray.push(tempItems[i].price);
          tempArray.push(tempItems[i].buyInspect);
          tempArray.push(tempItems[i].quantity);
          tempArray.push(tempItems[i].divCatStr);          
          this.items.push(new CartItem(tempArray));
        }
      }     
      this.save();
      this.updateShoppingCartOverview();
      this.updateShoppingSummary();
      
      if(bookError != '')
      {
        alert('The following book(s) is/are not available at the quantity requested:' + bookError + '\n\nWe\'re sorry for the inconvenience');
        
      }
      
    };
    */
    
    ShoppingCart.prototype.updateInspectBuy = function(i,ib)
    {
        this.items[i].buyInspect = this.items[i].buyInspect.substring(0,1) + ib;
        if(this.items[i].buyInspect == "II")
        {
          this.items[i].quantity = 1;
        }
        this.save();
        //this.updateShoppingCartOverview();
        this.attachShoppingCartOverview();
        this.updateShoppingSummary();
    }; 
/*   
ShoppingCart.prototype.getCheckoutConfirmationTable = function()
{
    var oTable = '';
    
    //attach table with unavailable books
    if(this.hasUnavailableBooks() == true)
    {
        oTable += this.getCheckoutConfirmationTableUnavailable();
    }
    
    //attach table with available books
    if(this.hasAvailableBooks() == true)
    {
        oTable += this.getCheckoutConfirmationTableAvailable();
    }
    
    //display purchase total      
    if((typeof checkTC != "undefined")&&(checkTC == true))
    {
        oTable += '<p id="totalPrice"><span>Total purchase<br />$' + this.getEClubTotal() + '</span></p>';
    } else {
                oTable += '<p id="totalPrice"><span>Total purchase<br />$' + this.getTotal() + '</span></p>';
           }
    
    return oTable;
};
*/

ShoppingCart.prototype.getCheckoutConfirmationTable = function()
{
    var oTable = '';
    
    //attach table with books
    if(this.items.length > 0)
    {
        oTable += this.getCheckoutConfirmationTableAvailableAndUnavailable();
    }
    
    //display purchase total      
    if((typeof checkTC != "undefined")&&(checkTC == true))
    {
        oTable += '<p id="totalPrice"><span>Total purchase<br />$' + this.getEClubTotal() + '</span></p>';
    } else {
                oTable += '<p id="totalPrice"><span>Total purchase<br />$' + this.getTotal() + '</span></p>';
           }
    
    return oTable;
};

ShoppingCart.prototype.getCheckoutConfirmationTableUnavailable = function()
{
    //create table tag
    var oText = "<table><caption>Requests:</caption>";
    
    //create table head
    oText += '<thead><tr>';
    //oText += '<th>ISBN</th><th>Title</th><th>RRP</th><th>e-Club price</th><th>Quantity</th><th>Inspect</th><th class="last">Buy</th>';
    oText += '<th>ISBN</th><th>Title</th><th>RRP</th><th>e-Club price</th><th>Quantity</th><th>Total</th><th>Available</th>';
    oText += '</tr></head>';
    
    //create table body
    oText += '<tbody>';
    
    //loop through items and add them to table
    for(var i=0; i < this.items.length; i++)
    {
        //if item is available
        if(!this.items[i].available)
        {
            //create table body
            oText += '<tr>';
            oText += '<td>' + isbn10to13(this.items[i].isbn) + '</td>';
            oText += '<td class="title">' + this.items[i].title + '</td>';
            
            //if there's eClub discount apply it
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                oText += '<td>-</td><td>$' + this.eClubPrice(i) + '</td>';
            } else {//calculate full total
                        oText += '<td>$' + this.items[i].price + '</td><td>-</td>';
                   } 
                   
            oText += '<td>' + this.items[i].quantity + '</td>';
            
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                var itemTotal = (this.eClubPrice(i).indexOf('$') != -1) ? this.eClubPrice(i).substring(1) : this.eClubPrice(i);
            } else {
                var itemTotal = (this.items[i].price.indexOf('$') != -1) ? this.items[i].price.substring(1) : this.items[i].price;
            }
            
            itemTotal = Number(itemTotal) * this.items[i].quantity;
            
            oText += '<td>$' + itemTotal.toFixed(2) + '</td>';
            
            oText += '<td>' + this.items[i].shipDate + '</td>';
            
            /*
            //if it is an inspect copy add tick to inspct. If not add tick to buy
            if(this.items[i].buyInspect.substring(1) == "I")
            {
                oText += '<td><span class="ibSelect">inspect</span></td><td></td>';
            } else {
                        oText += '<td></td><td><span class="ibSelect">buy</span></td>';
                   }
            */
            oText += '</tr>';
        }
    }
    oText += "</tbody></table>"; 
          
    return oText;
};

ShoppingCart.prototype.getCheckoutConfirmationTableAvailable = function()
{
    //create table tag
    var oText = "<table><caption>Purchases:</caption>";
    
    //create table head
    oText += '<thead><tr>';
    oText += '<th>ISBN</th><th>Title</th><th>RRP</th><th>e-Club price</th><th>Quantity</th><th>Inspect</th><th>Buy</th><th class="last">Total</th>';
    oText += '</tr></head>';
    
    //create table body
    oText += '<tbody>';
    
    //loop through items and add them to table
    for(var i=0; i < this.items.length; i++)
    {
        //if item is available
        if(this.items[i].available)
        {
            //create table body
            oText += '<tr>';
            oText += '<td>' + isbn10to13(this.items[i].isbn) + '</td>';
            oText += '<td class="title">' + this.items[i].title + '</td>';
            
            //if there's eClub discount apply it
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                oText += '<td>-</td><td>$' + this.eClubPrice(i) + '</td>';
            } else {//calculate full total
                        oText += '<td>$' + this.items[i].price + '</td><td>-</td>';
                   } 
                   
            oText += '<td>' + this.items[i].quantity + '</td>';
            
            //if it is an inspect copy add tick to inspct. If not add tick to buy
            if(this.items[i].buyInspect.substring(1) == "I")
            {
                oText += '<td><span class="ibSelect">inspect</span></td><td></td>';
            } else {
                        oText += '<td></td><td><span class="ibSelect">buy</span></td>';
                   }
                   
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                var itemTotal = (this.eClubPrice(i).indexOf('$') != -1) ? this.eClubPrice(i).substring(1) : this.eClubPrice(i);
            } else {
                var itemTotal = (this.items[i].price.indexOf('$') != -1) ? this.items[i].price.substring(1) : this.items[i].price;
            }
            
            itemTotal = Number(itemTotal) * this.items[i].quantity;
            
            oText += '<td>$' + itemTotal.toFixed(2) + '</td>';
                   
            oText += '</tr>';
        }
    }
    oText += "</tbody></table>";
    
    
          
    return oText;
};

ShoppingCart.prototype.getCheckoutConfirmationTableAvailableAndUnavailable = function()
{
    //create table tag
    //var oText = "<table><caption>Purchases:</caption>";
    var oText = "<table>";
    
    //create table head
    oText += '<thead><tr>';
    oText += '<th>ISBN</th><th>Title</th><th>RRP</th><th>e-Club price</th><th>Quantity</th><th>Inspect</th><th>Buy</th><th>Total</th><th class="last">Available</th>';
    oText += '</tr></thead>';
    
    //create table body
    oText += '<tbody>';
    
    //loop through items and add them to table
    for(var i=0; i < this.items.length; i++)
    {
        //create table body
        oText += '<tr>';
        oText += '<td>' + isbn10to13(this.items[i].isbn) + '</td>';
        oText += '<td class="title">' + this.items[i].title + '</td>';
            
        //if there's eClub discount apply it
        if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
        {
            oText += '<td>-</td><td>$' + this.eClubPrice(i) + '</td>';
        } else {//calculate full total
            oText += '<td>$' + this.items[i].price + '</td><td>-</td>';
        } 
                   
        oText += '<td>' + this.items[i].quantity + '</td>';
            
        //if it is an inspect copy add tick to inspct. If not add tick to buy
        if(this.items[i].buyInspect.substring(1) == "I")
        {
            oText += '<td><span class="ibSelect">inspect</span></td><td></td>';
        } else {
            oText += '<td></td><td><span class="ibSelect">buy</span></td>';
        }
        
        if(this.items[i].buyInspect.substring(1) == "I")
        {
            var itemTotal = 0;
        } else {           
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                var itemTotal = (this.eClubPrice(i).indexOf('$') != -1) ? this.eClubPrice(i).substring(1) : this.eClubPrice(i);
            } else {
                var itemTotal = (this.items[i].price.indexOf('$') != -1) ? this.items[i].price.substring(1) : this.items[i].price;
            }
          
            itemTotal = Number(itemTotal) * this.items[i].quantity;
        }
            
        oText += '<td>$' + itemTotal.toFixed(2) + '</td>';
        
        if(!this.items[i].available)
        {
            var shipDate = this.items[i].shipDate;    
        } else {
            var shipDate = 'available';
        }
        
        oText += '<td>' + shipDate + '</td>';
                   
        oText += '</tr>';
    }
    oText += "</tbody></table>";
    
    
          
    return oText;
};


/*
    ShoppingCart.prototype.getCheckoutConfirmationTable = function()
    {
          var oText = "<table><caption>Your pruchase:</caption>";
          oText += '<thead><tr>';
          oText += '<th>ISBN</th><th>Title</th><th>RRP</th><th>e-Club price</th><th>Quantity</th><th>Inspect</th><th class="last">Buy</th>';
          oText += '</tr></head>';
          oText += '<tbody>';
          for(var i=0; i < this.items.length; i++)
          {
            oText += '<tr>';
            oText += '<td>' + isbn10to13(this.items[i].isbn) + '</td>';
            oText += '<td class="title">' + this.items[i].title + '</td>';
            if(this.items[i].isEClub()&&((typeof checkTC != "undefined")&&(checkTC == true)))
            {
                oText += '<td>-</td><td>$' + this.eClubPrice(i) + '</td>';
            } else {
                oText += '<td>$' + this.items[i].price + '</td><td>-</td>';
            } 
            oText += '<td>' + this.items[i].quantity + '</td>';
            if(this.items[i].buyInspect.substring(1) == "I")
            {
              oText += '<td><span class="ibSelect">inspect</span></td><td></td>';
            } else {
              oText += '<td></td><td><span class="ibSelect">buy</span></td>';
            }
            oText += '</tr>';
          }
          oText += "</tbody></table>";
          if((typeof checkTC != "undefined")&&(checkTC == true))
          {
            oText += '<p id="totalPrice"><span>Total purchase<br />$' + this.getEClubTotal() + '</span></p>';
          } else {
            oText += '<p id="totalPrice"><span>Total purchase<br />$' + this.getTotal() + '</span></p>';
          }
          
          return oText;
    };
*/


    ShoppingCart.prototype.hasInspectedItem = function()
    {
      for(var i=0; i < this.items.length; i++)
      {
        if(this.items[i].buyInspect == "II")return true;
      }      
      return false;
    }; 
    
/*----------------------------------------------------------------------------------------------------------------------------------------*/    
    
    
    
  }  
  this.load();
}

function CartItem(item)
{
  this.isbn = item[0];
  this.title = item[1];
  this.price = item[2];
  this.buyInspect = item[3];
  this.quantity = item[4];  
  this.divCatStr = item[5];  
  if(typeof CartItem._initialized == "undefined")
  {
      CartItem.prototype.cookieString = function()
      {
        return (this.isbn + "|" + this.title + "|" + this.price + "|" + this.buyInspect + "|" + this.quantity + "|" + this.divCatStr);
      };
      
      CartItem.prototype.newCookieString = function()
      {
        if(this.divCatStr.indexOf('>') != -1)
        {
            var ciDivision = this.divCatStr.split('>')[0];
        } else {
                    var ciDivision = this.divCatStr;
        }
        return (this.isbn + "|" + this.buyInspect + "|" + this.quantity + "|" + ciDivision);
      }; 
      CartItem.prototype.isEClub = function()
      {                  
         var discounted = eClub.isDiscounted(this.divCatStr);          
         return discounted;
      };
      
      CartItem.prototype.getBookLink = function()
      {
        var bookLink = ["/primary/newonix/domisbn/" + this.isbn + "?open"];
        
        var divCatSplit = this.divCatStr.split('>');
        
        bookLink.push("&div=" + escape(divCatSplit[0]));
        if(divCatSplit.length == 3)
        {
            bookLink.push("&cat=" + escape(divCatSplit[1]) + ">" + escape(divCatSplit[2]));
        } else {
            bookLink.push("&cat=" + escape(divCatSplit[1]));
        }
        if(this.buyInspect.substring(0,1) == "I") 
        {
            bookLink.push("&bi=1");
        } else {
            bookLink.push("&bi=0");
        }
        bookLink.push("&ed=site/primed3BETA.nsf");
        return bookLink.join("");
      };
  }
  CartItem._initialized = true;
}

oCart = new ShoppingCart();