<!--
// ================================================================================================
var i;
var nPacks    = 0;    // number of packs in the cart
var nInCart   = 0;    // number of items selected on form but not in cart

function Cart( ID, plID, Qty ) 
{
  this.ID   = ID;
  this.plID = plID;  
  this.Qty  = Qty;
}

var aCart = new Array(4);
for( i=0; i<4; i++ )  
{
  aCart[i] = new Cart(0, 0, 0);  
}

// ================================================================================================



function Recount( )
{
  var nCnt = 0;
  for( var j=0; j<4; j++ )
    nCnt += aCart[j].Qty;    
  
  nInCart = nCnt;
}


function UpdateQty( plID, objIn )
{
  var strToEval = "";
  var strItem = "";
  var iStrLen = 0;
  var strTest = "";
  var ID, j;
  var intQty = parseInt(objIn.value);
  var bSkipUpdate = 0;

  strItem = new String(objIn.name);
  iStrLen = strItem.length;
  strToEval = strItem.substr(iStrLen-3, 3); 
  if( strToEval.length > 0 )
  {
    ID = parseInt(strToEval);
 
    for( j=0; j<4; j++ )
    {  
      if( aCart[j].ID == ID )
      {
        aCart[j].Qty = intQty;
        Recount();
        bSkipUpdate = 1;
      }
    }       


    if( !bSkipUpdate )
    {      
      if( (nInCart + intQty > 4)  )
      {
        objIn.selectedIndex = 0;
        alert("You cannot select more than four (4) containers for shipment.\n\nYou have " + nInCart + " of 4 selected for this 4-Pack." );
        return false;
      }
      else      
      {  
        aCart[nInCart].ID   = parseInt(strToEval);
        aCart[nInCart].plID = parseInt( plID );
        aCart[nInCart].Qty  = intQty; 
        nInCart += intQty;     
      }    
    }       
  }
  Recount();
}


function ChangeProduct( i, objIn )
{
  var nVal = 0;
  if( objIn )  nVal = objIn.value;
  var strEval = "document.getElementById('dvSub" + i + "').innerHTML = " + 
                "document.getElementById('dvFlavors_" + nVal + "').innerHTML";
  eval(strEval);
}


function Update( i )
{
  var j;
  var strEval, strDo;
  document.getElementById('dvShoppingCart').style.display = 'block'; 
  if( nInCart > 4 )
  {
    document.getElementByID('spMsg').innerHTML = "You cannot select more than four (4) containers for shipment.";
    alert("You cannot select more than four (4) containers for shipment.\n\nYou have " + nInCart + " of 4 selected for this 4-Pack." );
    return false;
  }
  else
  {
    strEval = "document.getElementById('btnUpdate" + i + "').style.display = 'none'";
    eval( strEval );
    
    strEval = "document.order.intProduct" + i + ".disabled = true";
    eval( strEval );
    ChangeProduct( i, null );  
    
 
    if( nInCart == 4 )    
      document.getElementById('dvProductLines').style.display = 'none';                   
    RefreshCart();
  }  
}


function RefreshCart( )
{
  document.getElementById('spCartItem1').innerHTML = DisplayCartContents();
}


function DisplayCartContents( )
{
  var j = 1;
  var strOut = "";
  var strProd = "";
  var intOrderType = parseInt(document.order.intOrderType.value);
  var strPage;
  strOut += "<table cellpadding='2' cellspacing='2' width='100%'>";
  if( nInCart > 0 )
    strOut += "<tr><td><a href='javascript: void 0;' onClick='PackRemove(" + nPacks + ");'>[Remove 4-Pack] </a></td><td align='center'><b class='heading2'>Qty.</b></td></tr>";  
  
  for( i=0; i<nInCart; i++ )
  {
    switch( intOrderType )
    {
      case 1:
        strPage = "../products/nutrition_facts.asp?pID=" + aCart[i].ID;
      case 2:
        strPage = "../products/nutrition_facts_drinks.asp?pID=" + aCart[i].ID;
      default:
        strPage = "nutrition_facts_drinks.asp";
    }
    
    if( aCart[i].Qty > 0 )
    {
      
      strProd = ProdLineName( aCart[i].plID ) + " - " + FlavorName( aCart[i].ID );
//      strOut += "<tr><td width='350'>" + strProd + " &nbsp;</td>";
      strOut += "<tr><td width='350'><a href=\"javascript:popUp('" + strPage + "', 700);\">" + strProd + "</a> &nbsp; </td>";            
      strOut += "<td width='15' align='center'>" + aCart[i].Qty + "<input type='hidden' name='intCartQty" + j + "' value='" + aCart[i].Qty + "'></td>";
      strOut += "<input type='hidden' name='intCartPID" + j + "' value='" + aCart[i].ID + "'></td></tr>";
      j++;
    }
  }  
  if( nInCart == 0 )
    strOut += "<tr><td>There are currently no items in your cart. </td></tr>";
  strOut += "</table>";
  return strOut;
}  
  
  
function FlavorName( ID )
{
  var i;
  for( i=0; i < aIDs.length; i++ )
  { 
    if( aIDs[i] == parseInt(ID) )
      return aNames[i];
  }  
  return "";  
}  


function ProdLineName( ID )
{
  var i;
  for( i=0; i < aPIDs.length; i++ )
  {
    if( aPIDs[i] == parseInt(ID) )
      return aPNames[i];
  }  
  return "";  
}  


function PackRemove( intPack )
{
  // when we have multiple packs, remove the correct one from the cart
  if( ConfirmRemove() )
  {
    for( i=0; i<4; i++ )  
    {
      aCart[i].ID   = 0;
      aCart[i].plID = 0;
      aCart[i].Qty  = 0;
      strEval = "document.order.intProduct" + parseInt(i+1) + ".disabled = false";
      eval( strEval );
      
      strEval = "document.order.btnUpdate" + parseInt(i+1) + ".disabled = false";
      eval( strEval );
    }
    nInCart   = 0;
    if( nPacks > 0 )  
      nPacks--;
    RefreshCart();    
    document.getElementById('dvProductLines').style.display = 'block';      
    document.getElementById('dvShoppingCart').style.display = 'none';                               
  }
}


  function CheckAddToCart( )
  {
    if( nInCart != 4 )
    {
      alert("You must select four (4) containers for shipment.");
      return false;
    }
    return true;
  }


// -->