﻿//Set up global constants
var EXPIRE_PERIOD = 2;    //IE: Expire in 2 days
var DELIMITER = "|";

//Pre-load the images
var mAddOver = new Image;
var mAddDown = new Image;
var mAddUp = new Image;
var mRemOver = new Image;
var mRemDown = new Image;
var mRemUp = new Image;

//Actual image sources
mAddOver.src = '../../images/buttons/purchasing/AddToShoppingListOver.gif';
mAddDown.src = '../../images/buttons/purchasing/AddToShoppingListDown.gif';
mAddUp.src = '../../images/buttons/purchasing/AddToShoppingList.gif';
mRemOver.src = '../../images/buttons/purchasing/RemoveFromShoppingListOver.gif';
mRemDown.src = '../../images/buttons/purchasing/RemoveFromShoppingListDown.gif';
mRemUp.src = '../../images/buttons/purchasing/RemoveFromShoppingList.gif';

//----------------Below: Direct Cookie Stuff----------------\\


    //Creates a cookies with specified name, value, and expirary date.
    function createCookie(name,value,days)
    {
	    if (days)
	    {
		    var date = new Date();
		    date.setTime(date.getTime()+(days*24*60*60*1000));
		    var expires = "; expires="+date.toGMTString();
	    }
	    else var expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
    }

    //Returns the value of a cookie with specified name.
    function readCookie(name)
    {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++)
	    {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
    }

    //Deletes a cookies with specified name.
    function eraseCookie(name)
    {
	    createCookie(name,"",-1);
    }

//----------------BookList based Methods----------------\\
    //Finds out if a book has been ordered or not
    function exists(bookID)
    {
        var serialBookList = readCookie("booklist");
        if (serialBookList != null)
        {
            if (serialBookList.indexOf(DELIMITER + bookID + DELIMITER) != -1)
            {
                //The bookID does exist. This book has been ordered.
                return true;
            }
        }
        return false;
    }
    
    //Adds a book order to the cookie array
    function addBook(bookID)
    {
        //If the book *is not* found in the booklist cookie string...
        var serialBookList = "";
        if (readCookie("booklist") != null)
        {
            serialBookList += readCookie("booklist");
        }

        if (serialBookList == "")
        {
            serialBookList = "|";
        }
        
        //Make sure you don't add it twice...
        if (serialBookList.indexOf(DELIMITER + bookID + DELIMITER) == -1)
        {
            //Add it to the cookie array
            var tmpList = serialBookList + bookID + DELIMITER;
            serialBookList = tmpList;
            eraseCookie("booklist");
            createCookie("booklist", serialBookList, EXPIRE_PERIOD);
            //alert("[Added Book] \nCurrent List:\n\n " + serialBookList);
        }
    }
   
    //Removes a book order from the cookie array
    function removeBook(bookID)
    {
        //If the book *is* found in the booklist cookie string...
        var serialBookList = ""; //Adding the = "" gets rid of the null cookie error
        serialBookList += readCookie("booklist");
        var startPos = serialBookList.indexOf(DELIMITER + bookID + DELIMITER);
        
        //Don't try and remove anything if this book is not in the list.
        if (startPos != -1)
        {
            //Get the first half of the string
            var tmpList = "";
            tmpList += serialBookList.substring(0, startPos);
            
            //Get the second half of the string
            tmpList += serialBookList.substring(startPos + bookID.length + 1);
            
            //Copy the tmpList to the serialBookList
            serialBookList = tmpList;
            
            //Set up the cookies
            eraseCookie("booklist");
            createCookie("booklist", serialBookList, EXPIRE_PERIOD);
            //alert("[Removed Book] \nCurrent List:\n\n " + serialBookList);
        }
    }
    
    
    //This will add or remove a book from the users shopping list.
    function doAction(bookID)
    {
        //eraseCookie("booklist"); //Uncomment this to always add :-)
        if (exists(bookID))
        {
            //Remove the book
            removeBook(bookID);
        }
        else
        {
            //Add the book
            addBook(bookID);
        }    
        ShoppingListOver(bookID);    
    }

//----------------Button Image Changes on Mouse Actions----------------\\

    //Changes the image's picture on mouseout
    function ShoppingListOver(bookID)
    {
        if (exists(bookID) == false)
        {
            document.getElementById(bookID).src=mAddOver.src;
        }
        else
        {
            document.getElementById(bookID).src=mRemOver.src;
        }
    }
    
    //Changes the image's picture on mouseout
    function ShoppingListDown(bookID)
    {
        if (exists(bookID) == false)
        {
            document.getElementById(bookID).src=mAddDown.src;
        }
        else
        {
            document.getElementById(bookID).src=mRemDown.src;
        }
    }
    
    //Changes the image's picture on mouseout
    function ShoppingListOut(bookID)
    {
        if (exists(bookID) == false)
        {
            document.getElementById(bookID).src=mAddUp.src;
        }
        else
        {
            document.getElementById(bookID).src=mRemUp.src;
        }
    }