﻿// UpdateOrderLineItem
Ajax.Commands.UpdateOrderLineItem = function(elementId, ajaxCommand, cmdCounter) {
    this.priority = net.CommandQueue.PRIORITY_IMMEDIATE;
    this.type = net.CommandQueue.TYPE_SINGLEPROCESS;
    this.id = cmdCounter;
    this.elementId = elementId;
    this.ajaxCommand = ajaxCommand;

    var cmdParams = elementId.toString().split('|');
    this.action = cmdParams[0];
    if (this.action == 'Delete') {
        this.elementId = cmdParams[1];
        this.orderId = this.elementId;
        this.deleteLineId = cmdParams[2];
        this.consumerStep = cmdParams[3];
        spin_div("OrderDetailTable");
    } else if (this.action == 'Update') {
        this.elementId = cmdParams[1];
        this.orderId = this.elementId;
        this.tipAmount = cmdParams[2];
        this.consumerStep = cmdParams[3];
        spin_div("OrderDetailTable");
    } else if (this.action == 'Get') {
        this.elementId = cmdParams[1];
        this.orderId = this.elementId;
        this.consumerStep = cmdParams[2];
        this.fromVendorWebsite = cmdParams[3];
    } else {
        this.userId = document.forms.orderAttributes.userId.value;
        //alert("USERID: " + this.userId);
        this.vendorLocationId = document.forms.orderAttributes.vendorLocationId.value;
        this.productId = document.forms.orderAttributes.productId.value;
        this.orderId = document.forms.orderAttributes.orderId.value;
        this.categoryId = document.forms.orderAttributes.categoryId.value;
        this.price = document.forms.orderAttributes.price.value.slice(1);
        this.lineId = document.forms.orderAttributes.lineId.value;
        this.quantity = document.forms.orderAttributes.quantity.value;
        this.specialInstructions = document.forms.orderAttributes.specialInstructions.value;
        this.tipAmount = document.forms.orderAttributes.tipAmount.value.slice(1);
        this.deliveryType = document.forms.orderAttributes.deliveryType.value;
        this.localASAPorDelivTime = document.forms.orderAttributes.localASAPorDelivTime.value;
        this.consumerStep = document.forms.orderAttributes.consumerStep.value;
        if (document.getElementById("PageClickTrackingId") != null) {
            this.pageClickTrackingId = document.getElementById("PageClickTrackingId").value;
        }
        var i = 0;
        var selectedRadioButtons = '';
        var selectedCheckBoxes = '';
        for (i = 0; i < document.forms.orderAttributes.elements.length; i++) {
            var type = document.forms.orderAttributes.elements[i].type;
            if (type == 'radio') {
                if (document.forms.orderAttributes.elements[i].checked) {
                    var value = document.forms.orderAttributes.elements[i].value;
                    var name = document.forms.orderAttributes.elements[i].name;
                    selectedRadioButtons = selectedRadioButtons + name + '|' + value + '|';
                }
            }

            if (type == 'checkbox') {
                if (document.forms.orderAttributes.elements[i].checked) {
                    var value = document.forms.orderAttributes.elements[i].value;
                    var name = document.forms.orderAttributes.elements[i].name;
                    selectedCheckBoxes = selectedCheckBoxes + name + '|' + value + '|';

                }
            }
        }
        this.selectedRadioButtons = selectedRadioButtons;
        this.selectedCheckBoxes = selectedCheckBoxes;
        this.action = 'Save';


        try {
            //This values will be set, only if gift card is being added to order.
            this.isDeliverGiftCardWithOrder = document.orderAttributes.AlternateGiftCardDeliveryAddress.checked;
            var giftCardDeliveryAddress = document.orderAttributes.street.value + "|" + document.orderAttributes.buildingFloor.value + "|" + document.orderAttributes.zipCode.value;
            this.giftCardDeliveryAddress = giftCardDeliveryAddress;
        }
        catch (err) {
            //Thorw it on the floor..
        }
        if(document.getElementById("TB_overlay") != null)
            TB_remove();

        spin_div("OrderDetailTable");
    }
}

Ajax.Commands.UpdateOrderLineItem.prototype.QueryStringVariables = function() {
    var commandVariables = new Hashtable();
    commandVariables.put("action", this.action);
    commandVariables.put("userId", this.userId);
    commandVariables.put("orderId", this.orderId);
    commandVariables.put("productId", this.productId);
    commandVariables.put("categoryId", this.categoryId);
    commandVariables.put("vendorLocationId", this.vendorLocationId);
    commandVariables.put("price", this.price);
    commandVariables.put("tipAmount", this.tipAmount);
    commandVariables.put("lineId", this.lineId);
    commandVariables.put("deleteLineId", this.deleteLineId);
    commandVariables.put("quantity", this.quantity);
    commandVariables.put("specialInstructions", this.specialInstructions);
    commandVariables.put("selectedRadioButtons", this.selectedRadioButtons);
    commandVariables.put("selectedCheckBoxes", this.selectedCheckBoxes);
    commandVariables.put("localASAPorDelivTime", this.localASAPorDelivTime);
    commandVariables.put("deliveryType", this.deliveryType);
    commandVariables.put("consumerStep", this.consumerStep);
    if (document.getElementById("PageClickTrackingId") != null) {
        commandVariables.put("pageClickTrackingId", this.pageClickTrackingId);
    }

    commandVariables.put("isDeliverGiftCardWithOrder", this.isDeliverGiftCardWithOrder);
    commandVariables.put("giftCardDeliveryAddress", this.giftCardDeliveryAddress);
    commandVariables.put("fromVendorWebsite", this.fromVendorWebsite);

    //alert("COMMANDVARS: " + commandVariables);
    return commandVariables;
}

Ajax.Commands.UpdateOrderLineItem.prototype.ParseResponse = function(docEl) {
    var attrs = docEl.attributes;
    var status = attrs.getNamedItem('status').value;
    var message = attrs.getNamedItem('message').value;

    if (status == 'success') {
        net.Base.ReleaseQueue();



        //Delete only if any address validation message was displayed...
        var alertMessage = document.getElementById("AlertMessage");
        if (alertMessage != null) {
            if (alertMessage.style.display == 'block') {
                document.getElementById("AlertMessage").style.visibility = "hidden";
                document.getElementById("AlertMessage").style.display = "none";
            }
        }

        //Some time user adds item to order & then clicks next step then press the back button..
        //then trying add more item & then the clicking the next button not working...
        if (document.pageForm.submitClicked != null) {
            document.pageForm.submitClicked.value = 'N';
        }

        //Close the thickbox
        //TB_remove();

        var orderId = attrs.getNamedItem('OrderId').value;
        var consumerStep = attrs.getNamedItem('ConsumerStep').value;
        var action = attrs.getNamedItem('Action').value;
        var tipAmt = attrs.getNamedItem('TipAmount').value;
        var fromVendorWebSite = attrs.getNamedItem('VendorWebsite').value;
        var params = orderId + "|" + consumerStep + "|" + action + "|" + tipAmt + "|" + fromVendorWebSite;
        CreateCommand('73', params);

        /*
        //Do not scroll to top while updating the tip amount or deleting the order
        if (action == 'Save')
        {
        javascript:scroll(0,50);
        }
        */
    } else {
        var addItemToOrderClickedElement = document.getElementById("addItemToOrderClicked");
        if (addItemToOrderClickedElement != null) {
            addItemToOrderClickedElement.value = 'N';
        }
        alert(message);
    }
}
