// NebuCart - The JavaScript Shopping Cart
//
// Copyright 1999-2005 all rights reserved.

// None of this script may be redistributed or sold
// without the authors express consent.
// Violations of copyright will be prosecuted.

// If you would like to use NebuCart,
// email us at admin@javascriptcart.com
// or visit http://www.javascriptcart.com

// ********************************************
// NebuCart MOD Multi-Add                     *
// ********************************************
// DO NOT CHANGE ANYTHING BELOW THIS LINE!    *
// ********************************************

/*
For each page you require Multi-Add functionality, include this 
script beneath the other NebuCart includes, alternatively you can
copy and paste this entire function into NC_formatting.js or NebuCart.js

Build your pages as normal and make sure the input type for 
the prodID is a text box so the user can enter the amount required:
         <input type="text" name="prodID" value="">
At the bottom of the page make a call to the following function 
AddItems(), no parameter is required, instead we'll find all the 
products that have a quantity > 0 and we'll add them. If an item
already exists with the same options, it is not added.
<input type="button" onclick="AddItems()" value="Add my selections">
<a href="javascript:AddItem()" title="Add my selections">Add my selections</a>
*/

//Define a variable for holding the coffee name.
var Coffee = '';

function AddItems(){
addedItems   = 0;
form = document.NC_form;
for (x=0;x<form.elements.length;x++){
 ID = form.elements[x].name;
 if (ID.indexOf('_desc')!=-1){
 itemID = ID.substring(0,ID.lastIndexOf('_'))// Changed this so you can include an underscore in your names
  if (itemID && getValue(itemID,'')>0){
 thisCoffee = (itemID.indexOf('_')!=-1)?itemID.split('_')[0]:itemID;//by darren
 if (Cart.length>0){
    for(i=0; i < Cart.length; i++){
	//if (Cart[i].prodID == itemID && getOptions(itemID,'opt')==Cart[i].opt){//by darren
       if (Cart[i].prodID == itemID && getValue(thisCoffee+'_instructions','')==Cart[i].opt){
	  Cart[i].qty = 0;
     }
     if(Cart[i].qty > 0){
      tmpArray[tmpArray.length] = Cart[i];
     }
    }
    Cart = new Array();
    for(i=0; i < tmpArray.length; i++){
    Cart[i] = tmpArray[i];
    }
   tmpArray = new Array();
   }
  qty = getValue(itemID,'');
//  alert(itemID)
   if(Number(qty)){
    price   = getValue(itemID,'price');
    desc    = getValue(itemID,'desc');
    opt     = getOptions(itemID,'opt');
      //thisCoffee = (itemID.indexOf(' ')!=-1)?itemID.split(' ')[0]:itemID;//by darren
      // thisCoffee now contains the itemID or the first word if there's a space
      // the textarea name doesn't like spaces.
      if (getValue(thisCoffee+'_instructions','') && Coffee != thisCoffee){
      // if the input box contains a value and it's not the same as the Coffee variable
       opt = getValue(thisCoffee+'_instructions','');
       // force the first selected option for this product to contain the value
 //      alert(opt)
       Coffee = thisCoffee;
       //Change the Coffee variable to thisCoffee so we don't add it again
      }
     tmpXtra = getOptionCosts(itemID,'opt');
     if(tmpXtra != 0){
      price = Number(price) + Number(tmpXtra);
     }
    limit  = '';
    Cart[Cart.length] = new CartItem(1, itemID, qty, desc, price, opt, limit);
    addedItems = 1;
    }
   }
  } 
 }
 if(addedItems){
  if(supressCart){
   alert('The item(s) have been added to your cart.');
   cartToCookie();
  } else {
   displayCart();
  }
 }
}
