﻿var vinespring = {};

/****************************
On Doc Load
****************************/
jQuery(document).ready(function () {
    jQuery(".vsValidate").validate({ onkeyup: false, ignore: ":hidden" });
    jQuery(".vsFocus:first").focus();
});

/****************************
Adds an item to the cart, then refreshes the account links
****************************/
vinespring.addToCart = function (id, quantity) {
    vinespring.loading("#vsModal");
    jQuery.get("/store/addToCart/" + id + "?minicart=true&quantity=" + quantity, function (data) {
        jQuery("#vsModal").html(data).fadeIn();
    });
};

/****************************
Adds an item to the cart, then refreshes the account links
****************************/
vinespring.addToCartMultiple = function (form) {
    vinespring.loading("#vsModal");
    jQuery.get("/store/addToCartMultiple?minicart=true", jQuery(form).serializeArray(), function (data) {
        jQuery("#vsModal").html(data).fadeIn();
    });
    return false;
};

/****************************
Adds an allocated item to the cart, then refreshes the account links
****************************/
vinespring.addAllocationToCart = function (f) {
    jQuery.post("/store/addToCart?minicart=true", jQuery(f).serialize(), function (data) {
        jQuery("#vsModal").html(data).fadeIn();
    });
    return false;
};

/****************************
An ajax request
****************************/
vinespring.ajax = function (o) {
    o.error = o.error || function(){alert("We apologize. An error occurred while processing this request. Please contact us if this problem persists.")};
    jQuery.ajax(o);
    return false;
};

/****************************
Applies a promotional code to an order
****************************/
vinespring.applyOrderPromotionCode = function (f) {
    vinespring.post({
        url: '/store/applyOrderPromotionCode',
        data: jQuery(f).serialize(),
        success: function (d) {
            if (d) {
                jQuery('#vsOrderDetailWrapper').html(d); //set order detail
                jQuery('#vsPromotionCodeDiv').hide().find('input').val(''); //hide and clear promo code box
            }
            else alert('We were unable to locate an active promotion with this code.'); //notify user
        }
    });
    return false;
};

/****************************
Copies field values from one array to another
****************************/
vinespring.copyValues = function (source, target) {
    source = source.find(":input");
    target = target.find(":input");
    for (var i = 0; i < source.length; i++) {
        target.eq(i).val(source.eq(i).val());
    }
    return false;
};

/****************************
Deletes an item from the cart
****************************/
vinespring.deleteCartItem = function (id) {
    jQuery.get("/store/deleteCartItem/" + id, { minicart: true }, function (data) {
        jQuery("#vsModal").html(data).fadeIn();
    });
    return false;
};

/****************************
Deletes an item from the cart
****************************/
vinespring.deleteCartItemFull = function (id) {
    jQuery.get("/store/deleteCartItem/" + id, { minicart: false }, function (data) {
        jQuery("#vsCartItems").html(data);
    });
    return false;
};

/****************************
Hides the modal box
****************************/
vinespring.hideCart = function () {
    jQuery("#vsModal").fadeOut();
    return false;
};

/****************************
Shows a loader
****************************/
vinespring.loading = function (e) {
    jQuery(e).show().html('<img class="vsImage" src="/content/images/loading.gif" />');
};

/****************************
An ajax POST request
****************************/
vinespring.post = function (o) {
    o.type = 'POST';
    vinespring.ajax(o);
    return false;
};

/****************************
Refresh shipping rates for order process
****************************/
vinespring.refreshShippingRatesInOrderProcess = function (f, e) {
    //vinespring.loading('#vsShippingMethods');
    f = jQuery(f);
    var b = f.find('button[type=submit]').text('Please wait...').attr('disabled', 'disabled');
    vinespring.post({
        url: '/store/getAvailableMethods/',
        data: f.serialize(),
        success: function (h) {
            if (h) {
                h = jQuery(h);
                jQuery('#vsShippingOptions').html(h.find('#vsShippingOptions').html());
                jQuery('#vsShippingMethods').html(h.find('#vsShippingMethods').html());
                h = null;
                b.text('Next').removeAttr('disabled');
            }
        }
    });
    return false;
};

/****************************
Removes a promotional code from an order
****************************/
vinespring.removeOrderPromotionCode = function (f, c) {
    vinespring.post({
        url: "/store/removeOrderPromotionCode/" + c,
        data: jQuery(f).serialize(),
        success: function (d) {
            if (d)
                jQuery('#vsOrderDetailWrapper').html(d);
            else
                alert('We were unable to locate an active promotion with this code.');
        }
    });
};

/****************************
Loads the mini cart modal
****************************/
vinespring.showCart = function () {
    jQuery("#vsModal").load("/store/minicart").fadeIn();
    return false;
};

/****************************
Update Mini Cart
****************************/
vinespring.updateCart = function () {
    var form = jQuery("#vsCartForm");
    if (form.valid()) {
        vinespring.loading("#vsUpdateCartButton");
        jQuery.post("/store/updateCart?minicart=true", form.serialize(), function (data) {
            jQuery("#vsModal").html(data);
        });
    }
    return false;
};

/****************************
Update Full Cart
****************************/
vinespring.updateFullCart = function () {
    var form = jQuery("#vsFullCartForm");
    if (form.valid()) {
        vinespring.loading("#vsFullCartUpdateButton");
        jQuery.post("/store/updateCart", form.serialize(), function (data) {
            jQuery("#vsCartItems").html(data);
        });
    }
    return false;
};
