﻿// Cookie and order form functions

    function storeToCookie(objProduct) {
        //Declare Vars
        var strNewItem = (objProduct.name + "_" + objProduct.value)
        strNewItem = strNewItem.substring(4, strNewItem.length);
        var strTempItem;
        var arrTempItem;
        var strNewCookie = "";
              
        //Get code of new item
        var arrNewItemCode = strNewItem.split("_");

        //Add new item if greater than 0
        if (arrNewItemCode[1] >= 1)
        {
            strNewCookie = strNewItem;
        }
        

        //Get the cookie if it exists and remove any duplicate info
        if (getCookie("NPOrderForm") != null)
        {
            //Break it down
            var strCookie = getCookie("NPOrderForm");
            var arrCookieItems = strCookie.split(",");
            
            //check for the code in each item
            for (var i = 0; i < arrCookieItems.length; i++)
            {
                strTempItem = arrCookieItems[i];
                arrTempItemCode = strTempItem.split("_");
                
                //Add all other items, if one of them was same code, do not re-add the old info
                if (arrTempItemCode[0] != arrNewItemCode[0])
                {
                    //strNewCookie will be empty if the new item has a zero value
                    if (strNewCookie != "") 
                    {
                        strNewCookie += "," + strTempItem;
                    } else {
                        strNewCookie = strTempItem;
                    }
                }
            }            
        }

        //set cookie
        setCookie("NPOrderForm", strNewCookie, 1);
    }

    function getCookie(NameOfCookie)
    {
        // First we check to see if there is a cookie stored.
        // Otherwise the length of document.cookie would be zero.

        if (document.cookie.length > 0) 
        { 

            begin = document.cookie.indexOf(NameOfCookie+"="); 
            
            if (begin != -1) // Note: != means "is not equal to"
            { 

                // Our cookie was set. 
                // The value stored in the cookie is returned from the function.

                begin += NameOfCookie.length+1; 
                end = document.cookie.indexOf(";", begin);

                if (end == -1) end = document.cookie.length;
                {
                    return unescape(document.cookie.substring(begin, end)); 
                }
            }
                     
        }
        return null; 

        // Our cookie was not set. 
        // The value "null" is returned from the function.
    } 
     
    function setCookie(NameOfCookie, value, expiredays) 
    {
        var ExpireDate = new Date ();
        ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

        document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
    } 
     
    function delCookie (NameOfCookie) 
    {
        // The function simply checks to see if the cookie is set.
        // If so, the expiration date is set to Jan. 1st 1970.

        if (getCookie(NameOfCookie)) 
        {
            document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    } 

    function addQty(objProduct) 
    {
        var intCurrentValue = parseInt (objProduct.value);
        objProduct.value = (intCurrentValue + 12);
    }

    function decreaseQty(objProduct) 
    {
        if (objProduct.value >= 12)    
        {
            objProduct.value -= 12;
        } else {
            objProduct.value = 0;
        }
    }
    
