(function($){
'use strict';
jQuery(document).ready(function(){
var tooltip=false;
var timer2, timer3;
var timePeriod=cartbounty_ec.hours;
var early_capture_form='';
var custom_button_selectors=cartbounty_co.custom_button_selectors;
if((timePeriod==0)){
localStorage.removeItem('cartbounty_pro_ec_last_time');
}
function pauseAddToCartButtons(){
var button=jQuery(this);
var contact_saved=localStorage.getItem('cartbounty_pro_contact_saved');
var custom_email=localStorage.getItem('cartbounty_pro_custom_email');
var custom_phone=localStorage.getItem('cartbounty_pro_custom_phone');
if(!button.hasClass('product_type_variable')&&!button.hasClass('disabled')&&(!contact_saved||timePeriod==0)&&!custom_email&&!custom_phone){
if(early_capture_form!=''){
insertEarlyCaptureForm(button);
}
return false;
}}
function insertEarlyCaptureForm(button){
var currentTime=new Date().getTime();
var last_time_displayed=localStorage.getItem('cartbounty_pro_ec_last_time');
var form=jQuery('#cartbounty-pro-early-capture-form');
var button_triggered=button.hasClass('cartbounty-pro-ec-on');
if(!button_triggered){
destroyEarlyCapturePopup();
removeTarget();
button.addClass('cartbounty-pro-button-target');
}
if(cartbounty_ec.mandatory_input||last_time_displayed==null||(last_time_displayed==null&&timePeriod==0)){
if(!button_triggered){
button.addClass('cartbounty-pro-ec-on');
displayEarlyCapture(currentTime, button);
}else{
if(!cartbounty_ec.mandatory_input){
removeEarlyCaptureForm();
resumeAddToCartButton(button);
if(currentTime - last_time_displayed > timePeriod * 60 * 60 * 1000){
localStorage.removeItem('cartbounty_pro_ec_last_time');
}}
}}else{
removeEarlyCaptureForm();
resumeAddToCartButton(button);
if(currentTime - last_time_displayed > timePeriod * 60 * 60 * 1000){
localStorage.removeItem('cartbounty_pro_ec_last_time');
}}
}
function loadEarlyCaptureForm(){
var data={
action: 							"insert_early_capture",
cartbounty_pro_show_early_capture: 	true
}
jQuery.post(cartbounty_co.ajaxurl, data,
function(response){
early_capture_form=response;
})
.fail(function(response){
if(response.status=='404'){
console.log('Ajax request unable to fire due to missing access to native WordPress file: ' + cartbounty_co.ajaxurl + '. Please check with your server administrator that mod_security is disabled and nothing else has been setup to restrict access to this file.');
resumeAddToCartButton(button);
}});
}
function displayEarlyCapture(currentTime, button){
if(cartbounty_ec.style==2){
jQuery('body').append(early_capture_form);
}else{
tooltip=button.tooltipster({
'trigger': 				'custom',
'contentAsHTML': 		true,
'interactive': 			true,
'content': 				early_capture_form,
'onlyOne': 				true,
'arrow': 				false,
'side': 				'bottom',
'distance': 			15,
'zIndex': 				8000,
'debug': 				false,
'theme': 				'cartbounty-pro'
});
tooltip.tooltipster('show');
}
jQuery("#cartbounty-pro-early-capture-submit").on('click', {button: button }, getEarlyCaptureInput);
jQuery('#cartbounty-pro-early-capture-email, #cartbounty-pro-early-capture-phone').on('keyup keypress change', {button: button }, getEarlyCaptureInput);
jQuery('#cartbounty-pro-early-capture-close, #cartbounty-pro-early-capture-form-backdrop').on('click', {button: button }, closeEarlyCaptureForm);
localStorage.setItem('cartbounty_pro_ec_last_time', currentTime);
if(typeof CartBountyProPhoneInput!="undefined"){
CartBountyProPhoneInput.start();
}}
function getEarlyCaptureInput(e){
var fields=jQuery("#cartbounty-pro-early-capture-email, #cartbounty-pro-early-capture-phone");
let email=jQuery('#cartbounty-pro-early-capture-email').val()||'';
let phone=jQuery('#cartbounty-pro-early-capture-phone').val()||'';
var form_closing_time=3500;
var button=e.data.button;
var submit_button_clicked=false;
if(jQuery(this).attr('id')=='cartbounty-pro-early-capture-submit'){
form_closing_time=0;
submit_button_clicked=true;
}
if(email.length > 0||phone.length > 0){
clearTimeout(timer2);
clearTimeout(timer3);
var emailValidation=cartbounty_co.email_validation;
var phoneValidation=cartbounty_co.phone_validation;
fields.closest('form').addClass("cartbounty-pro-loading");
timer2=setTimeout(function(){
fields.closest('form').removeClass("cartbounty-pro-loading");
}, 1000);
if(email.match(emailValidation)||phone.match(phoneValidation)){
if(email!=''){
localStorage.setItem('cartbounty_pro_email', email);
}else if(phone!=''){
localStorage.setItem('cartbounty_pro_phone', phone);
}
fields.parent().addClass("cartbounty-pro-input-valid");
fields.parent().removeClass("cartbounty-pro-input-error");
timer3=setTimeout(function(){
removeEarlyCaptureForm();
resumeAddToCartButton(button);
}, form_closing_time);
}else{
removeFields();
fields.parent().removeClass("cartbounty-pro-input-valid");
if(submit_button_clicked){
fields.parent().addClass("cartbounty-pro-input-error");
}}
}}
function closeEarlyCaptureForm(e){
removeEarlyCaptureForm();
if(!cartbounty_ec.mandatory_input){
resumeAddToCartButton(e.data.button);
}}
function removeEarlyCaptureForm(){
var form=jQuery('#cartbounty-pro-early-capture-form');
if(form.length > 0){
form.addClass('cartbounty-pro-fade-out');
setTimeout(function(){
if(jQuery('#cartbounty-pro-early-capture-close')){
destroyEarlyCapturePopup();
}}, 200);
}}
function resumeAddToCartButton(button){
setTimeout(function(){
jQuery(custom_button_selectors).off("click", pauseAddToCartButtons);
jQuery(document).off('click', pauseAddToCartButtons);
jQuery(custom_button_selectors).off("click", insertEarlyCaptureForm);
destroyEarlyCapturePopup();
var buttons=document.getElementsByClassName('cartbounty-pro-button-target');
for (var i=0;i<buttons.length; i++){
buttons[i].click();
}
removeTarget();
}, 210);
}
function passInputToCartBounty(){
var email=localStorage.getItem('cartbounty_pro_email');
var phone=localStorage.getItem('cartbounty_pro_phone');
var consent=cartbounty_co.consent_field;
var language=cartbounty_co.language;
if(email==null&&phone==null){
return;
}
var fields={
email: 			email,
phone: 			phone,
};
if(consent){
fields[consent]=1;
}
var gtranslate_language=get_gtranslate_language();
if(gtranslate_language){
language=gtranslate_language;
}
var data={
action:			"cartbounty_pro_save",
nonce:			cartbounty_co.nonce,
source:			"cartbounty_pro_early_capture",
customer:		fields,
language: 		language
}
if(!cartbounty_co.is_user_logged_in&&cartbounty_co.recaptcha_enabled){
grecaptcha.ready(function(){
grecaptcha.execute(cartbounty_co.recaptcha_site_key, {action: 'cartbounty_pro_abandoned_cart_tool'}).then(function(token){
data['cartbounty_pro_recaptcha_token']=token;
jQuery.post(cartbounty_co.ajaxurl, data, handleResponse);
});
});
}else{
jQuery.post(cartbounty_co.ajaxurl, data, handleResponse);
}}
function handleResponse(response){
if(response.success){
if(response.data.coupon_html!=''){
jQuery('body').append(response.data.coupon_html);
jQuery('#cartbounty-pro-tool-coupon-message').html(response.data.message);
jQuery('#cartbounty-pro-tool-coupon').addClass('cartbounty-pro-fade-in-down');
jQuery('#cartbounty-pro-tool-close-coupon-message').on('click', closeCouponMessage);
setTimeout(closeCouponMessage, 10000);
}
jQuery(document.body).trigger('update_checkout');
jQuery(document.body).trigger('wc_fragment_refresh');
localStorage.setItem('cartbounty_pro_contact_saved', true);
removeFields();
removeExitIntentForm();
}
if(timePeriod==0){
localStorage.removeItem('cartbounty_pro_ec_last_time');
}}
function closeCouponMessage(){
jQuery('#cartbounty-pro-tool-coupon').removeClass('cartbounty-pro-fade-in-down');
jQuery('#cartbounty-pro-tool-coupon').addClass('cartbounty-pro-fade-out-up');
}
function removeExitIntentForm(){
if(jQuery('#cartbounty-pro-exit-intent-form').length > 0){
jQuery('#cartbounty-pro-exit-intent-form').remove();
jQuery('#cartbounty-pro-exit-intent-form-backdrop').remove();
}}
function destroyEarlyCapturePopup(){
jQuery(custom_button_selectors).removeClass('cartbounty-pro-ec-on');
if(cartbounty_ec.style==2){
jQuery('#cartbounty-pro-early-capture-form').remove();
jQuery('#cartbounty-pro-early-capture-form-backdrop').remove();
}else{
if(tooltip){
tooltip.tooltipster('destroy');
tooltip=false;
}}
}
function removeTarget(){
jQuery(custom_button_selectors).removeClass('cartbounty-pro-button-target');
}
function removeFields(){
localStorage.removeItem('cartbounty_pro_email');
localStorage.removeItem('cartbounty_pro_phone');
}
function autofillCheckoutField(){
if(jQuery("#billing_email").length > 0||jQuery("#billing_phone").length > 0){
var email=localStorage.getItem('cartbounty_pro_email');
var phone=localStorage.getItem('cartbounty_pro_phone');
if(email!=null){
jQuery("#billing_email").val(email);
}
if(phone!=null){
jQuery("#billing_phone").val(phone);
}}
}
function get_gtranslate_language(){
var cookie=document.cookie.match(/(?:^|;\s*)googtrans=([^;]*)/);
if(cookie&&cookie[1]){
var parts=cookie[1].split('/');
if(parts.length===3&&parts[2]){
return parts[2];
}}
return document.documentElement.getAttribute('lang')||'';
}
passInputToCartBounty();
autofillCheckoutField();
loadEarlyCaptureForm();
jQuery(custom_button_selectors).on("click", pauseAddToCartButtons);
jQuery(document).on('click', custom_button_selectors, pauseAddToCartButtons); ////Disabling Add to Cart button in case Ajax loads product Quick view
jQuery(document).on('added_to_cart', passInputToCartBounty);
});
})(jQuery);