var sicnt=0;
var sCount;
var posts=[];
var pvals={};
var mit=0;
var mxt=0;
var smusr;
var checkoutLink = "/cart";
var ctype = 0;
var linkvalidation = 0;
var disabledProduct=0;
var disabledMultiple=0;
var nickname = '';
var source = 'webshop';
function gshop(type) {
if($('#custom_comments').length && pvals.comment_type == 2) {
let commentsArea = $('#custom_comments');
let comments = commentsArea.val().trim();
if (comments == ""){
$.alert(commentsArea.attr('err-msg'));
return;
}
comments = comments.replace('\r', '').split('\n');
if (comments.length != pvals.cc){
$.alert(commentsArea.attr('err-msg'));
return;
}
pvals.comments = comments;
}
if (pvals.c==0) {
let data = {
pg:'atc',
tdf:pvals.id,
cart:pvals.id,
posts:posts,
ta:pvals.ta,
sta:pvals.sta,
ttype:pvals.o,
np:pvals.np,
ncc:pvals.ncc,
op:pvals.op,
hv:pvals.hv,
smusr:smusr
};
if(pvals.comment_type >= 0)
data.comment_type = pvals.comment_type;
if(pvals.comment_type == 2)
data.comments = pvals.comments;
if(pvals.product)
data.express_product = pvals.product;
if(typeof pvals.product_options != "undefined")
data.product_options = pvals.product_options;
if(pvals.additional_post_data)
data.additional_post_data = pvals.additional_post_data
$.ajax({cache: false,url:'/ajax.php', type:'post', data:data,
success: function(d){
posts = [];
pvals = [];
$('#cart_items_num').html(d.items);
if(type === undefined) {clsmodal();}
else {document.location.href = checkoutLink;}
return;
}
});
}
}
function suusr(val){
smusr = val;
}
function showmodal(modalid) {
//$('.productselection').fadeIn();
//$('.black_overlay').fadeIn();
$('#myModal').modal();
}
function clsmodal(modalid=0) {
//$('.productselection').fadeOut();
//$('.black_overlay').fadeOut();
$('#myModal').modal('hide');
}
function resetPostLoad() {
sicnt = 0;
}
function atcn(id,cp,op,cc,hv,flag) {
if(cp === undefined) cp = 0;
if(op === undefined) op = 0;
if(cc === undefined) cc = 0;
if(hv === undefined) hv = 0;
if(flag === undefined) flag = 0;
let owlitem = this.tagName ? $(this).parents('.owl-item').first() : $('.owl-item.active:has(#'+id.replace(/=/gi, '\\=')+')');
let card = owlitem.find('.card');
let lc = card.parents('.loading-container');
let loader = lc ? lc.find('.loading') : null;
if (loader)
loader.toggleClass('active');
$.ajax({cache: false,type: "post", url: "/ajax.php", data: {pg:'getCartParams', id: id}, success: function(d) {
var r = jQuery.parseJSON(d);
ctype=r.ctype;
linkvalidation=r.linkvalidation;
mcat=r.mcat;
nickname=r.nickname;
disabledProduct=r.isdisabled;
mxt=r.max;
mit=r.min;
sCount=r.qty;
multiple_order_enabled=r.multiple_order;
currentCategoryCountInCart=r.currentCategoryCountInCart;
if (multiple_order_enabled == 0 && currentCategoryCountInCart > 0)
disabledMultiple = 1;
if (loader)
loader.toggleClass('active');
if (disabledProduct == 1 || disabledMultiple == 1) {
CreateModalElems();
showmodal();
return;
} else {
var c=0,o=0,np=0,nc=0,ncc=0,t=-2,tx=0;
let n = id.replace(/=+?/gi,"");
var tElem=null, txElem=null;
if (cc>0) sCount = cc;
let selects = card.find('*[data-role]');
for(let i = 0; i < selects.length; i++){
if($(selects[i]).val() < 0){
$.alert($(selects[i]).attr('err-msg'));
return;
}
}
CreateModalElems();
if (card.find("#timer_" + flag).length > 0) {
tElem = card.find("#timer_" + flag);
t=tElem.find(':selected').data('val');
o=tElem.attr("data-type");
} else if ($("#timer_"+flag).val() !== 'undefined') {
tElem = $("#timer_"+flag);
t = tElem.val();
o = tElem.attr("data-type");
}
if (hv>0) {
if ($("#autoview_"+hv+":checked").val())
hv = 1;
}
pvals = {c:c,id:id,ta:t,sta:tx,np:np,ncc:ncc,op:op,o:o,cc:cc,hv:hv};
processCardOptions(card, pvals);
if (cp>0) {
pvals.np = cp;
pvals.ncc = $("#ncnt").val();
}
tPname = r.pname;
var pnamedesc = "";
var pnameExp = "";
/*if (r.cart_description !== null) {
pnamedesc = r.cart_description;
}*/
if (pvals.product_options) {
$.ajax({cache: false,type:'post', url:'/ajax.php',async:false,
data:{pg:'productOptionsToStr',opt:pvals.product_options,id:id},
success:function (d) {
pnameExp = d;
}});
}
if (ctype!=4 && mcat<2000)
$("#pname").html(sCount + " " + r.pname);
else
$("#pname").html(r.pname);
if (pnameExp)
$("#pname").html($("#pname").html() + " (" + pnameExp + ")");
$("#pname_desc").html(pnamedesc);
resetPostLoad();
showmodal();
$(".productselection #productlink").hide();
$('.productselection .ctnbtn').hide();
cwopost();
}
}});
}
function cwopost() {
$(".shopbtns").show();
var link = $('#productlink').val();
posts = [];
var postobj = new Object();
postobj.type = 'dlink';
postobj.url = link;
postobj.isvalid = 1;
posts.push(JSON.stringify(postobj));
suusr(link);
}
function selItem(elem){
var canSelect = 1;
source = $(elem).parent().data('src');
if (source === 'vip') {
sCount = $(".enteramt").val();
mit = $("#mit").val();
}
if(pvals)
pvals.additional_post_data = {};
let deductQ = function(q, c) {
return function(q,c) { return Math.ceil(q/c); };
}
if (sCount > 0) {
if (sicnt > 0) {
canSelect = deductQ(sCount, sicnt + 1)(sCount, sicnt + 1) >= mit;
} else {
canSelect = 1;
}
let qcheck = $(elem).parent().find(".qCheck");
if (canSelect == 1 || qcheck.is(":visible")) {
$(elem).toggleClass("check");
qcheck.toggle();
let fetchedInstaImage = $(elem).parent().find(".fetchedInstaImage");
let textboxcontainer = $(elem).parent().find(".textboxcontainer");
if (qcheck.is(":visible")) {
sicnt++;
$("#check_" + qcheck.attr("id")).prop('checked', true);
fetchedInstaImage.prop('checked', true);
textboxcontainer.show();
} else {
sicnt--;
$("#check_" + qcheck.attr("id")).prop('checked', false);
fetchedInstaImage.prop('checked', false);
textboxcontainer.hide();
textboxcontainer.find(".countbox").val(0);
}
if (sicnt <= 0 ) {
$("#selected_image_count").html("0");
} else {
$("#selected_image_count").html(sicnt);
$("#op_per_image").html(deductQ(sCount, sicnt)(sCount, sicnt));
$("#op_per_image_form").val(deductQ(sCount, sicnt)(sCount, sicnt));
}
let buttons = $('.shopbtns');
let thumbs = $(".fetchedInstaImage:checked");
posts = [];
thumbs.each(function(i) {
let q = deductQ(sCount, sicnt)(sCount, sicnt);
console.log(q);
$(this).parent().parent().find(".textboxcontainer>.countbox").val(q).trigger('input');
valJson = $.parseJSON($(this).val());
valJson['quantity'] = q;
posts.push(JSON.stringify(valJson));
$(this).val(JSON.stringify(valJson));
});
shouldShowShopButtons('post') ? buttons.show() : buttons.hide();
} else {
strmsg = TRN.lesslikes;
msgparam = [];
msgparam[0] = mit;
$.alert(strmsg)
}
} else {
$.alert(TRN.enterqty);
}
}
function dfpp() {
posts.push($("#fetchedProfile").val());
}
function shouldShowShopButtons(contentType) {
let unused = sCount;
if (contentType === 'undefined')
contentType = 'post';
if (contentType === 'post')
{
try {
$(".fetchedInstaImage:checked").each(function() {
let countbox = $(this).parent().parent().find(".textboxcontainer>.countbox");
let val = parseInt(countbox.val());
if (val < parseInt(mit))
throw "invalid quantity per post";
unused -= val
});
} catch (e) {
return false;
}
return Math.abs(unused) < (sCount * 0.05) ;
}
else if (contentType === 'profile') {
return true;
}
}
function getUnusedAmount() {
let left = sCount;
$(".fetchedInstaImage:checked").each(function() {
let countbox = $(this).parent().parent().find(".textboxcontainer>.countbox");
left -= countbox.val();
});
return left;
}
function CreateModalElems(source='webshop') {
$('.productselection').remove();
$.ajax({cache: false,url:'/ajax.php', async:false, type:'post', data:{pg:'productLinkInputPopup',
disabledMultiple:disabledMultiple,disabledProduct:disabledProduct,ctype:ctype,lval:linkvalidation,commentType:pvals.comment_type,
nickname:nickname,likesIndex:nickname.indexOf('like'),viewIndex:nickname.indexOf('view'),src:source
},success: function(res){
$("#modal-container").html('').append(res);
}});
$("#custom_comments").emojioneArea({pickerPosition: "bottom", filtersPosition: "bottom", tonesStyle: "square", filters: {flags: false}});
}
function updatecounts() {
var totalVal = 0;
$(".countbox").each(function() {
if ($.trim(this.value).length > 0 && $(this).parent().is(":visible") ) {
var currVal = parseInt($.trim(this.value));
if (currVal > 0) {
totalVal = totalVal+parseInt(currVal);
}
}
});
$('.enteramt').val(parseInt(totalVal));
}
var can_update_price = true;
function debounce(func, wait, immediate) {
let timeout;
return function executedFunction() {
let context = this;
let args = arguments;
let later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
let callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function composeInputs(inputs){
let groups = [];
for(let i = 0; i < inputs.length; i++) {
let data = inputs[i];
let input = null;
let input_id = 'input' + Math.random() + '_' + Date.now();
switch (data.type) {
case 'hidden':
input = $(``);
break;
case 'select':
let options = ``;
for(let j = 0; j < data.options.length; j++){
let option = ``
options += option;
}
input = $(``);
break;
default:
continue;
}
let label = '';
if (data.label) {
let f = '';
if ('id' in data.attributes)
f = data.attributes.id;
else {
f = input_id;
input.attr('id', input_id);
}
label = ``;
}
let attrKeys = Object.keys(data.attributes);
for(let j = 0; j < attrKeys.length; j++){
let key = attrKeys[j];
input.attr(key, data.attributes[key]);
}
input.addClass('form-control');
let group = $(`${label}
`);
group.append(input);
groups.push(group);
}
return groups;
}
function express_checkout(express_id){
checkPrice(function(d){
ctype = d.ctype;
nickname = d.nickname;
disabledProduct = d.isdisabled; if(!d.success){
$.alert(d.error);
return;
}
mxt=d.max;
mit=d.min;
sCount=d.qty;
let id = d.product;
CreateModalElems();
if (disabledProduct == 1) {
showmodal();
} else {
let c=0,o=0,np=0,nc=0,ncc=0,t=-2,tx=0;
let card = $('.express');
let selects = card.find('select');
for(let i = 0; i < selects.length; i++){
let temp = $(selects[i]);
if(temp.val() < 0){
$.alert(temp.attr('err-msg'));
return;
}
}
np = d.price;
pvals = {c:c,id:express_id,ta:t,sta:tx,np:np,ncc:d.quantity,op:0,o:o,cc:d.quantity,hv:0};
processCardOptions(card, pvals);
pvals.np = np;
pvals.product = id;
if (ctype != 4)
$("#pname").html(sCount + " " + d.pname);
else
$("#pname").html(d.pname);
resetPostLoad();
showmodal();
}
})
}
function checkPrice(callback){
if (!can_update_price)
setTimeout(function(){
checkPrice(callback);
}, 100);
let data = $('.express').serializeArray().reduce(function(obj, item) {
obj[item.name] = $(item).data('val') ? $(item).data('val') : item.value;
return obj;
}, {});
data.pg = 'calcExpressPrice';
data.price_modifiers = [];
$('.express').find('[aria-required=true]').each(function(){
data.price_modifiers.push($(this).find(':selected').data('val') ? $(this).find(':selected').data('val') : $(this).val())
})
can_update_price = false;
$.ajax({cache: false,
type: "post",
url: "/ajax.php",
data: data,
success: function(d) {
d = JSON.parse(d);
callback(d);
can_update_price = true;
}
});
}
$(document).ready(function(){
let update_price = function(){
let spinner = $('.loading-container .loading');
spinner.addClass('active');
checkPrice(function(d){
if(!d.success){
d.price = 0;
}
let priceParts = ('' + d.price).split('\.');
if (priceParts.length < 2)
priceParts[1] = '00';
let main = $(".express .value-num");
main.html(priceParts[0] + '.');
main.attr('content', d.price);
$(".express .rest").html(priceParts[1]);
spinner.removeClass('active');
can_update_price = true;
})
};
$(`#express_subcategory`).on('change', debounce(function(){
if ($(`#express_subcategory`).val() < 0)
return;
$('.loading-container .loading').addClass('active');
can_update_price = false;
$.ajax({cache: false,
type: "post",
url: "/ajax.php",
data: {
pg:'getExpressInputs',
product: $('#express_subcategory').val()
},
success: function(d) {
d = JSON.parse(d);
if(d.success){
$('.inputs').html('');
$('.inputs').append(composeInputs(d.additional_criteria.inputs));
$('.loading-container .loading').removeClass('active');
can_update_price = true;
update_price();
}
}
});
}, 200));
$('#express_category').on('change', function(){
let self = $('#express_category');
let category = self.val().replace(/=/gi,'\\=');
$(`#express_subcategory option`).attr('disabled', 'disabled');
$(`option[aria-category=${category}]`).removeAttr('disabled');
});
$(document).on('change', ".inputs .form-control", debounce(update_price, 200));
$("#express_amount").on('input', debounce(update_price, 200));
});
function processCardOptions(card, pvals)
{
let options = card.find("*[data-role]");
for(let i = 0; i < options.length; i++)
{
let option = $(options[i]);
if (window.cardHooks)
{
let hook = null;
if (typeof window.cardHooks[option.data("role")] != "undefined") {
hook = window.cardHooks[option.data("role")];
} else {
hook = window.cardHooks.default;
}
if (hook)
hook(card, option, pvals);
}
}
}
function setDatatypeAndNP(card, option, pvals, field)
{
let selected = option.find(":selected");
pvals[field] = selected.data("val") ? selected.data("val") : selected.val();
pvals.o = option.data("type");
if(card.find(".value-num").length) {
pvals.np = card.find(".value-num").attr("content");
}
if(typeof pvals.product_options === "undefined"){
pvals.product_options = {};
}
pvals.product_options[option.data("role")] = pvals[field];
}
window.cardHooks = {
delay: function(card, option, pvals) {
setDatatypeAndNP(card, option, pvals, "ta");
},
timer_live: function(card, option, pvals) {setDatatypeAndNP(card, option, pvals, 'ta');},
duration: function(card, option, pvals) {
setDatatypeAndNP(card, option, pvals, 'sta');
},
startin_live: function(card, option, pvals) {
setDatatypeAndNP(card, option, pvals, 'sta');
},
options: function(card, option, pvals) {
let selected = option.find(':selected');
let value = selected.data('val') ? selected.data('val') : selected.val();
pvals.op = value;
if(typeof pvals.product_options == "undefined"){
pvals.product_options = {};
}
pvals.product_options[option.data('role')] = value;
},
comment_type: function(card, option, pvals) {
let selected = option.find(':selected');
let value = selected.data('val') ? selected.data('val') : selected.val();
pvals.comment_type = value;
if(typeof pvals.product_options == "undefined"){
pvals.product_options = {};
}
pvals.product_options[option.data('role')] = value;
CreateModalElems();
},
default: function(card, option, pvals) {
let selected = option.find(':selected');
let value = selected.data('val') ? selected.data('val') : selected.val();
pvals.o = option.data("type");
if(card.find('.value-num').length)
pvals.np = card.find('.value-num').attr('content');
if(typeof pvals.product_options == "undefined"){
pvals.product_options = {};
}
pvals.product_options[option.data('role')] = value;
}
};
function carouselGoTo(carousel, index){
carousel.trigger("to.owl.carousel", index);
}
function onCardAnyClick(){
let owlitem = $(this).parents(".owl-item").first();
if(owlitem.hasClass("center"))
return;
let carousel = owlitem.parents(".owl-carousel").first()
let n = owlitem.index();
console.log(carousel.data("owl.carousel"));
carouselGoTo(carousel, n);
}