////////////////////////////////////////////////////////////////////////////////
//                        Shopping Cart v1.00                                 //
//      This is a Javascript client based, cookies shopping cart. Therefore it//
//is not very secure.  It is posible for the enduser to adjust the cookies and//
//change the information so that the products are listed as cheaper.  Please  //
//relize this when implementing this software.                                //
//      This shopping cart was design to be used with the companion scripts,  //
//Addshop.cgi, loader.cgi, and sendmail.cgi.  It is posible to use without    //
//using those, or any of the other template pages that come with this, but    //
//some parts might be included in the other scripts.                          //
//                                                                            //
//      This Page, Script, and/or Associated Scripts are copyrighted 2002     //
//                      Derek Mantey, Hyper                                   //
//      Distribution of this file is not permitted without the writen consent //
//of Derek Mantey or Hyper.                                                   //
////////////////////////////////////////////////////////////////////////////////
// Globals
var ProductNumb = new Array();
var ProductName = new Array();
var ProductCost = new Array();
var NumItems = 0;

//Get cookie, called internaly
function GetCookie(NameOfCookie)
{
    //Check to see if there is a cookie stored.
    if (document.cookie.length > 0)
    {
        //Check to see if the cookie's name is stored in the "document.cookie"
        //object for the page.  Since more than one cookie can be set to a page
        //it is possible that the cookie is not present. If our cookie name is
        //not present the value -1 is stored in the variable called "begin".

        begin = document.cookie.indexOf(NameOfCookie+"=");
        if (begin != -1)
        {   // Our cookie was set.
            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;
    //Cookie was not set, Return NULL
}

//Set cookie, called internaly
function SetCookie(NameOfCookie, value, expiredays)
{
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    //Convert the number of days till expiration to a valid date.

    document.cookie = NameOfCookie + "=" + escape(value) + "; expires=" + ExpireDate.toGMTString();
    //Store cookie of NameOfCookie equal to value that expires on ExpireDate..
    // Note the date is converted to Greenwich Mean time using the "toGMTstring()" function.
}

//Delete cookie, called internaly
function delCookie (NameOfCookie)
{
    //Checks to see if the cookie is set, then set the expiration date is set to Jan. 1st 1970.
    if (GetCookie(NameOfCookie))
    {
        document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

//Parse Array Called internaly
function ParseArray(Data)
{
        var i = -1,CurrentVar = 0,LastEntry = 0;
        var PBuffer = new Array();
        Data = Data + ';';
        do
        {
                i++;
                if(Data.charAt(i) == ',' || Data.charAt(i) == ';')
                {
                        PBuffer[CurrentVar] = Data.substring(LastEntry,i);
                        CurrentVar++;
                        LastEntry = i + 1;
                }
        }while(Data.charAt(i) != ';')
        return(PBuffer);
}

////////// Initialize Cart /////
function InitCart()
{
    var Buffer;

    if(GetCookie("CartQTY") != null)
    {
        Buffer = GetCookie("CartQTY");
        ProductNumb = ParseArray(Buffer);
        Buffer = GetCookie("CartName");
        ProductName = ParseArray(Buffer);
        Buffer = GetCookie("CartCost");
        ProductCost = ParseArray(Buffer);
        NumItems = ProductNumb.length;
    }
}

////////// Add Item ////////////
function AddItem(Name,Price,Numb)
{
    if (!isNum(Numb))
    {
        Numb = 1;
    }

    var i = 0,Flag = false;
    if(NumItems == 0)
    {
        ProductName[0] = escape(Name);
        ProductCost[0] = Price;
        ProductNumb[0] = Numb;
        Flag = true;
        NumItems++;
    }
    else
    {
        for(i = 0;i < NumItems && Flag != true; i++)
        {
            if(ProductName[i] == escape(Name))
            {
                ProductNumb[i] = Numb;
                ProductCost[i] = Price;
                Flag = true;
            }
        }

        if(Flag == false && i == NumItems)
        {
            ProductName[i] = escape(Name);
            ProductCost[i] = Price;
            ProductNumb[i] = Numb;
            Flag = true;
            NumItems++;
        }
    }
}

/////////////Update Cart//////////
function UpdateCart()
{
    InitCart();
    var docnumb = new Array();
    var docname = new Array();
    var doccost = new Array();

    for (e=0;e<document.PurchaceOrder.elements.length;e++)
    {
        var temp = new String(document.PurchaceOrder.elements[e].name);
        for(var i=0; i < NumItems; i++)
        {
            if(temp == ("It" + i + "Qty"))
            {
                docnumb[i] = document.PurchaceOrder.elements[e].value;
                if (!isNum(docnumb[i]))
                {
                    docnumb[i] = 1;
                }
            }
            if(temp == ("It" + i + "Name"))
            {
                docname[i] = document.PurchaceOrder.elements[e].value;
            }
            if(temp == ("It" + i + "Cost"))
            {
                doccost[i] = document.PurchaceOrder.elements[e].value;
            }
        }
    }

    var olditems = NumItems;

    ProductNumb = new Array();
    ProductName = new Array();
    ProductCost = new Array();
    NumItems = 0;

    for(var i=0; i < olditems; i++)
    {
        if(docnumb[i] != 0)
            AddItem(unescape(docname[i]), doccost[i], docnumb[i]);
    }

    CloseCart();
}


////////// Close Cart //////////
// Cart must be closed at the end of each page, because when the cart is closed
//the information is saved. If not closed, then the data will not pass to the
//next page
// The cart is set to expire in 7 days
function CloseCart()
{
    if(NumItems !=0)
    {
        SetCookie("CartName", ProductName, 7);
        SetCookie("CartCost", ProductCost, 7);
        SetCookie("CartQTY", ProductNumb, 7);
    }
    else
    {
        delCookie("CartName");
        delCookie("CartCost");
        delCookie("CartQTY");
    }
}

////////// Remove Cart /////////
function RemoveCart()
{
    NumItems = 0;
    delCookie("CartName");
    delCookie("CartCost");
    delCookie("CartQTY");
}

////////// Misc Functions /////////
function isNum(str)
{
    if(!str) return false;
    for(var i=0; i<str.length; i++)
    {
        var ch=str.charAt(i);
        if ("-0123456789".indexOf(ch) ==-1) return false;
    }
    return true;
}