//Functions to deal with data passed from Cart Data.
//REQUIRES currency.js, explode.js be loaded first

function parseCartData(DATA)
 {
  //Parses Cart Data String of the form item_id,quantity,price|<next item>
  //Returns a 2D array of the form items[i][item_id], items[i][quantity], items[i][price]
  DATA = DATA.substring(0,DATA.length-1);
  var i=0;
  var dataArray = explode('|', DATA);
  var items = new Array(dataArray.length);

  for (i=0; i < dataArray.length; i++)
   {items[i] = explode(',', dataArray[i]);}
  return(items);
 }

function checkForDuplicateItems(ITEM, DATA)
 {
  //Compares ITEM array in form of ITEM[item_id], ITEM[quantity], ITEM[price] to cart string
  //Returns true if item_id already exists in Cart, else false.

  var items = parseCartData(DATA);
  var i=0;

  for (i=0; i < items.length; i++) {if (items[i][0] == ITEM[0]) {return(true);}}

  return(false);
 }


        function addItemsToShoppingCart(field)
         {
          var added = false;
          var item;
          if (field.length > 0)
           {
            for (i=0; i < field.length; i++)
             {
              if (field[i].checked)
               {
                if (checkForDuplicateItems(explode(',', field[i].value), document.searchForm.cart.value))
                 {alert("One of the selected items is already in your cart.  To change quantity, please proceed to the checkout.");   return;}
                document.searchForm.cart.value += field[i].value + "|";
                added=true;
               }
             }
           }
          else
           {
            if (checkForDuplicateItems(explode(',', field.value), document.searchForm.cart.value))
             {alert("One of the selected items is already in your cart.  To change quantity, please proceed to the checkout.");   return;}
            document.searchForm.cart.value += field.value + "|";
            added=true;
           }

          if (added)
           {
//            if (confirm("Are you sure you wish to add the selected items to your cart?"))
//             {document.searchForm.submit();}
            document.searchForm.submit();
           }
          else {alert("Sorry, you do not have any items selected to be added to your cart at this time.  Please check at least one item to your cart.");}
         }








        function addItemsToShoppingCart2(field)
         {
          var added = false;
          if (field.length > 0)
           {
            for (i=0; i < field.length; i++)
             {
              if (checkForDuplicateItems(explode(',', field[i]), document.searchForm.cart.value))
               {alert("One of the selected field is already in your cart.  To change quantity, please proceed to the checkout.");   return;}
              document.searchForm.cart.value += field[i] + "|";
              added=true;
             }
           }
          else
           {
            if (checkForDuplicatefield(explode(',', field), document.searchForm.cart.value))
             {alert("One of the selected items is already in your cart.  To change quantity, please proceed to the checkout.");   return;}
            document.searchForm.cart.value += field + "|";
            added=true;
           }

          if (added)
           {
//            if (confirm("Are you sure you wish to add the selected field to your cart?"))
//             {document.searchForm.submit();}
            document.searchForm.submit();
           }
          else {alert("Sorry, you do not have any items selected to be added to your cart at this time.  Please check at least one item to your cart.");}
         }



















function writeCartHeader(CART, COLORS)
 {
  var cost = 0;
  var i = 0;
  var text = "";

  DATA = parseCartData(CART);
  for (i=0; i < DATA.length; i++)
   {cost += (parseInt(DATA[i][1]) * parseFloat(DATA[i][2]));}
  cost = convertToCurrency(""+cost);

  text += "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BACKGROUND='"+COLORS[2]+"' BGCOLOR='"+COLORS[1]+"'>";
  text += "  <TR>";
  text += "    <TD ALIGN=left VALIGN=top COLSPAN=6><FONT COLOR='"+COLORS[0]+"'><B>&nbsp;CART:</B></FONT></TD>";
  text += "    <TD WIDTH=100%></TD>";
  text += "  </TR>";


  text += "  <TR>";
  text += "    <TD WIDTH=25 NOWRAP></TD>";
  text += "    <TD ALIGN=right VALIGN=top NOWRAP><FONT COLOR='"+COLORS[0]+"'>Items:</FONT></TD>";
  text += "    <TD WIDTH=15 NOWRAP><B></B></TD>";
  text += "    <TD ALIGN=left VALIGN=top NOWRAP><FONT COLOR='"+COLORS[0]+"'>"+DATA.length+"</FONT></TD>";
  text += "  </TR>";
  text += "  <TR>";
  text += "    <TD WIDTH=25 NOWRAP></TD>";
  text += "    <TD ALIGN=right VALIGN=top NOWRAP><FONT COLOR='"+COLORS[0]+"'>Price:</FONT></TD>";
  text += "    <TD WIDTH=10 NOWRAP><B></B></TD>";
  text += "    <TD ALIGN=left VALIGN=top NOWRAP><FONT COLOR='"+COLORS[0]+"'>"+cost+"</FONT></TD>";
  text += "    <TD WIDTH=10 NOWRAP><B></B></TD>";
  text += "    <TD ALIGN=left NOWRAP>";
  text += "      <FORM NAME=cartForm METHOD=POST ACTION='"+WEBSITESECURE+"functions/cartView.php'>";
  text += "        <INPUT TYPE=hidden NAME='cart' VALUE='"+CART+"'>";
  text += "        <INPUT TYPE=submit VALUE='View Cart'>";
  text += "      </FORM>";
  text += "    </TD>";
  text += "  </TR>";
  text += "  <TR>";
  text += "    <TD COLSPAN=7 HEIGHT=5></TD>";
  text += "  </TR>";
  text += "</TABLE><BR>";

  return(text);
 }

