var iStockSelectClass = ".single-option-selector"; var iStockVarId = 0; var iStockVarTitle = ''; if (typeof jQuery != 'undefined') { jQuery( document ).ready(function() { iStockInit(); iStockOnSelectChange(0); }); } else { window.onload = function() { iStockInit(); iStockOnSelectChange(1); } } function iStockInit() { jQuery("#iStock-wrapper").hide(); iStockVarId = getiStockVarId(); if(!iStockVarId) { iStockVarId = iStockVariants[0].id; } console.log('iStockInit: VarId: '+ iStockVarId); istockNotifyShow(iStockVarId); jQuery("#iStock-notify-btn").click(function(e){ jQuery('#iStock-msg').removeClass('iStock-error').hide(); var emailId = jQuery('input#iStock-email-id').val(); var iStockPTitle = ''+jQuery('input#iStock-product').val(); if(iStockValidateEmail(emailId)==1) { jQuery('#iStock-msg').html("").addClass('iStock-error').show(); } else { jQuery.ajax({ url: '//istock.shopapps.in/front/add_notify.php', data: { shop: iStockShop, cust: emailId, vid: iStockVarId, pid: iStockProductId, vTitle: iStockVarTitle, pTitle: iStockPTitle }, dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'iStockCallback', success: function(json) { console.log(JSON.stringify(json)); jQuery('#iStock-msg').html(json.msg).show(); jQuery('input#iStock-email-id').val(''); if (typeof iStockAddFn !== 'undefined' && typeof iStockAddFn === 'function') { iStockAddFn();} }, error: function(err, errorThrown) { } }); } }); } function iStockOnSelectChange(iniVal) { console.log("iStock change event called"); if(typeof iStockproductFrm!= 'undefined') { iStockSelectClass = iStockproductFrm + ' ' + iStockSelectClass; } if(iniVal==1){ jQuery(iStockSelectClass).change(function () { setTimeout(function(){ iStockVarId = getiStockVarId(); istockNotifyShow(iStockVarId); },500); }); } else{ var iCanUseOn = !!jQuery.fn.on; if(iCanUseOn) { jQuery(document).on('change', iStockSelectClass, function () { setTimeout(function(){ iStockVarId = getiStockVarId(); console.log('iStockOnSelectChange :: iStockVarId: '+iStockVarId); istockNotifyShow(iStockVarId); },500); }); } else { jQuery(document).delegate(iStockSelectClass, 'change', function () { setTimeout(function(){ iStockVarId = getiStockVarId(); console.log('iStockOnSelectChange :: iStockVarId: '+iStockVarId); istockNotifyShow(iStockVarId); },500); }); } } } function istockNotifyShow(iStockVarId) { var variant = iStockGetVariant(iStockVarId); if(variant) { console.log('iStockVarId: '+variant.id+' quantity: '+ variant.inventory_quantity + ' title: '+variant.title); } if(variant && variant.inventory_management=='shopify' && variant.inventory_policy=='deny' && variant.inventory_quantity <= 0) { iStockVarTitle = variant.title; jQuery("#iStock-wrapper").show(); jQuery.ajax({ url: '//istock.shopapps.in/front/check-availability.php', data: { shop: iStockShop, vid: iStockVarId, pid: iStockProductId }, dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'iStockCallback', success: function(json) { if(json.dt!=''){ jQuery("#iStock-availability").html(json.dt).show(); } }, error: function(err) { } }); } else{ jQuery("#iStock-wrapper").hide(); } } function iStockGetVariant(iStockVarId) { for(i=0; i < iStockVariants.length; i++) { if(iStockVariants[i].id == iStockVarId) { return iStockVariants[i]; } } } function getiStockVarId() { if(typeof iStockproductFrm!= 'undefined') { iStockvarid = jQuery(iStockproductFrm).find('[name=id]').val(); } else { iStockvarid = jQuery('[name=id]').val(); if(iStockvarid=='') { if (jQuery('[name=id]').length > 1) { iStockvarid = jQuery('[name=id]:eq(1)').val(); } } } return iStockvarid; } function iStockValidateEmail(emailId) { var atpos = emailId.indexOf("@"); var dotpos = emailId.lastIndexOf("."); if (atpos< 1 || dotpos=emailId.length) { return 1; } else { return 0; } } /*function iStockCallback() { }*/