﻿function CartManagerObject() {
}

CartManagerObject.prototype.templateNameArray = new Array();

CartManagerObject.prototype.setTemplateName = function (WrapDivId, TemplateName) {
    this.templateNameArray[WrapDivId] = TemplateName;
}

// order of identifiers are crucial
CartManagerObject.prototype.cartWrapDivIdList = new Array("ProductsListCartPreview", "ProductsListCart");

CartManagerObject.prototype.getActionDivId = function () {
    for (var i = 0; i < this.cartWrapDivIdList.length; i++) {
        var id = this.cartWrapDivIdList[i];
        if ($('#' + id)[0] != undefined) {
            return id;
        }
    }
    return undefined;
}

CartManagerObject.prototype.getCartManagerActionUrl = function (ActionName) {
    return this.applicationPath + "ew_handler/products_list_orders/cart_manager/" + ActionName;
}

CartManagerObject.prototype.getRestWrapDivIdList = function (WrapDivId) {
    var ret = new Array();
    for (var i = 0; i < this.cartWrapDivIdList.length; i++) {
        var id = this.cartWrapDivIdList[i];
        if (id != WrapDivId) {
            if ($('#' + id)[0] != undefined) {
                ret.push(id);
            }
        }
    }
    return ret;
}

CartManagerObject.prototype.addProductCallBackFunction = function (Element) {
}

CartManagerObject.prototype.addProductCallBack = function (EwPlItemId, DetailUrl, Element, UserDataCallBack) {
    var AddProductData;
    if (UserDataCallBack != undefined) {
        if (typeof UserDataCallBack == 'string') {
            var ev = "AddProductData = " + UserDataCallBack + "(Element);";
            eval(ev);
        } else {
            AddProductData = UserDataCallBack(Element);
        }
    } else {
        AddProductData = this.addProductCallBackFunction(Element);
    }

    if (AddProductData["Cancel"] != true) {
        this.addProductFromData(EwPlItemId, DetailUrl, AddProductData);
    }
}

CartManagerObject.prototype.addProduct = function (EwPlItemId, DetailUrl, Quantity, UserData1, UserData2) {
    this.addProductFromData(EwPlItemId, DetailUrl, { UserData1: UserData1, UserData2: UserData2, Quantity: Quantity });
}

CartManagerObject.prototype.notUndefined = function (Value) {
    if (Value != undefined) {
        return Value;
    }
    return "";
}

CartManagerObject.prototype.addProductFromData = function (EwPlItemId, DetailUrl, AddProductData) {
    var ad = {}; adf = AddProductData;
    if (AddProductData == undefined) return;
    ad["WrapDivId"] = this.getActionDivId();
    ad["TemplateName"] = this.templateNameArray[ad["WrapDivId"]];
    ad["EwPlItemId"] = EwPlItemId;
    ad["DetailUrl"] = DetailUrl;
    ad["Quantity"] = this.notUndefined(adf["Quantity"]); ad["UserData1"] = this.notUndefined(adf["UserData1"]); ad["UserData2"] = this.notUndefined(adf["UserData2"]);
    var postBackWrite = function (data) {
        $('#' + ad["WrapDivId"]).html(data);
    }
    $.post(this.getCartManagerActionUrl("item_add"), ad, postBackWrite);
}

CartManagerObject.prototype.refreshDivIdList = function (WrapDivIdExcept) {
    var restIdList = this.getRestWrapDivIdList(WrapDivIdExcept);
    for (var i = 0; i < restIdList.length; i++) {
        var id = restIdList[i];
        var postBackWriteSimple = function (data) {
            $('#' + id).html(data);
        }
        var TemplateName = this.templateNameArray[id]; if (TemplateName == undefined) { TemplateName = ''; }
        $.post(CartManager.getCartManagerActionUrl("cart_refresh"), { WrapDivId: id, TemplateName: TemplateName }, postBackWriteSimple);
    }
}

CartManagerObject.prototype.doCartItemCommand = function (WrapDivId, CartItemIndex, Command) {
    if (WrapDivId == undefined) { WrapDivId = this.getActionDivId(); }
    var TemplateName = this.templateNameArray[WrapDivId];
    if (TemplateName == undefined) { TemplateName = ''; }
    if (Command == undefined) { Command = ''; }
    var postBackWrite = function (data) {
        $('#' + WrapDivId).html(data);
        CartManager.refreshDivIdList(WrapDivId);
    }
    $.post(this.getCartManagerActionUrl("item_command"), { WrapDivId: WrapDivId, TemplateName: TemplateName, CartItemIndex: CartItemIndex, Command: Command }, postBackWrite);
}

CartManagerObject.prototype.setDeliveryAndPayTypeFromSelectControl = function (selectObject) {
    var WrapDivId = this.cartWrapDivIdList[0];
    var EwPloDeliveryPayTypeId = selectObject.value;
    var TemplateName = this.templateNameArray[WrapDivId];
    if (TemplateName == undefined) { TemplateName = ''; }
    var postBackWrite = function (data) {
        $('#' + WrapDivId).html(data);
        CartManager.refreshDivIdList(WrapDivId);
    }
    $.post(this.getCartManagerActionUrl("set_delivery_and_pay_type"), { WrapDivId: WrapDivId, TemplateName: TemplateName, EwPloDeliveryPayTypeId: EwPloDeliveryPayTypeId }, postBackWrite);
}

var CartManager = new CartManagerObject();
