﻿function addNewProduct() {
    editProduct('', 'create')
}

function addPromoCodeToCart() {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Cart/ApplyDiscountCode/',
        data: { code: $("#promoCode").val() },
        type: 'POST',
        success: function (data) {
            $("#_divEdit").dialog("close");
            eval(data);
        },
        beforeSend: function () {
            $("#_divEdit").dialog({ title: "Applying Promo Code" });
            $("#_divEdit").dialog("open");
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
            $("#promoCode").val("");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error applying the promo code to your cart.  Please close this box and try again.");
        }
    });
}

function removeDiscountCode(cartDiscountCodeId) {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Cart/RemoveDiscountCode/',
        data: { cartDiscountCodeId: cartDiscountCodeId },
        type: 'POST',
        success: function (data) {
            $("#_divEdit").dialog("close");
            eval(data);
        },
        beforeSend: function () {
            $("#_divEdit").dialog({ title: "Removing discount code" });
            $("#_divEdit").dialog("open");
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error removing the selected discount code.  Please close this box and try again.");
        }
    });
}

function removeItemFromCart(itemId) {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Cart/RemoveItem/',
        data: { itemId: itemId },
        type: 'POST',
        success: function (data) {
            $("#_divEdit").dialog("close");
            window.location.reload();
        },
        beforeSend: function () {
            $("#_divEdit").dialog({ title: "Removing item to Cart" });
            $("#_divEdit").dialog("open");
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error removing the selected item to your cart.  Please close this box and try again.");
        }
    });
}

function addToCart(itemId) {
    var quantity = $("#_txtAddToCart_" + itemId).val();
    if (isNaN(quantity)) {
        alert("Quantity must be numeric to add a product to your cart.");
        return;
    }
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Cart/AddItem/',
        data: { itemId: itemId, quantity: quantity },
        type: 'POST',
        success: function (data) {
            $("#_divEdit").dialog("close");
            window.location.reload();
        },
        beforeSend: function () {
            $("#_divEdit").dialog({ title: "Adding item to Cart" });
            $("#_divEdit").dialog("open");
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error saving the selected item to your cart.  Please close this box and try again.");
        }
    });
}

function editBulletPoint(id, productId) {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/EditBulletPoint/',
        data: { id: id, productId: productId },
        type: 'POST',
        beforeSend: function () {
            $("#_divEditLoading").show();
            if (id == "") {
                $("#_divEdit").dialog({ title: "Add New Product Bullet" });
            } else {
                $("#_divEdit").dialog({ title: "Edit Product Bullet" });
            }
            $("#_divEdit").dialog("open");
            $("#_divEditDynamicInfoHolder").hide();
        },
        success: function (data) {
            $("#_divEditDynamicInfoHolder").html(data);
        },
        complete: function () {
            $("#_divEditLoading").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("<label class='validation-summary-errors'>There was an error loading the requested form.  Close this window and try again.<br /><br />If the problem persists, please report the error using the link near the top of this page.</label>");
        }
    });
}

function editDiscountCode(id, productId) {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'DiscountCode/EditDiscountCode/',
        data: { id: id, productId: productId },
        type: 'POST',
        beforeSend: function () {
            $("#_divEditLoading").show();
            if (id == "") {
                $("#_divEdit").dialog({ title: "Add New Discount Code" });
            } else {
                $("#_divEdit").dialog({ title: "Edit Discount Code" });
            }
            $("#_divEdit").dialog("open");
            $("#_divEditDynamicInfoHolder").hide();
        },
        success: function (data) {
            $("#_divEditDynamicInfoHolder").html(data);
        },
        complete: function () {
            $("#_divEditLoading").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("<label class='validation-summary-errors'>There was an error loading the requested form.  Close this window and try again.<br /><br />If the problem persists, please report the error using the link near the top of this page.</label>");
        }
    });
}

function editImage(id, productId) {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/EditImage',
        data: { id: id, productId: productId },
        type: 'POST',
        beforeSend: function () {
            $("#_divEditLoading").show();
            if (id == "") {
                $("#_divEdit").dialog({ title: "Add New Product Image" });
            } else {
                $("#_divEdit").dialog({ title: "Edit Product Image" });
            }
            $("#_divEdit").dialog("open");
            $("#_divEditDynamicInfoHolder").hide();
        },
        success: function (data) {
            $("#_divEditDynamicInfoHolder").html(replaceHtmlTags(data));
            $('#editImage_file').uploadify({
                'uploader': $("#applicationRootLocation").val() + 'Content/plugins/uploadify/uploadify.swf',
                'script': $("#applicationRootLocation").val() + 'Product/UploadifyImage',
                'cancelImg': $("#applicationRootLocation").val() + 'Content/plugins/uploadify/cancel.png',
                'folder': $("#applicationRootLocation").val() + 'Content/images/product',
                'auto': true,
                'expressInstall': $("#applicationRootLocation").val() + 'Content/plugins/uploadify/expressInstall.swf',
                'fileExt': '*.jpg;*.gif;*.png',
                'fileDesc': 'Image Files (.JPG, .GIF, .PNG)',
                'scriptData': { 'imageId': $("#editImage_imageId").val(), 'productId': $("#editImage_productId").val() },
                'sizeLimit': 2097152,
                'onComplete': function (event, ID, fileObj, response, data) {
                    $("#editImage_imageId").val(response);
                    $('#editImage_fileUploaded').show();
                },
                'fileDataName': 'filedata',
                'buttonText': 'Upload New File'
            });
        },
        complete: function () {
            $("#_divEditLoading").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("<label class='validation-summary-errors'>There was an error loading the requested form.  Close this window and try again.<br /><br />If the problem persists, please report the error using the link near the top of this page.</label>");
        }
    });
}

function editProduct(id, recordMode) {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/Edit/',
        data: { id: id, recordMode: recordMode },
        type: 'POST',
        beforeSend: function () {
            $("#_divEditLoading").show();
            if (recordMode == "create") {
                $("#_divEdit").dialog({ title: "Add New Product" });
            } else {
                $("#_divEdit").dialog({ title: "Edit Product" });
            }
            $("#_divEdit").dialog("open");
            $("#_divEditDynamicInfoHolder").hide();
        },
        success: function (data) {
            $("#_divEditDynamicInfoHolder").html(data);
        },
        complete: function () {
            $("#_divEditLoading").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("<label class='validation-summary-errors'>There was an error loading the requested form.  Close this window and try again.<br /><br />If the problem persists, please report the error using the link near the top of this page.</label>");
        }
    });
}

function editProductHighlight(id, productId) {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/EditProductHighlight/',
        data: { id: id, productId: productId },
        type: 'POST',
        beforeSend: function () {
            $("#_divEditLoading").show();
            if (id == "") {
                $("#_divEdit").dialog({ title: "Add New Product Highlight" });
            } else {
                $("#_divEdit").dialog({ title: "Edit Product Highlight" });
            }
            $("#_divEdit").dialog("open");
            $("#_divEditDynamicInfoHolder").hide();
        },
        success: function (data) {
            $("#_divEditDynamicInfoHolder").html(data);
        },
        complete: function () {
            $("#_divEditLoading").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("<label class='validation-summary-errors'>There was an error loading the requested form.  Close this window and try again.<br /><br />If the problem persists, please report the error using the link near the top of this page.</label>");
        }
    });
}

function replaceHtmlTags(inputValue) {
    inputValue = inputValue.replace(/&lt;/g, "<");
    inputValue = inputValue.replace(/&gt;/g, ">");
    return inputValue;
}

function saveBulletPoint() {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/SaveBulletPoint/',
        data: $("#form_editBulletPoint").serialize(),
        type: 'POST',
        success: function (data) {
            if (data.length < 5) {
                $("#_divEdit").dialog("close");
                window.location = "/Product/EditProduct/" + data;
            }
            $("#_divEditDynamicInfoHolder").html(data);
        },
        beforeSend: function () {
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error saving the provided form.  Please close this box and try again.");
        }
    });
}

function saveDiscountCode() {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'DiscountCode/SaveDiscountCount/',
        data: $("#form_editDiscountCode").serialize(),
        type: 'POST',
        success: function (data) {
            if (data.length < 5) {
                $("#_divEdit").dialog("close");
                window.location = "/Product/EditProduct/" + data;
            }
            $("#_divEditDynamicInfoHolder").html(data);
        },
        beforeSend: function () {
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error saving the provided form.  Please close this box and try again.");
        }
    });
}

function saveImage() {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/SaveImage/',
        data: $("#form_editProductImage").serialize(),
        type: 'POST',
        success: function (data) {
            if (data.length < 5) {
                $("#_divEdit").dialog("close");
                window.location = "/Product/EditProduct/" + data;
            }
            $("#_divEditDynamicInfoHolder").html(data);
        },
        beforeSend: function () {
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error saving the image details form.  Please close this box and try again.");
        }
    });
}

function saveProduct() {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/Save/',
        data: $("#form_editProduct").serialize(),
        type: 'POST',
        success: function (data) {
            if (data.length < 5) {
                $("#_divEdit").dialog("close");
                window.location = "/Product/EditProduct/" + data;
            }
            $("#_divEditDynamicInfoHolder").html(data);
        },
        beforeSend: function () {
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error saving the product form.  Please close this box and try again.");
        }
    });
}

function saveProductHighlight() {
    $.ajax({
        url: $("#applicationRootLocation").val() + 'Product/SaveProductHighlight/',
        data: $("#form_editProductHighlight").serialize(),
        type: 'POST',
        success: function (data) {
            if (data.length < 5) {
                $("#_divEdit").dialog("close");
                window.location = "/Product/EditProduct/" + data;
            }
            $("#_divEditDynamicInfoHolder").html(data);
        },
        beforeSend: function () {
            $("#_divEditSaving").show();
            $("#_divEditDynamicInfoHolder").hide();
        },
        complete: function () {
            $("#_divEditSaving").hide();
            $("#_divEditDynamicInfoHolder").show();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#_divEditDynamicInfoHolder").html("There was an error saving the provided form.  Please close this box and try again.");
        }
    });
}

