var ESHOP_COOKIE_NAME = 'eshop_show_alert';
function eshop_bind_buy_buttons() {
// Click on the button "Add to cart"
$("input.basket_button").click(function(){
var eshop_add_product_to_cart = $("input[name='eshop_add_product_to_cart']").val();
var product_id = parseInt($(this).parent().find("input[name='product_id']").val());
var product_amount = parseInt($(this).parent().find("input.product_amount").val());
var use_loyalty_points = $(this).hasClass('use_loyalty_points') ? true : false;
var product_months = '';
var a = this;
$('.add-to-cart-from-list-popup').hide();
for(var i = 0; i<=12; i++) {
if($('#month-' + i + ':checked').length) {
if(product_months.length) {
product_months += ',';
}
product_months += i;
}
}
if (product_id > 0)
{// Add product to the cart
$.getJSON(eshop_add_product_to_cart, { product_id: product_id, product_amount: product_amount, product_months: product_months, use_loyalty_points:use_loyalty_points }, function(data){
if (data.result == "ok")
{// The product was added
// Set amount of products
$(".eshop-product-amount").html(data.products_amount);
$(".eshop-product-amount-text").html(data.products_amount_text);
cart_final_price = data.final_price_value;
console.log('final price ' + cart_final_price);
var vbc_cart = parseInt(Cookies.get('vbc_cart'));
if(!$.isNumeric(vbc_cart)) {
vbc_cart = 0;
}
Cookies.set('vbc_cart', parseInt(vbc_cart) + parseInt(data.added_points), { secure : true, path: '/' });
usePoints(parseInt(data.points) + parseInt(data.added_points));
console.log('added..' + (parseInt(data.points) + parseInt(data.added_points)) + 'points');
// product detail
var product_img = $('.image-main');
var list_img = $(a).closest('.product-box-container').find('.product-list-image img');
var cart = $('.cart-link:visible span');
if (product_img.length) {
addToCartAnimate(product_img, cart);
}
if (list_img.length) {
console.log('animate');
addToCartAnimate(list_img, cart);
}
$('.cart-items-count').html(data.items_count);
$('.small-cart-mini-status').html(data.small_cart_mini_status);
$.get(eshop_add_product_to_cart + '&small-cart-reload=1', function(data) {
$(".small-cart").replaceWith(data);
});
}
});
}
});
$(document).on('click', '.cart-icon-minus, .cart-icon-plus, .cart-icon-delete', function(e) {
e.stopPropagation();
e.preventDefault();
var eshop_add_product_to_cart = $("input[name='eshop_add_product_to_cart']").val();
var product_id = parseInt($(this).closest('.product-item').find("input.product_id").val());
var $row = $('.product-item-' + product_id);
var product_amount = $(this).hasClass('cart-icon-plus') ? 1:-1;
var useMonths = $row.hasClass('use-months');
var current_amount = parseInt($row.find('.amount').first().text());
if($(this).hasClass('cart-icon-delete')) {
product_amount = -1 * current_amount;
}
// console.log('id=' + product_id + ' amount=' + product_amount + ' url=' + eshop_add_product_to_cart);
if(!useMonths || $(this).hasClass('cart-icon-delete')) {
if (product_id > 0 && ((current_amount + product_amount) >= 1 || (product_amount + current_amount == 0 && $(this).hasClass('cart-icon-delete')))) {
$row.find('.amount').text(current_amount + parseInt(product_amount));
$.getJSON(eshop_add_product_to_cart, {
product_id: product_id,
product_amount: product_amount,
delete: $(this).hasClass('cart-icon-delete')
}, function (data) {
if (data.result == "ok") {// The product was added
// Set amount of products
$row.find('.amount').text(data.item_amount);
$row.find('.item-price-total').text(data.item_price_total);
$('.main-cart .total-price').text(data.final_price);
$('.small-cart .total-price').text(data.final_price);
cart_final_price = data.final_price_value;
usePoints(data.points);
$('.cart-items-count').html(data.items_count);
$('.small-cart-mini-status').html(data.small_cart_mini_status);
if (!data.item_amount) {
$row.slideUp();
$('.nitems-in-cart').val(parseInt($('.nitems-in-cart').val()) -1);
}
if(parseInt($('.nitems-in-cart').val()) <= 0) {
$('.nitems-in-cart').val('0');
$('.small-cart').addClass('empty');
}
else {
$('.small-cart').removeClass('empty');
}
}
});
}
}
});
}
function addToCartAnimate(image, cart) {
console.log('animate cart start');
var image_container = jQuery('
', {
css: {'position' : 'absolute'}
}).prependTo(image.parent());
var img_height = image.height() + 'px';
var img_width = image.width() + 'px';
var img = image.clone().appendTo(image_container);
img.css({'position' : 'absolute', 'height' : img_height, 'width': img_width, 'z-index': 2000 });
var left = cart.offset().left - img.offset().left;
var top = img.offset().top - cart.offset().top;
img.animate({"opacity": "0.4",
"left": "+="+left+"px",
"top" : "-="+top+"px",
"height" : "37px",
"width" : "37px"
}, 500, function() {
img.remove();
image_container.remove();
}
);
}
$(document).ready(function(){
// Change flag of showing alert
$("input[name='eshop_show_alert']").change(function(){
// Set status
var eshop_show_alert = ($("input[name='eshop_show_alert']").attr("checked") ? "false" : "true");
$.cookie(ESHOP_COOKIE_NAME, eshop_show_alert);
});
eshop_bind_buy_buttons();
$('.image-thumbnail').click(function() {
//console.log($(this).data('url'));
//$('.image-main').attr('src', $(this).data('img'));
});
// Click on the link to submit form
$(".eshop-submit-form").click(function(){
$(this).parents("form").submit();
return false;
});
// Click on the link to go to next step in shopping cart
$(".eshop-shoping-cart-next").click(function(event){
var form = $(this).closest("form")[0];
event.preventDefault();
event.stopPropagation();
if (form.checkValidity() === false) {
form.classList.add('was-validated');
$('html,body').animate({
'scrollTop': ($('.invalid-feedback:visible').first().offset().top -250)
}, 300);
return false;
}
$(this).closest("form").attr('action', $('#submit-url').val());
$('#page_id_submit').val($('#submit-id').val());
$(this).closest("form")[0].submit();
return false;
});
// Hide company fields
$("input[name='customer_type']").change(function(){
if ($("input:checked[name='customer_type']").val() == "person")
{ // It is a person
$(".eshop-person-fields").show();
$(".eshop-body-corporate-fields").hide();
}
else
{ // It is a company
$(".eshop-person-fields").hide();
$(".eshop-body-corporate-fields").show();
}
}).change();
$("input[name='customer_type']").click(function(){
$("input[name='customer_type']").change();
});
// Hide delivery address fields
$("input[name='delivery_address_choice']").change(function(){
if ($("input:checked[name='delivery_address_choice']").val() == "identical")
{ // The addresses are identical
$(".eshop-delivery-address").hide();
}
else
{
$(".eshop-delivery-address").show();
}
}).change();
$("input[name='delivery_address_choice']").click(function(){
$("input[name='delivery_address_choice']").change();
});
// Confirm order
$(".eshop-confirm-order").click(function(){
$(".eshop-confirm-order-form").submit();
return false;
});
// Select delivery type
$("input[name='transport_type']").change(function(){
$("input[name='payment_type']").parents("tr").hide();
var $selected_payment_types = $(".transport_type_payments-" + $("input:checked[name='transport_type']").val());
if ($selected_payment_types.length > 0)
{// The transport type is selected
$("label[for='payment_type']").parents("tr").show();
var selected_payment_types = $selected_payment_types.val().split(";");
for(var i = 0; i < selected_payment_types.length; i++)
{
$("input[name='payment_type'][value='" + selected_payment_types[i] + "']").parents("tr").show();
}
}
else
{// The transport type is not selected
$("label[for='payment_type']").parents("tr").hide();
$(".eshop-payment-type-errors").parents("tr").hide();
}
}).change();
$("input[name='transport_type']").click(function(){
$("input[name='transport_type']").change();
});
$( ".eshop-cart-form" ).submit(function( event ) {
if($('#first_name').val() == '') {
$('.error_message').show();
$('#first_name').addClass('err');
event.preventDefault();
}
else {
$('#first_name').removeClass('err');
}
if($('#surname').val() == '') {
$('.error_message').show();
$('#surname').addClass('err');
event.preventDefault();
}
else {
$('#surname').removeClass('err');
}
if($('#e_mail').val() == '' || !validateEmail($('#e_mail').val())) {
$('.error_message').show();
$('#e_mail').addClass('err');
event.preventDefault();
}
else {
$('#e_mail').removeClass('err');
}
if($('#phone').val() == '') {
$('.error_message').show();
$('#phone').addClass('err');
event.preventDefault();
}
else {
$('#phone').removeClass('err');
}
if($('#lekaren').val() == '') {
$('.error_message').show();
$('#lekaren').addClass('err');
event.preventDefault();
}
else {
$('#lekaren').removeClass('err');
}
});
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
$(document).on('click', '*', function(e) {
if(!($(this).closest('.add-to-cart-from-list').length)) {
$('.add-to-cart-from-list-popup').hide();
}
});
$(document).on('click', '.add-to-cart-from-list-popup', function(e) {
//e.preventDefault();
e.stopPropagation();
});
$(document).on('click', '.add-to-cart-from-list > a', function(e) {
e.preventDefault();
e.stopPropagation();
if(!($(this).closest('.add-to-cart-from-list').find('.add-to-cart-from-list-popup').is(':visible'))) {
$('.add-to-cart-from-list-popup').hide();
$('.product-box-container').removeClass('active');
$(this).closest('.product-box-container').addClass('active');
$(this).closest('.add-to-cart-from-list').find('.add-to-cart-from-list-popup').slideDown();
}
else {
$('.product-box-container').removeClass('active');
$(this).closest('.add-to-cart-from-list').find('.add-to-cart-from-list-popup').slideUp();
}
});
// kategorie zakomentovane
// $(document).on('click', '#eshop-category-menu .eshop-category-menu-first-level a', function(e) {
// e.stopPropagation();
// e.preventDefault();
//
// $('.eshop-category-menu-first-level li').removeClass('sel');
// $(this).parent().addClass('sel');
//
// $('.eshop-category-menu-second-level').removeClass('sel');
//
// $('.eshop-category-menu-second-level-' + $(this).attr('rel')).addClass('sel');
//
// });
});
document.addEventListener('DOMContentLoaded', function(event) {
$(document).on('change', '#variant-select', function() {
$(this).closest('.variants').find('.product_id').val($(this).val());
})
});
function usePoints(points)
{
vbc_used_in_cart = points;
vbc_points_available = Math.floor(vbc_points_total - vbc_used_in_cart);
$('.points').val(points);
$('.points-text').html(points);
$('.points-available').html(vbc_points_available);
recountTotalWithPoints();
}
function recountTotalWithPoints()
{
if(!use_vbc) {
$('.price-to-pay').html(cart_final_price + ' €');
}
else {
var pouzit = parseFloat($('.points').val().replace(',', '.'));
if (isNaN(pouzit) || pouzit < 0) {
pouzit = 0;
}
if (pouzit != Math.ceil(pouzit)) {
pouzit = Math.ceil(pouzit);
$('.points').val(Math.ceil(pouzit));
}
pouzit = Math.min(pouzit, parseInt(vbc_points_total));
pouzit = Math.min(pouzit, Math.ceil(cart_final_price / vbc_price_per_point));
if ($('.points').val() != pouzit) {
$('.points').val(pouzit);
}
Cookies.set('vbc_cart', pouzit, {secure: true, path: '/'});
var total = Math.max(cart_final_price - (vbc_price_per_point * pouzit), 0);
total = Math.round(total * 100) / 100;
if (total < 0) {
total = 0;
}
total = total.toFixed(2).replace('.', ',');
$('.price-to-pay').html(total + ' €');
}
}