maxCollection_show = 188;

function add_collection(product_id)
{
     old_collection = GetCookie('collection_cookie');

     if (product_id == 0)
     {
         collection_cookie = old_collection;
     }
     else
     {
         if (old_collection != null)
         {
             var collection_arg = new Array();

             collection_arg = old_collection.split("i");

             if (collection_arg.length <= maxCollection_show)
             {
                 search = 0;

                 for (i=1; i<maxCollection_show; i++)
                 {
                      if(collection_arg[i] != null)
                      {
                         if (collection_arg[i] == product_id)
                         {
                             search = 1;
                             break;
                          }
                      }
                      else
                      {
                          break;
                      }
                  }   /* end of for */

                  if (search == 0)
                  {
                      collection_cookie = old_collection + "i" + product_id;
                  }
                  else
                  {
                      collection_cookie = old_collection;
                  }
             }
             else
             {
                  window.alert("已经达到收藏商品上限，请先整理收藏夹！");
                  collection_cookie = old_collection;
             }
         }
         else
         {
            collection_cookie = "i" + product_id;
         }

         var expdays = 365;
         var expires = expdays*24*60*60;

         SetCookie('collection_cookie',collection_cookie,expires);
     }

     url = "show_collection.php?max=" + maxCollection_show +"&collection=" + collection_cookie;
     window.open(url, "我的收藏夹");
}


function del_collection(max)
{
     var del_arg = new Array();
     frm = document.FORMcollection;
     collection_cookie = '';

     checkNum = 0;
     for (i=0; i<frm.elements.length; i++) 
     {
         if (frm.elements[i].type == 'checkbox') 
         {                
             if(frm.elements[i].checked) 
             {
                del_arg[checkNum] = frm.elements[i].name;
                checkNum++;
             }                    
         }
     }                 

     if (checkNum == 0)
     {
        return (false);
     }

     old_collection = GetCookie('collection_cookie'); 

     if (old_collection != null)
     {
        var collection_arg = new Array();
        var item = new Array();

        collection_arg = old_collection.split("i");

        for (i=1; i<=maxCollection_show; i++)
        {
            if(collection_arg[i] != null)
            {
               product_id = collection_arg[i];
               search = 0;

               for (j=0; j<checkNum; j++)
               {
                   if (del_arg[j] == product_id)
                   {
                      search = 1;

                      break;
                   }
               }

               if (search == 0)
               {
                   collection_cookie = collection_cookie + "i" + product_id;
               }
            }
            else
            {
                break;
            }
        }   /* end of for */
     }

     var expdays = 365;
     var expires = expdays*24*60*60;

     SetCookie('collection_cookie',collection_cookie,expires);

     return (true);
}

