﻿ 
// Abstract cart object
var Cart = {
    
    // Internal array to hold the items we add to the cart
    items: new Array(),
    
    // If these are set, the will be called when items are added/removed
    addFunction: null,
    removeFunction: null,

    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Returns the item for this id, or an empty item if the id was not found
	getItemByID: function(id){
	    var i=this.items.length;
	    while(i--){
	        if(this.items[i].id==id){
	            return this.items[i];
	        }
	    }
        
        // If we got this far, there is no matching id - return empty item
        return { "id" : null, "quantity" : null};
	},

    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Returns boolean indicating whether the passed id is in the cart
	exists: function(id){
	    return (this.quantity(id) > 0);
	},
    
    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Returns the number of items in the cart for the id passed in
	quantity: function(id){
        return this.getItemByID(id).quantity;
	},
	
    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Adds an id/quantity to the cart and calls addFunction, if defined
	add: function(id, quantity){

	    // If quantity wasn't passed in, assume 1
	    if (quantity == null) quantity = 1;

        // See if this id is already any in the cart	    
	    if (this.quantity(id) > 0) {
	        // id in cart, just add to quantity
	        var item = this.getItemByID(id);
	        item.quantity += quantity;
	    } else {
	        // id not found in cart, add it
	        this.items[this.items.length] = { "id" : id, "quantity" : quantity };
	    }
	        
        // Call custom add function if available
        if(this.addFunction){
            this.addFunction.call(this, id, quantity);
        }
	},
	
    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Removes an id from the cart and calls removeFunction, if defined
	remove: function(id){
	    var i=this.items.length;
	    while(i--) 
	        if(this.items[i].id==id){
	            this.items.splice(i,1);
                
                // Call custom remove function if available
                if(this.removeFunction != null){
                    this.removeFunction.call(this, id);
                }
	            break;
	        }
	},
	
	/////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Empties the cart (and calls removeFunction if defined)
	empty: function(){
	    var i=this.items.length;
		while(i--) {
            // Call custom remove function if available
            if(this.removeFunction != null){
                this.removeFunction.call(this, this.items[i].id);
            }
	        this.items.splice(i,1);
	    }
	},

    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Returns an array of all ids for the items in the cart
	getIDs: function(){
	    var ids = new Array();
	    for (var i=0;i<this.items.length;i++){
	        ids[ids.length] = this.items[i].id;
	    }
	    return ids;
	}
	    
}; // End Cart




	   
// Abstract bundles object
var Bundles = {

    // Internal array to hold the items we add to the cart
    items: new Array(),

    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Returns an array of all bundleIDs
	getBundleIDs: function(){
	    var ids = new Array();
	    for (var i=0;i<this.items.length;i++){
	        ids[ids.length] = this.items[i].bundleID;
	    }
	    return ids;
	},
    
    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Returns the bundle given the bundleID
	getBundleByID: function(bundleID){
	    var i=this.items.length;
	    while(i--){
	        if(this.items[i].bundleID==bundleID){
	            return this.items[i];
	        }
	    }
	},

    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Returns the bundle for this name (case-insensitive)
	getBundleByName: function(name){
	    var i=this.items.length;
	    while(i--){
	        if(this.items[i].name.toLowerCase() == name.toLowerCase()){
	            return this.items[i];
	        }
	    }
	},

    /////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////
    // Returns true if bundle1 is identical to bundle2, otherwise false.
    areIdentical: function(bundle1, bundle2){
        for(var key in bundle1) {
            if (bundle1[key] != bundle2[key]) {
                return false;
            }
        }
        return true;
    },
    
    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Adds a bundle to the items() array
	add: function(bundle){
        this.items[this.items.length] = bundle;
	},

    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Adds a bundle to the beginning of the items() array
	addToBeginning: function(bundle){
        this.items.splice(0, 0, bundle);
	},
	
    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Removes an id from the cart and calls removeFunction, if defined
	remove: function(bundleID){
	    var i=this.items.length;
	    while(i--){
	        if(this.items[i].bundleID == bundleID){
	            this.items.splice(i,1);
	            break;
	        }
	    }
	},
	
    /////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	// Removes all bundles belonging to the userID passed in
	removeByUserID: function(userID){
	    var i=this.items.length;
	    while(i--){
	        if(this.items[i].userID == userID){
	            this.items.splice(i,1);
	        }
	    }
	}
	
}; // End Bundles