﻿Type.registerNamespace("Json");

Json.ShoppingCart = function(element) {
    Json.ShoppingCart.initializeBase(this, [element]);
    this.MinPrice = 0;
    this.DataSourceID = "";
    this.DataSource = new Array();
}

Json.ShoppingCart.prototype = {
    initialize: function() {
        Json.ShoppingCart.callBaseMethod(this, 'initialize');
        if (this.DataSourceID != "")
            this.DataSource = eval(this.DataSourceID);
        var RenderDelegate = Function.createDelegate(this, this.Render);
        _shoppingCartFacade.OnSelectedEventHandler(RenderDelegate);
        _shoppingCartFacade.OnUpdatedEventHandler(RenderDelegate);
        _shoppingCartFacade.OnRemovedEventHandler(RenderDelegate);
        _shoppingCartFacade.Select(this.DataSource);

    },
    dispose: function() {
        //Add custom dispose actions here
        Json.ShoppingCart.callBaseMethod(this, 'dispose');
    },

    RequestData: function() {
        _shoppingCartFacade.Select(this.DataSource);
    },
    UpdateProduct: function(productID, quantity) {
        _shoppingCartFacade.Update(productID, quantity);
    },
    RemoveProduct: function(productID) {
        _shoppingCartFacade.Remove(productID);
    },
    RemoveAllProduct: function() {
        if (confirm('Are you sure you want to remove all items from the order?'))
            _shoppingCartFacade.RemoveAll();
    },
    Render: function() {
       // debugger;
        var itemTemplate = this.get_itemTemplate();
        var body = this.get_headerTemplate();
        total = 0;
        for (i = 0; i < this.DataSource.collection.length; i++) {
            var item = itemTemplate;
            item = item.ReplaceAll("<@Quantity@>", this.DataSource.collection[i].quantity);
            item = item.ReplaceAll("<@UnitType@>", this.get_UnitTypeString(this.DataSource.collection[i].quantityUnit));
            item = item.ReplaceAll("<@TotalPrice@>", this.DataSource.collection[i].amount.toFixed(2));
            item = item.ReplaceAll("<@ProductName@>", this.DataSource.collection[i].name);
            item = item.ReplaceAll("<@Product_ID@>", this.DataSource.collection[i].id);
            body = body + item;
            total += this.DataSource.collection[i].amount;
        }
        var footer = this.get_footerTemplate();
        if (total == 0)
            footer = footer.replace("<@Total@>", "Your shopping cart is empty. ");
        else
            footer = footer.replace("<@Total@>", "Total: " + total.toFixed(2));

        footer = footer.replace("<@IsMinPrice@>", (this.MinPrice > 0 && this.MinPrice > total ? 'inline' : 'none'));
        footer = footer.replace("<@IsNotMinPrice@>", ((this.MinPrice > 0 && this.MinPrice > total) || total == 0 ? 'none' : 'inline'));

        body = body + footer;
        var div = this.get_template("container");
        div.innerHTML = body;

    },


    get_UnitTypeString: function(unitType) {
        var selectBox = "";
        switch (unitType) {
            case 1:
                selectBox = "Units";
                break;
            case 2:
                selectBox = "Dozens";
                break;
            case 4:
                selectBox = "Cases";
                break;
        }
        return selectBox;
    },
    get_headerTemplate: function() {
        var node = this.get_template("header_template");
        if (node != null)
            return node.innerHTML;
    },
    get_itemTemplate: function() {
        var node = this.get_template("item_template");
        if (node != null)
            return node.innerHTML;
    },
    get_footerTemplate: function() {
        var node = this.get_template("footer_template");
        if (node != null)
            return node.innerHTML;
    },
    get_template: function(id) {
        for (i = 0; i < this.get_element().childNodes.length; i++)
            if (this.get_element().childNodes[i].id == id)
            return this.get_element().childNodes[i];
        return null;
    }
}
Json.ShoppingCart.registerClass('Json.ShoppingCart', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


