/*
For each page you require Multi-Add functionality, include this AddItems()
function 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="Add()" value="Add my selections">
<a href="javascript:Add()" title="Add my selections">Add my selections</a>

This page is setup for use with the MultiAdd() function which works a
little differently to Add()
*/

function Add(){
added   = 0;
form = document.NC_form;
for (x=0;x<form.elements.length;x++){
 ID = form.elements[x].name;
 if (ID.indexOf('_price')!=-1){
 itemID = ID.substring(0,ID.indexOf('_'))
  if (itemID && getValue(itemID,'')>0){
   if (Cart.length>0){
    for(i=0; i < Cart.length; i++){
     if (Cart[i].prodID == itemID && getOptions(itemID,'opt')==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,'');
   if(Number(qty)){
    price   = getValue(itemID,'price');
    desc    = getValue(itemID,'desc');
    opt     = getOptions(itemID,'opt');
    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();
  }
 }
}


function MultiAddItem(itemID){
 maxFields    = 1000;
 addedItems   = 0;
 for(fldCount = 0; fldCount < maxFields; fldCount++){

  fldName = itemID + '_' + fldCount;
  itemFld = eval('document.NC_form.' + fldName);

  if(itemFld != null){

   qty = getValue(fldName,'');
   if(Number(qty)){
    price   = getValue(fldName,'price');
    desc    = getValue(fldName,'desc');
    opt     = getOptions(fldName,'opt');
    tmpXtra = getOptionCosts(fldName,'opt');
    if(tmpXtra != 0){
     price = Number(price) + Number(tmpXtra);
    }
    limit  = '';
    Cart[Cart.length] = new CartItem(1, itemID, qty, desc, price, opt, limit);
    addedItems = 1;
   }

  } else {
   break;
  }
 }

 if(addedItems){
  if(supressCart){
   alert('The item(s) have been added to your cart.');
   cartToCookie();
  } else {
   displayCart();
  }
 }
}
