if (!donationcart) var donationcart = function(){};
if (!donationcart.classes) donationcart.classes = function(){};

donationcart.classes.CartItem = jscLib.libFuncs.subclass(jscLib.system.extendedObject, "Donation Cart Item");
donationcart.classes.CartItem.create = function(param){
    var _proto = this.prototype;
    
    _proto.init = function(param){
        if (param.cart == null) throw new jscLib.system.ESystem( { message: "Cart item must be associated to a donation cart", sysObject: _proto.self() } );
        this.cart = param.cart;
        this.itemcode = param.itemcode;
        if (param.isSplit && param.isSplit == true){
            this.isSplit = true;
            this.breakup = new Object();
        } else {
            this.isSplit = false;
            this.breakup = null;
        }
        
        if (param.unit){
            this.units = param.unit;
        }
        
        if (param.amount){
            this.amount = param.amount;
        }
    }
    
    _proto.getCauseCode = function(){
        return this.itemcode;
    }
    
    _proto.getItemTotal = function(fixeddecimal){
        var result = 0;
        if (this.isSplit && this.isSplit == true){
            for(var breakup in this.breakup){
                result += this.breakup[breakup] * this.getUnits();
            }           
        } else {
            result = this.getAmount() * this.getUnits();
        }
        if (fixeddecimal){
            return result.toFixed(2);
        } else {
            return result;
        }
    }
    
    _proto.getAmount = function(){
        if (!this.amount) return 0;
        return this.amount;
    }
    
    _proto.getUnits = function(){
        if (!this.unit || this.unit == 0) return 1;
        else return this.unit;
    }
    
    _proto.updateUnits = function(unit){
        this.unit = unit;
        this.cart.notifyChange(this);
    }
    
    _proto.updateAmount = function(amount, amountid){
        if (this.isSplit){
            this.breakup[amountid] = amount;
        } else {
            this.amount = amount;
        }
        this.cart.notifyChange(this);
    }
}

donationcart.classes.DonationCart = jscLib.libFuncs.subclass(jscLib.system.extendedObject, "Donation Cart");
donationcart.classes.DonationCart.create = function(param){
    var _proto = this.prototype;
    
    _proto.init = function(param){
        this.cartItems = new Object();
        if (param.onChangeEvent){
            this.onChangeEvent = param.onChangeEvent;
        }
    }
    
    _proto.addCartItem = function(itemCode, item){
        this.cartItems[itemCode] = item;
    }
    
    _proto.getCartTotal = function(fixeddecimal){
        var result = 0;
        for(var item in this.cartItems){
            var obj = this.cartItems[item];
            result += obj.getItemTotal();
        }
        
        if (fixeddecimal){
            return result.toFixed(2);
        } else {
            return result;
        }
    }
    
    _proto.notifyChange = function(cartitem){
        if (cartitem && this.onChangeEvent){
            this.onChangeEvent(this, cartitem);
        }
    }
    
    _proto.createCartItem = function(_itemcode, _issplit, _units, _amount){
        var item = new donationcart.classes.CartItem({
            cart: this,
            itemcode: _itemcode,
            isSplit: (_issplit == 'True'),
            unit: _units,
            amount: _amount 
        });
        this.addCartItem(_itemcode, item);
    }
    
    _proto.serialize = function(data){
        if(data){
            var causes = data.split('#');
            for(cause in causes){
                alert(causes[cause]);
            }
        }
    }
    
    _proto.deserialize = function(){
        result = "";
        for(var item in this.cartItems){
            var obj = this.cartItems[item];
            var total = obj.getItemTotal();
            if (total > 0){
                if (obj.isSplit){
                    result += obj.getCauseCode();
                    result += "," + obj.getUnits();
                    for(var breakup in obj.breakup){
                        result += "," + breakup;
                        result += "," + obj.breakup[breakup];
                    }                
                } else {
                    result += obj.getCauseCode();
                    result += "," + obj.getUnits();
                    result += "," + obj.getAmount();
                }
                result += "#";
            }
        }  
        return result;  
    }
}
